20
20
import static java .nio .charset .StandardCharsets .UTF_8 ;
21
21
import static java .util .Comparator .comparing ;
22
22
23
+ import java .io .File ;
23
24
import java .io .IOError ;
24
25
import java .io .IOException ;
25
26
import java .io .PrintWriter ;
@@ -131,6 +132,7 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
131
132
// }
132
133
// }
133
134
}
135
+
134
136
errWriter .flush ();
135
137
ImmutableList <FormattedDiagnostic > diagnostics = diagnosticsBuilder .build ();
136
138
@@ -151,15 +153,56 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
151
153
}
152
154
}
153
155
}
156
+
157
+ String output = errOutput .toString ();
154
158
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 (
156
173
status ,
157
174
filterDiagnostics (werror , diagnostics ),
158
- errOutput . toString () ,
175
+ output ,
159
176
compiler ,
160
177
builder .build ());
161
178
}
162
179
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
+
163
206
private static Status fromResult (Boolean result ) {
164
207
if (result == null )
165
208
return Status .CRASH ;
0 commit comments