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: relative path computation handles parent dir #70

Merged
merged 1 commit into from
Jun 28, 2022
Merged
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
14 changes: 14 additions & 0 deletions examples/extends_chain/main/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

# Uses tsconfig.json in this source folder by default
ts_project(
name = "main",
srcs = ["main.ts"],
extends = "//examples/extends_chain:tsconfig_node",
)

# Alternately, we could define tsconfig dictionary
ts_project(
name = "config_dict",
srcs = ["main.ts"],
extends = "//examples/extends_chain:tsconfig_node",
out_dir = "config_dict",
tsconfig = {
"compilerOptions": {
"declaration": False,
},
},
)
24 changes: 3 additions & 21 deletions ts/private/ts_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

"tsconfig.json files using extends"

load("@aspect_bazel_lib//lib:paths.bzl", "relative_file")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_file_to_bin_action", "copy_files_to_bin_actions")
load("@bazel_skylib//lib:paths.bzl", "paths")
load(":ts_lib.bzl", _lib = "lib")

TsConfigInfo = provider(
Expand Down Expand Up @@ -60,24 +60,6 @@ extended configuration file as well, to pass them both to the TypeScript compile
""",
)

def _relative_path(bindir, tsconfig, dest):
relative_to = tsconfig.dirname
destpath = dest.path
if dest.is_source:
# We assume that sources will be copied to the output folder by the time
# typescript tries to load them.
destpath = paths.join(bindir.path, destpath)

# Bazel guarantees that srcs are beneath the package directory, and we disallow
# tsconfig.json being generated with a "/" in the name.
# So we can calculate a relative path from e.g.
# bazel-out/darwin-fastbuild/bin/packages/typescript/test/ts_project/generated_tsconfig/gen_src
# to <generated file packages/typescript/test/ts_project/generated_tsconfig/gen_src/subdir/a.ts>
result = destpath[len(relative_to) + 1:]
if not result.startswith("."):
result = "./" + result
return result

def _filter_input_files(files, allow_js, resolve_json_module):
return [
f
Expand All @@ -91,14 +73,14 @@ def _write_tsconfig_rule(ctx):
if ctx.attr.extends:
content = content.replace(
"__extends__",
_relative_path(ctx.bin_dir, ctx.outputs.out, ctx.file.extends),
relative_file(ctx.file.extends.short_path, ctx.outputs.out.short_path),
)

filtered_files = _filter_input_files(ctx.files.files, ctx.attr.allow_js, ctx.attr.resolve_json_module)
if filtered_files:
content = content.replace(
"\"__files__\"",
str([_relative_path(ctx.bin_dir, ctx.outputs.out, f) for f in filtered_files]),
str([relative_file(f.short_path, ctx.outputs.out.short_path) for f in filtered_files]),
)
ctx.actions.write(
output = ctx.outputs.out,
Expand Down