Skip to content

refactor: normalize root/out/declaration dir in macro #810

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

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
11 changes: 11 additions & 0 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ def ts_project(
resolve_json_module = resolve_json_module,
)

# Normalize common directory path shortcuts such as None vs "." vs "./"
out_dir = _clean_dir_arg(out_dir)
root_dir = _clean_dir_arg(root_dir)
declaration_dir = _clean_dir_arg(declaration_dir)

# Inferred paths
typings_out_dir = declaration_dir if declaration_dir else out_dir
tsbuildinfo_path = ts_build_info_file if ts_build_info_file else name + ".tsbuildinfo"

Expand Down Expand Up @@ -527,3 +533,8 @@ def _invoke_custom_transpiler(type_str, transpiler, transpile_target_name, srcs,
)
else:
fail("%s attribute should be a rule/macro or a skylib partial. Got %s" % (type_str, type(transpiler)))

def _clean_dir_arg(p):
if not p or p == "." or p == "./":
return None
return p.removeprefix("./")
6 changes: 0 additions & 6 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def _is_ts_src(src, allow_js, resolve_json_module, include_typings):

def _to_out_path(f, out_dir, root_dir):
f = f[f.find(":") + 1:]
out_dir = out_dir if out_dir != "." else None

if out_dir and f.startswith(out_dir + "/"):
return f
Expand All @@ -235,8 +234,6 @@ 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 @@ -335,9 +332,6 @@ 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
2 changes: 1 addition & 1 deletion ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
# Add user specified arguments *before* rule supplied arguments
common_args.extend(ctx.attr.args)

if (ctx.attr.out_dir and ctx.attr.out_dir != ".") or ctx.attr.root_dir:
if 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
Expand Down
4 changes: 2 additions & 2 deletions ts/test/mock_transpiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def mock(name, srcs, source_map = False, **kwargs):
srcs = srcs,
# Calculate pre-declared outputs so they can be referenced as targets.
# This is an optional transpiler feature aligning with the default tsc transpiler.
js_outs = lib.calculate_js_outs(srcs, ".", ".", False, False, False, False),
map_outs = lib.calculate_map_outs(srcs, ".", ".", True, False, False, False) if source_map else [],
js_outs = lib.calculate_js_outs(srcs, None, None, False, False, False, False),
map_outs = lib.calculate_map_outs(srcs, None, None, True, False, False, False) if source_map else [],
**kwargs
)