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

Deno panic using JSX #21927

Closed
begoon opened this issue Jan 13, 2024 · 9 comments · Fixed by #22341
Closed

Deno panic using JSX #21927

begoon opened this issue Jan 13, 2024 · 9 comments · Fixed by #22341
Labels
bug Something isn't working swc related to swc (bundling/transpiling)

Comments

@begoon
Copy link

begoon commented Jan 13, 2024

Code to reproduce:

/** @jsx react-jsx */
/** @jsxImportSource https://esm.sh/preact */

import { render } from "https://esm.sh/preact-render-to-string@6.3.1";

const component = <h1>Hello World</h1>;

console.log(render(component));

execute: deno run main-deno.tsx

deno.json:

{
    "compilerOptions": {}
}
RUST_BACKTRACE=1 deno run main-deno.tsx    

============================================================
Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.

Platform: macos aarch64
Version: 1.39.3
Args: ["deno", "run", "main-deno.tsx"]

thread 'main' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_typescript-0.186.14/src/typescript.rs:131:13:
failed to determine top-level Id for jsx expression
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: swc_ecma_transforms_typescript::typescript::id_for_jsx
   3: <swc_ecma_ast::module::Program as swc_ecma_visit::FoldWith<V>>::fold_with
   4: deno_ast::transpiling::fold_program
   5: deno_ast::transpiling::<impl deno_ast::parsed_source::ParsedSource>::transpile
   6: deno::emit::Emitter::emit_parsed_source
   7: deno::module_loader::PreparedModuleLoader::load_prepared_module
   8: <deno::module_loader::CliModuleLoader as deno_core::modules::loaders::ModuleLoader>::load
   9: <deno_core::modules::recursive_load::RecursiveModuleLoad as futures_core::stream::Stream>::poll_next::{{closure}}
  10: <deno_core::modules::recursive_load::RecursiveModuleLoad as futures_core::stream::Stream>::poll_next
  11: <deno_core::modules::recursive_load::RecursiveModuleLoad as futures_core::stream::Stream>::poll_next
  12: deno::worker::CliMainWorker::execute_main_module_possibly_with_npm::{{closure}}
  13: deno::worker::CliMainWorker::run::{{closure}}
  14: deno::tools::run::run_script::{{closure}}
  15: deno::spawn_subcommand::{{closure}}
  16: <deno_unsync::task::MaskFutureAsSend<F> as core::future::future::Future>::poll
  17: tokio::runtime::task::raw::poll
  18: deno::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@begoon
Copy link
Author

begoon commented Jan 13, 2024

If I remove the first line /** @jsx react-jsx */, it does NOT crash anymore and prints:

<h1>Hello World</h1>

as expected.

@begoon
Copy link
Author

begoon commented Jan 15, 2024

In Deno v1.39.4 the issue still stands.

@begoon
Copy link
Author

begoon commented Jan 15, 2024

============================================================
Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.

Platform: macos aarch64
Version: 1.39.4
Args: ["deno", "run", "a.tsx"]

thread 'main' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_typescript-0.186.14/src/typescript.rs:131:13:
failed to determine top-level Id for jsx expression
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: swc_ecma_transforms_typescript::typescript::id_for_jsx
   3: <swc_ecma_ast::module::Program as swc_ecma_visit::FoldWith<V>>::fold_with
   4: deno_ast::transpiling::fold_program
   5: deno_ast::transpiling::<impl deno_ast::parsed_source::ParsedSource>::transpile
   6: deno::emit::Emitter::emit_parsed_source
   7: deno::module_loader::PreparedModuleLoader::load_prepared_module
   8: <deno::module_loader::CliModuleLoader as deno_core::modules::loaders::ModuleLoader>::load
   9: <deno_core::modules::recursive_load::RecursiveModuleLoad as futures_core::stream::Stream>::poll_next::{{closure}}
  10: <deno_core::modules::recursive_load::RecursiveModuleLoad as futures_core::stream::Stream>::poll_next
  11: <deno_core::modules::recursive_load::RecursiveModuleLoad as futures_core::stream::Stream>::poll_next
  12: deno::worker::CliMainWorker::execute_main_module_possibly_with_npm::{{closure}}
  13: deno::worker::CliMainWorker::run::{{closure}}
  14: deno::tools::run::run_script::{{closure}}
  15: deno::spawn_subcommand::{{closure}}
  16: <deno_unsync::task::MaskFutureAsSend<F> as core::future::future::Future>::poll
  17: tokio::runtime::task::raw::poll
  18: deno::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@bartlomieju
Copy link
Member

@dsherret could you please take a look?

@bartlomieju bartlomieju added bug Something isn't working swc related to swc (bundling/transpiling) labels Jan 15, 2024
@magic-akari
Copy link

react-jsx will be parsed as react - jsx which is a subtraction binary expression and cannot be used as valid JSX.
A valid JSX can be h or preact.h.

For SWC, a user-friendly error is preferable to a panic.

@marvinhagemeister
Copy link
Contributor

marvinhagemeister commented Jan 16, 2024

I think there is a bit of confusion as to what the @jsx comment does. It sets the pragma for the classic JSX transform. That value is expected to be something callable like React.createElement or h. It doesn't set the jsx transform mode which may seem like it does because TypeScript's jsx compiler option is named the same way.

That said Deno should not panic on this though.

@melbourne2991
Copy link

In the context of Deno I don't think it should error at all right? Given it's a runtime, not a compiler.

@marvinhagemeister
Copy link
Contributor

Deno is kinda both. To support TypeScript the types need to be stripped somewhere since V8 only works with normal JavaScript. Same is true for JSX, so there are some light transpilation passes in Deno to make this work.

@melbourne2991
Copy link

Yeah that's true. I think in this case though, an invalid pragma is still valid javascript/typescript?

I did a quick test and it panics even if you have just the pragma without any JSX.

kdy1 pushed a commit to swc-project/swc that referenced this issue Jan 19, 2024
**Description:**

Currently a jsx pragma with an invalid js identifier (eg, with dashes: `@jsx bad-pragma` causes a panic.

This PR prevents a panic and will ignore an invalid pragma in a comment.

(There may be an argument for showing an error or warning instead, but
given a jsx pragma is still valid ES I'm not sure whether that makes
sense?)

[Original (Deno) issue
here](denoland/deno#21927)
littledivy pushed a commit that referenced this issue Feb 15, 2024
* Uses diagnostics from deno_ast
* Real fix for #22310
* Moves `deno lint --json` code here
* Upgrades swc
   
 Closes #22117
 Closes #22109
 Closes #21927
 Closes #20993
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working swc related to swc (bundling/transpiling)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants