Skip to content

Commit

Permalink
fix session caching for the default scalafixInterface & parser
Browse files Browse the repository at this point in the history
Regression introduced in 2ca68e4: to be memoized, settings must be
bound to a setting key and defined on a scope.
  • Loading branch information
github-brice-jaglin committed Jul 7, 2020
1 parent 0e65416 commit 9a283a7
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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[_]] =
Expand All @@ -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,
Expand Down Expand Up @@ -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))
)

Expand All @@ -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))
)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
()
}

Expand Down

0 comments on commit 9a283a7

Please sign in to comment.