Skip to content

StringIndexOutOfBoundsException when printing warnings in transformed files #3720

Open
@jridgewell

Description

@jridgewell

AMP is seeing an exception being thrown when printing a code warning: ampproject/amp-closure-compiler#22

java.lang.StringIndexOutOfBoundsException: String index out of range: 75
        at java.lang.StringLatin1.charAt(StringLatin1.java:47)
        at java.lang.String.charAt(String.java:693)
        at com.google.javascript.jscomp.LightweightMessageFormatter.padLine(LightweightMessageFormatter.java:197)
        at com.google.javascript.jscomp.LightweightMessageFormatter.padMultipleLines(LightweightMessageFormatter.java:251)
        at com.google.javascript.jscomp.LightweightMessageFormatter.getExcerptWithPosition(LightweightMessageFormatter.java:161)
        at com.google.javascript.jscomp.LightweightMessageFormatter.format(LightweightMessageFormatter.java:133)
        at com.google.javascript.jscomp.LightweightMessageFormatter.formatWarning(LightweightMessageFormatter.java:86)
        at com.google.javascript.jscomp.JSError.format(JSError.java:171)
        at com.google.javascript.jscomp.PrintStreamErrorReportGenerator.println(PrintStreamErrorReportGenerator.java:56)
        at com.google.javascript.jscomp.PrintStreamErrorReportGenerator.generateReport(PrintStreamErrorReportGenerator.java:50)
        at com.google.javascript.jscomp.SortingErrorManager.generateReport(SortingErrorManager.java:116)
        at com.google.javascript.jscomp.ThreadSafeDelegatingErrorManager.generateReport(ThreadSafeDelegatingErrorManager.java:40)
        at com.google.javascript.jscomp.Compiler.generateReport(Compiler.java:724)
        at com.google.javascript.jscomp.AbstractCommandLineRunner.performFullCompilation(AbstractCommandLineRunner.java:1336)
        at com.google.javascript.jscomp.AbstractCommandLineRunner.doRun(AbstractCommandLineRunner.java:1254)
        at com.google.javascript.jscomp.AbstractCommandLineRunner.run(AbstractCommandLineRunner.java:540)
        at org.ampproject.AmpCommandLineRunner.main(Unknown Source)

After a bit of tracking, we've managed to isolate it down to the file's input sourcemap causing a confusion in the source code excerpt printer. Essentially, it's trying to print the transformed file's source code using locations from the original source. This is because the transformed code and the original source file share the same file name (the code transform is happening in memory with Gulp before passing it to Closure).

I've made an easy reproduction at https://github.com/jridgewell/closure-warn-sourcemaps-oob.

Metadata

Metadata

Assignees

No one assigned

    Labels

    customer issuecustomer question, problem, or pull request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions