This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 6b04d9008831 CAMEL-27401: camel-groovy - do not count the platform 
line separator in the index of a script validation error (#30621)
6b04d9008831 is described below

commit 6b04d9008831e85a90f5f990dcbb41426d3d726c
Author: Saravanakumar Selvaraj <[email protected]>
AuthorDate: Mon Nov 23 06:52:17 2026 +0000

    CAMEL-27401: camel-groovy - do not count the platform line separator in the 
index of a script validation error (#30621)
    
    GroovyValidationException.getIndex computes the position of a syntax error 
in the script. For each earlier line, it
    added the length of the line and of System.lineSeparator(). So it counted 
the line break of the platform and not the
    line break that the script has. On Windows, a script with LF line ends got 
an index one too high for each earlier line.
    On Linux and macOS, a script with CRLF line ends got an index one too low 
for each earlier line. So
    testValidateExpression of GroovyLanguageTest and testValidateGroovyLanguage 
of CamelCatalogTest failed on Windows.
    
    The method now finds the start of the line of the error in the script 
itself. The Groovy compiler starts a new line
    after each LF, and the method does the same. The index is then the same on 
every platform.
    
    A new test of GroovyLanguageTest covers scripts with LF line ends, with 
CRLF line ends, and with both.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../language/groovy/GroovyValidationException.java   | 20 ++++++++------------
 .../camel/language/groovy/GroovyLanguageTest.java    | 18 ++++++++++++++++++
 2 files changed, 26 insertions(+), 12 deletions(-)

diff --git 
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyValidationException.java
 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyValidationException.java
index c8f0d883f8fc..108a54b3a9b1 100644
--- 
a/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyValidationException.java
+++ 
b/components/camel-groovy/src/main/java/org/apache/camel/language/groovy/GroovyValidationException.java
@@ -16,9 +16,6 @@
  */
 package org.apache.camel.language.groovy;
 
-import java.io.LineNumberReader;
-import java.io.StringReader;
-
 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
 import org.codehaus.groovy.control.messages.Message;
 import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
@@ -43,18 +40,17 @@ public class GroovyValidationException extends Exception {
         if (getCause() instanceof MultipleCompilationErrorsException me) {
             Message gm = me.getErrorCollector().getLastError();
             if (gm instanceof SyntaxErrorMessage sem) {
-                LineNumberReader lr = new LineNumberReader(new 
StringReader(script));
-                int pos = -1;
+                // find the start of the line of the error in the script 
itself. The Groovy compiler starts a new line
+                // after each \n, so the line separator of the platform does 
not matter.
+                int lineStart = 0;
                 for (int i = 1; i < sem.getCause().getLine(); i++) {
-                    try {
-                        String line = lr.readLine();
-                        pos += line.length() + System.lineSeparator().length();
-                    } catch (Exception e) {
-                        // ignore
+                    int lineEnd = script.indexOf('\n', lineStart);
+                    if (lineEnd == -1) {
+                        break;
                     }
+                    lineStart = lineEnd + 1;
                 }
-                pos += sem.getCause().getStartColumn();
-                return pos;
+                return lineStart + sem.getCause().getStartColumn() - 1;
             }
         }
         return -1;
diff --git 
a/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java
 
b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java
index ad56aedfc56a..5aab6855c80d 100644
--- 
a/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java
+++ 
b/components/camel-groovy/src/test/java/org/apache/camel/language/groovy/GroovyLanguageTest.java
@@ -76,6 +76,24 @@ public class GroovyLanguageTest extends LanguageTestSupport {
         }
     }
 
+    @Test
+    public void testValidateExpressionIndexWithLfAndCrLf() throws Exception {
+        try (GroovyLanguage g = new GroovyLanguage()) {
+            g.setCamelContext(context);
+
+            // LF line ends, CRLF line ends, and both in one script
+            assertIndexOfError(g, "var a = 123;\nprintln a */ 2;\n");
+            assertIndexOfError(g, "var a = 123;\r\nprintln a */ 2;\r\n");
+            assertIndexOfError(g, "var a = 123;\r\nvar b = 4;\nprintln a */ 
2;\n");
+        }
+    }
+
+    private static void assertIndexOfError(GroovyLanguage g, String script) {
+        GroovyValidationException e = 
assertThrows(GroovyValidationException.class, () -> 
g.validateExpression(script));
+        // the syntax error is the * of the script
+        assertEquals(script.indexOf('*'), e.getIndex());
+    }
+
     @Test
     public void testGroovyAttachments() throws Exception {
         DefaultAttachment da

Reply via email to