Skip to content

Commit db281f2

Browse files
committed
allow usage of scalafix-interfaces without scalafix-properties
This non-deterministic fallback will allow scalafixEnable to work in sbt-scalafix even though no scalafix-interfaces was explicitly provided.
1 parent b6943bf commit db281f2

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

build.sbt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,15 @@ lazy val integration = projectMatrix
380380
.collect { case p @ LocalProject(n) if n.startsWith("cli3") => p }
381381
.map(_ / publishLocalTransitive)): _*
382382
)
383-
.value
383+
.value,
384+
// Mimic sbt-scalafix usage of interfaces (without properties per default)
385+
// to exercise dynamic loading of latest scalafix-properties artifact
386+
Test / internalDependencyClasspath := {
387+
val prev = (Test / internalDependencyClasspath).value
388+
val propertiesClassDirectory =
389+
(properties / Compile / classDirectory).value
390+
prev.filter(_.data != propertiesClassDirectory)
391+
}
384392
)
385393
.defaultAxes(VirtualAxis.jvm)
386394
.jvmPlatformFull(cliScalaVersions)

scalafix-interfaces/src/main/java/scalafix/interfaces/Scalafix.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,26 @@ static Scalafix fetchAndClassloadInstance(String requestedScalaVersion, List<Rep
163163

164164
Properties properties = new Properties();
165165
String propertiesPath = "scalafix-interfaces.properties";
166-
InputStream stream = Scalafix.class.getClassLoader().getResourceAsStream(propertiesPath);
167166
try {
167+
InputStream stream = Scalafix.class.getClassLoader().getResourceAsStream(propertiesPath);
168168
properties.load(stream);
169-
} catch (IOException | NullPointerException e) {
170-
throw new ScalafixException("Failed to load '" + propertiesPath + "' to lookup versions", e);
169+
} catch (Exception e) {
170+
System.err.println(
171+
"Failed to load '" + propertiesPath + "' from local artifact, " +
172+
"falling back to fetching the latest scalafix version...");
173+
174+
try {
175+
List<URL> jars = ScalafixCoursier.latestScalafixPropertiesJars(repositories);
176+
URLClassLoader classLoader =
177+
new URLClassLoader(jars.stream().toArray(URL[]::new), null);
178+
179+
InputStream stream = classLoader.getResourceAsStream(propertiesPath);
180+
properties.load(stream);
181+
} catch (Exception ee) {
182+
throw new ScalafixException(
183+
"Failed to load '" + propertiesPath + "' from local & remote artifacts",
184+
ee);
185+
}
171186
}
172187

173188
String scalafixVersion = properties.getProperty("scalafixVersion");

scalafix-interfaces/src/main/java/scalafix/internal/interfaces/ScalafixCoursier.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515

1616
public class ScalafixCoursier {
1717

18+
private static VersionListing versions(
19+
List<Repository> repositories,
20+
coursierapi.Module module
21+
) throws ScalafixException {
22+
try {
23+
return Versions.create()
24+
.withModule(module)
25+
.withRepositories(repositories.stream().toArray(Repository[]::new))
26+
.versions()
27+
.getMergedListings();
28+
} catch (CoursierError e) {
29+
throw new ScalafixException("Failed to list versions for " + module + " from " + repositories, e);
30+
}
31+
}
32+
1833
private static FetchResult fetch(
1934
List<Repository> repositories,
2035
List<Dependency> dependencies,
@@ -44,6 +59,16 @@ private static List<URL> toURLs(FetchResult result) throws ScalafixException {
4459
return urls;
4560
}
4661

62+
public static List<URL> latestScalafixPropertiesJars(
63+
List<Repository> repositories
64+
) throws ScalafixException {
65+
Module module = Module.of("ch.epfl.scala", "scalafix-properties");
66+
// TODO: switch to release once we have a stable cut
67+
String version = versions(repositories, module).getLatest();
68+
Dependency scalafixProperties = Dependency.of(module, version);
69+
return toURLs(fetch(repositories, Collections.singletonList(scalafixProperties), ResolutionParams.create()));
70+
}
71+
4772
public static List<URL> scalafixCliJars(
4873
List<Repository> repositories,
4974
String scalafixVersion,

0 commit comments

Comments
 (0)