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

Add tsconfig exclude validation #730

Open
wants to merge 5 commits 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
5 changes: 4 additions & 1 deletion examples/assets/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ ts_project(
],
extends = ":config",
out_dir = "out",
tsconfig = {"compilerOptions": {"outDir": "out"}},
tsconfig = {
"compilerOptions": {"outDir": "out"},
"exclude": [],
},
)

filegroup(
Expand Down
3 changes: 3 additions & 0 deletions examples/declaration_dir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ ts_project(
out_dir = "out/code",
root_dir = "dir",
source_map = True,
tsconfig = {
"exclude": [],
},
)

# Assert that the output locations we wrote to match expectations
Expand Down
1 change: 1 addition & 0 deletions examples/extends_chain/main/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ ts_project(
"compilerOptions": {
"declaration": False,
},
"exclude": [],
},
)
10 changes: 10 additions & 0 deletions examples/jsx/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ ts_project(
out_dir = "out",
preserve_jsx = True,
source_map = True,
tsconfig = {
"exclude": [],
"compilerOptions": {
"allowJs": True,
"sourceMap": True,
"declaration": True,
"declarationMap": True,
"jsx": "preserve",
},
},
)

filegroup(
Expand Down
6 changes: 6 additions & 0 deletions examples/module_ext/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ ts_project(
declaration_map = True,
out_dir = "out",
source_map = True,
tsconfig = {
"exclude": [],
},
)

# You can also produce different module syntaxt outputs from a .ts file.
Expand All @@ -44,6 +47,9 @@ ts_project(
# Write the output files to an extra nested directory
out_dir = format,
source_map = True,
tsconfig = {
"exclude": [],
},
)
for format in [
"commonjs",
Expand Down
1 change: 1 addition & 0 deletions examples/no_emit/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ts_project(
"allowJs": True,
"noEmit": True,
},
"exclude": [],
},
)

Expand Down
9 changes: 7 additions & 2 deletions examples/out_dir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ts_project(
"declaration": True,
"declarationMap": True,
},
"exclude": [],
},
)

Expand All @@ -19,7 +20,9 @@ ts_project(
declaration = True,
declaration_map = True,
out_dir = "param",
tsconfig = {},
tsconfig = {
"exclude": [],
},
)

ts_project(
Expand All @@ -29,7 +32,9 @@ ts_project(
declaration_dir = "decl_map",
declaration_map = True,
out_dir = "declaration_dir",
tsconfig = {},
tsconfig = {
"exclude": [],
},
)

build_test(
Expand Down
2 changes: 2 additions & 0 deletions examples/resolve_json_module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ts_project(
"outDir": "ts-dict-override",
"resolveJsonModule": True,
},
"exclude": [],
},
)

Expand All @@ -40,6 +41,7 @@ ts_project(
"compilerOptions": {
"outDir": "ts-dict-unspecified",
},
"exclude": [],
},
)

Expand Down
3 changes: 3 additions & 0 deletions examples/root_dir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ ts_project(
name = "replace",
out_dir = "otherdir",
root_dir = "subdir",
tsconfig = {
"exclude": [],
},
)

assert_outputs(
Expand Down
5 changes: 4 additions & 1 deletion examples/root_dirs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ ts_project(
srcs = ["src/lib.ts"],
declaration = True,
out_dir = "dist",
tsconfig = {},
tsconfig = {
"exclude": [],
},
)

ts_project(
Expand All @@ -26,6 +28,7 @@ ts_project(
"dist",
],
},
"exclude": [],
},
deps = [":lib"],
)
3 changes: 3 additions & 0 deletions examples/simple/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ ts_project(
# "." is the same as default
# explicitly given as a regression test for https://github.com/aspect-build/rules_ts/issues/195
out_dir = ".",
tsconfig = {
"exclude": [],
},
# Note, the tsconfig attribute defaults to the tsconfig.json file in this directory.
# tsconfig = "<default>",
deps = [
Expand Down
9 changes: 9 additions & 0 deletions examples/srcs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ load("@bazel_skylib//rules:write_file.bzl", "write_file")
ts_project(
name = "srcs-auto",
out_dir = "auto",
tsconfig = {
"exclude": [],
},
)

# The sources can come from the default output of some other target.
Expand All @@ -24,6 +27,9 @@ ts_project(
name = "srcs-filegroup",
srcs = [":srcs"],
out_dir = "filegroup",
tsconfig = {
"exclude": [],
},
)

# Tools can also generate .ts source files, so the sources are actually in the bazel-out tree.
Expand All @@ -45,6 +51,9 @@ ts_project(
"generated.ts",
],
out_dir = "generated",
tsconfig = {
"exclude": [],
},
)

# Testing what outputs are actually produced
Expand Down
3 changes: 2 additions & 1 deletion examples/targets/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"compilerOptions": {
"declaration": true,
"sourceMap": true
}
},
"exclude": []
}
12 changes: 12 additions & 0 deletions examples/transpiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ ts_project(
out_dir = "build-babel",
source_map = True,
transpiler = babel,
tsconfig = {
"exclude": [],
},
)

