Skip to content

Commit 03ab674

Browse files
fix: handle dot root dir depth (#306)
1 parent ec7b6c3 commit 03ab674

File tree

10 files changed

+98
-1
lines changed

10 files changed

+98
-1
lines changed

examples/root_dir/BUILD.bazel

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ swc(
1414
source_maps = True,
1515
)
1616

17+
# Test case for root_dir="." (should have depth 0)
18+
swc(
19+
name = "compile_dot",
20+
srcs = [
21+
"src/a.ts",
22+
"src/b.ts",
23+
"src/sub/c.ts",
24+
],
25+
out_dir = "out_dot",
26+
root_dir = ".",
27+
source_maps = True,
28+
)
29+
30+
# Test case for nested root_dir
31+
swc(
32+
name = "compile_nested",
33+
srcs = [
34+
"src/sub/c.ts",
35+
],
36+
out_dir = "out_nested",
37+
root_dir = "src/sub",
38+
source_maps = True,
39+
)
40+
1741
# Assert that the output of "compile" rule matches the expected file.
1842
write_source_files(
1943
name = "test",
@@ -23,6 +47,24 @@ write_source_files(
2347
},
2448
)
2549

50+
# Assert outputs for dot root_dir test
51+
write_source_files(
52+
name = "test_dot",
53+
files = {
54+
"expected_dot/src/a.js": ":out_dot/src/a.js",
55+
"expected_dot/src/b.js": ":out_dot/src/b.js",
56+
"expected_dot/src/sub/c.js": ":out_dot/src/sub/c.js",
57+
},
58+
)
59+
60+
# Assert outputs for nested root_dir test
61+
write_source_files(
62+
name = "test_nested",
63+
files = {
64+
"expected_nested/c.js": ":out_nested/c.js",
65+
},
66+
)
67+
2668
[
2769
assert_json_matches(
2870
name = "test_%s" % f.replace("/", "-"),
@@ -37,3 +79,26 @@ write_source_files(
3779
"sub/c",
3880
]
3981
]
82+
83+
[
84+
assert_json_matches(
85+
name = "test_dot_%s" % f.replace("/", "-"),
86+
file1 = ":out_dot/%s.js.map" % f,
87+
file2 = "expected_dot/%s.js.map.golden" % f,
88+
filter1 = ".sourceRoot,.sources",
89+
filter2 = ".sourceRoot,.sources",
90+
)
91+
for f in [
92+
"src/a",
93+
"src/b",
94+
"src/sub/c",
95+
]
96+
]
97+
98+
assert_json_matches(
99+
name = "test_nested_c",
100+
file1 = ":out_nested/c.js.map",
101+
file2 = "expected_nested/c.js.map.golden",
102+
filter1 = ".sourceRoot,.sources",
103+
filter2 = ".sourceRoot,.sources",
104+
)

examples/root_dir/expected_dot/src/a.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sources": [
3+
"../../src/a.ts"
4+
]
5+
}

examples/root_dir/expected_dot/src/b.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sources": [
3+
"../../src/b.ts"
4+
]
5+
}

examples/root_dir/expected_dot/src/sub/c.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sources": [
3+
"../../../src/sub/c.ts"
4+
]
5+
}

examples/root_dir/expected_nested/c.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sources": [
3+
"../src/sub/c.ts"
4+
]
5+
}

swc/private/swc.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def _calculate_source_file(ctx, src):
245245
# out of src subdir
246246
if src_pkg:
247247
src_pkg_depth = len(src_pkg.split("/"))
248-
root_dir_depth = len(ctx.attr.root_dir.split("/")) if ctx.attr.root_dir else 0
248+
root_dir_depth = len(ctx.attr.root_dir.split("/")) if ctx.attr.root_dir and ctx.attr.root_dir != "." else 0
249249
effective_depth = max(0, src_pkg_depth - root_dir_depth)
250250
s = paths.join(s, "/".join([".." for _ in range(effective_depth)]))
251251

0 commit comments

Comments
 (0)