Skip to content

Commit 7b521d9

Browse files
committed
fix: ts_project declaration sources from other directories
Fix #727
1 parent 3849ce6 commit 7b521d9

File tree

8 files changed

+74
-25
lines changed

8 files changed

+74
-25
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
2+
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
3+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
4+
5+
ts_project(
6+
name = "lib",
7+
srcs = [
8+
"index.ts",
9+
"//examples/global_dts_generated_tsconfig:lib_types",
10+
],
11+
tsconfig = {},
12+
)
13+
14+
build_test(
15+
name = "test",
16+
targets = [":lib"],
17+
)
18+
19+
write_source_files(
20+
name = "generated_tsconfig",
21+
files = {
22+
"tsconfig_gen.json.expected": ":_gen_tsconfig_lib",
23+
},
24+
)

examples/global_consumer/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (myGlobalFunction(42) != myGlobalResult) {
2+
console.log('Not equal!?')
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"compilerOptions":{"allowJs":false,"declaration":false,"declarationMap":false,"emitDeclarationOnly":false,"noEmit":false,"resolveJsonModule":null,"sourceMap":false},"files":["./index.ts", "./../../examples/global_dts_generated_tsconfig/index.d.ts", "./../../examples/global_dts_generated_tsconfig/global.d.ts"]}

examples/global_dts_generated_tsconfig/BUILD.bazel

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
12
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
23
load("@bazel_skylib//rules:build_test.bzl", "build_test")
34

@@ -9,11 +10,18 @@ ts_project(
910
],
1011
# Intentionally a generated config to catch regressions of
1112
# https://github.com/aspect-build/rules_ts/issues/204
12-
tsconfig = {"declaration": True},
13+
tsconfig = {"compilerOptions": {"declaration": True}},
1314
visibility = ["//examples:__subpackages__"],
1415
)
1516

1617
build_test(
1718
name = "test",
1819
targets = [":lib"],
1920
)
21+
22+
write_source_files(
23+
name = "generated_tsconfig",
24+
files = {
25+
"tsconfig_gen.json.expected": ":_gen_tsconfig_lib",
26+
},
27+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
// A global definition within a .d.ts file
2+
13
declare function myGlobalFunction(a: number): string
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
myGlobalFunction(42)
1+
;(typeof globalThis !== 'undefined'
2+
? globalThis
3+
: (window as any)
4+
).myGlobalResult = myGlobalFunction(42)
5+
6+
export {}
7+
8+
// A definition within a .ts file that must be compiled.
9+
declare global {
10+
const myGlobalResult: string
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"compilerOptions":{"allowJs":false,"declaration":true,"declarationMap":false,"emitDeclarationOnly":false,"noEmit":false,"resolveJsonModule":null,"sourceMap":false},"files":["./global.d.ts", "./index.ts"]}

ts/private/ts_config.bzl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,6 @@ extended configuration file as well, to pass them both to the TypeScript compile
114114
toolchains = COPY_FILE_TO_BIN_TOOLCHAINS,
115115
)
116116

117-
def _filter_input_files(files, allow_js, resolve_json_module):
118-
return [
119-
f
120-
for f in files
121-
# include typescript, json & declaration sources
122-
if _lib.is_ts_src(f.basename, allow_js, resolve_json_module) or _lib.is_typings_src(f.basename)
123-
]
124-
125117
def _write_tsconfig_rule(ctx):
126118
# TODO: is it useful to expand Make variables in the content?
127119
content = ctx.attr.content
@@ -135,22 +127,30 @@ def _write_tsconfig_rule(ctx):
135127
extends_path = "./" + extends_path
136128
content = content.replace("__extends__", extends_path)
137129

138-
filtered_files = _filter_input_files(ctx.files.files, ctx.attr.allow_js, ctx.attr.resolve_json_module)
139-
140-
# Update file paths to be relative to the tsconfig file, including a ./ prefix
141-
# to ensure paths are all relative to the config file.
142-
package_prefix = ctx.label.package + "/"
143-
144-
# If the target is inside another workspace, we will need to also add the workspace
145-
# root to the prefix.
130+
# The prefix of source files that are within the same package as the tsconfig file.
131+
local_package_prefix = "%s/" % ctx.label.package if ctx.label.package else ""
146132
if (len(ctx.label.repo_name) > 0):
147-
package_prefix = "../{}/{}".format(ctx.label.repo_name, package_prefix)
148-
filtered_files = [
149-
"./" + f.short_path.removeprefix(package_prefix)
150-
for f in filtered_files
151-
]
152-
153-
content = content.replace("\"__files__\"", str(filtered_files))
133+
# If the target is inside another workspace the prefix also contains the navigation to that workspace.
134+
local_package_prefix = "../{}/{}".format(ctx.label.repo_name, local_package_prefix)
135+
136+
# The path to navigate to the root of the workspace
137+
path_to_root = "/".join([".." for _ in ctx.label.package.split("/")])
138+
139+
# Compute the list of source files with paths relative to the generated tsconfig file.
140+
src_files = []
141+
for f in ctx.files.files:
142+
# Only include typescript source files
143+
if not (_lib.is_ts_src(f.basename, ctx.attr.allow_js, ctx.attr.resolve_json_module) or _lib.is_typings_src(f.basename)):
144+
continue
145+
146+
if f.short_path.startswith(local_package_prefix):
147+
# Files within this project or subdirs can avoid the ugly ../ prefix
148+
src_files.append("./{}".format(f.short_path.removeprefix(local_package_prefix)))
149+
else:
150+
# Files from parent/sibling projects must navigate up to the workspace root
151+
src_files.append("./{}/{}".format(path_to_root, f.short_path))
152+
153+
content = content.replace("\"__files__\"", str(src_files))
154154
ctx.actions.write(
155155
output = ctx.outputs.out,
156156
content = content,

0 commit comments

Comments
 (0)