@@ -25,6 +25,7 @@ public final class SemanticdbTaskListener implements TaskListener {
2525 private final SemanticdbReporter reporter ;
2626 private final JavacTypes javacTypes ;
2727 private final Trees trees ;
28+ private boolean sourceGeneratorsMessageIsLogged = false ;
2829
2930 public SemanticdbTaskListener (
3031 SemanticdbJavacOptions options ,
@@ -50,11 +51,7 @@ public void finished(TaskEvent e) {
5051 if (!options .alreadyReportedErrors ) {
5152 options .alreadyReportedErrors = true ;
5253 for (String error : options .errors ) {
53- trees .printMessage (
54- Diagnostic .Kind .ERROR ,
55- "semanticdb-javac: " + error ,
56- e .getCompilationUnit (),
57- e .getCompilationUnit ());
54+ reporter .error (error , e );
5855 }
5956 }
6057 return ;
@@ -63,9 +60,12 @@ public void finished(TaskEvent e) {
6360 try {
6461 onFinishedAnalyze (e );
6562 } catch (Throwable ex ) {
66- // Catch exceptions because we don't want to stop the compilation even if this plugin has a
67- // bug. We report the full stack trace because it's helpful for bug reports. Exceptions
68- // should only happen in *exceptional* situations and they should be reported upstream.
63+ // Catch exceptions because we don't want to stop the compilation even if this
64+ // plugin has a
65+ // bug. We report the full stack trace because it's helpful for bug reports.
66+ // Exceptions
67+ // should only happen in *exceptional* situations and they should be reported
68+ // upstream.
6969 Throwable throwable = ex ;
7070 if (e .getSourceFile () != null ) {
7171 throwable =
@@ -79,13 +79,15 @@ public void finished(TaskEvent e) {
7979
8080 private void onFinishedAnalyze (TaskEvent e ) {
8181 Result <Path , String > path = semanticdbOutputPath (options , e );
82- if (path .isOk ()) {
83- Semanticdb .TextDocument textDocument =
84- new SemanticdbVisitor (task , globals , e , options , javacTypes )
85- .buildTextDocument (e .getCompilationUnit ());
86- writeSemanticdb (e , path .getOrThrow (), textDocument );
87- } else {
88- reporter .error (path .getErrorOrThrow (), e .getCompilationUnit (), e .getCompilationUnit ());
82+ if (path != null ) {
83+ if (path .isOk ()) {
84+ Semanticdb .TextDocument textDocument =
85+ new SemanticdbVisitor (task , globals , e , options , javacTypes )
86+ .buildTextDocument (e .getCompilationUnit ());
87+ writeSemanticdb (e , path .getOrThrow (), textDocument );
88+ } else {
89+ reporter .error (path .getErrorOrThrow (), e );
90+ }
8991 }
9092 }
9193
@@ -112,12 +114,17 @@ public static Path absolutePathFromUri(SemanticdbJavacOptions options, JavaFileO
112114 throw new IllegalArgumentException ("unsupported URI: " + uri );
113115 }
114116 } else if (options .uriScheme == UriScheme .BAZEL ) {
115- String toString = file .toString ();
116- // This solution is hacky, and it would be very nice to use a dedicated API instead.
117- // The Bazel Java compiler constructs `SimpleFileObject/DirectoryFileObject` with a
118- // "user-friendly" name that points to the original source file and an underlying/actual
119- // file path in a temporary directory. We're constrained by having to use only public APIs of
120- // the Java compiler and `toString()` seems to be the only way to access the user-friendly
117+ String toString = file .toString ().replace (":" , "/" );
118+ // This solution is hacky, and it would be very nice to use a dedicated API
119+ // instead.
120+ // The Bazel Java compiler constructs `SimpleFileObject/DirectoryFileObject`
121+ // with a
122+ // "user-friendly" name that points to the original source file and an
123+ // underlying/actual
124+ // file path in a temporary directory. We're constrained by having to use only
125+ // public APIs of
126+ // the Java compiler and `toString()` seems to be the only way to access the
127+ // user-friendly
121128 // path.
122129 String [] knownBazelToStringPatterns =
123130 new String [] {"SimpleFileObject[" , "DirectoryFileObject[" };
@@ -146,11 +153,11 @@ private void inferBazelSourceroot(JavaFileObject file) {
146153 // /private/var/tmp/com/example/Hello.java
147154 //
148155 // We infer sourceroot by iterating the names of both files in reverse order
149- // and stop at the first entry where the two paths are different. For the
156+ // and stop at the first entry where the two paths are different. For the
150157 // example above, we compare "Hello.java", then "example", then "com", and
151158 // when we reach "repo" != "tmp" then we guess that "/home/repo" is the
152- // sourceroot. This logic is brittle and it would be nice to use more
153- // dedicated APIs, but Bazel actively makes an effort to sandbox
159+ // sourceroot. This logic is brittle and it would be nice to use more
160+ // dedicated APIs, but Bazel actively makes an effort to sandbox
154161 // compilation and hide access to the original workspace, which is why we
155162 // resort to solutions like this.
156163 int relativePathDepth = 0 ;
@@ -184,6 +191,26 @@ private Result<Path, String> semanticdbOutputPath(SemanticdbJavacOptions options
184191 .resolveSibling (filename );
185192 return Result .ok (semanticdbOutputPath );
186193 } 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+ }
213+
187214 return Result .error (
188215 String .format (
189216 "sourceroot '%s does not contain path '%s'. To fix this problem, update the -sourceroot flag to "
0 commit comments