6

I'm having an issue with getting SBT Subprojects to recognize commands provided by plugins. I have the following plugin source:

object DemoPlugin extends AutoPlugin {
  override lazy val projectSettings = Seq(commands += demoCommand)

  lazy val demoCommand =
    Command.command("demo") { (state: State) =>
      println("Demo Plugin!")
      state
    }
}

Which is used by a project configured as follows:

lazy val root = project in file(".")

lazy val sub = (project in file("sub")).
  enablePlugins(DemoPlugin).
  settings(
    //...
  )

The plugin is, of course, listed in project/plugins.sbt. However, when I open up sbt in the project, I see the following:

> sub/commands
[info] List(sbt.SimpleCommand@413d2cd1)
> sub/demo
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: demo (similar: doc)
[error] sub/demo

Even stranger, using consoleProject, I can see that the command in the project is the one defined by DemoPlugin!

scala> (commands in sub).eval.map { c => c.getClass.getMethod("name").invoke(c) }
res0: Seq[Object] = List(demo)

I'm looking to be able to type sub/demo, and have it perform the demo command. Any help would be much appreciated!

Robert Grider
  • 261
  • 1
  • 7

1 Answers1

1

Commands aren't per-project. They only work for the top-level project.

It's also recommended to try and use tasks, or if needed input tasks where you might want to use a command.

If you really need a command, there's a way to have a sort of "holder" task, see the answer to Can you access a SBT SettingKey inside a Command?

Community
  • 1
  • 1
Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55