sbt-metals should not overwrite javaHome
Describe the bug
In my sbt project https://github.com/sourcegraph/scip-java, the sbt-metals plugin overwrites javaHome to become None causing my codebase to fail compilation. The culprit code appears to be https://github.com/scalameta/metals/blob/8488a02cbab8628b6bf990249bc5483badb705a1/sbt-metals/src/main/scala/scala/meta/metals/MetalsPlugin.scala#L51-L64
I ran inspect javaHome to verify what plugin was overwriting javaHome
sbt:root> inspect minimized/javaHome
[info] Setting: scala.Option[java.io.File] = None
[info] Description:
[info] Selects the Java installation used for compiling and forking. If None, uses the Java installation running the build.
[info] Provided by:
[info] ProjectRef(uri("file:/Users/olafurpg/dev/sourcegraph/scip-java/"), "minimized") / javaHome
[info] Defined at:
[info] (metals.MetalsPlugin.projectSettings) MetalsPlugin.scala:51
[info] Reverse dependencies (D=derives):
[info] minimized / javacOptions
[info] D minimized / compilers
[info] minimized / reForkOptions
[info] minimized / forkOptions
I don't understand why the second case defined @ Some(_) => defined is not working, but I was able to fix my issue by running rm project/metals.sbt.
Expected behavior
The javaHome setting should not be set to None by the sbt-metals plugin.
Operating system
macOS
Editor/Extension
VS Code
Version of Metals
v0.11.7
Extra context or search terms
No response
Thanks for reporting! Will look what is going on there
@olafurpg it looks like everything is fine with javaHome. If you check inspect minimized/Compile/javaHome it should return an expected result.
The fun thing is that compiling the semanticdb-javac plugin using semanticdb-javac from classpath leads to an error:
sbt:root> plugin/compile
[info] compiling 20 Java sources to /home/dos65/projects/metals/samples/scip-java/semanticdb-javac/target/classes ...
[warn] Unexpected javac output: An exception has occurred in the compiler (1.8.0_292). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
[warn] java.util.ServiceConfigurationError: com.sun.source.util.Plugin: Provider com.sourcegraph.semanticdb_javac.SemanticdbPlugin not found
[warn] at com.sun.tools.javac.util.ServiceLoader.fail(ServiceLoader.java:110)
[warn] at com.sun.tools.javac.util.ServiceLoader.access$300(ServiceLoader.java:60)
[warn] at com.sun.tools.javac.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:257)
[warn] at com.sun.tools.javac.util.ServiceLoader$1.next(ServiceLoader.java:337)
[warn] at com.sun.tools.javac.main.Main.compile(Main.java:463)
[warn] at com.sun.tools.javac.main.Main.compile(Main.java:381)
[warn] at com.sun.tools.javac.main.Main.compile(Main.java:370)
[warn] at com.sun.tools.javac.main.Main.compile(Main.java:361)
[warn] at com.sun.tools.javac.Main.compile(Main.java:56)
[warn] at com.sun.tools.javac.Main.main(Main.java:42).
[warn] javac exited with exit code 4
The compilation works with sbt-metals if you add: disablePlugin(MetalsPlugin) for plugin and do the same for minimized$N projects.
I'm not sure on 100% but it looks like the issue comes from ServiceLoader and I now I don't have any idea how it might be fixed on Metals side except of recommending using bloop-bsp instead of sbt-bsp.
recommending using bloop-bsp instead of sbt-bsp.
Update - the suggestion is wrong. If compile on bloop is called from metals it uses the same compiler options and fails for plugin project too. The log is incomplete but javac fails:
2022.07.19 19:58:33 INFO compiling plugin (1 java source)
2022.07.19 19:58:33 WARN javac exited with exit code 1