Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "set" can't assign value to custom SettingKey I can "show" in sbt shell?

Tags:

scala

sbt

The following is a sbt 0.13.1 project with a custom setting and a value for it:

% pwd
/Users/tisue/myproj
% ls
build.sbt
% cat build.sbt
val foo = settingKey[String]("This is a custom setting")

foo := "bar"
% sbt
[info] Set current project to myproj (in build file:/Users/tisue/myproj/)
> show foo
[info] bar

So far so good. But now:

> set foo := "qux"
<set>:1: error: not found: value foo
foo := "qux"
^
[error] Type error in expression

Shouldn't this work?

I partially understand what's going wrong here; set evaluates a Scala expression, and that expression is apparently being compiled in a context where val foo is not in scope.

But I would expect that the magic that makes sure foo is in scope when foo := ... is compiled from the .sbt file, would also be in effect when the same thing is compiled in the shell.

like image 599
Seth Tisue Avatar asked Jan 03 '14 04:01

Seth Tisue


1 Answers

As of release 0.13.6 (2014-09-12) this is no longer a limitation (#1059/#1456)


Original Answer - for any projects using sbt 0.13.0 - 0.13.5

As it turns out 0.13.0 changes clearly state that this is an expected behavior:

All definitions are compiled before settings, but it will probably be best practice to put definitions together. Currently, the visibility of definitions is restricted to the .sbt file it is defined in. They are not visible in consoleProject or the set command at this time, either. Use Scala files in project/ for visibility in all .sbt files.

With this, you should be able to share the setting after it's defined in project/Build.scala as follows:

import sbt._
import Keys._

object HelloBuild extends Build {
  lazy val foo = settingKey[String]("This is a custom setting")

  foo := "Build.scala"
}

build.sbt

scalaVersion := "2.10.4-RC1"

foo := "build.sbt"

Then, in sbt shell:

[sbt-0-13-1]> foo
[info] build.sbt
[sbt-0-13-1]> set foo := "shell"
[info] Defining *:foo
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[sbt-0-13-1]> foo
[info] shell
like image 160
Eugene Yokota Avatar answered Oct 13 '22 23:10

Eugene Yokota