0

I have this simple command that changes the value of myBoolSetting:

commands += Command("mycommand") {
    state ⇒ (Space ~> Bool).?
} { (state, arg) ⇒
    val b = arg.getOrElse(true)
    Project.extract(state).append(Seq(myBoolSetting in Global := b), state)
}

When I change invoke it does change myBoolSetting but I lose any setting whose value has been changed by means of the set command.

> set myStringSetting := "new value"
> myCommand false

In this example, the value of myStringSetting has been lost and has its default value.

How can I change a setting and keep manual changed settings?

UPDATE:

Found related question: Why sbt.Extracted remove the previously defined TaskKey while append method?, but seems not to work in my case.

Modified code:

commands += Command("mycommand") {
    state ⇒ (Space ~> Bool).?
} { (state, arg) ⇒
    val b = arg.getOrElse(true)
    val session = Project.session(state)
    val state2 = Project.extract(state).append(Seq(myBoolSetting in Global := b), state)
    SessionSettings.reapply(session, state2)
}
Community
  • 1
  • 1
david.perez
  • 6,090
  • 4
  • 34
  • 57

1 Answers1

1

I might only partially answer your question.

The reason for losing myStringSetting new value is that myCommand reloads a session effectively leaving you with what you have already in build.sbt-like files and what the command itself set. You simply drop the value while reloading the session.

It is similar to doing set scalaVersion := "2.11.7" and reload afterwards. scalaVersion becomes the default Scala version you had in sbt configuration.

You now know why you lose the value. If you want to retain the value you'd have to save the current session session save and execute your command. You need to add session save to your command.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420