aspect-build / rules_swc

Bazel rules for the swc toolchain https://swc.rs/
https://docs.aspect.build/rules/aspect_rules_swc
Apache License 2.0
40 stars 21 forks source link

[Bug]: Using latest SWC rule, swc crash while not finding 'base_dir' #248

Closed supercairos closed 1 month ago

supercairos commented 1 month ago

What happened?

While running the latest version of SWC rules, it crashes with this error:

ERROR: /Users/romain/git/oidc/packages/i18n/express/BUILD:25:11: Compiling //packages/i18n/express:ts_transpile [swc packages/i18n/express/src/i18n.ts] failed: (Exit 101): swc-darwin-arm64 failed: error executing command (from target //packages/i18n/express:ts_transpile) 
  (cd /private/var/tmp/_bazel_romain/aab898f10bfeb90def8d69f3c9616e24/sandbox/darwin-sandbox/1652/execroot/com_shadow_cbp && \
  exec env - \
  external/swc_darwin-arm64/swc-darwin-arm64 compile --source-maps true --source-file-name ../src/i18n.ts --source-root packages/i18n/express --config-file bazel-out/darwin_arm64-fastbuild/bin/packages/i18n/express/.swcrc --out-file bazel-out/darwin_arm64-fastbuild/bin/packages/i18n/express/src/i18n.js packages/i18n/express/src/i18n.ts)
# Configuration: 7f308466bd98cfb2b391af4037586009214979163f843b0f91a1705af46bb64c
# Execution platform: @local_config_platform//:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
thread 'main' panicked at 'base_dir(``) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.', /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.173.38/src/path.rs:104:13

Version

Development (host) and target OS/architectures:

Output of bazel --version: bazel 7.1.1

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: rules_swc-1.2.2 rules_ts-2.2.0 rules_js-1.40.0

Language(s) and/or frameworks involved: TS

How to reproduce

No response

Any other information?

While upgrading from rules_swc-1.1.0 to rules_swc-1.2.2

supercairos commented 1 month ago

Some more info:

Version 1.2.2 works with SWC SWC_VERSION = "v1.3.75" [1.1.0 LATEST] but doesn't with LATEST_SWC_VERSION (v1.3.78)

supercairos commented 1 month ago

Error lies between version SWC v1.3.75 and v1.3.76

supercairos commented 1 month ago

Probably due to this fix: https://github.com/swc-project/swc/pull/7775

supercairos commented 1 month ago

Ok, I've found the solution:

With new versions of SWC, if you specify path rewrite, you need to specify a baseUrl.

For exemple if you have this in you .swcrc:

"jsc": {
...
  "paths": {
      "i18next": [
        "./node_modules/i18next"
      ]
    },
...
}

You have to specify:

"jsc": {
...
  "baseUrl": "."
...
}
jbedard commented 4 weeks ago

Thanks for following up and recording your findings 👍