Why sbt.Extracted remove the previously defined TaskKey while append method?

会有一股神秘感。 提交于 2020-01-21 12:18:27

问题


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.



来源:https://stackoverflow.com/questions/29251711/why-sbt-extracted-remove-the-previously-defined-taskkey-while-append-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!