2121
2222import java .io .IOException ;
2323import java .util .Comparator ;
24+ import java .util .Collections ;
2425import java .util .HashMap ;
2526import java .util .Map ;
2627import java .util .Arrays ;
@@ -62,6 +63,25 @@ private static VirtualFile asVirtualFileOrNull(SourceFile sourceFile) {
6263 }
6364 }
6465
66+ private static void reportMissingFile (DelegatingReporter reporter , dotty .tools .dotc .interfaces .SourceFile sourceFile ) {
67+ String underline = String .join ("" , Collections .nCopies (sourceFile .path ().length (), "^" ));
68+ String message =
69+ sourceFile .path () + ": Missing virtual file\n " +
70+ underline + "\n " +
71+ " Falling back to placeholder for the given source file (of class " + sourceFile .getClass ().getName () + ")\n " +
72+ " This is likely a bug in incremental compilation for the Scala 3 compiler. Please report it to the Scala 3 maintainers." ;
73+ reporter .reportBasicWarning (message );
74+ }
75+
76+ private static VirtualFile fallbackVirtualFile (DelegatingReporter reporter ,
77+ dotty .tools .dotc .interfaces .SourceFile sourceFile ,
78+ Map <String , VirtualFile > placeholders ) {
79+ return placeholders .computeIfAbsent (sourceFile .path (), path -> {
80+ reportMissingFile (reporter , sourceFile );
81+ return new PlaceholderVirtualFile (sourceFile );
82+ });
83+ }
84+
6585 synchronized public void run (VirtualFile [] sources , AnalysisCallback callback , Logger log , Reporter delegate ) {
6686 VirtualFile [] sortedSources = new VirtualFile [sources .length ];
6787 System .arraycopy (sources , 0 , sortedSources , 0 , sources .length );
@@ -86,17 +106,19 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
86106 }
87107 });
88108
109+ HashMap <String , VirtualFile > placeholders = new HashMap <>();
110+
89111 IncrementalCallback incCallback = new IncrementalCallback (callback , sourceFile -> {
90112 if (sourceFile instanceof SourceFile ) {
91113 SourceFile sf = (SourceFile ) sourceFile ;
92114 VirtualFile vf = asVirtualFileOrNull (sf );
93115 if (vf != null ) {
94116 return vf ;
95117 } else {
96- throw new IllegalStateException ( "Unknown source file: " + sourceFile . path () );
118+ return fallbackVirtualFile ( reporter , sourceFile , placeholders );
97119 }
98120 } else {
99- throw new IllegalStateException ( "Unknown source file: " + sourceFile . path () );
121+ return fallbackVirtualFile ( reporter , sourceFile , placeholders );
100122 }
101123 });
102124
0 commit comments