Skip to content

[groovy] Fix #7100: CPD fails on GStrings ending in an interpolated variable - #7110

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
anyingiit:issue-7100-groovy-gstring-lexer
Sep 29, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
anyingiit:issue-7100-groovy-gstring-lexer

Conversation

@anyingiit

Copy link
Copy Markdown
Contributor

Describe the PR

GroovyCpdLexer instantiated org.apache.groovy.parser.antlr4.GroovyLexer directly. The Groovy compiler uses its subclass GroovyLangLexer, which implements rollbackOneChar(); 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 a LexException or with a GroovySyntaxError
  • "x $i y": no error, but the next GString part loses its leading space. You can see this in the existing sample.txt / cpdoff.txt expected files (is already in the tree" started at column 45 instead of 44). Those 4 lines are updated here.

Changes:

  • GroovyCpdLexer now uses GroovyLangLexer, as suggested in the issue.
  • GroovyTokenManager turns a GroovySyntaxError thrown by the lexer into a LexException with the same line/column. GroovySyntaxError extends AssertionError, so before this change it wasn't handled like other lexical errors (the issue mentions it bypassing --no-fail-on-error). This happens on main too for invalid input such as def a = `x` or an unterminated string.
  • New CPD test data gstrings.groovy covering the examples from the issue plus "${i}", "$i.name" and a triple-quoted GString, and a test for the LexException conversion.

I didn't touch docs/pages/release_notes.md because 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?

  • Added unit tests for fixed bug/feature (GroovyCpdLexerTest: new tests fail on main, 4/4 pass with the fix)
  • Passing all unit tests (./mvnw -pl pmd-groovy verify: tests, checkstyle, PMD and CPD checks pass. The full multi-module build was not run locally)
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed): comments at the lexer and at the error conversion

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.
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-09-29 10:23:17+00:00 for 7e2e7b8)

@UncleOwen UncleOwen added this to the 7.29.0 milestone Sep 29, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen
UncleOwen merged commit 0fcba96 into pmd:main Sep 29, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[groovy] CPD fails on GStrings that end in an interpolated variable

2 participants