metals icon indicating copy to clipboard operation
metals copied to clipboard

metals sbt xdg specification

Open tgodzik opened this issue 1 year ago • 12 comments

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:

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.

> 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?

tgodzik avatar Oct 27 '23 12:10 tgodzik

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

untainsYD avatar Oct 27 '23 18:10 untainsYD

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.

kasiaMarek avatar Dec 08 '23 18:12 kasiaMarek

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 avatar Dec 08 '23 19:12 tgodzik

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)

tgodzik avatar Jan 17 '24 09:01 tgodzik

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

kasiaMarek avatar Jan 31 '24 13:01 kasiaMarek

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.

github-actions[bot] avatar Mar 04 '24 11:03 github-actions[bot]

@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

untainsYD avatar Mar 07 '24 18:03 untainsYD

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:

tgodzik avatar Mar 08 '24 10:03 tgodzik

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.

kasiaMarek avatar Mar 13 '24 12:03 kasiaMarek

@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

untainsYD avatar Jul 05 '24 04:07 untainsYD

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

tgodzik avatar Jul 05 '24 08:07 tgodzik

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.

untainsYD avatar Jul 05 '24 23:07 untainsYD