Skip to content

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

Closed

Description

@krzsztf

Affects PMD Version: 7.27.0

Description:

CPD reports a lexical error on valid Groovy when a GString ends in a $name interpolation. For some
inputs the error is a GroovySyntaxError that stops the whole run, even with --no-fail-on-error.

GroovyCpdLexer builds org.apache.groovy.parser.antlr4.GroovyLexer. The Groovy compiler does not
use that class on its own. It uses the subclass GroovyLangLexer, which lexes these inputs without
error.

Code Sample demonstrating the issue:

def a = "$i"
$ pmd cpd -l groovy --minimum-tokens 10 -d A.groovy
[ERROR] Skipping file: Lexical error in file 'A.groovy' at line 1, column 12: token recognition error at: '\n'

"x $i" fails the same way. "x $i y" and "${i}" pass.

When another string follows on the same line, the lexer throws GroovySyntaxError instead:

f("x-$i", "y")
$ pmd cpd -l groovy --minimum-tokens 10 --no-fail-on-error -d B.groovy
Exception in thread "main" org.apache.groovy.parser.antlr4.GroovySyntaxError: Unexpected character: '"'
      at org.apache.groovy.parser.antlr4.SyntaxErrorReportable.throwSyntaxError(SyntaxErro
      ...
      at org.apache.groovy.parser.antlr4.GroovyLexer.UNEXPECTED_CHAR_action(GroovyLexer.ja

GroovySyntaxError extends AssertionError, so CPD does not skip the file. The run ends with no
report.

Expected outcome:

CPD tokenizes both files, as groovyc compiles them.

I ran both lexers over the Groovy test files of one project (Spock specs), with an error
listener that throws, as GroovyTokenManager does:

  • GroovyLexer: many files fail.
  • GroovyLangLexer: 0 files fail.

So the fix is likely one line in GroovyCpdLexer:

return new GroovyTokenManager(new GroovyLangLexer(charStream), doc);

A separate question: should CPD catch GroovySyntaxError, so that one bad file is skipped
of ending the run?

Running PMD through: CLI

Activity

  1. added
    a:bug
    on Sep 23, 2026
  2. added a commit that references this issue on Sep 24, 2026
  3. added this to the 7.29.0 milestone on Sep 29, 2026
  4. added a commit that references this issue on Sep 29, 2026
    0fcba96
  5. added
    in:cpd
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugin:cpd

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions