-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Worksheet does not recognize stuff in the default package at top level #2363
Comments
Thanks for reporting! So I think that actually works as intended. Empty package cannot be imported or accessed from outside from what I understand. And worksheets are outside the compilation unit that contains the empty packages. I am not even sure it is possible to make it work properly. |
From what I found:
and since we would need to import the members of that empty package, it might be impossible to do so sensibly. |
But And there is something going on in metals; if you start typing Also just because the empty package cannot be imported does not mean it cannot be "automatically" visible... As it is in "normal" compiled code and as in |
So I don't think it works as intended/expected if you follow how it works in It seems to me like an arbitrary, metals-specific restriction that you must put your code in a package, instead of just in |
I agree that it would be much better if it worked, I am just not sure how feasible it is to do. What I think happens here is that we give the compiler a classpath where that empty package is already compiled and the compiler does not see it because of the quoted rule. Maybe there is a compiler flag? We'll need to dig in.
Sbt sees everything because it's all in the same compilation unit and that's probably why it's working. Worksheets are a separate entity, which causes a problem here. |
Aha. Thanks for the explanation! Seems tricky... Sbt has all compiled stuff in As the top level stuff in metals is visible when you click on run over |
The presentation compiler seems to see that empty package (hence the completions etc.), so maybe we could force it somehow too... Maybe some kind of a REPL mode? |
Sounds promissing! 💡 Do you mean that the user should shift a setting in metals for worksheets to work that way, or do you mean that the worksheet is in this "REPL mode" from start? |
I think this one, but I am just guessing here. Nothing like this might exist. |
Ok that would be great! And there is no other way, except special "REPL mode" of getting the bytecode of the default package on classpath like happens when clicking "run/debug"? |
Honestly, I don't know yet. |
Ok, thanks! I'd be happy to help out with testing new stuff and give input to how it could work from a user perspective if needed. |
@tgodzik Any further info on the possibility of worksheets at top level seeing default package stuff esp in builds with no src/blah ceremony? (I'm planning changes in student workspaces and teaching material so this will have impact on which way I go) |
Checked a couple of things again and my current conclusion is that we can't really do it without some kind of guessing or at least workarounds. There are two main problems:
|
The second point seems to be a Java limitation as far as I can tell. |
Related to scalameta/metals#2363 This simplifies worksheets for users, since they can define separate classes in files and use in workspaces.
Related to scalameta/metals#2363 This simplifies worksheets for users, since they can define separate classes in files and use in workspaces.
Or is it? Seems I am able to work around it in scalameta/mdoc#505 Though not sure if that would work for Scala 3. As for the files in root directory I think it is not possible for us to work around sensibly aside from mimicking sbt logic. Not sure if that is a good solution. Edit: I would probably just recommend having at least |
Thanks for looking into this! In the beginning of a beginner programming course I think it is best to use the bare minimum build.sbt and keeping it as simple as possible. Each new cryptic line is a hurdle for pure beginners that have never seen code before and drains their grit and feeling of being lost. That is why I'm so engaged in this. A beginner gets an important sense of control if a file is created from scratch by hand and the the beginner has written every keystroke be themselves. If it is at all possible to use the sbt logic I think it would make the barrier to scala+metals significantly lower for pure beginners. But if it turns out to be impossible or too fragile or too much work I think, I will in my teaching probably stick with plain REPL in terminal on my lectures and in exercises for students instead of worksheets in vscode+metals. But I will investigate the workarounds you propose. Thanks again. |
just tested: empty package does not work in scala3 using a file test.worksheet.scala that access compiled code in .scala fiels, even if the worksheet file is in src/main/scala But if a package declaration is made it works as expected. |
Describe the bug
When creating modules on top level in src or in src/main/scala without any package (i.e. the default package) then these values are not recognized in any worksheet.
To Reproduce
Steps to reproduce the behavior:
object Main
insrc/main/scala/Main.scala
containing aval msg = "hello"
Main.msg
Expected behavior
Top level values should be recognized in worksheets.
Example: The
test2.worksheet.sc
should recognizeMain.msg
in screenshot below.Screenshots
Installation:
The text was updated successfully, but these errors were encountered: