You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "efegokdemir (via GitHub)" <gi...@apache.org> on 2026/10/07 20:59:51 UTC

[PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

efegokdemir opened a new pull request, #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160

   ## What
   
   Fall back to `Diagnostic.toString()` when formatting a compiler diagnostic throws, so an unavailable CLASS-retained annotation type does not abort compilation while logging a warning.
   
   ## Why
   
   On JDK 25, formatting a deprecation diagnostic for an API annotated with a non-transitive `CLASS`-retained type-use annotation can throw `CompletionFailure`. The exception currently escapes `DiagnosticLogger` and fails compilation even though the compiler can continue.
   
   ## How
   
   `DiagnosticLogger` now catches failures from `Diagnostic.getMessage(Locale)` and logs the diagnostic's string representation. A unit test covers the fallback, and an integration fixture models a provided annotation dependency that is absent from the consumer's classpath.
   
   ## Testing
   
   - Fail-before: the new Maven Invoker fixture failed on the unmodified implementation with `CompletionFailure: class file for lombok.NonNull not found`.
   - Pass-after: `JAVA_HOME=/opt/homebrew/opt/openjdk@25/libexec/openjdk.jdk/Contents/Home /tmp/maven4/apache-maven-4.0.0-rc-7/bin/mvn -B -Prun-its verify` — 20 unit tests passed; 82 integration tests passed, 2 skipped.
   - `JAVA_HOME=/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home /tmp/maven4/apache-maven-4.0.0-rc-7/bin/mvn -B -Dtest=DiagnosticLoggerTest test` — passed.
   
   ## Checklist
   
   - [x] I hereby declare this contribution to be licensed under the [Apache License Version 2.0, January 2004](https://www.apache.org/licenses/LICENSE-2.0)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4228441879


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +119,25 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        String code = diagnostic.getCode();

Review Comment:
   Since the next use of `code` is 50 lines lower, add the `final` keyword for clarity.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212021196


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath (for example, a CLASS-retained annotation
+             * on a deprecated API). The compiler can still emit a useful fallback representation.
+             */
+            logger.debug("Cannot format compiler diagnostic; falling back to its string representation.");
+            logger.debug(e);

Review Comment:
   Should be with the previous `logger.debug`, not a separated log record.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212664462


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +119,25 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        String code = diagnostic.getCode();
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (RuntimeException e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath. CompletionFailure is a RuntimeException; the
+             * compiler can still emit a useful fallback representation.
+             */
+            if (loggedFormattingFailures.add(code)) {
+                logger.debug("Cannot format compiler diagnostic; falling back to its string representation.", e);
+            }
+            try {
+                message = diagnostic.toString();
+            } catch (RuntimeException e2) {

Review Comment:
   💡 **Guard against null `code`**
   
   `diagnostic.getCode()` may return `null` (the `Diagnostic` contract allows it). When it does, this line produces `"WARNING: null"` — a non-blank string that passes the `isBlank()` check below and gets logged verbatim to users.
   
   ```suggestion
                   message = diagnostic.getKind() + (code != null ? ": " + code : "");
   ```



##########
src/it/MCOMPILER-1150/invoker.properties:
##########
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.goals = clean compile

Review Comment:
   💡 **Add a `verify.groovy` to assert the IT actually tests something**
   
   `invoker.goals = clean compile` only asserts a zero exit code. On any JDK that does not trigger `CompletionFailure`, the build succeeds trivially and the test proves nothing. A `verify.groovy` that checks the build log for the expected deprecation warning would confirm the fix works and guard against regressions:
   
   ```groovy
   // src/it/MCOMPILER-1150/verify.groovy
   def log = new File(basedir, 'build.log').text
   assert log.contains('deprecated') : 'Expected deprecation warning in build log'
   ```
   
   Note: the exact assertion depends on what the fixed code now emits — adjust the `contains` expression to match the actual fallback message.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212024670


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath (for example, a CLASS-retained annotation
+             * on a deprecated API). The compiler can still emit a useful fallback representation.
+             */
+            logger.debug("Cannot format compiler diagnostic; falling back to its string representation.");
+            logger.debug(e);
+            message = diagnostic.toString();

Review Comment:
   ⚠️ **`toString()` shares the same failure path as `getMessage()`**
   
   `JCDiagnostic.toString()` calls `defaultFormatter.format(this, Locale.getDefault())`, which delegates to `formatDiagnostic()` → `formatMeta('m')` → `formatMessage()` → `formatArguments()`. This is the same code path that throws. If a `CompletionFailure` fires during argument formatting, both `getMessage()` and `toString()` will throw, leaving `message` unset and the exception propagating out of `report()`.
   
   Consider a nested try/catch with a safe-last-resort fallback:
   
   ```suggestion
               try {
                   message = diagnostic.toString();
               } catch (Throwable e2) {
                   message = diagnostic.getKind() + ": " + diagnostic.getCode();
               }
   ```



##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+    @Test
+    void fallsBackWhenDiagnosticMessageCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new LinkageError("missing type"));

Review Comment:
   💡 **Consider using `RuntimeException` instead of `LinkageError`**
   
   `CompletionFailure` (the actual exception thrown by the JDK in this scenario) extends `RuntimeException`, not `Error`. Using `LinkageError` (an `Error` subtype) still exercises the `catch (Throwable)` block correctly, but it's a less faithful proxy for the real failure mode. Swapping it for `RuntimeException` would better document why `Throwable` (rather than `Exception`) is caught here.



##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+    @Test
+    void fallsBackWhenDiagnosticMessageCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new LinkageError("missing type"));
+        when(diagnostic.toString()).thenReturn("deprecated API used");

Review Comment:
   💡 **Add a second test for the double-failure path**
   
   This test stubs `toString()` to succeed, but as noted above, `toString()` can throw the same way `getMessage()` does. Adding a second test case where `diagnostic.toString()` also throws would verify that `report()` handles the double-failure gracefully (rather than propagating the exception). That test would fail today — which is exactly why it should exist.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "efegokdemir (via GitHub)" <gi...@apache.org>.
efegokdemir commented on PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#issuecomment-6089323090

   Fixed in `50a7282`. The Invoker assertion now checks the current kind-and-code fallback message, and the redundant unit test has been removed. The focused test (3 tests), MCOMPILER-1150 integration test (22 unit tests plus 1 IT), Spotless and `git diff --check` pass.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4232429823


##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+    @Test
+    void fallsBackToKindAndCodeWhenDiagnosticMessageCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new RuntimeException("missing type"));
+        when(diagnostic.getKind()).thenReturn(Diagnostic.Kind.WARNING);
+        when(diagnostic.getSource()).thenReturn(null);
+        when(diagnostic.getLineNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getColumnNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getCode()).thenReturn("compiler.warn.has.been.deprecated");
+
+        var listener = new DiagnosticLogger(logger, new DefaultMessageBuilderFactory(), null, null);
+        listener.report(diagnostic);
+
+        verify(logger).warn((String)
+                argThat(message -> ((String) message).contains("WARNING: compiler.warn.has.been.deprecated")));
+    }
+
+    @Test
+    void fallsBackToKindAndCodeWhenDiagnosticStringCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new RuntimeException("missing type"));
+        when(diagnostic.getKind()).thenReturn(Diagnostic.Kind.WARNING);
+        when(diagnostic.getSource()).thenReturn(null);
+        when(diagnostic.getLineNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getColumnNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getCode()).thenReturn("compiler.warn.has.been.deprecated");
+
+        var listener = new DiagnosticLogger(logger, new DefaultMessageBuilderFactory(), null, null);

Review Comment:
   ⚠️ **Duplicate test — `fallsBackToKindAndCodeWhenDiagnosticStringCannotBeFormatted` is now identical to `fallsBackToKindAndCodeWhenDiagnosticMessageCannotBeFormatted`**
   
   Both methods used to differ by the `toString()` mock: the first stubbed `toString()` to return a string, the second stubbed it to throw. Removing both stubs collapsed them into identical setups and identical assertions — they now exercise exactly the same code path.
   
   The second test should be updated to cover the scenario it was originally designed for: when `diagnostic.toString()` also throws. Since the production code no longer calls `toString()` at all (it goes straight to `getKind() + code`), this test is now redundant. Either remove it or make it explicitly verify that no `toString()` call is made (e.g. using a strict mock or `verify(diagnostic, never()).toString()`).



##########
src/it/MCOMPILER-1150/verify.groovy:
##########
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+def buildLog = new File(basedir, 'build.log')
+assert buildLog.isFile() : 'Expected the Maven Invoker build log'
+assert buildLog.text.contains('Cannot format compiler diagnostic; falling back to its string representation.') : 'Expected the diagnostic-formatting fallback to run'

Review Comment:
   🐛 **Bug: stale log message — IT will always fail**
   
   This assertion checks for the old log message `'falling back to its string representation.'`, but `DiagnosticLogger.java` was updated (in commit 87ac535) to emit `'falling back to its kind and code.'` instead.
   
   The integration test will fail whenever the fallback path is exercised on JDK 25+.
   
   ```suggestion
   assert buildLog.text.contains('Cannot format compiler diagnostic; falling back to its kind and code.') : 'Expected the diagnostic-formatting fallback to run'
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4228362850


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -69,6 +71,9 @@ final class DiagnosticLogger implements DiagnosticListener<JavaFileObject> {
      */
     private final Map<String, Integer> codeCount;
 
+    /** Diagnostic codes for which a formatting failure was already logged. */
+    private final Set<String> loggedFormattingFailures = new HashSet<>();

Review Comment:
   Move `new HashSet<>()` in the constructor for consistency with the initialization of other fields.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4232464588


##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+    @Test
+    void fallsBackToKindAndCodeWhenDiagnosticMessageCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new RuntimeException("missing type"));
+        when(diagnostic.getKind()).thenReturn(Diagnostic.Kind.WARNING);
+        when(diagnostic.getSource()).thenReturn(null);
+        when(diagnostic.getLineNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getColumnNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getCode()).thenReturn("compiler.warn.has.been.deprecated");
+
+        var listener = new DiagnosticLogger(logger, new DefaultMessageBuilderFactory(), null, null);
+        listener.report(diagnostic);
+
+        verify(logger).warn((String)
+                argThat(message -> ((String) message).contains("WARNING: compiler.warn.has.been.deprecated")));
+    }
+
+    @Test
+    void fallsBackToKindAndCodeWhenDiagnosticStringCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new RuntimeException("missing type"));
+        when(diagnostic.getKind()).thenReturn(Diagnostic.Kind.WARNING);
+        when(diagnostic.getSource()).thenReturn(null);
+        when(diagnostic.getLineNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getColumnNumber()).thenReturn(Diagnostic.NOPOS);
+        when(diagnostic.getCode()).thenReturn("compiler.warn.has.been.deprecated");
+
+        var listener = new DiagnosticLogger(logger, new DefaultMessageBuilderFactory(), null, null);

Review Comment:
   I suggest to remove the test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux merged PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "efegokdemir (via GitHub)" <gi...@apache.org>.
efegokdemir commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212587946


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath (for example, a CLASS-retained annotation
+             * on a deprecated API). The compiler can still emit a useful fallback representation.
+             */
+            logger.debug("Cannot format compiler diagnostic; falling back to its string representation.");
+            logger.debug(e);

Review Comment:
   Updated the debug call to attach the exception to the same log record, and formatting failures are now logged only once per diagnostic code.



##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath (for example, a CLASS-retained annotation
+             * on a deprecated API). The compiler can still emit a useful fallback representation.
+             */
+            logger.debug("Cannot format compiler diagnostic; falling back to its string representation.");
+            logger.debug(e);
+            message = diagnostic.toString();

Review Comment:
   Added a nested fallback: if both getMessage() and toString() throw, the logger reports the diagnostic kind and code. Added a regression for both failures.



##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+    @Test
+    void fallsBackWhenDiagnosticMessageCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new LinkageError("missing type"));

Review Comment:
   Changed the catch and regression to RuntimeException, matching javac CompletionFailure rather than using LinkageError.



##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+    @Test
+    void fallsBackWhenDiagnosticMessageCannotBeFormatted() {
+        var logger = mock(Log.class);
+        @SuppressWarnings("unchecked")
+        Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+        when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new LinkageError("missing type"));
+        when(diagnostic.toString()).thenReturn("deprecated API used");

Review Comment:
   Added the double-failure regression; it verifies report() completes and emits the kind/code fallback when toString() also throws.



##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {

Review Comment:
   Narrowed the handler to RuntimeException, the actual CompletionFailure type, and added RuntimeException-based regression coverage.



##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath (for example, a CLASS-retained annotation
+             * on a deprecated API). The compiler can still emit a useful fallback representation.

Review Comment:
   Clarified the fixture: invoking the deprecated constructor triggers the diagnostic; the compiler formatting that diagnostic is what exposes the missing CLASS-retained annotation type.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "efegokdemir (via GitHub)" <gi...@apache.org>.
efegokdemir commented on PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#issuecomment-6085125869

   Fixed in `87ac535`. The formatting-failure set is initialised in the constructor, the diagnostic code is final, and the fallback now uses the kind and non-null code without relying on `Diagnostic.toString()`. `DiagnosticLoggerTest` passes (4 tests); Spotless and `git diff --check` pass.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212048518


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath (for example, a CLASS-retained annotation
+             * on a deprecated API). The compiler can still emit a useful fallback representation.
+             */
+            logger.debug("Cannot format compiler diagnostic; falling back to its string representation.");
+            logger.debug(e);

Review Comment:
   Another issue is that if this warning occurred once, then it may also occur many more times. The same warning is likely to appear elsewhere in the code to compile. We don't want to repeat the log message for all occurrences.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212059339


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath (for example, a CLASS-retained annotation
+             * on a deprecated API). The compiler can still emit a useful fallback representation.

Review Comment:
   The "class-retained annotation on a deprecated API" example is confusing. What deprecation is doing in this issue?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "efegokdemir (via GitHub)" <gi...@apache.org>.
efegokdemir commented on PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#issuecomment-6048941785

   Fixed in `b03638170858ae69451d8cff3aa0bb15174e0007`: the MCOMPILER-1150 IT is constrained to JDK 25+, and its root POM now uses the matching 4.1.0 namespace/schema. Validation on JDK 25 with Maven 4.0.0-rc-7: `mvn -Dtest=DiagnosticLoggerTest test` (4 tests), `mvn -Prun-its -Dinvoker.test=MCOMPILER-1150 verify` (1 IT; 23 unit tests), `mvn spotless:check`, and `git diff --check` all pass.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "efegokdemir (via GitHub)" <gi...@apache.org>.
efegokdemir commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212751509


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +119,25 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        String code = diagnostic.getCode();
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (RuntimeException e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath. CompletionFailure is a RuntimeException; the
+             * compiler can still emit a useful fallback representation.
+             */
+            if (loggedFormattingFailures.add(code)) {
+                logger.debug("Cannot format compiler diagnostic; falling back to its string representation.", e);
+            }
+            try {
+                message = diagnostic.toString();
+            } catch (RuntimeException e2) {

Review Comment:
   Fixed in `c8d79739b8a3c88a5105e6f878d48569783a1b0f`. The final fallback now appends the diagnostic code only when non-null, and a regression covers a null code with both formatting methods throwing. `mvn -Dtest=DiagnosticLoggerTest test` passes (4 tests); the targeted MCOMPILER-1150 integration test also passes.



##########
src/it/MCOMPILER-1150/invoker.properties:
##########
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.goals = clean compile

Review Comment:
   Fixed in `c8d79739b8a3c88a5105e6f878d48569783a1b0f`. Added `verify.groovy` to assert that the diagnostic-formatting fallback actually ran in the Invoker build log, rather than accepting only a zero exit code. Validation: `mvn -Prun-its -Dinvoker.test=MCOMPILER-1150 verify` passed (1 targeted IT; 23 module unit tests in the build).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212051841


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (Throwable e) {

Review Comment:
   `Throwable` if probably too wide as an exception type.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212834204


##########
src/it/MCOMPILER-1150/invoker.properties:
##########
@@ -0,0 +1,18 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.goals = clean compile

Review Comment:
   ⚠️ **Missing JDK version constraint — will break CI on JDK < 25.**
   
   `verify.groovy` asserts the `Cannot format compiler diagnostic; falling back to its string representation.` debug message is present in `build.log`. That message is only logged inside the `catch (RuntimeException e)` block in `DiagnosticLogger.java`, which is only reached when `getMessage()` throws `CompletionFailure` — a bug introduced in JDK 25. On JDK 17/21/22/23/24 `getMessage()` succeeds and the catch block is never entered, so `verify.groovy` fails with `AssertionError` on every CI matrix entry except JDK 25.
   
   ```suggestion
   invoker.goals = clean compile
   invoker.java.version = 25+
   ```



##########
src/it/MCOMPILER-1150/pom.xml:
##########
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" root="true" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

Review Comment:
   💡 The root POM declares `xmlns="http://maven.apache.org/POM/4.0.0"` and references the 4.0.0 XSD, but uses `modelVersion 4.1.0` and the 4.1.0-specific `root="true"` attribute. Other 4.1.0 ITs in this project use the 4.1.0 namespace consistently. Consider aligning:
   
   ```suggestion
   <project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" root="true" xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4228416375


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +119,25 @@ private String relativize(String file) {
      */
     @Override
     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
-        String message = diagnostic.getMessage(locale);
+        String message;
+        String code = diagnostic.getCode();
+        try {
+            message = diagnostic.getMessage(locale);
+        } catch (RuntimeException e) {
+            /*
+             * Some JDK versions may fail while formatting a diagnostic if a referenced class has an
+             * annotation type that is not on the classpath. CompletionFailure is a RuntimeException; the
+             * compiler can still emit a useful fallback representation.
+             */
+            if (loggedFormattingFailures.add(code)) {
+                logger.debug("Cannot format compiler diagnostic; falling back to its string representation.", e);
+            }
+            try {
+                message = diagnostic.toString();
+            } catch (RuntimeException e2) {

Review Comment:
   Remove this block. There is no guarantee that `toString()` will produce a useful message since there is nothing about that in `Diagnostic` interface contract. Instead, use unconditionally the next fallback. Then replace or remove the _"falling back to its string representation"_ text in above log message.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCOMPILER-1150: fall back when formatting diagnostics fails [maven-compiler-plugin]

Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #1160:
URL: https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4228362850


##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -69,6 +71,9 @@ final class DiagnosticLogger implements DiagnosticListener<JavaFileObject> {
      */
     private final Map<String, Integer> codeCount;
 
+    /** Diagnostic codes for which a formatting failure was already logged. */
+    private final Set<String> loggedFormattingFailures = new HashSet<>();

Review Comment:
   Move `newHashSet` in the constructor for consistency with the initialization of other fields.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org