Skip to content

Commit eb51fa3

Browse files
authored
Work around JDT absolute path issue. (#14)
This is a simple attempt to make paths in the output of the JDT compiler relative again. Somehow they end up becoming absolute, which breaks IDEs decided to rely on compiler output parsing for error reporting. eclipse-jdt/eclipse.jdt.core#1061
1 parent 47b9de7 commit eb51fa3

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

.bazelproject

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# The project view file (.bazelproject) is used to import targets into the IDE.
2+
#
3+
# See: https://ij.bazel.build/docs/project-views.html
4+
#
5+
# This files provides a default experience for developers working with the project
6+
7+
8+
directories:
9+
.
10+
11+
derive_targets_from_directories: true
12+
13+
java_language_level: 11

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ bazel-*
1111
.project
1212
.settings/
1313
.classpath
14+
.eclipse
1415

1516
# output folders
1617
bin/

WORKSPACE

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ jvm_maven_import_external(
7070
name = "rules_jdt_guava",
7171
artifact = "com.google.guava:guava:31.0.1-jre",
7272
artifact_sha256 = "d5be94d65e87bd219fb3193ad1517baa55a3b88fc91d21cf735826ab5af087b9",
73+
srcjar_urls = [server + "/com/google/guava/guava/31.0.1-jre/guava-31.0.1-jre-sources.jar" for server in _DEFAULT_REPOSITORIES],
74+
srcjar_sha256 = "fc0fb66f315f10b8713fc43354936d3649a8ad63f789d42fd7c3e55ecf72e092",
7375
licenses = ["notice"],
7476
server_urls = _DEFAULT_REPOSITORIES,
7577
)

compiler/src/main/buildjar/com/google/devtools/build/buildjar/javac/BlazeEcjMain.java

+45-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.nio.charset.StandardCharsets.UTF_8;
2121
import static java.util.Comparator.comparing;
2222

23+
import java.io.File;
2324
import java.io.IOError;
2425
import java.io.IOException;
2526
import java.io.PrintWriter;
@@ -131,6 +132,7 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
131132
// }
132133
// }
133134
}
135+
134136
errWriter.flush();
135137
ImmutableList<FormattedDiagnostic> diagnostics = diagnosticsBuilder.build();
136138

@@ -151,15 +153,56 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
151153
}
152154
}
153155
}
156+
157+
String output = errOutput.toString();
154158

155-
return BlazeJavacResult.createFullResult(
159+
// JDT uses getAbsolutePath, which makes reporting of file paths to point into Bazel sandbox
160+
// this causes issues in IntelliJ
161+
String canonicalPathPrefix = null;
162+
try {
163+
canonicalPathPrefix = detectWorkingDirPathPrefix(arguments);
164+
if(canonicalPathPrefix != null) {
165+
output = output.replace(canonicalPathPrefix, "");
166+
}
167+
} catch (IOException e) {
168+
e.printStackTrace(errWriter);
169+
errWriter.flush();
170+
}
171+
172+
return BlazeJavacResult.createFullResult(
156173
status,
157174
filterDiagnostics(werror, diagnostics),
158-
errOutput.toString(),
175+
output,
159176
compiler,
160177
builder.build());
161178
}
162179

180+
private static String detectWorkingDirPathPrefix(BlazeJavacArguments arguments) throws IOException {
181+
// since the JDT compiler is executed from within the sandbox, the absolute path will be resolved to the working directory
182+
// we simple remove the working directory
183+
String workDir = System.getProperty("user.dir");
184+
if(workDir == null)
185+
throw new IOException("No working directory returned by JVM for property user.dir!");
186+
187+
if(!workDir.endsWith("/")) {
188+
workDir += "/";
189+
}
190+
191+
// the following code is only for our own sanity
192+
Optional<Path> first = arguments.sourcePath().stream().findFirst();
193+
if (!first.isPresent()) {
194+
first = arguments.sourceFiles().stream().findFirst();
195+
}
196+
197+
String absoluteFilePath = first.get().toAbsolutePath().toString();
198+
if (!absoluteFilePath.startsWith(workDir)) {
199+
String filePath = first.get().toString();
200+
throw new IOException(String.format("Unable to confirm working dir '%s' using file '%s' with absolute path '%s'!", workDir, filePath, absoluteFilePath));
201+
}
202+
203+
return workDir;
204+
}
205+
163206
private static Status fromResult(Boolean result) {
164207
if(result == null)
165208
return Status.CRASH;

0 commit comments

Comments
 (0)