|
6 | 6 | import xsbti.AnalysisCallback;
|
7 | 7 | import xsbti.Logger;
|
8 | 8 | import xsbti.Reporter;
|
9 |
| -import xsbti.Severity; |
10 | 9 | import xsbti.compile.*;
|
11 | 10 |
|
12 |
| -import java.io.File; |
13 |
| - |
14 |
| -import dotty.tools.dotc.core.Contexts.ContextBase; |
15 | 11 | import dotty.tools.dotc.Main;
|
16 |
| -import dotty.tools.dotc.interfaces.*; |
17 | 12 |
|
18 | 13 | import java.lang.reflect.InvocationTargetException;
|
19 |
| -import java.net.URLClassLoader; |
| 14 | +import java.io.File; |
20 | 15 |
|
21 | 16 | /**
|
22 | 17 | * The new compiler interface is [[dotty.tools.xsbt.CompilerBridge]] that extends the new `xsbti.CompilerInterface2`.
|
23 | 18 | * This interface is kept for compatibility with Mill and the sbt 1.3.x series.
|
24 | 19 | */
|
25 | 20 | 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 | + } |
32 | 45 |
|
| 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; |
33 | 50 | 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) { |
39 | 53 | throw new RuntimeException(e);
|
40 | 54 | }
|
41 | 55 | }
|
|
0 commit comments