From 64dadd64594b13b77edf0d49c394e1b296fb07e4 Mon Sep 17 00:00:00 2001 From: bitloi <89318445+bitloi@users.noreply.github.com> Date: Fri, 23 Jan 2026 11:41:16 -0500 Subject: [PATCH] [2.x] fix: Throw on addCompilerPlugin(foo % Test) (#8622) Problem When using `addCompilerPlugin((dependency) % Test)`, the compiler plugin was incorrectly added to BOTH `test:scalacOptions` AND `compile:scalacOptions`, instead of only `test:scalacOptions`. Solution Throw when the dependency is scoped, since we do not support this use case. --- main/src/main/scala/sbt/Defaults.scala | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/main/src/main/scala/sbt/Defaults.scala b/main/src/main/scala/sbt/Defaults.scala index dcd16d260..67f2bd8fc 100644 --- a/main/src/main/scala/sbt/Defaults.scala +++ b/main/src/main/scala/sbt/Defaults.scala @@ -4642,8 +4642,19 @@ trait BuildExtra extends BuildCommon with DefExtra { } /** Transforms `dependency` to be in the auto-compiler plugin configuration. */ - def compilerPlugin(dependency: ModuleID): ModuleID = - dependency.withConfigurations(Some("plugin->default(compile)")) + def compilerPlugin(dependency: ModuleID): ModuleID = { + dependency.configurations match { + case Some(confs) if confs.toLowerCase != "compile" && confs.nonEmpty => + sys.error( + s"""Configuration-scoped compiler plugins are not supported. + |Found: addCompilerPlugin(... % $confs) + |Use: addCompilerPlugin(...) without configuration scope. + |The plugin will be applied to all configurations.""".stripMargin + ) + case _ => + dependency.withConfigurations(Some("plugin->default(compile)")) + } + } /** Adds `dependency` to `libraryDependencies` in the auto-compiler plugin configuration. */ def addCompilerPlugin(dependency: ModuleID): Setting[Seq[ModuleID]] =