diff --git a/build.sbt b/build.sbt index 3c4e4f378..3e375b196 100644 --- a/build.sbt +++ b/build.sbt @@ -380,7 +380,15 @@ lazy val integration = projectMatrix .collect { case p @ LocalProject(n) if n.startsWith("cli3") => p } .map(_ / publishLocalTransitive)): _* ) - .value + .value, + // Mimic sbt-scalafix usage of interfaces (without properties per default) + // to exercise dynamic loading of latest scalafix-properties artifact + Test / internalDependencyClasspath := { + val prev = (Test / internalDependencyClasspath).value + val propertiesClassDirectory = + (properties / Compile / classDirectory).value + prev.filter(_.data != propertiesClassDirectory) + } ) .defaultAxes(VirtualAxis.jvm) .jvmPlatformFull(cliScalaVersions) diff --git a/scalafix-interfaces/src/main/java/scalafix/interfaces/Scalafix.java b/scalafix-interfaces/src/main/java/scalafix/interfaces/Scalafix.java index 6e915d949..b22183855 100644 --- a/scalafix-interfaces/src/main/java/scalafix/interfaces/Scalafix.java +++ b/scalafix-interfaces/src/main/java/scalafix/interfaces/Scalafix.java @@ -163,11 +163,26 @@ static Scalafix fetchAndClassloadInstance(String requestedScalaVersion, List jars = ScalafixCoursier.latestScalafixPropertiesJars(repositories); + URLClassLoader classLoader = + new URLClassLoader(jars.stream().toArray(URL[]::new), null); + + InputStream stream = classLoader.getResourceAsStream(propertiesPath); + properties.load(stream); + } catch (Exception ee) { + throw new ScalafixException( + "Failed to load '" + propertiesPath + "' from local & remote artifacts", + ee); + } } String scalafixVersion = properties.getProperty("scalafixVersion"); diff --git a/scalafix-interfaces/src/main/java/scalafix/internal/interfaces/ScalafixCoursier.java b/scalafix-interfaces/src/main/java/scalafix/internal/interfaces/ScalafixCoursier.java index f0ed2f663..391048b12 100644 --- a/scalafix-interfaces/src/main/java/scalafix/internal/interfaces/ScalafixCoursier.java +++ b/scalafix-interfaces/src/main/java/scalafix/internal/interfaces/ScalafixCoursier.java @@ -15,6 +15,21 @@ public class ScalafixCoursier { + private static VersionListing versions( + List repositories, + coursierapi.Module module + ) throws ScalafixException { + try { + return Versions.create() + .withModule(module) + .withRepositories(repositories.stream().toArray(Repository[]::new)) + .versions() + .getMergedListings(); + } catch (CoursierError e) { + throw new ScalafixException("Failed to list versions for " + module + " from " + repositories, e); + } + } + private static FetchResult fetch( List repositories, List dependencies, @@ -44,6 +59,22 @@ private static List toURLs(FetchResult result) throws ScalafixException { return urls; } + public static List latestScalafixPropertiesJars( + List repositories + ) throws ScalafixException { + Module module = Module.of("ch.epfl.scala", "scalafix-properties"); + String version = versions(repositories, module) + .getAvailable() + .stream() + // Ignore RC & SNAPSHOT versions + .filter(v -> v.startsWith("0.14.2+") || !v.contains("-")) + .reduce((older, newer) -> newer) + .orElseThrow(() -> new ScalafixException("Could not find any stable version for " + module)); + + Dependency scalafixProperties = Dependency.of(module, version); + return toURLs(fetch(repositories, Collections.singletonList(scalafixProperties), ResolutionParams.create())); + } + public static List scalafixCliJars( List repositories, String scalafixVersion,