# Runs babel to transpile ts -> js
Expand Down Expand Up @@ -76,6 +79,7 @@ ts_project(
"compilerOptions": {
"declaration": False,
},
"exclude": [],
},
)

Expand Down Expand Up @@ -137,6 +141,9 @@ ts_project(
tsc_js,
out_dir = "build-custom_transpilers",
),
tsconfig = {
"exclude": [],
},
)

build_test(
Expand Down Expand Up @@ -207,6 +214,9 @@ ts_project(
out_dir = "build-custom_dts_transpiler",
source_map = True,
transpiler = "tsc",
tsconfig = {
"exclude": [],
},
)

build_test(
Expand Down Expand Up @@ -236,6 +246,7 @@ ts_project(
"compilerOptions": {
"declaration": False,
},
"exclude": [],
},
)

Expand Down Expand Up @@ -263,6 +274,7 @@ ts_project(
"declaration": True,
"emitDeclarationOnly": True,
},
"exclude": [],
},
)

Expand Down
3 changes: 0 additions & 3 deletions ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
common_args.extend(ctx.attr.args)

if (ctx.attr.out_dir and ctx.attr.out_dir != ".") or ctx.attr.root_dir:
# TODO: add validation that excludes is non-empty in this case, as passing the --outDir or --declarationDir flag
# to TypeScript causes it to set a default for excludes such that it won't find our sources that were copied-to-bin.
# See https://github.com/microsoft/TypeScript/issues/59036 and https://github.com/aspect-build/rules_ts/issues/644
common_args.extend([
"--outDir",
_lib.join(ctx.label.workspace_root, ctx.label.package, ctx.attr.out_dir),
Expand Down
13 changes: 12 additions & 1 deletion ts/private/ts_project_options_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function main(_a) {
output = _a[1],
target = _a[2],
packageDir = _a[3],
attrsStr = _a[4]
attrsStr = _a[4],
out_dir = _a[5]
// The Bazel ts_project attributes were json-encoded
// (on Windows the quotes seem to be quoted wrong, so replace backslash with quotes :shrug:)
var attrs = JSON.parse(attrsStr.replace(/\\/g, '"'))
Expand Down Expand Up @@ -142,6 +143,15 @@ function main(_a) {
}
}
}

function check_exclude_and_outDir() {
if (config.exclude === undefined && out_dir !== undefined && out_dir !== '.' && out_dir !== '') {
console.error('tsconfig validation failed: when out_dir is set, exclude must also be set. See: https://github.com/aspect-build/rules_ts/issues/644 for more details.')

throw new Error('tsconfig validation failed: when out_dir is set, exclude must also be set.')
}
}

if (options.preserveSymlinks) {
console.error(
'ERROR: ts_project rule ' +
Expand All @@ -165,6 +175,7 @@ function main(_a) {
check('tsBuildInfoFile', 'ts_build_info_file')
check_nocheck()
check_preserve_jsx()
check_exclude_and_outDir()
if (failures.length > 0) {
console.error(
'ERROR: ts_project rule ' +
Expand Down
1 change: 1 addition & 0 deletions ts/private/ts_validate_options.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def _validate_action(ctx, tsconfig_inputs):
str(ctx.label),
ctx.label.package,
json.encode(config),
ctx.attr.out_dir,
])

ctx.actions.run(
Expand Down
Loading