Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler crashes with dymamic imports (IllegalStateException) #4126

Open
corrideat opened this issue Oct 26, 2023 · 3 comments
Open

Compiler crashes with dymamic imports (IllegalStateException) #4126

corrideat opened this issue Oct 26, 2023 · 3 comments

Comments

@corrideat
Copy link

% echo 'const x = async () => { const buffer = await import(/** @type {string} */("buffer")); alert("1", buffer);}; x();' | google-closure-compiler -W QUIET --module_resolution WEBPACK -O ADVANCED
The compiler is waiting for input via stdin.
java.lang.IllegalStateException: AST should not contain Dynamic module import. Reference node:
DYNAMIC_IMPORT 1:45  [length: 39] [source_file: stdin]
    STRINGLIT buffer 1:74  [length: 8] [is_parenthesized: 1] [source_file: stdin]

 Parent node:
AWAIT 1:39  [length: 45] [source_file: stdin]
    DYNAMIC_IMPORT 1:45  [length: 39] [source_file: stdin]
        STRINGLIT buffer 1:74  [length: 8] [is_parenthesized: 1] [source_file: stdin]

        at com.google.javascript.jscomp.AstValidator$1.handleViolation(AstValidator.java:87)
        at com.google.javascript.jscomp.AstValidator.violation(AstValidator.java:2135)
        at com.google.javascript.jscomp.AstValidator.validateFeature(AstValidator.java:2214)
        at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:456)
        at com.google.javascript.jscomp.AstValidator.validateAwait(AstValidator.java:635)
        at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:452)
        at com.google.javascript.jscomp.AstValidator.validateNameDeclarationChild(AstValidator.java:1352)
        at com.google.javascript.jscomp.AstValidator.validateNameDeclarationHelper(AstValidator.java:1316)
        at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:210)
        at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:157)
        at com.google.javascript.jscomp.AstValidator.validateBlock(AstValidator.java:985)
        at com.google.javascript.jscomp.AstValidator.validateFunctionExpressionHelper(AstValidator.java:1103)
        at com.google.javascript.jscomp.AstValidator.validateFunctionExpression(AstValidator.java:1084)
        at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:432)
        at com.google.javascript.jscomp.AstValidator.validateCall(AstValidator.java:1172)
        at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:420)
        at com.google.javascript.jscomp.AstValidator.validateExprStmt(AstValidator.java:1561)
        at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:213)
        at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:157)
        at com.google.javascript.jscomp.AstValidator.validateStatements(AstValidator.java:151)
        at com.google.javascript.jscomp.AstValidator.validateScript(AstValidator.java:140)
        at com.google.javascript.jscomp.AstValidator.validateCodeRoot(AstValidator.java:126)
        at com.google.javascript.jscomp.AstValidator.process(AstValidator.java:111)
        at com.google.javascript.jscomp.PhaseOptimizer$NamedPass.process(PhaseOptimizer.java:240)
        at com.google.javascript.jscomp.PhaseOptimizer.process(PhaseOptimizer.java:179)
        at com.google.javascript.jscomp.Compiler.performFinalizations(Compiler.java:2981)
        at com.google.javascript.jscomp.Compiler.lambda$stage3Passes$8(Compiler.java:1054)
        at com.google.javascript.jscomp.CompilerExecutor.lambda$runInCompilerThread$0(CompilerExecutor.java:100)
        at java.base@17.0.7/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base@17.0.7/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base@17.0.7/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base@17.0.7/java.lang.Thread.run(Thread.java:833)
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
        at org.graalvm.nativeimage.builder/com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:203)
@frigus02
Copy link
Contributor

Dynamic imports are only supported if the output chunk type is ES_MODULES. This works:

$ echo 'const x = async () => { const buffer = await import(/** @type {string} */("buffer")); alert("1", buffer);}; x();' | google-closure-compiler -W QUIET --module_resolution WEBPACK -O ADVANCED --chunk_output_type ES_MODULES
The compiler is waiting for input via stdin.
(async()=>{const a=await import("buffer");alert("1",a)})();export{};

The crash is not great, though. Would be nice if there was a good error message that mentioned the chunk type requirement.

@corrideat
Copy link
Author

Thanks, I'll see if that works in my application.

Dynamic imports are only supported if the output chunk type is ES_MODULES.

Hm, that may be a design decision you've made and I'm fine leaving it at that. However, dynamic imports are, in principle, allowed everywhere (providing support for import, of course). E.g., <script>import('foo').finally(()=>alert("Yay!"))</script> is perfectly valid, no need for <script type="module"> there.

@corrideat corrideat changed the title Compiler crashes with dymamic improts (IllegalStateException) Compiler crashes with dymamic imports (IllegalStateException) Oct 28, 2023
@hanguokai
Copy link
Contributor

It works in v20230502 and before, but it fails from v20230802. Hope this issue can be resolved soon.

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

No branches or pull requests

3 participants