sbt icon indicating copy to clipboard operation
sbt copied to clipboard

Forking doesn't respect environment variables SBT_OPTS and JAVA_OPTS

Open Zhen-hao opened this issue 3 years ago • 2 comments

steps

I had Test / fork := false in my build.sbt and started sbt(version 1.5.4 on Java 17.0.3) shell with environment variable SBT_OPTS (and also JAVA_OPTS) containing --add-opens java.base/sun.nio.ch=ALL-UNNAMED

problem

When running a test, it was clear that the forked JVM didn't use JAVA_OPTS or SBT_OPTS.

expectation

A forked JVM should have the same parameters as the one running sbt.

notes

I use Nix flakes to set up my environment. I don't know if this issue is unique to Nix.

Zhen-hao avatar Aug 08 '22 18:08 Zhen-hao

The motivation for forking in to isolate test process so it's not tied to sbt process. The documentation (https://www.scala-sbt.org/1.x/docs/Forking.html) says

By default, a forked process uses the same Java and Scala versions being used for the build and the working directory and JVM options of the current process.

but as far as I can tell default JVM options has been Nil since forking was introduced as a setting (https://github.com/sbt/sbt/commit/a68e4c74f9b78fdfa677c0dcdb6ee021cab0209f) in 2011, and probably even before that too. At this point, making this change might break more builds than solve things.

For now I'd suggest following https://www.scala-sbt.org/1.x/docs/Forking.html#Forked+JVM+options to manually configure JVM options in the build file. Maybe we could introduce some setting to explicitly inherit memory option from the sbt process.

eed3si9n avatar Aug 08 '22 22:08 eed3si9n

Maybe we could introduce some setting to explicitly inherit memory option from the sbt process.

@eed3si9n An option to explicitly pass the same jvmOpts to forked process that sbt was started with would be great (and IMO would not really break any builds) 🙂

Right not we have to duplicate some configuration in .jvmopts and build.sbt so that both compilation and tests run successfully, which is not a big deal, but we would like to have a single source of truth for our builds 🙃

worekleszczy avatar Sep 13 '22 09:09 worekleszczy