11

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.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
  • It's similar to the error `eval` gives. With `eval foo = "hello"` sbt errors with `:1: error: not found: value foo` while `eval name = 4` ends with `:1: error: reassignment to val`. It appears custom settings are not *sourced* in a session. – Jacek Laskowski Jan 03 '14 at 13:07
  • 1
    https://github.com/sbt/sbt/pull/1456 – Kenji Yoshida Jul 21 '14 at 14:36

1 Answers1

8

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
Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • Thanks Eugene. The “currently” and “at this time” in the passage you quote seem to indicate that Mark hopes to lift this limitation in a future version. – Seth Tisue Jan 04 '14 at 17:09