Skip to content

Commit

Permalink
fix: add tsconfig root_dir/exclude validation
Browse files Browse the repository at this point in the history
Throw a better error when exclude needs to be added to tsconfig.json, normalize ts_project *_dir paths.

Close #644
  • Loading branch information
Mivr authored Jan 16, 2025
1 parent 8c328d0 commit 5ac25c1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ def ts_project(
declaration_dir = compiler_options.pop("declarationDir", declaration_dir)
root_dir = compiler_options.pop("rootDir", root_dir)

# When generating a tsconfig.json and we set rootDir we need to add the exclude field,
# because of these tickets (validation for not generated tsconfig is also present elsewhere):
# https://github.com/microsoft/TypeScript/issues/59036 and
# 'https://github.com/aspect-build/rules_ts/issues/644
if root_dir != None and "exclude" not in tsconfig:
tsconfig["exclude"] = []

if srcs == None:
# Default sources based on macro attributes after applying tsconfig properties
srcs = _default_srcs(
Expand Down
5 changes: 5 additions & 0 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ def _to_out_path(f, out_dir, root_dir):

def _to_js_out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, ext_map, default_ext):
outs = []
out_dir = _remove_leading_dot_slash(out_dir)
root_dir = _remove_leading_dot_slash(root_dir)
for f in srcs:
if _is_ts_src(f, allow_js, resolve_json_module, False):
out = _to_out_path(f, out_dir, root_dir)
Expand Down Expand Up @@ -325,6 +327,9 @@ def _declare_outputs(ctx, paths):
for path in paths
]

def _remove_leading_dot_slash(string_path):
return string_path.removeprefix("./") if string_path else string_path

lib = struct(
declare_outputs = _declare_outputs,
join = _join,
Expand Down
21 changes: 21 additions & 0 deletions ts/private/ts_project_options_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ function main(_a) {
)
}
}
function checkRootDirExclude() {
var rootDirAttrValue = attrs["root_dir"];
var outDirAttrValue = attrs["out_dir"];
var excludeOptionValue = config["exclude"];

let rootDirNotEmpty = rootDirAttrValue !== undefined && rootDirAttrValue !== "";
let outDirEmpty = outDirAttrValue === undefined || outDirAttrValue === "";

if (rootDirNotEmpty && outDirEmpty && excludeOptionValue === undefined) {
throw new Error(
'\n\nWhen root dir is set, exclude must also be set to empty array in the tsconfig file.\n\n' +
'For example, tsconfig.json:\n' +
'{\n' +
' "exclude": []\n' +
'}\n\n' +
'See tickets: https://github.com/microsoft/TypeScript/issues/59036 and ' +
'https://github.com/aspect-build/rules_ts/issues/644\n'
)
}
}
var jsxEmit =
((_b = {}),
(_b[ts.JsxEmit.None] = 'none'),
Expand Down Expand Up @@ -175,6 +195,7 @@ function main(_a) {
check('incremental')
check('tsBuildInfoFile', 'ts_build_info_file')
check_out_dir()
checkRootDirExclude()
check_nocheck()
check_preserve_jsx()
if (failures.length > 0) {
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 @@ -26,6 +26,7 @@ Assumes all tsconfig file deps are already copied to the bin directory.
incremental = ctx.attr.incremental,
ts_build_info_file = ctx.attr.ts_build_info_file,
isolated_typecheck = ctx.attr.isolated_typecheck,
root_dir = ctx.attr.root_dir,
)
arguments.add_all([
to_output_relative_path(tsconfig),
Expand Down
20 changes: 20 additions & 0 deletions ts/test/ts_project_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,32 @@ def ts_project_test_suite(name):
targets = [":wrapper.d.ts.map"],
)

write_file(
name = "dirty_out_dir_ts",
out = "dirty_out_dir.ts",
content = ["console.log(1)"],
tags = ["manual"],
)
ts_project_wrapper(
name = "dirty_out_dir",
srcs = ["dirty_out_dir.ts"],
out_dir = "./out-dir",
tsconfig = _TSCONFIG,
tags = ["manual"],
)

build_test(
name = "dirty_out_dir_test",
targets = [":dirty_out_dir"],
)

native.test_suite(
name = name,
tests = [
":dir_test",
":use_dir_test",
":wrapper_test",
":dirty_out_dir_test",
],
)

Expand Down

0 comments on commit 5ac25c1

Please sign in to comment.