66import com .sun .source .util .Trees ;
77import com .sun .tools .javac .model .JavacTypes ;
88
9- import javax .tools .Diagnostic ;
109import javax .tools .JavaFileObject ;
1110import java .io .IOException ;
1211import java .net .URI ;
@@ -25,7 +24,7 @@ public final class SemanticdbTaskListener implements TaskListener {
2524 private final SemanticdbReporter reporter ;
2625 private final JavacTypes javacTypes ;
2726 private final Trees trees ;
28- private boolean sourceGeneratorsMessageIsLogged = false ;
27+ private int noRelativePathCounter = 0 ;
2928
3029 public SemanticdbTaskListener (
3130 SemanticdbJavacOptions options ,
@@ -130,7 +129,11 @@ public static Path absolutePathFromUri(SemanticdbJavacOptions options, JavaFileO
130129 new String [] {"SimpleFileObject[" , "DirectoryFileObject[" };
131130 for (String pattern : knownBazelToStringPatterns ) {
132131 if (toString .startsWith (pattern ) && toString .endsWith ("]" )) {
133- return Paths .get (toString .substring (pattern .length (), toString .length () - 1 ));
132+ Path path = Paths .get (toString .substring (pattern .length (), toString .length () - 1 ));
133+ if (path .isAbsolute ()) {
134+ return path ;
135+ }
136+ return options .sourceroot .resolve (path );
134137 }
135138 }
136139 throw new IllegalArgumentException ("unsupported source file: " + toString );
@@ -190,32 +193,41 @@ private Result<Path, String> semanticdbOutputPath(SemanticdbJavacOptions options
190193 .resolve (relativePath )
191194 .resolveSibling (filename );
192195 return Result .ok (semanticdbOutputPath );
193- } else {
194-
195- if (options .uriScheme == UriScheme .BAZEL && options .generatedTargetRoot != null ) {
196- try {
197- if (absolutePath .toRealPath ().startsWith (options .generatedTargetRoot )) {
198- if (!sourceGeneratorsMessageIsLogged ) {
199- sourceGeneratorsMessageIsLogged = true ;
200- reporter .info (
201- "Usage of source generators detected - scip-java does not produce SemanticDB files for generated files.\n "
202- + "This message is logged only once" ,
203- e );
204- }
205-
206- return null ;
207- }
208- } catch (IOException exc ) {
209- reporter .exception (exc , e );
210- return null ;
211- }
212- }
196+ }
213197
214- return Result .error (
215- String .format (
216- "sourceroot '%s does not contain path '%s'. To fix this problem, update the -sourceroot flag to "
217- + "be a parent directory of this source file." ,
218- options .sourceroot , absolutePath ));
198+ switch (options .noRelativePath ) {
199+ case INDEX_ANYWAY :
200+ // Come up with a unique relative path for this file even if it's not under the sourceroot.
201+ // By indexing auto-generated files, we collect SymbolInformation for auto-generated symbol,
202+ // which results in more useful hover tooltips in the editor.
203+ // In the future, we may want to additionally embed the full text contents of these files
204+ // so that it's possible to browse generated files with precise code navigation.
205+ String uniqueFilename =
206+ String .format ("%d.%s.semanticdb" , ++noRelativePathCounter , absolutePath .getFileName ());
207+ Path semanticdbOutputPath =
208+ options
209+ .targetroot
210+ .resolve ("META-INF" )
211+ .resolve ("semanticdb" )
212+ .resolve ("no-relative-path" )
213+ .resolve (uniqueFilename );
214+ return Result .ok (semanticdbOutputPath );
215+ case WARNING :
216+ reporter .info (
217+ String .format (
218+ "Skipping file '%s' because it is not under the sourceroot '%s'" ,
219+ absolutePath , options .sourceroot ),
220+ e );
221+ case SKIP :
222+ return null ;
223+ case ERROR :
224+ default :
225+ return Result .error (
226+ String .format (
227+ "Unable to detect the relative path of '%s'. A common reason for this error is that the file is that this file is auto-generated. "
228+ + "To fix this problem, either configure the -sourceroot:PATH flag to be the parent directory of all indexed files, or "
229+ + "configure -no-relative-path:VALUE flag to have one of the following values: index_anyway, skip, warning." ,
230+ absolutePath ));
219231 }
220232 }
221233}
0 commit comments