Skip to content

allow usage of scalafix-interfaces without scalafix-properties #2231

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

Merged
merged 2 commits into from
Apr 15, 2025
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
10 changes: 9 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,26 @@ static Scalafix fetchAndClassloadInstance(String requestedScalaVersion, List<Rep

Properties properties = new Properties();
String propertiesPath = "scalafix-interfaces.properties";
InputStream stream = Scalafix.class.getClassLoader().getResourceAsStream(propertiesPath);
try {
InputStream stream = Scalafix.class.getClassLoader().getResourceAsStream(propertiesPath);
properties.load(stream);
} catch (IOException | NullPointerException e) {
throw new ScalafixException("Failed to load '" + propertiesPath + "' to lookup versions", e);
} catch (Exception e) {
System.err.println(
"Failed to load '" + propertiesPath + "' from local artifact, " +
"falling back to fetching the latest scalafix version...");

try {
List<URL> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@

public class ScalafixCoursier {

private static VersionListing versions(
List<Repository> 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<Repository> repositories,
List<Dependency> dependencies,
Expand Down Expand Up @@ -44,6 +59,22 @@ private static List<URL> toURLs(FetchResult result) throws ScalafixException {
return urls;
}

public static List<URL> latestScalafixPropertiesJars(
List<Repository> 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<URL> scalafixCliJars(
List<Repository> repositories,
String scalafixVersion,
Expand Down