From 7449df5d6d344591cb3bd14a055d4a7acffd139c Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Sat, 5 Jun 2021 21:51:47 +0200 Subject: [PATCH] ensure keys do not need to be silenced with excludeLintKeys Build linting yields false positives when keys are looked up within dynamic tasks: https://github.com/sbt/sbt/issues/5647. Also, clarify which keys must be set in ThisBuild. --- .../scala/scalafix/sbt/ScalafixPlugin.scala | 21 ++++++++++++------- src/sbt-test/sbt-1.4/build-lint/build.sbt | 19 +++++++++++++++++ .../build-lint/project/build.properties | 1 + .../sbt-1.4/build-lint/project/plugins.sbt | 2 ++ src/sbt-test/sbt-1.4/build-lint/test | 1 + 5 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/sbt-test/sbt-1.4/build-lint/build.sbt create mode 100644 src/sbt-test/sbt-1.4/build-lint/project/build.properties create mode 100644 src/sbt-test/sbt-1.4/build-lint/project/plugins.sbt create mode 100644 src/sbt-test/sbt-1.4/build-lint/test diff --git a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index da6b6a9f..d562be57 100644 --- a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala +++ b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala @@ -68,19 +68,20 @@ object ScalafixPlugin extends AutoPlugin { } val scalafixResolvers: SettingKey[Seq[Repository]] = settingKey[Seq[Repository]]( - "Optional list of Maven/Ivy repositories to use for fetching custom rules." + "Optional list of Maven/Ivy repositories to use for fetching custom rules. " + + "Must be set in ThisBuild." ) val scalafixDependencies: SettingKey[Seq[ModuleID]] = settingKey[Seq[ModuleID]]( "Optional list of custom rules to install from Maven Central. " + - "This setting is read from the global scope so it only needs to be defined once in the build." + "Must be set in ThisBuild." ) val scalafixScalaBinaryVersion: SettingKey[String] = settingKey[String]( - "The Scala binary version used for scalafix execution. Defaults to 2.12. " + - s"Rules must be compiled against that binary version, or for advanced rules such as " + - s"ExplicitResultTypes which have a full cross-version, against the corresponding full" + - s"version that scalafix is built against." + "The Scala binary version used for scalafix execution. Must be set in ThisBuild. " + + "Defaults to 2.12. Rules must be compiled against that binary version, or for " + + "advanced rules such as ExplicitResultTypes which have a full cross-version, " + + "against the corresponding full version that scalafix is built against." ) val scalafixConfig: SettingKey[Option[File]] = settingKey[Option[File]]( @@ -96,8 +97,12 @@ object ScalafixPlugin extends AutoPlugin { inConfig(config)( relaxScalacOptionsConfigSettings ++ Seq( scalafix := { - // force detection of usage of `scalafixCaching` to workaround https://github.com/sbt/sbt/issues/5647 - val _ = scalafixCaching.?.value + // force evaluation of keys looked up in the same scope (config) within + // dynamic tasks to workaround https://github.com/sbt/sbt/issues/5647 + val _ = Seq( + scalafixCaching.?.value, + scalafixConfig.?.value + ) scalafixInputTask(config).evaluated }, compile := Def.taskDyn { diff --git a/src/sbt-test/sbt-1.4/build-lint/build.sbt b/src/sbt-test/sbt-1.4/build-lint/build.sbt new file mode 100644 index 00000000..8639100a --- /dev/null +++ b/src/sbt-test/sbt-1.4/build-lint/build.sbt @@ -0,0 +1,19 @@ +ThisBuild / scalafixDependencies := Nil +ThisBuild / scalafixResolvers := Nil +ThisBuild / scalafixScalaBinaryVersion := "2.12" +scalafixCaching := false +scalafixConfig := None +scalafixOnCompile := false + +lazy val checkLintWarns = taskKey[Unit]("") +checkLintWarns := { + // https://github.com/sbt/sbt/blob/v1.4.9/sbt/src/sbt-test/project/lint/build.sbt + val state = Keys.state.value + val includeKeys = + (Global / includeLintKeys).value.map(_.scopedKey.key.label) + val excludeKeys = + (Global / excludeLintKeys).value.map(_.scopedKey.key.label) + val result = + sbt.internal.LintUnused.lintUnused(state, includeKeys, excludeKeys) + assert(result.size == 0) +} diff --git a/src/sbt-test/sbt-1.4/build-lint/project/build.properties b/src/sbt-test/sbt-1.4/build-lint/project/build.properties new file mode 100644 index 00000000..dbae93bc --- /dev/null +++ b/src/sbt-test/sbt-1.4/build-lint/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.4.9 diff --git a/src/sbt-test/sbt-1.4/build-lint/project/plugins.sbt b/src/sbt-test/sbt-1.4/build-lint/project/plugins.sbt new file mode 100644 index 00000000..2d3b4d3b --- /dev/null +++ b/src/sbt-test/sbt-1.4/build-lint/project/plugins.sbt @@ -0,0 +1,2 @@ +resolvers += Resolver.sonatypeRepo("public") +addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version")) diff --git a/src/sbt-test/sbt-1.4/build-lint/test b/src/sbt-test/sbt-1.4/build-lint/test new file mode 100644 index 00000000..ca70dea1 --- /dev/null +++ b/src/sbt-test/sbt-1.4/build-lint/test @@ -0,0 +1 @@ +> checkLintWarns