Home > Back-end >  sbt val evaluated more than once
sbt val evaluated more than once

Time:06-27

I'm creating a val into my build.sbt, made of a random string, to be used in the Setup and Cleanup methods for scalatest, like this:

val foo = Random.alphanumeric.take(3).mkString
...
Test / testOptions  = Tests.Setup(() => {
    // do stuff with it
})
...
Test / testOptions  = Tests.Cleanup(() => {
    // do stuff with the same string
  }

but it seems that the two functions are actually re-evaluating the val, resulting in two different strings. It seems that the forking of the JVM (fork := true) does not play a role into it, so I'm kinda out of ideas. Is that intended and/or is there a way to fix it/finding another approach to the problem (native to Scala/sbt)?

CodePudding user response:

Apparently the solution was easier than thought:

lazy val foo = SettingKey[String]("foo", "Random string")
foo := Random.alphanumeric.take(3).mkString

and then call foo.value in the sbt code after

  • Related