-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce scalafixAll InputTask #126
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,10 @@ import com.geirsson.coursiersmall.Repository | |
import sbt.KeyRanks.Invisible | ||
import sbt.Keys._ | ||
import sbt._ | ||
import sbt.complete._ | ||
import sbt.internal.sbtscalafix.Caching._ | ||
import sbt.internal.sbtscalafix.{Compat, JLineAccess} | ||
import sbt.plugins.JvmPlugin | ||
import sbt.internal.sbtscalafix.Caching._ | ||
import scalafix.interfaces.ScalafixError | ||
import scalafix.internal.sbt._ | ||
|
||
|
@@ -29,9 +30,14 @@ object ScalafixPlugin extends AutoPlugin { | |
|
||
val scalafix: InputKey[Unit] = | ||
inputKey[Unit]( | ||
"Run scalafix rule in this project and configuration. " + | ||
"Run scalafix rule(s) in this project and configuration. " + | ||
"For example: scalafix RemoveUnusedImports. " + | ||
"To run on test sources use test:scalafix." | ||
"To run on test sources use test:scalafix or scalafixAll." | ||
) | ||
val scalafixAll: InputKey[Unit] = | ||
inputKey[Unit]( | ||
"Run scalafix rule(s) in this project, for all configurations where scalafix is enabled. " + | ||
"Compile and Test are enabled by default, other configurations can be enabled via scalafixConfigSettings." | ||
) | ||
val scalafixCaching: SettingKey[Boolean] = | ||
settingKey[Boolean]( | ||
|
@@ -100,22 +106,12 @@ object ScalafixPlugin extends AutoPlugin { | |
override lazy val projectSettings: Seq[Def.Setting[_]] = | ||
Seq(Compile, Test).flatMap(c => inConfig(c)(scalafixConfigSettings(c))) ++ | ||
inConfig(ScalafixConfig)(Defaults.configSettings) ++ | ||
Seq(ivyConfigurations += ScalafixConfig) | ||
Seq( | ||
ivyConfigurations += ScalafixConfig, | ||
scalafixAll := scalafixAllInputTask.evaluated | ||
) | ||
|
||
override lazy val globalSettings: Seq[Def.Setting[_]] = Seq( | ||
initialize := { | ||
val _ = initialize.value | ||
// Ideally, we would not resort to storing mutable state in `initialize`. | ||
// The optimal solution would be to run `scalafixDependencies.value` | ||
// inside `scalafixInputTask`. | ||
// However, we can't do that due to an sbt bug: | ||
// https://github.com/sbt/sbt/issues/3572#issuecomment-417582703 | ||
workingDirectory = baseDirectory.in(ThisBuild).value.toPath | ||
scalafixInterface = ScalafixInterface.fromToolClasspath( | ||
scalafixDependencies = scalafixDependencies.in(ThisBuild).value, | ||
scalafixCustomResolvers = scalafixResolvers.in(ThisBuild).value | ||
) | ||
}, | ||
scalafixConfig := None, // let scalafix-cli try to infer $CWD/.scalafix.conf | ||
scalafixCaching := false, | ||
scalafixResolvers := ScalafixCoursier.defaultResolvers, | ||
|
@@ -124,12 +120,28 @@ object ScalafixPlugin extends AutoPlugin { | |
) | ||
|
||
lazy val stdoutLogger = Compat.ConsoleLogger(System.out) | ||
private var workingDirectory = file("").getAbsoluteFile.toPath | ||
private var scalafixInterface: () => ScalafixInterface = | ||
() => throw new UninitializedError | ||
|
||
private val scalafixInterface: Def.Initialize[() => ScalafixInterface] = | ||
Def.setting { | ||
ScalafixInterface.fromToolClasspath( | ||
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, | ||
projectDepsExternal: Seq[ModuleID], | ||
baseResolvers: Seq[Repository], | ||
projectDepsInternal: Seq[File] | ||
|
@@ -158,20 +170,49 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome to see a workaround for this issue, nice job tracking down the issue! |
||
InputTask | ||
.createDyn(InputTask.initParserAsInput(parser))( | ||
Def.task(shellArgs => scalafixAllTask(shellArgs, thisProject.value)) | ||
) | ||
|
||
private def scalafixAllTask( | ||
shellArgs: ShellArgs, | ||
project: ResolvedProject | ||
): Def.Initialize[Task[Unit]] = | ||
Def.taskDyn { | ||
val configsWithScalafixInputKey = project.settings | ||
.map(_.key) | ||
.filter(_.key == scalafix.key) | ||
.flatMap(_.scope.config.toOption) | ||
|
||
configsWithScalafixInputKey | ||
.map(config => scalafixTask(shellArgs, config)) | ||
.joinWith(_.join.map(_ => ())) | ||
} | ||
|
||
private def scalafixInputTask( | ||
config: Configuration | ||
): Def.Initialize[InputTask[Unit]] = | ||
Def.inputTaskDyn { | ||
val shell0 = new ScalafixCompletions( | ||
workingDirectory = () => workingDirectory, | ||
// Unfortunately, local rules will not show up as completions in the parser, as that parser can only depend | ||
// on global settings (see note in initialize), while local rules classpath must | ||
// be looked up via tasks on projects | ||
loadedRules = () => scalafixInterface().availableRules(), | ||
terminalWidth = Some(JLineAccess.terminalWidth) | ||
).parser.parsed | ||
// workaround https://github.com/sbt/sbt/issues/3572 by invoking directly what Def.inputTaskDyn would via macro | ||
InputTask | ||
.createDyn(InputTask.initParserAsInput(parser))( | ||
Def.task(shellArgs => scalafixTask(shellArgs, config)) | ||
) | ||
|
||
private def scalafixTask( | ||
shellArgs: ShellArgs, | ||
config: ConfigKey | ||
): Def.Initialize[Task[Unit]] = | ||
Def.taskDyn { | ||
val errorLogger = | ||
new PrintStream(LoggingOutputStream(streams.value.log, Level.Error)) | ||
new PrintStream( | ||
LoggingOutputStream( | ||
streams.in(config, scalafix).value.log, | ||
Level.Error | ||
) | ||
) | ||
val projectDepsInternal = products.in(ScalafixConfig).value ++ | ||
internalDependencyClasspath.in(ScalafixConfig).value.map(_.data) | ||
val projectDepsExternal = | ||
|
@@ -180,18 +221,21 @@ object ScalafixPlugin extends AutoPlugin { | |
.value | ||
.flatMap(_.get(moduleID.key)) | ||
|
||
if (shell0.rules.isEmpty && shell0.extra == List("--help")) { | ||
if (shellArgs.rules.isEmpty && shellArgs.extra == List("--help")) { | ||
scalafixHelp | ||
} else { | ||
val scalafixConf = scalafixConfig.value.map(_.toPath) | ||
val scalafixConf = scalafixConfig.in(config).value.map(_.toPath) | ||
val (shell, mainInterface0) = scalafixArgsFromShell( | ||
shell0, | ||
shellArgs, | ||
scalafixInterface.value, | ||
projectDepsExternal, | ||
scalafixResolvers.in(ThisBuild).value, | ||
projectDepsInternal | ||
) | ||
val maybeNoCache = | ||
if (shell.noCache || !scalafixCaching.value) Seq(Arg.NoCache) else Nil | ||
if (shell.noCache || !scalafixCaching.in(config).value) | ||
Seq(Arg.NoCache) | ||
else Nil | ||
val mainInterface = mainInterface0 | ||
.withArgs(maybeNoCache: _*) | ||
.withArgs( | ||
|
@@ -212,32 +256,35 @@ object ScalafixPlugin extends AutoPlugin { | |
} | ||
private def scalafixHelp: Def.Initialize[Task[Unit]] = | ||
Def.task { | ||
scalafixInterface().withArgs(Arg.ParsedArgs(List("--help"))).run() | ||
scalafixInterface.value().withArgs(Arg.ParsedArgs(List("--help"))).run() | ||
() | ||
} | ||
|
||
private def scalafixSyntactic( | ||
mainInterface: ScalafixInterface, | ||
shellArgs: ShellArgs, | ||
config: Configuration | ||
config: ConfigKey | ||
): Def.Initialize[Task[Unit]] = | ||
Def.task { | ||
val files = filesToFix(shellArgs, config).value | ||
runArgs(mainInterface.withArgs(Arg.Paths(files)), streams.value) | ||
runArgs( | ||
mainInterface.withArgs(Arg.Paths(files)), | ||
streams.in(config, scalafix).value | ||
) | ||
} | ||
|
||
private def scalafixSemantic( | ||
ruleNames: Seq[String], | ||
mainArgs: ScalafixInterface, | ||
shellArgs: ShellArgs, | ||
config: Configuration | ||
config: ConfigKey | ||
): Def.Initialize[Task[Unit]] = | ||
Def.taskDyn { | ||
val dependencies = allDependencies.value | ||
val dependencies = allDependencies.in(config).value | ||
val files = filesToFix(shellArgs, config).value | ||
val withScalaInterface = mainArgs.withArgs( | ||
Arg.ScalaVersion(scalaVersion.value), | ||
Arg.ScalacOptions(scalacOptions.value) | ||
Arg.ScalacOptions(scalacOptions.in(config).value) | ||
) | ||
val errors = new SemanticRuleValidator( | ||
new SemanticdbNotFound(ruleNames, scalaVersion.value, sbtVersion.value) | ||
|
@@ -246,9 +293,12 @@ object ScalafixPlugin extends AutoPlugin { | |
Def.task { | ||
val semanticInterface = withScalaInterface.withArgs( | ||
Arg.Paths(files), | ||
Arg.Classpath(fullClasspath.value.map(_.data.toPath)) | ||
Arg.Classpath(fullClasspath.in(config).value.map(_.data.toPath)) | ||
) | ||
runArgs( | ||
semanticInterface, | ||
streams.in(config, scalafix).value | ||
) | ||
runArgs(semanticInterface, streams.value) | ||
} | ||
} else { | ||
Def.task { | ||
|
@@ -389,7 +439,7 @@ object ScalafixPlugin extends AutoPlugin { | |
} | ||
private def filesToFix( | ||
shellArgs: ShellArgs, | ||
config: Configuration | ||
config: ConfigKey | ||
): Def.Initialize[Task[Seq[Path]]] = | ||
Def.taskDyn { | ||
// Dynamic task to avoid redundantly computing `unmanagedSources.value` | ||
|
@@ -400,7 +450,7 @@ object ScalafixPlugin extends AutoPlugin { | |
} else { | ||
Def.task { | ||
for { | ||
source <- unmanagedSources.in(config).in(scalafix).value | ||
source <- unmanagedSources.in(config, scalafix).value | ||
if source.exists() | ||
if isScalaFile(source) | ||
} yield source.toPath | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
-> ; example/scalafix --test ; example/it:scalafix --test | ||
-> example/scalafixAll --test | ||
> example/it:scalafix | ||
> example/it:scalafix --test | ||
-> example/scalafix --test | ||
> tests/test | ||
> example/scalafixAll | ||
> example/scalafixAll --test | ||
> example/scalafix --test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,24 @@ $ exec chmod 000 src/test/scala/Valid.scala | |
$ delete src/main/scala | ||
$ delete src/test/scala | ||
|
||
# scalafixAll and scalafix should share the same cache per configuration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👏 |
||
> set scalafixConfig := None | ||
$ mkdir src/main/scala | ||
$ mkdir src/test/scala | ||
$ copy-file files/UnusedImports.scala src/main/scala/Valid.scala | ||
$ copy-file files/ProcedureSyntax.scala src/test/scala/InitiallyInvalid.scala | ||
-> scalafixAll --check ProcedureSyntax | ||
$ exec chmod 000 src/main/scala/Valid.scala | ||
> scalafix --check ProcedureSyntax | ||
-> test:scalafix --check ProcedureSyntax | ||
> test:scalafix ProcedureSyntax | ||
$ exec chmod 000 src/test/scala/InitiallyInvalid.scala | ||
> scalafix --check ProcedureSyntax | ||
> test:scalafix --check ProcedureSyntax | ||
> scalafixAll --check ProcedureSyntax | ||
$ delete src/main/scala | ||
$ delete src/test/scala | ||
|
||
# make sure the cache is disabled when using `--stdout` | ||
> set scalafixConfig := None | ||
$ mkdir src/main/scala | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By being a
Setting
, the function wrapped here is instantiated at sbt load time (capturing other globalscalafix*
settings) as it's referenced byscalafix[All]InputTask
throughparser
, but evaluated at task execution time, and since it's backed by aLazyValue
, it's memoized.