-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Issue with macros and in-memory (virtual) source files #20591
Comments
I would suggest that it is munit at fault - if the source file path is null then it should recover from this |
@bishabosha Can you please comment on the fact that the Scala 3 documentation shows the exact same code, which I presume the MUnit implementation is based on? Thanks. https://docs.scala-lang.org/scala3/reference/metaprogramming/reflection.html#positions-1 |
Well that code is not great either - we now have an optional variant also - |
Where exactly is |
… key #SCL-22338 #SCL-22736 fixed - The physical temporary file document compiler (previous implementation) is used as the default. - This is done due to issues with macro expansion and in-memory files in MUnit and possibly other libraries. - scala/scala3#20591
My technical lead @pavelfatin created a nice reproduction. You have to have MUnit 1.0.0 resolved locally:
|
If I type
Nothing wrong with just wrapping the call in |
I would expect code to use The Scala 2 usages by munit and sourcecode use |
@som-snytt Just to be clear, you're also advocating for making no changes in Scala 2 and 3, instead address specific issues in libraries as they arise? That's totally fine, I just want to clarify things. Do you see an issue with the fix in MUnit? Thank you. |
Yes, with respect to this issue. The underlying problem is that the API is hard to use. But if the genie gave you only three wishes, would you use one on this problem? |
Can you please patch the examples using
To avoid future issues related to following them in other libraries |
Compiler version
All Scala 3 and Scala 2 versions.
Background information
IntelliJ IDEA is very particular when it comes to saving files to disk. IDEA likes to work with in-memory files and occasionally, in a very controlled manner, persist the files to disk.
In order to avoid more frequent than absolutely necessary saving to disk, I recently implemented compilation of in-memory files in IntelliJ IDEA, as part of this ticket, when using the Scala 3 compiler to provide error highlighting. This has been rolled out in an EAP version of IDEA 2024.2. If anyone wants to reproduce this project, it is enough to run IDEA 2024.2 EAP with Scala Plugin 2024.2.6 (or 2024.2.405+ nightly), clone https://github.com/scala/scala3-example-project and open the
MySuite.scala
MUnit test suite.Initially, everything worked fine. But I didn't think to test it with sources that contain macro expansion.
Since then, there has been the following report.
When in-memory source files are used, the following MUnit macro implementation throws an NPE.
For easy reference, here's the exception:
This specific MUnit Location macro is a perfectly well-written macro. In fact, the Scala 3 documentation shows the same idiom.
What I think the problem is
I searched for
"def jpath"
in the Scala 3 repo and found the following:all of which are implemented as:
To be fair, these are completely legitimate implementations of the
dotty.tools.io.AbstractFile
interface, which says:(in both cases,
JPath
is a type alias forjava.nio.file.Path
)What I tried
I modified
VirtualFile.scala
in the Scala 3 repo to return:I published a local snapshot and rebased https://github.com/scala/scala3-example-project on it, and the issue was gone.
What will we do
Obviously, I will have to disable in-memory sources and revert to always using file system sources for providing error highlighting using the compiler. I will keep the feature around with the hope of enabling it for future versions of Scala.
I will also open the same ticket in Scala 2, as it also contains the same
null
implementations. That ticket will most likely be just a reference to this one, as the relevant details are the same.Discussion
How should this problem be handled? When a proper solution for this problem is attempted, it should also be covered by tests, not just for the regular compilation part, but also macro expansion, and any other places that might be using
AbstractFile#jpath
.It also feels like the macro documentation should be revised, as it clearly shows unsafe usage of methods that are documented to be nullable.
Looking forward to a discussion.
Thank you in advance.
The text was updated successfully, but these errors were encountered: