Skip to content
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

support running rules from source on WIndows #2035

Merged
merged 1 commit into from
Aug 15, 2024
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
2 changes: 2 additions & 0 deletions docs/developers/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,8 @@ custom rule using the `file:/path/to/NamedLiteralArguments.scala` syntax.
scalafix --rules=file:/path/to/NamedLiteralArguments.scala
```

On Windows, the URI syntax is `file:///C:/Users/...`.

### Using `http:`

Another way to run a rule from source is to publish it as a gist and share the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.nio.file.Path
import java.nio.file.Paths

import scala.collection.concurrent.TrieMap
import scala.util.Try

import scala.meta.io.AbsolutePath

Expand Down Expand Up @@ -100,7 +101,9 @@ object RuleDecoderOps {
)
def unapply(arg: Conf.Str): Option[Configured[Input]] = arg match {
case UriRule("file", uri) =>
val path = AbsolutePath(Paths.get(uri.getSchemeSpecificPart))(cwd)
val path = AbsolutePath(
Try(Paths.get(uri)).getOrElse(Paths.get(uri.getSchemeSpecificPart))
)(cwd)
Option(Ok(Input.File(path.toNIO)))
case UrlRule(Ok(url)) =>
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scalafix.internal.reflect

import java.io.File
import java.net.URLClassLoader
import java.nio.file.Paths
import java.util.function

import metaconfig.Configured
Expand Down Expand Up @@ -49,7 +50,9 @@ class ScalafixToolbox {
): Configured[CompiledRules] =
synchronized {
val classpath =
toolClasspath.getURLs.map(_.getPath).mkString(File.pathSeparator)
toolClasspath.getURLs
.map(url => Paths.get(url.toURI))
.mkString(File.pathSeparator)
val compiler = compilerCache.computeIfAbsent(classpath, newCompiler)
(
compiler.compile(code) |@|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.scalatest.funsuite.AnyFunSuite
import scalafix.interfaces.Scalafix
import scalafix.interfaces.ScalafixDiagnostic
import scalafix.interfaces.ScalafixMainCallback
import scalafix.internal.tests.utils.SkipWindows

/**
* Tests in this suite require scalafix-cli & its dependencies to be
Expand All @@ -28,10 +27,7 @@ class ScalafixSuite extends AnyFunSuite {
}

def fetchAndLoad(scalaVersion: String): Unit = {
test(
s"fetch & load instance for Scala version $scalaVersion",
SkipWindows
) {
test(s"fetch & load instance for Scala version $scalaVersion") {
val scalafixAPI = Scalafix.fetchAndClassloadInstance(
scalaVersion,
Seq[Repository](
Expand Down Expand Up @@ -66,11 +62,11 @@ class ScalafixSuite extends AnyFunSuite {
maybeDiagnostic = Some(diagnostic)
}
args
.withPaths(
Seq(ruleSource).asJava
) // any file would do, we just want rules to be loaded
.withRules(Seq(s"file:$ruleSource").asJava)
.withRules(Seq(ruleSource.toUri.toString).asJava)
.withMainCallback(scalafixMainCallback)
// any target file would do to emit a diagnostic, so run the rule on itself
.withPaths(List(ruleSource).asJava)
.withWorkingDirectory(ruleSource.getParent)
.run()
assert(maybeDiagnostic.get.message.startsWith(scalaVersion))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@ class RuleDecoderSuite extends AnyFunSuite {
val decoder: ConfDecoder[Rules] = RuleDecoder.decoder(decoderSettings)
val expectedName = "NoDummy"

test("absolute path resolves as is", SkipWindows) {
test("absolute path resolves as URI") {
val rules = decoder.read(Conf.Str(abspath.toURI.toString)).get
assert(expectedName == rules.name.value)
}

test(
"absolute path resolves as is",
// heading slashes are needed on Windows, see https://en.wikipedia.org/wiki/File_URI_scheme#Examples
SkipWindows
) {
val rules = decoder.read(Conf.Str(s"file:$abspath")).get
assert(expectedName == rules.name.value)
}

test("relative resolves from custom working directory", SkipWindows) {
test("relative resolves from custom working directory") {
val rules = decoder.read(Conf.Str(s"file:$relpath")).get
assert(expectedName == rules.name.value)
}

test("resolved classes can be reloaded", SkipWindows) {
test("resolved classes can be reloaded") {
val tmp = Files.createTempFile("scalafix", "CustomRule.scala")

val customRuleV1 =
Expand All @@ -49,7 +58,7 @@ class RuleDecoderSuite extends AnyFunSuite {
""".stripMargin
Files.write(tmp, customRuleV1.getBytes)
val rules1 =
decoder.read(Conf.Str(s"file:${tmp.toFile.getAbsolutePath}")).get
decoder.read(Conf.Str(tmp.toUri.toString)).get
val class1 = rules1.rules.head.getClass

val customRuleV2 =
Expand All @@ -61,7 +70,7 @@ class RuleDecoderSuite extends AnyFunSuite {
""".stripMargin
Files.write(tmp, customRuleV2.getBytes)
val rules2 =
decoder.read(Conf.Str(s"file:${tmp.toFile.getAbsolutePath}")).get
decoder.read(Conf.Str(tmp.toUri.toString)).get
val class2 = rules2.rules.head.getClass

assert(!class1.isAssignableFrom(class2))
Expand Down