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

fix: re-establish pre-3.2.1 copy_data_to_bin behavior #717

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def ts_project(
declaration_dir = None,
out_dir = None,
root_dir = None,
copy_data_to_bin = None,
supports_workers = -1,
**kwargs):
"""Compiles one TypeScript project using `tsc --project`.
Expand Down Expand Up @@ -240,6 +241,9 @@ def ts_project(
ts_build_info_file: The user-specified value of `tsBuildInfoFile` from the tsconfig.
Helps Bazel to predict the path where the .tsbuildinfo output is written.

copy_data_to_bin: When True, `data` files are copied to the Bazel output tree before being passed as inputs to runfiles.
Defaults to `transpiler != None` due to backwards compatiblity (see #716/ #411).

supports_workers: Whether the "Persistent Worker" protocol is enabled.
This uses a custom `tsc` compiler to make rebuilds faster.
Note that this causes some known correctness bugs, see
Expand Down Expand Up @@ -421,6 +425,9 @@ def ts_project(
"//conditions:default": False,
})

if copy_data_to_bin == None:
copy_data_to_bin = transpiler != None

ts_project_rule(
name = name,
srcs = srcs,
Expand Down Expand Up @@ -460,6 +467,7 @@ def ts_project(
is_typescript_5_or_greater = is_typescript_5_or_greater,
validate = validate,
validator = validator,
copy_data_to_bin = copy_data_to_bin,
**kwargs
)

Expand Down
4 changes: 4 additions & 0 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more
default = True,
),
"validator": attr.label(mandatory = True, executable = True, cfg = "exec"),
"copy_data_to_bin": attr.bool(
doc = """When True, data files are copied to the Bazel output tree before being passed as inputs to runfiles.""",
default = False,
),
"_options": attr.label(
default = "@aspect_rules_ts//ts:options",
),
Expand Down
2 changes: 2 additions & 0 deletions ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
sources = output_sources_depset,
data = ctx.attr.data,
deps = srcs_tsconfig_deps,
data_files = ctx.files.data,
copy_data_files_to_bin = ctx.attr.copy_data_to_bin,
)

providers = [
Expand Down
78 changes: 78 additions & 0 deletions ts/test/copy_data_to_bin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Test data file copying behavior. See
#
# https://github.com/aspect-build/rules_ts/issues/411
# https://github.com/aspect-build/rules_ts/issues/716
#
# Ideally, the behaviors with and without `transpiler` would align, but that is
# backwards incompatible.

load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_contains")
load("@aspect_rules_js//js:defs.bzl", "js_binary", "js_run_binary")
load("//ts:defs.bzl", "ts_project")
load("//ts/test:mock_transpiler.bzl", "mock")

copy_file(
name = "no_transpiler_src",
src = "check_has_data.ts",
out = "no_transpiler.ts",
)

ts_project(
name = "no_transpiler",
srcs = ["no_transpiler.ts"],
data = ["data.txt"],
tsconfig = {},
)

js_binary(
name = "no_transpiler_bin",
data = [":no_transpiler"],
entry_point = "no_transpiler.ts",
)

js_run_binary(
name = "no_transpiler_gen",
chdir = package_name(),
stdout = "no_transpiler_out.txt",
tool = ":no_transpiler_bin",
)

assert_contains(
name = "no_transpiler_test",
actual = "no_transpiler_out.txt",
expected = "false",
)

copy_file(
name = "with_transpiler_src",
src = "check_has_data.ts",
out = "with_transpiler.ts",
)

ts_project(
name = "with_transpiler",
srcs = ["with_transpiler.ts"],
data = ["data.txt"],
transpiler = mock,
tsconfig = {},
)

js_binary(
name = "with_transpiler_bin",
data = [":with_transpiler"],
entry_point = "with_transpiler.js",
)

js_run_binary(
name = "with_transpiler_gen",
chdir = package_name(),
stdout = "with_transpiler_out.txt",
tool = ":with_transpiler_bin",
)

assert_contains(
name = "with_transpiler_test",
actual = "with_transpiler_out.txt",
expected = "true",
)
4 changes: 4 additions & 0 deletions ts/test/copy_data_to_bin/check_has_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-ignore
const { readdirSync } = require("node:fs");

console.log(readdirSync(".").includes("data.txt"));
1 change: 1 addition & 0 deletions ts/test/copy_data_to_bin/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
irrelevant data in a file
Loading