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
41 stars 21 forks source link

[Bug]: When using a wrapped `swc` transpiler in a `ts_project`, source maps are wrong. #222

Open supercairos opened 5 months ago

supercairos commented 5 months ago

What happened?

I'm using something simple like this in order to produce source_maps:

This file is in a BUILD.bazel in a folder hello

ts_project(
    name = "lib",
    srcs = glob([
        "src/**/*.ts",
    ]),
    declaration = True,
    declaration_map = True,
    source_map = True,
    transpiler = partial.make(
        swc,
        source_maps = True,
        swcrc = ":.swcrc",
    ),
    tsconfig = "tsconfig",
    validate = True,
    visibility = ["//visibility:public"],
    deps = [
        ":node_modules",
    ],
)

It produce:

{
   "version":3,
   "sources":[
      "index.ts"
   ],
   ...
}

Where it should be:

{
   "version":3,
   "sources":[
      "src/index.ts"
   ],
   "sourceRoot":  "hello"
   ...
}

Version

Development (host) and target OS/architectures:

Output of bazel --version: 6.4.0

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file:

http_archive(
    name = "rules_nodejs",
    sha256 = "162f4adfd719ba42b8a6f16030a20f434dc110c65dc608660ef7b3411c9873f9",
    strip_prefix = "rules_nodejs-6.0.2",
    url = "https://github.com/bazelbuild/rules_nodejs/releases/download/v6.0.2/rules_nodejs-v6.0.2.tar.gz",
)

http_archive(
    name = "aspect_rules_swc",
    sha256 = "8eb9e42ed166f20cacedfdb22d8d5b31156352eac190fc3347db55603745a2d8",
    strip_prefix = "rules_swc-1.1.0",
    url = "https://github.com/aspect-build/rules_swc/releases/download/v1.1.0/rules_swc-v1.1.0.tar.gz",
)

http_archive(
    name = "aspect_rules_ts",
    sha256 = "bd3e7b17e677d2b8ba1bac3862f0f238ab16edb3e43fb0f0b9308649ea58a2ad",
    strip_prefix = "rules_ts-2.1.0",
    url = "https://github.com/aspect-build/rules_ts/releases/download/v2.1.0/rules_ts-v2.1.0.tar.gz",
)

http_archive(
    name = "aspect_rules_js",
    sha256 = "76a04ef2120ee00231d85d1ff012ede23963733339ad8db81f590791a031f643",
    strip_prefix = "rules_js-1.34.1",
    url = "https://github.com/aspect-build/rules_js/releases/download/v1.34.1/rules_js-v1.34.1.tar.gz",
)

Language(s) and/or frameworks involved:

How to reproduce

No response

Any other information?

No response

pat-trunk-io commented 3 months ago

You can set sourceRoot with:

swc( ... source_root = package_name() # or native.package_name() in macros )

However I haven't solved the missing path in the sources array

pat-trunk-io commented 3 months ago

Actually, this commit made it work for me: https://github.com/aspect-build/rules_swc/compare/main...pat-trunk-io:rules_swc:pat/FixSourcesArraySourceMap

jbedard commented 2 months ago

@pat-trunk-io that fix looks correct, would you like to open a PR in rules_swc? A test showing it's fixed would also be great if you can add one? Or if your fix breaks an existing test maybe the existing one just needs an update. See the tests that have a *.map.golden file and try using one of those to demo the fix 👍

pat-trunk-io commented 2 months ago

Sounds good. Little busy this week, should be able to get around next week.