Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

prevent concurrent modifications of sources by limiting concurrency #153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main/scala/scalafix/sbt/ScalafixPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ object ScalafixPlugin extends AutoPlugin {
override def requires: Plugins = JvmPlugin

object autoImport {
@deprecated("not used internally, use your own tag", "0.9.17")
val Scalafix = Tags.Tag("scalafix")

val ScalafixConfig = config("scalafix")
Expand Down Expand Up @@ -207,7 +206,8 @@ object ScalafixPlugin extends AutoPlugin {
scalafixInterfaceCache := new BlockingCache[
ToolClasspath,
ScalafixInterface
]
],
concurrentRestrictions += Tags.exclusiveGroup(Scalafix)
)

override def buildSettings: Seq[Def.Setting[_]] =
Expand Down Expand Up @@ -324,8 +324,8 @@ object ScalafixPlugin extends AutoPlugin {
private def scalafixTask(
shellArgs: ShellArgs,
config: ConfigKey
): Def.Initialize[Task[Unit]] =
Def.taskDyn {
): Def.Initialize[Task[Unit]] = {
val task = Def.taskDyn {
val errorLogger =
new PrintStream(
LoggingOutputStream(
Expand Down Expand Up @@ -375,6 +375,9 @@ object ScalafixPlugin extends AutoPlugin {
}
}
}
task.tag(Scalafix)
}

private def scalafixHelp: Def.Initialize[Task[Unit]] =
Def.task {
scalafixInterfaceProvider
Expand Down
33 changes: 33 additions & 0 deletions src/sbt-test/sbt-scalafix/concurrency/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
val V = _root_.scalafix.sbt.BuildInfo

scalaVersion := V.scala212
libraryDependencies += "ch.epfl.scala" %% "scalafix-core" % V.scalafixVersion % ScalafixConfig

// make it possible to run scalafix in parallel with other tasks via the `all` command
// (work around "Cannot mix input tasks with plain tasks/settings")
val scalafixAddFooter = taskKey[Unit]("task wrapping scalafix")

val addHeader = taskKey[Unit]("")

val check = taskKey[Unit]("")

inConfig(Compile)(
Seq(
scalafixAddFooter := scalafix.toTask(" --rules=class:fix.AddFooter").value,
addHeader := {
unmanagedSources.value.foreach { file =>
val content = IO.read(file)
// artificial delay between read/parsing and write/patching to exercise concurrency
Thread.sleep(10000)
IO.write(file, s"// HEADER\n$content")
}
},
check := {
unmanagedSources.value.foreach { file =>
val content = IO.read(file)
assert(content.contains("FOOTER"), s"missing footer in\n<<<$content>>>")
assert(content.contains("HEADER"), s"missing header in\n<<<$content>>>")
}
}
)
)
2 changes: 2 additions & 0 deletions src/sbt-test/sbt-scalafix/concurrency/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resolvers += Resolver.sonatypeRepo("public")
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % sys.props("plugin.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fix

import scalafix.v1._

class AddFooter extends SyntacticRule("AddFooter") {
override def fix(implicit doc: SyntacticDocument): Patch = {
// artificial delay between read/parsing and write/patching to exercise concurrency
Thread.sleep(1000)
Patch.addRight(doc.tree, "\n// FOOTER\n")
}
}
5 changes: 5 additions & 0 deletions src/sbt-test/sbt-scalafix/concurrency/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# warm up so that tasks themselves (not their dependencies) run in parallel
> scalafix:compile

> all addHeader scalafixAddFooter
> check