From 9a283a74c4a27770d42915c191e84db17cd1ef73 Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Tue, 7 Jul 2020 08:46:53 +0200 Subject: [PATCH] fix session caching for the default scalafixInterface & parser Regression introduced in 2ca68e4: to be memoized, settings must be bound to a setting key and defined on a scope. --- .../scala/scalafix/sbt/ScalafixPlugin.scala | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index 7df562be..3a98e409 100644 --- a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala +++ b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala @@ -123,6 +123,20 @@ object ScalafixPlugin extends AutoPlugin { Invisible ) + private val scalafixInterfaceProvider: SettingKey[() => ScalafixInterface] = + SettingKey( + "scalafixInterfaceProvider", + "Implementation detail - do not use", + Invisible + ) + + private val scalafixParser: SettingKey[Parser[ShellArgs]] = + SettingKey( + "defaultScalafixInterface", + "Implementation detail - do not use", + Invisible + ) + override lazy val projectConfigurations: Seq[Configuration] = Seq(ScalafixConfig) @@ -147,7 +161,19 @@ object ScalafixPlugin extends AutoPlugin { ) ), scalafixDependencies := Nil, - commands += ScalafixEnable.command + commands += ScalafixEnable.command, + scalafixInterfaceProvider := ScalafixInterface.fromToolClasspath( + scalafixScalaBinaryVersion.in(ThisBuild).value, + scalafixDependencies = scalafixDependencies.in(ThisBuild).value, + scalafixCustomResolvers = scalafixResolvers.in(ThisBuild).value + ), + scalafixParser := new ScalafixCompletions( + workingDirectory = baseDirectory.in(ThisBuild).value.toPath, + // Unfortunately, local rules will not show up as completions in the parser, as that parser can only + // depend on settings, while local rules classpath must be looked up via tasks + loadedRules = () => scalafixInterfaceProvider.value().availableRules(), + terminalWidth = Some(JLineAccess.terminalWidth) + ).parser ) override def buildSettings: Seq[Def.Setting[_]] = @@ -157,25 +183,6 @@ object ScalafixPlugin extends AutoPlugin { lazy val stdoutLogger = Compat.ConsoleLogger(System.out) - private val scalafixInterface: Def.Initialize[() => ScalafixInterface] = - Def.setting { - ScalafixInterface.fromToolClasspath( - scalafixScalaBinaryVersion.in(ThisBuild).value, - scalafixDependencies = scalafixDependencies.in(ThisBuild).value, - scalafixCustomResolvers = scalafixResolvers.in(ThisBuild).value - ) - } - - private val parser: Def.Initialize[Parser[ShellArgs]] = Def.setting { - new ScalafixCompletions( - workingDirectory = baseDirectory.in(ThisBuild).value.toPath, - // Unfortunately, local rules will not show up as completions in the parser, as that parser can only - // depend on settings, while local rules classpath must be looked up via tasks - loadedRules = () => scalafixInterface.value().availableRules(), - terminalWidth = Some(JLineAccess.terminalWidth) - ).parser - } - private def scalafixArgsFromShell( shell: ShellArgs, scalafixInterface: () => ScalafixInterface, @@ -222,7 +229,7 @@ object ScalafixPlugin extends AutoPlugin { private def scalafixAllInputTask(): Def.Initialize[InputTask[Unit]] = // workaround https://github.com/sbt/sbt/issues/3572 by invoking directly what Def.inputTaskDyn would via macro InputTask - .createDyn(InputTask.initParserAsInput(parser))( + .createDyn(InputTask.initParserAsInput(scalafixParser))( Def.task(shellArgs => scalafixAllTask(shellArgs, thisProject.value)) ) @@ -246,7 +253,7 @@ object ScalafixPlugin extends AutoPlugin { ): Def.Initialize[InputTask[Unit]] = // workaround https://github.com/sbt/sbt/issues/3572 by invoking directly what Def.inputTaskDyn would via macro InputTask - .createDyn(InputTask.initParserAsInput(parser))( + .createDyn(InputTask.initParserAsInput(scalafixParser))( Def.task(shellArgs => scalafixTask(shellArgs, config)) ) @@ -276,7 +283,7 @@ object ScalafixPlugin extends AutoPlugin { val scalafixConf = scalafixConfig.in(config).value.map(_.toPath) val (shell, mainInterface0) = scalafixArgsFromShell( shellArgs, - scalafixInterface.value, + scalafixInterfaceProvider.value, projectDepsExternal, scalafixDependencies.in(ThisBuild).value, scalafixResolvers.in(ThisBuild).value, @@ -306,7 +313,10 @@ object ScalafixPlugin extends AutoPlugin { } private def scalafixHelp: Def.Initialize[Task[Unit]] = Def.task { - scalafixInterface.value().withArgs(Arg.ParsedArgs(List("--help"))).run() + scalafixInterfaceProvider + .value() + .withArgs(Arg.ParsedArgs(List("--help"))) + .run() () }