How to enable compiler plugin from libraryDependencies?

sbt

Solution

You probably want something like this (see sbt API)

scalacOptions ++= {
  val compileConfig = update.value.configurations.find(_.configuration == "compile").get
  val pluginModule = compileConfig.modules.find(_.module.name contains "continuations-plugin").get
  val pluginFile = pluginModule.artifacts.head._2
  Seq(s"-Xplugin:${pluginFile.getCanonicalPath}", "-P:continuations:enable")
}

The compilePlugin method is designed to ease the task when you are using Ivy to resolve the plugins, and you can do so correctly.

Option #2 is to try to ensure their is a transitive `compiler-plugin->compiler-plugin` configuration dependency chain between your project and the project where you discover the continuations plugin (where eventually there will be a `compiler-plugin->default(compile)` link). However, without seeing your dependency tree, I can't advice around option #2 (which is the more robust method).

Problem

I have a compiler plugin in library dependencies and would like to enable it. Something like ``` autoCompilerPlugins := true libraryDependencies += compilerPlugin(update.value.allModules.find(_.name contains "continuations-plugin").get) scalacOptions += "-P:continuations:enable" ``` gives ``` /Users/luc/scala/release-sanity-check/build.sbt:20: error: A setting cannot depend on a task libraryDependencies += compilerPlugin(update.value.allModules.find(_.name contains "continuations-plugin").get) ^ ``` Can I do it with a custom task? ``` val addContinuationsPlugin = taskKey[Unit]("Add continuations plugin") addContinuationsPlugin := { val plugin = update.value.allModules.find(_.name contains "continuations-plugin") // add plugin? } ``` Repository in question: https://github.com/scala/scala-dist-smoketest

Original source

Related problems