Skip to content

Commit 5ac25c1

Browse files
authored
fix: add tsconfig root_dir/exclude validation
Throw a better error when exclude needs to be added to tsconfig.json, normalize ts_project *_dir paths. Close #644
1 parent 8c328d0 commit 5ac25c1

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

ts/defs.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ def ts_project(
298298
declaration_dir = compiler_options.pop("declarationDir", declaration_dir)
299299
root_dir = compiler_options.pop("rootDir", root_dir)
300300

301+
# When generating a tsconfig.json and we set rootDir we need to add the exclude field,
302+
# because of these tickets (validation for not generated tsconfig is also present elsewhere):
303+
# https://github.com/microsoft/TypeScript/issues/59036 and
304+
# 'https://github.com/aspect-build/rules_ts/issues/644
305+
if root_dir != None and "exclude" not in tsconfig:
306+
tsconfig["exclude"] = []
307+
301308
if srcs == None:
302309
# Default sources based on macro attributes after applying tsconfig properties
303310
srcs = _default_srcs(

ts/private/ts_lib.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ def _to_out_path(f, out_dir, root_dir):
227227

228228
def _to_js_out_paths(srcs, out_dir, root_dir, allow_js, resolve_json_module, ext_map, default_ext):
229229
outs = []
230+
out_dir = _remove_leading_dot_slash(out_dir)
231+
root_dir = _remove_leading_dot_slash(root_dir)
230232
for f in srcs:
231233
if _is_ts_src(f, allow_js, resolve_json_module, False):
232234
out = _to_out_path(f, out_dir, root_dir)
@@ -325,6 +327,9 @@ def _declare_outputs(ctx, paths):
325327
for path in paths
326328
]
327329

330+
def _remove_leading_dot_slash(string_path):
331+
return string_path.removeprefix("./") if string_path else string_path
332+
328333
lib = struct(
329334
declare_outputs = _declare_outputs,
330335
join = _join,

ts/private/ts_project_options_validator.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ function main(_a) {
113113
)
114114
}
115115
}
116+
function checkRootDirExclude() {
117+
var rootDirAttrValue = attrs["root_dir"];
118+
var outDirAttrValue = attrs["out_dir"];
119+
var excludeOptionValue = config["exclude"];
120+
121+
let rootDirNotEmpty = rootDirAttrValue !== undefined && rootDirAttrValue !== "";
122+
let outDirEmpty = outDirAttrValue === undefined || outDirAttrValue === "";
123+
124+
if (rootDirNotEmpty && outDirEmpty && excludeOptionValue === undefined) {
125+
throw new Error(
126+
'\n\nWhen root dir is set, exclude must also be set to empty array in the tsconfig file.\n\n' +
127+
'For example, tsconfig.json:\n' +
128+
'{\n' +
129+
' "exclude": []\n' +
130+
'}\n\n' +
131+
'See tickets: https://github.com/microsoft/TypeScript/issues/59036 and ' +
132+
'https://github.com/aspect-build/rules_ts/issues/644\n'
133+
)
134+
}
135+
}
116136
var jsxEmit =
117137
((_b = {}),
118138
(_b[ts.JsxEmit.None] = 'none'),
@@ -175,6 +195,7 @@ function main(_a) {
175195
check('incremental')
176196
check('tsBuildInfoFile', 'ts_build_info_file')
177197
check_out_dir()
198+
checkRootDirExclude()
178199
check_nocheck()
179200
check_preserve_jsx()
180201
if (failures.length > 0) {

ts/private/ts_validate_options.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Assumes all tsconfig file deps are already copied to the bin directory.
2626
incremental = ctx.attr.incremental,
2727
ts_build_info_file = ctx.attr.ts_build_info_file,
2828
isolated_typecheck = ctx.attr.isolated_typecheck,
29+
root_dir = ctx.attr.root_dir,
2930
)
3031
arguments.add_all([
3132
to_output_relative_path(tsconfig),

ts/test/ts_project_test.bzl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,32 @@ def ts_project_test_suite(name):
170170
targets = [":wrapper.d.ts.map"],
171171
)
172172

173+
write_file(
174+
name = "dirty_out_dir_ts",
175+
out = "dirty_out_dir.ts",
176+
content = ["console.log(1)"],
177+
tags = ["manual"],
178+
)
179+
ts_project_wrapper(
180+
name = "dirty_out_dir",
181+
srcs = ["dirty_out_dir.ts"],
182+
out_dir = "./out-dir",
183+
tsconfig = _TSCONFIG,
184+
tags = ["manual"],
185+
)
186+
187+
build_test(
188+
name = "dirty_out_dir_test",
189+
targets = [":dirty_out_dir"],
190+
)
191+
173192
native.test_suite(
174193
name = name,
175194
tests = [
176195
":dir_test",
177196
":use_dir_test",
178197
":wrapper_test",
198+
":dirty_out_dir_test",
179199
],
180200
)
181201

0 commit comments

Comments
 (0)