Why sbt.Extracted remove the previously defined Ta

2020-04-12 07:12发布

问题:

There is a suitable method in the sbt.Exctracted to add the TaskKey to the current state. Assume I have inState: State:

val key1 = TaskKey[String]("key1")
Project.extract(inState).append(Seq(key1 := "key1 value"), inState)

I have faced with the strange behavior when I do it twice. I got the exception in the following example:

val key1 = TaskKey[String]("key1")
val key2 = TaskKey[String]("key2")
val st1: State = Project.extract(inState).append(Seq(key1 := "key1 value"), inState)
val st2: State = Project.extract(st1).append(Seq(key2 := "key2 value"), st1)
Project.extract(st2).runTask(key1, st2)

leads to:

java.lang.RuntimeException: */*:key1 is undefined.

The question is - why does it work like this? Is it possible to add several TaskKeys while executing the particular task by several calls to sbt.Extracted.append?

The example sbt project is sbt.Extracted append-example, to reproduce the issue just run sbt fooCmd

回答1:

Josh Suereth posted the answer to sbt-dev mail list. Quote:

The append function is pretty dirty/low-level. This is probably a bug in its implementation (or the lack of documentation), but it blows away any other appended setting when used.

What you want to do, (I think) is append into the current "Session" so things will stick around and the user can remove what you've done via "sesison clear" command.

Additonally, the settings you're passing are in "raw" or "fully qualified" form. If you'd for the setting you write to work exactly the same as it would from a build.sbt file, you need to transform it first, so the Scopes match the current project, etc.

We provide a utility in sbt-server that makes it a bit easier to append settings into the current session:

https://github.com/sbt/sbt-remote-control/blob/master/server/src/main/scala/sbt/server/SettingUtil.scala#L11-L29

I have tested the proposed solution and that works like a charm.



标签: scala sbt