Skip to content

Commit 96401b6

Browse files
authored
Merge pull request #10960 from adpi2/sbt-bridge
Stop using the CompilerClassLoader in sbt-bridge
2 parents ea87402 + c93f13f commit 96401b6

File tree

11 files changed

+41
-27
lines changed

11 files changed

+41
-27
lines changed
Submodule PPrint updated 1 file
Submodule fansi updated 1 file
Submodule geny updated 1 file
Submodule os-lib updated 1 file
Submodule scas updated 1 file
Submodule upickle updated 1 file
Submodule utest updated 1 file

sbt-bridge/src/xsbt/CompilerClassLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ private static ClassLoader computeFixedLoader(ClassLoader bridgeLoader) {
119119
throw new RuntimeException(e);
120120
}
121121
}
122-
}
122+
}

sbt-bridge/src/xsbt/CompilerInterface.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,50 @@
66
import xsbti.AnalysisCallback;
77
import xsbti.Logger;
88
import xsbti.Reporter;
9-
import xsbti.Severity;
109
import xsbti.compile.*;
1110

12-
import java.io.File;
13-
14-
import dotty.tools.dotc.core.Contexts.ContextBase;
1511
import dotty.tools.dotc.Main;
16-
import dotty.tools.dotc.interfaces.*;
1712

1813
import java.lang.reflect.InvocationTargetException;
19-
import java.net.URLClassLoader;
14+
import java.io.File;
2015

2116
/**
2217
* The new compiler interface is [[dotty.tools.xsbt.CompilerBridge]] that extends the new `xsbti.CompilerInterface2`.
2318
* This interface is kept for compatibility with Mill and the sbt 1.3.x series.
2419
*/
2520
public final class CompilerInterface {
26-
public CachedCompiler newCompiler(String[] options, Output output, Logger initialLog, Reporter initialDelegate) {
27-
// The classloader that sbt uses to load the compiler bridge is broken
28-
// (see CompilerClassLoader#fixBridgeLoader for details). To workaround
29-
// this we construct our own ClassLoader and then run the following code
30-
// with it:
31-
// new CachedCompilerImpl(options, output)
21+
public CachedCompiler newCompiler(String[] options, Output output, Logger initialLog, Reporter initialDelegate) throws java.lang.Exception {
22+
if(isClassLoaderValid()) {
23+
return new CachedCompilerImpl(options, output);
24+
} else {
25+
initialLog.warn(() ->
26+
"The compiler class loader is badly configured.\n" +
27+
"Consider using a more recent version of your build tool:\n" +
28+
" - sbt >= 1.4.0 and sbt-dotty >= 1.5.0\n" +
29+
" - Mill >= 0.9.3-21-002361\n" +
30+
" - Bloop >= 1.4.6-23-20a501bc"
31+
);
32+
// To workaround the wrong class loader, we construct our own and run
33+
// the following code with it:
34+
// new CachedCompilerImpl(options, output)
35+
try {
36+
ClassLoader bridgeLoader = this.getClass().getClassLoader();
37+
ClassLoader fixedLoader = CompilerClassLoader.fixBridgeLoader(bridgeLoader);
38+
Class<?> cciClass = fixedLoader.loadClass("xsbt.CachedCompilerImpl");
39+
return (CachedCompiler) cciClass.getConstructors()[0].newInstance(options, output);
40+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}
44+
}
3245

46+
private boolean isClassLoaderValid() {
47+
// Check that the `xsbti.*` classes are loaded from the same class loader in the compiler and the bridge
48+
ClassLoader compilerClassLoader = Main.class.getClassLoader();
49+
Class<Logger> clazz = Logger.class;
3350
try {
34-
ClassLoader bridgeLoader = this.getClass().getClassLoader();
35-
ClassLoader fixedLoader = CompilerClassLoader.fixBridgeLoader(bridgeLoader);
36-
Class<?> cciClass = fixedLoader.loadClass("xsbt.CachedCompilerImpl");
37-
return (CachedCompiler) cciClass.getConstructors()[0].newInstance(options, output);
38-
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
51+
return compilerClassLoader.loadClass("xsbti.Logger") == clazz;
52+
} catch (ClassNotFoundException e) {
3953
throw new RuntimeException(e);
4054
}
4155
}

0 commit comments

Comments
 (0)