Skip to content

Commit 6de1bc2

Browse files
committed
fix: re-establish pre-3.2.1 copy_data_to_bin behavior
1 parent a6a7588 commit 6de1bc2

File tree

7 files changed

+106
-7
lines changed

7 files changed

+106
-7
lines changed

docs/rules.md

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts/defs.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def ts_project(
5959
declaration_dir = None,
6060
out_dir = None,
6161
root_dir = None,
62+
copy_data_to_bin = None,
6263
supports_workers = -1,
6364
**kwargs):
6465
"""Compiles one TypeScript project using `tsc --project`.
@@ -240,6 +241,9 @@ def ts_project(
240241
ts_build_info_file: The user-specified value of `tsBuildInfoFile` from the tsconfig.
241242
Helps Bazel to predict the path where the .tsbuildinfo output is written.
242243
244+
copy_data_to_bin: When True, `data` files are copied to the Bazel output tree before being passed as inputs to runfiles.
245+
Defaults to `transpiler != None` due to backwards compatiblity (see #716/ #411).
246+
243247
supports_workers: Whether the "Persistent Worker" protocol is enabled.
244248
This uses a custom `tsc` compiler to make rebuilds faster.
245249
Note that this causes some known correctness bugs, see
@@ -421,6 +425,9 @@ def ts_project(
421425
"//conditions:default": False,
422426
})
423427

428+
if copy_data_to_bin == None:
429+
copy_data_to_bin = transpiler != None
430+
424431
ts_project_rule(
425432
name = name,
426433
srcs = srcs,
@@ -460,6 +467,7 @@ def ts_project(
460467
is_typescript_5_or_greater = is_typescript_5_or_greater,
461468
validate = validate,
462469
validator = validator,
470+
copy_data_to_bin = copy_data_to_bin,
463471
**kwargs
464472
)
465473

ts/private/ts_lib.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more
116116
default = True,
117117
),
118118
"validator": attr.label(mandatory = True, executable = True, cfg = "exec"),
119+
"copy_data_to_bin": attr.bool(
120+
doc = """When True, data files are copied to the Bazel output tree before being passed as inputs to runfiles.""",
121+
default = False,
122+
),
119123
"_options": attr.label(
120124
default = "@aspect_rules_ts//ts:options",
121125
),

ts/private/ts_project.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
325325
sources = output_sources_depset,
326326
data = ctx.attr.data,
327327
deps = srcs_tsconfig_deps,
328+
data_files = ctx.files.data,
329+
copy_data_files_to_bin = ctx.attr.copy_data_to_bin,
328330
)
329331

330332
providers = [

ts/test/copy_data_to_bin/BUILD.bazel

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Test data file copying behavior. See
2+
#
3+
# https://github.com/aspect-build/rules_ts/issues/411
4+
# https://github.com/aspect-build/rules_ts/issues/716
5+
#
6+
# Ideally, the behaviors with and without `transpiler` would align, but that is
7+
# backwards incompatible.
8+
9+
load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
10+
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
11+
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
12+
load("//ts:defs.bzl", "ts_project")
13+
load("//ts/test:mock_transpiler.bzl", "mock")
14+
15+
copy_file(
16+
name = "no_transpiler_src",
17+
src = "check_has_data.ts",
18+
out = "no_transpiler.ts",
19+
)
20+
21+
ts_project(
22+
name = "no_transpiler",
23+
srcs = ["no_transpiler.ts"],
24+
data = ["data.txt"],
25+
tsconfig = {},
26+
)
27+
28+
js_binary(
29+
name = "no_transpiler_bin",
30+
data = [":no_transpiler"],
31+
entry_point = "no_transpiler.ts",
32+
)
33+
34+
js_run_binary(
35+
name = "no_transpiler_gen",
36+
chdir = package_name(),
37+
stdout = "no_transpiler_out.txt",
38+
tool = ":no_transpiler_bin",
39+
)
40+
41+
assert_contains(
42+
name = "no_transpiler_test",
43+
actual = "no_transpiler_out.txt",
44+
expected = "false",
45+
)
46+
47+
copy_file(
48+
name = "with_transpiler_src",
49+
src = "check_has_data.ts",
50+
out = "with_transpiler.ts",
51+
)
52+
53+
ts_project(
54+
name = "with_transpiler",
55+
srcs = ["with_transpiler.ts"],
56+
data = ["data.txt"],
57+
transpiler = mock,
58+
tsconfig = {},
59+
)
60+
61+
js_binary(
62+
name = "with_transpiler_bin",
63+
data = [":with_transpiler"],
64+
entry_point = "with_transpiler.js",
65+
)
66+
67+
js_run_binary(
68+
name = "with_transpiler_gen",
69+
chdir = package_name(),
70+
stdout = "with_transpiler_out.txt",
71+
tool = ":with_transpiler_bin",
72+
)
73+
74+
assert_contains(
75+
name = "with_transpiler_test",
76+
actual = "with_transpiler_out.txt",
77+
expected = "true",
78+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @ts-ignore
2+
const { readdirSync } = require("node:fs");
3+
4+
console.log(readdirSync(".").includes("data.txt"));

ts/test/copy_data_to_bin/data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
irrelevant data in a file

0 commit comments

Comments
 (0)