Skip to content

Commit

Permalink
fix: ts_project declaration sources from other directories (#729)
Browse files Browse the repository at this point in the history
Fix #727
  • Loading branch information
jbedard authored Nov 5, 2024
1 parent 60a3e9f commit e852fc0
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 25 deletions.
24 changes: 24 additions & 0 deletions examples/global_consumer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:build_test.bzl", "build_test")

ts_project(
name = "lib",
srcs = [
"index.ts",
"//examples/global_dts_generated_tsconfig:lib_types",
],
tsconfig = {},
)

build_test(
name = "test",
targets = [":lib"],
)

write_source_files(
name = "generated_tsconfig",
files = {
"tsconfig_gen.json.expected": ":_gen_tsconfig_lib",
},
)
3 changes: 3 additions & 0 deletions examples/global_consumer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (myGlobalFunction(42) != myGlobalResult) {
console.log('Not equal!?')
}
1 change: 1 addition & 0 deletions examples/global_consumer/tsconfig_gen.json.expected
Original file line number Diff line number Diff line change
@@ -0,0 +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"]}
10 changes: 9 additions & 1 deletion examples/global_dts_generated_tsconfig/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:build_test.bzl", "build_test")

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

build_test(
name = "test",
targets = [":lib"],
)

write_source_files(
name = "generated_tsconfig",
files = {
"tsconfig_gen.json.expected": ":_gen_tsconfig_lib",
},
)
2 changes: 2 additions & 0 deletions examples/global_dts_generated_tsconfig/global.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// A global definition within a .d.ts file

declare function myGlobalFunction(a: number): string
12 changes: 11 additions & 1 deletion examples/global_dts_generated_tsconfig/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
myGlobalFunction(42)
;(typeof globalThis !== 'undefined'
? globalThis
: (window as any)
).myGlobalResult = myGlobalFunction(42)

export {}

// A definition within a .ts file that must be compiled.
declare global {
const myGlobalResult: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"compilerOptions":{"allowJs":false,"declaration":true,"declarationMap":false,"emitDeclarationOnly":false,"noEmit":false,"resolveJsonModule":null,"sourceMap":false},"files":["./global.d.ts", "./index.ts"]}
46 changes: 23 additions & 23 deletions ts/private/ts_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ extended configuration file as well, to pass them both to the TypeScript compile
toolchains = COPY_FILE_TO_BIN_TOOLCHAINS,
)

def _filter_input_files(files, allow_js, resolve_json_module):
return [
f
for f in files
# include typescript, json & declaration sources
if _lib.is_ts_src(f.basename, allow_js, resolve_json_module) or _lib.is_typings_src(f.basename)
]

def _write_tsconfig_rule(ctx):
# TODO: is it useful to expand Make variables in the content?
content = ctx.attr.content
Expand All @@ -135,22 +127,30 @@ def _write_tsconfig_rule(ctx):
extends_path = "./" + extends_path
content = content.replace("__extends__", extends_path)

filtered_files = _filter_input_files(ctx.files.files, ctx.attr.allow_js, ctx.attr.resolve_json_module)

# Update file paths to be relative to the tsconfig file, including a ./ prefix
# to ensure paths are all relative to the config file.
package_prefix = ctx.label.package + "/"

# If the target is inside another workspace, we will need to also add the workspace
# root to the prefix.
# The prefix of source files that are within the same package as the tsconfig file.
local_package_prefix = "%s/" % ctx.label.package if ctx.label.package else ""
if (len(ctx.label.repo_name) > 0):
package_prefix = "../{}/{}".format(ctx.label.repo_name, package_prefix)
filtered_files = [
"./" + f.short_path.removeprefix(package_prefix)
for f in filtered_files
]

content = content.replace("\"__files__\"", str(filtered_files))
# If the target is inside another workspace the prefix also contains the navigation to that workspace.
local_package_prefix = "../{}/{}".format(ctx.label.repo_name, local_package_prefix)

# The path to navigate to the root of the workspace
path_to_root = "/".join([".."] * (ctx.label.package.count("/") + 1))

# Compute the list of source files with paths relative to the generated tsconfig file.
src_files = []
for f in ctx.files.files:
# Only include typescript source files
if not (_lib.is_ts_src(f.basename, ctx.attr.allow_js, ctx.attr.resolve_json_module) or _lib.is_typings_src(f.basename)):
continue

if f.short_path.startswith(local_package_prefix):
# Files within this project or subdirs can avoid the ugly ../ prefix
src_files.append("./{}".format(f.short_path.removeprefix(local_package_prefix)))
else:
# Files from parent/sibling projects must navigate up to the workspace root
src_files.append("./{}/{}".format(path_to_root, f.short_path))

content = content.replace("\"__files__\"", str(src_files))
ctx.actions.write(
output = ctx.outputs.out,
content = content,
Expand Down

0 comments on commit e852fc0

Please sign in to comment.