google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.4k stars 1.15k forks source link

StringIndexOutOfBoundsException #3876

Closed nicopowa closed 2 years ago

nicopowa commented 3 years ago

Hello Closure community,

I have a StringIndexOutOfBoundsException problem sometimes while compiling lots of ES6 Vanilla files and many externs. There are a few posts out there but I couldn't find a solution. I know it comes from my code, usually from bad JSDoc annotations, or generated externs with inconsistencies, but there are no hints or details in the stack trace.

It would be so nice to get the line where the crash comes from ! I'll try to isolate the problem in a minimal example, but it will take some time.

Here's an error :

java.lang.StringIndexOutOfBoundsException: String index out of range: 8 at java.lang.String.charAt(Unknown Source) at com.google.javascript.jscomp.LightweightMessageFormatter.padLine(LightweightMessageFormatter.java:198) at com.google.javascript.jscomp.LightweightMessageFormatter.padMultipleLines(LightweightMessageFormatter.java:252) 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:731) 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:533) at com.google.javascript.jscomp.CommandLineRunner.main(CommandLineRunner.java:2150)

h-joo commented 3 years ago

Hi, thank you for the report. May I ask which version of closure you're using? Also, we'll be waiting for a small repro if possible 🙂 From my understanding you're request is to

  1. Have a better report for the location when the compiler fails ungracefully.
  2. To fix the bug.

Would that be correct?

nicopowa commented 3 years ago

Hi, thanks for your answer !

  1. It would be great to make the compiler show line number and character index
  2. I don't think its a bug, maybe it's related to the way I use the compiler.

using JSDoc to annotate VanillaJS ES6 class methods and types, I wrote a method that needs two args : a string and a MediaStreamTrack, typed the JSDoc bloc quickly, specified @param {MediaStreamTrack} track, got the exception again, Fixed the type to non-nullable {!MediaStreamTrack}, and the compiler was happy again !

I'm still not sure how it happened the first time, as I wrote probably because of corrupted externs file.

lauraharker commented 2 years ago

I'm sending out a PR to make the compiler report more information when it crashes here.

I suspect some internal invariant of the compiler has been violated before we get to LightweightMessageFormatter. It would be useful to figure out where that's happening and fix it.

lauraharker commented 2 years ago

Closing this issue now that the stack trace for these issues should be more helpful.

@nicopowa if you run into this crash again (after the stack trace fix is released), please do file a new bug.