为什么 sbt.Extracted 在 append 方法时删除先前定义的 TaskKey? [英] Why sbt.Extracted remove the previously defined TaskKey while append method?

查看:23
本文介绍了为什么 sbt.Extracted 在 append 方法时删除先前定义的 TaskKey?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sbt.Exctracted 中有一个合适的方法可以将TaskKey 添加到当前状态.假设我有 inState: State:

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)

导致:

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

问题是 - 为什么它会这样工作?是否可以通过多次调用 sbt.Extracted.append 在执行特定任务时添加多个 TaskKey?

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?

示例 sbt 项目是 sbt.Extracted append-example,要重现问题,只需运行 sbtfooCmd

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

推荐答案

Josh Suereth 发布了 answer 到 sbt-dev 邮件列表.引用:

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

append 函数非常脏/低级.这可能是其实现中的一个错误(或缺乏文档),但它在使用时会破坏任何其他附加设置.

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.

你想要做的,(我认为)是附加到当前的会话"中所以事情会留下来,用户可以通过sesison clear"删除你所做的事情;命令.

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.

此外,您传递的设置为原始"格式.或完全合格"形式.如果您希望您编写的设置与 build.sbt 文件中的设置完全相同,您需要先对其进行转换,以便范围与当前项目相匹配,等等.

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.

我们在 sbt-server 中提供了一个实用程序,可以更轻松地将设置附加到当前会话中:

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.

这篇关于为什么 sbt.Extracted 在 append 方法时删除先前定义的 TaskKey?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