Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sbt idiomatic way to add settings

Tags:

scala

sbt

I see this a lot in open source sbt projects:

lazy val project = Project(
  id = "root",
  base = file("."),
  settings = Project.defaultSettings ++ Seq(
    ...
  )
)

We adopted this convention for our in-house projects also. However today i tried sbt-ensime with a project like this and running "gen-ensime" gave me an error:

[error] (*:update) java.lang.IllegalArgumentException: Cannot add dependency 'org.scala-lang#scala-compiler;2.11.7' to configuration 'ensime-internal' of module ... because this configuration doesn't exist!

The suggested fix is here: https://github.com/ensime/ensime-sbt/issues/145

It suggests i change my project to:

lazy val project = Project(
  id = "root",
  base = file(".")
).settings(Seq(
  ...
)

My question is: is this suggested way of defining the project idiomatic and preferred for sbt? Do i lose anything by using this (in particular, are the defaultSettings still added to my project)?

like image 840
Koert Kuipers Avatar asked Jan 28 '16 16:01

Koert Kuipers


Video Answer


1 Answers

It seems like difference between those two that

.settings(Seq(...))

is append your sequence to 'settings' in project, but

Project(settings = ...)

just writing settings without saving old values.

Thereby it looks like .settings() is more safe approach.

In general .settings() more idiomatic now, because some sbt plugins may try to modify settings at Project construction.

Few snippets from sbt sources:

/**
 * The explicitly defined sequence of settings that configure this project.
 * These do not include the automatically appended settings as configured by `auto`.
 */
def settings: Seq[Setting[_]]

/** Appends settings to the current settings sequence for this project. */
def settings(ss: Def.SettingsDefinition*): Project = copy(settings = (settings: Seq[Def.Setting[_]]) ++ Def.settings(ss: _*))

// TODO: Modify default settings to be the core settings, and automatically add the IvyModule + JvmPlugins.
def apply(id: String, base: File, aggregate: => Seq[ProjectReference] = Nil, dependencies: => Seq[ClasspathDep[ProjectReference]] = Nil,
    delegates: => Seq[ProjectReference] = Nil, settings: => Seq[Def.Setting[_]] = Nil, configurations: Seq[Configuration] = Nil,
    auto: AddSettings = AddSettings.allDefaults): Project =
    unresolved(id, base, aggregate, dependencies, delegates, settings, configurations, auto, Plugins.empty, Nil) // Note: JvmModule/IvyModule auto included...

def copy(id: String = id, base: File = base, aggregate: => Seq[ProjectReference] = aggregate, dependencies: => Seq[ClasspathDep[ProjectReference]] = dependencies,
    delegates: => Seq[ProjectReference] = delegates, settings: => Seq[Setting[_]] = settings, configurations: Seq[Configuration] = configurations,
    auto: AddSettings = auto): Project =
    unresolved(id, base, aggregate = aggregate, dependencies = dependencies, delegates = delegates, settings, configurations, auto, plugins, autoPlugins)
like image 145
strobe Avatar answered Oct 05 '22 08:10

strobe