Repository navigation
[groovy] Fix #7100: CPD fails on GStrings ending in an interpolated variable - #7110
Merged
Merged
Conversation
GroovyCpdLexer used org.apache.groovy.parser.antlr4.GroovyLexer directly. That lexer depends on its subclass GroovyLangLexer (the one the Groovy compiler uses) to roll back one character after a GString path such as "$i". Without it the character after the variable is swallowed: for "x $i y" the following GString part lost its leading space, and for "$i" or "x-$i" the closing quote was consumed, which made lexing fail. Use GroovyLangLexer instead, and report GroovySyntaxError (an AssertionError thrown by the Groovy lexer for some errors) as a LexException so that it is handled like any other lexical error.
Contributor
|
Compared to main: (comment created at 2026-09-29 10:23:17+00:00 for 7e2e7b8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the PR
GroovyCpdLexerinstantiatedorg.apache.groovy.parser.antlr4.GroovyLexerdirectly. The Groovy compiler uses its subclassGroovyLangLexer, which implementsrollbackOneChar(); the base lexer needs that after a GString path like$i. Without it the character right after the variable is swallowed:"$i"/f("x-$i", "y"): the closing quote is eaten, so lexing fails with aLexExceptionor with aGroovySyntaxError"x $i y": no error, but the next GString part loses its leading space. You can see this in the existingsample.txt/cpdoff.txtexpected files (is already in the tree"started at column 45 instead of 44). Those 4 lines are updated here.Changes:
GroovyCpdLexernow usesGroovyLangLexer, as suggested in the issue.GroovyTokenManagerturns aGroovySyntaxErrorthrown by the lexer into aLexExceptionwith the same line/column.GroovySyntaxErrorextendsAssertionError, so before this change it wasn't handled like other lexical errors (the issue mentions it bypassing--no-fail-on-error). This happens onmaintoo for invalid input such asdef a = `x`or an unterminated string.gstrings.groovycovering the examples from the issue plus"${i}","$i.name"and a triple-quoted GString, and a test for theLexExceptionconversion.I didn't touch
docs/pages/release_notes.mdbecause I wasn't sure whether you'd prefer to update it yourselves. Happy to add the entry if you want.Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to
help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed
below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it — please feel free to
close it, no hard feelings at all 🙂
Related issues
Ready?
GroovyCpdLexerTest: new tests fail onmain, 4/4 pass with the fix)./mvnw -pl pmd-groovy verify: tests, checkstyle, PMD and CPD checks pass. The full multi-module build was not run locally)./mvnw clean verifypasses (checked automatically by github actions)