scalameta / metals

Scala language server with rich IDE features 🚀
https://scalameta.org/metals/
Apache License 2.0
2.1k stars 332 forks source link

metals sbt xdg specification #5796

Open tgodzik opened 1 year ago

tgodzik commented 1 year ago

Discussed in https://github.com/scalameta/metals/discussions/5775

Originally posted by **untainsYD** October 21, 2023 When I import an `sbt` build in `metals` (Linux `6.5.8-arch1-1`/`VSCode`), it creates a `~/.sbt` folder in the home directory, ignoring the XDG specification. Even though I have set `SBT_OPTS` in `.zprofile` as follows, it is still being ignored: ```sh export SBT_OPTS="$SBT_OPTS \ -Dsbt.ivy.home=$XDG_CACHE_HOME/ivy \ -Dsbt.boot.directory=$XDG_CACHE_HOME/sbt/boot \ -Dsbt.preloaded=$XDG_CACHE_HOME/sbt/preloaded \ -Dsbt.global.base=$XDG_CACHE_HOME/sbt \ -Dsbt.global.staging=$XDG_CACHE_HOME/sbt/staging \ -Dsbt.global.zinc=$XDG_CACHE_HOME/sbt/zinc \ -Dsbt.dependency.base=$XDG_CACHE_HOME/sbt/dependency \ -Dsbt.repository.config=$XDG_CONFIG_HOME/sbt/repositories \ -Dsbt.global.settings=$XDG_CONFIG_HOME/sbt/global \ -Dsbt.global.plugins=$XDG_CONFIG_HOME/sbt/plugins \ -Dmaven.repo.local=$XDG_CACHE_HOME/maven/repository \ -Divy.settings.dir=$XDG_CONFIG_HOME/ivy2 \ -Divy.home=$XDG_CACHE_HOME/ivy2 \ -Divy.cache.dir=$XDG_CACHE_HOME/ivy2/cache" ``` I have configured the `Custom sbt launcher` to propagate environment variables, but it doesn't seem to be effective. ```sh > ls -lah ~/.sbt/boot Permissions Size User Group Date Modified Name drwxr-xr-x - untainsYD untainsYD 21 Oct 23:27  scala-2.12.18 .rw-r--r-- 0 untainsYD untainsYD 21 Oct 23:39  sbt.boot.lock .rw-r--r-- 0 untainsYD untainsYD 21 Oct 23:27  update.log ``` How can i force metals sbt to start the server in order to my `SBT_OPTS`?
untainsYD commented 1 year ago

If you will need an sbt file or smth like that, just mention me.

kasiaMarek commented 11 months ago

The issue seems to be the bsp config generated by sbt (.bsp/sbt.json). The args for starting the server are:

[some-path]/java -Xms100m -Xmx100m -classpath [some-path]/sbt-launch.jar -Dsbt.script=[some-path]/sbt xsbt.boot.Boot -bsp

Which will work if -Dsbt.boot.directory=... is added explicitly but won't pick up sbt opts. I feel like maybe sbt should pass those options when generating the config.

If one manually adds this option to args in .bsp/sbt.json it will work as expected, the problem is that sbt regenerates .bsp/sbt.json a lot, so this would have to be fixed constantly.

In metals we could insert such options when we start the sbt server, but that would work under the assumption that we know how the command for starting the server looks like, which is suboptimal, since metals should be oblivious to such things.

tgodzik commented 11 months ago

Shouldn't the environment variable work though? We start the process, so maybe we must make sure these variables are passed to sbt? We can suggest to restart if they changed.

tgodzik commented 9 months ago

It seems that it's not possible to fix this while launching via launcher, instead we should check if sbt is available on PATH and use it. For most advanced users it should be on the PATH.

Alternatively, you should be able to specify path to sbt by hand in the settings and it should work. (to be checked)

kasiaMarek commented 9 months ago

@untainsYD did setting sbt script in metals settings actually solved the issue for you?

github-actions[bot] commented 8 months ago

This issue was closed because no new information was added for the last 30 days. If you have any relevant information, feel free to add it and reopen the issue.

untainsYD commented 8 months ago

@untainsYD did setting sbt script in metals settings actually solved the issue for you?

No, it didn't, i have sbt option set, but nothing changed image

It always recreates ~/.sbt folder in home directory. image

tgodzik commented 8 months ago

Looks like maybe the env variable is not forwarded to Metals. Might be useful to start VS Code using code command from console. We should be forwarding the environment variable correctly otherwise :thinking:

kasiaMarek commented 7 months ago

I don't think it will work right now. So it seems that starting sbt server using.../java ... xsbt.boot.Boot -bsp causes .sbt to be created (one would have to pass Dsbt.boot.directory= arg to it). This command isn't generated by Metals but by sbt via sbt bspConfig command, so an upstream fix seems to be needed here. Here is the sbt issue https://github.com/sbt/sbt/issues/7469.

untainsYD commented 4 months ago

@tgodzik @kasiaMarek sorry for bothering you, but now with Metals version: 1.3.2+31-093989ec-SNAPSHOT last update i have .metals folder generated in my home user folder within the project folder .metals.

image image

tgodzik commented 4 months ago

I think that would be shared indexes so that we don't reindex JDK etc. which can take a long while

untainsYD commented 4 months ago

I think that would be shared indexes so that we don't reindex JDK etc. which can take a long while

i got it, but having it on XDG_PATH would be very nice.