google / google/closure-compiler

StringIndexOutOfBoundsException when printing warnings in transformed files

Open
#3,720 4 comments 2 reactions 0 assignees View on GitHub
customer issue
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

AMP is seeing an exception being thrown when printing a code warning: https://github.com/ampproject/amp-closure-compiler/issues/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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.