Skip to content

Commit 036107a

Browse files
committedJul 13, 2023
always fallback when missing virtual file
1 parent 7dfb0cf commit 036107a

7 files changed

+39
-24
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dotty.tools.xsbt;
2+
3+
import dotty.tools.dotc.interfaces.SourceFile;
4+
import java.io.InputStream;
5+
import java.nio.charset.StandardCharsets;
6+
7+
public class BasicPathBasedFile extends PlaceholderVirtualFile implements xsbti.PathBasedFile {
8+
9+
private final SourceFile sourceFile;
10+
11+
public BasicPathBasedFile(SourceFile sourceFile) {
12+
super(sourceFile);
13+
this.sourceFile = sourceFile;
14+
}
15+
16+
public java.nio.file.Path toPath() {
17+
return sourceFile.jfile().get().toPath();
18+
}
19+
20+
}

‎sbt-bridge/src/dotty/tools/xsbt/CompilerBridgeDriver.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ public boolean sourcesRequired() {
5555
return false;
5656
}
5757

58-
private static VirtualFile asVirtualFileOrNull(SourceFile sourceFile) {
58+
private static VirtualFile asVirtualFile(SourceFile sourceFile, DelegatingReporter reporter,
59+
Map<String, VirtualFile> placeholders) {
5960
if (sourceFile.file() instanceof AbstractZincFile) {
6061
return ((AbstractZincFile) sourceFile.file()).underlying();
6162
} else {
62-
return null;
63+
return fallbackVirtualFile(reporter, sourceFile, placeholders);
6364
}
6465
}
6566

@@ -78,7 +79,10 @@ private static VirtualFile fallbackVirtualFile(DelegatingReporter reporter,
7879
Map<String, VirtualFile> placeholders) {
7980
return placeholders.computeIfAbsent(sourceFile.path(), path -> {
8081
reportMissingFile(reporter, sourceFile);
81-
return new PlaceholderVirtualFile(sourceFile);
82+
if (sourceFile.jfile().isPresent())
83+
return new BasicPathBasedFile(sourceFile);
84+
else
85+
return new PlaceholderVirtualFile(sourceFile);
8286
});
8387
}
8488

@@ -97,26 +101,14 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
97101
sourcesSet.put(abstractFile);
98102
}
99103

100-
DelegatingReporter reporter = new DelegatingReporter(delegate, sourceFile -> {
101-
VirtualFile vf = asVirtualFileOrNull(sourceFile);
102-
if (vf != null) {
103-
return vf.id();
104-
} else {
105-
return sourceFile.path(); // same as in Zinc for 2.13
106-
}
107-
});
108-
109104
HashMap<String, VirtualFile> placeholders = new HashMap<>();
110105

106+
DelegatingReporter reporter = new DelegatingReporter(delegate, (self, sourceFile) ->
107+
asVirtualFile(sourceFile, self, placeholders).id());
108+
111109
IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile -> {
112110
if (sourceFile instanceof SourceFile) {
113-
SourceFile sf = (SourceFile) sourceFile;
114-
VirtualFile vf = asVirtualFileOrNull(sf);
115-
if (vf != null) {
116-
return vf;
117-
} else {
118-
return fallbackVirtualFile(reporter, sourceFile, placeholders);
119-
}
111+
return asVirtualFile(((SourceFile) sourceFile), reporter, placeholders);
120112
} else {
121113
return fallbackVirtualFile(reporter, sourceFile, placeholders);
122114
}

‎sbt-bridge/src/dotty/tools/xsbt/DelegatingReporter.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
final public class DelegatingReporter extends AbstractReporter {
2626
private xsbti.Reporter delegate;
27+
private final BiFunction<DelegatingReporter, SourceFile, String> baseLookup;
2728
private final Function<SourceFile, String> lookup;
2829

29-
public DelegatingReporter(xsbti.Reporter delegate, Function<SourceFile, String> lookup) {
30+
public DelegatingReporter(xsbti.Reporter delegate, BiFunction<DelegatingReporter, SourceFile, String> baseLookup) {
3031
super();
3132
this.delegate = delegate;
32-
this.lookup = lookup;
33+
this.baseLookup = baseLookup;
34+
this.lookup = sourceFile -> baseLookup.apply(this, sourceFile);
3335
}
3436

3537
public void dropDelegate() {

‎sbt-bridge/src/dotty/tools/xsbt/PlaceholderVirtualFile.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.io.InputStream;
55
import java.nio.charset.StandardCharsets;
66

7-
public final class PlaceholderVirtualFile extends xsbti.BasicVirtualFileRef implements xsbti.VirtualFile {
7+
public class PlaceholderVirtualFile extends xsbti.BasicVirtualFileRef implements xsbti.VirtualFile {
88

99
private final SourceFile sourceFile;
1010

‎sbt-bridge/src/dotty/tools/xsbt/Problem.java

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import xsbti.Position;
1818
import xsbti.Severity;
19+
import xsbti.VirtualFile;
1920

2021

2122
final public class Problem implements xsbti.Problem {

‎sbt-bridge/src/xsbt/CachedCompilerImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ synchronized public void run(File[] sources, DependencyChanges changes, Analysis
6767

6868
Context ctx = new ContextBase().initialCtx().fresh()
6969
.setIncCallback(incCallback)
70-
.setReporter(new DelegatingReporter(delegate, source -> source.file().absolutePath()));
70+
.setReporter(new DelegatingReporter(delegate, (self, source) -> source.file().absolutePath()));
7171

7272
dotty.tools.dotc.reporting.Reporter reporter = Main.process(commandArguments(sources), ctx);
7373
if (reporter.hasErrors()) {

‎sbt-bridge/src/xsbt/DottydocRunner.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void run() {
5353
args = retained.toArray(new String[retained.size()]);
5454

5555
Context ctx = new ContextBase().initialCtx().fresh()
56-
.setReporter(new DelegatingReporter(delegate, source -> source.file().absolutePath()));
56+
.setReporter(new DelegatingReporter(delegate, (self, source) -> source.file().absolutePath()));
5757

5858
try {
5959
Class<?> dottydocMainClass = Class.forName("dotty.tools.dottydoc.Main");

0 commit comments

Comments
 (0)