Skip to content

Commit 5e25e91

Browse files
authored
fix: always declare ts_project_rule as primary ts_project target (#710)
1 parent b04439b commit 5e25e91

File tree

4 files changed

+20
-50
lines changed

4 files changed

+20
-50
lines changed

docs/rules.md

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts/defs.bzl

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ inputs and produces JavaScript or declaration (.d.ts) outputs.
55
"""
66

77
load("@aspect_bazel_lib//lib:utils.bzl", "to_label")
8-
load("@aspect_rules_js//js:defs.bzl", "js_library")
98
load("@bazel_skylib//lib:partial.bzl", "partial")
109
load("//ts/private:build_test.bzl", "build_test")
1110
load("//ts/private:ts_config.bzl", "write_tsconfig", _TsConfigInfo = "TsConfigInfo", _ts_config = "ts_config")
@@ -340,7 +339,6 @@ def ts_project(
340339
emit_tsc_dts = emit_dts and not emit_transpiler_dts
341340

342341
# Target names for tsc, dts+js transpilers
343-
tsc_target_name = name
344342
declarations_target_name = None
345343
transpile_target_name = None
346344

@@ -368,35 +366,6 @@ def ts_project(
368366
transpile_target_name = "%s_transpile" % name
369367
_invoke_custom_transpiler("transpiler", transpiler, transpile_target_name, srcs, common_kwargs)
370368

371-
# Default target produced by the macro gives the js, dts and map outs,
372-
# with the transitive dependencies.
373-
if transpile_target_name or declarations_target_name:
374-
tsc_target_name = "%s_tsc" % name
375-
376-
# Always include tsc target since even if it doesn't output js+dts, it may
377-
# still be needed for JSON files to be included in the sources. Downstream
378-
# dependencies of the js_library won't inadvertently cause this target to be
379-
# typechecked when both transpiler targets for js+dts are present because
380-
# the js_library doesn't include the ".typecheck" file which is only in the
381-
# "typecheck" output group.
382-
lib_srcs = [tsc_target_name]
383-
384-
# Include the transpiler targets for both js+dts if they exist.
385-
if transpile_target_name:
386-
lib_srcs.append(transpile_target_name)
387-
if declarations_target_name:
388-
lib_srcs.append(declarations_target_name)
389-
390-
# Include direct & transitive deps in addition to transpiled sources so
391-
# that this js_library can be a valid dep for downstream ts_project or other rules_js derivative rules.
392-
js_library(
393-
name = name,
394-
srcs = lib_srcs + assets,
395-
deps = deps,
396-
data = data,
397-
**common_kwargs
398-
)
399-
400369
# If the primary target does not output dts files then type-checking has a separate target.
401370
if not emit_tsc_js or not emit_tsc_dts:
402371
types_target_name = "%s_types" % name
@@ -415,15 +384,15 @@ def ts_project(
415384
# tsc outputs the types and must be extracted via output_group
416385
native.filegroup(
417386
name = types_target_name,
418-
srcs = [tsc_target_name],
387+
srcs = [name],
419388
output_group = "types",
420389
**common_kwargs
421390
)
422391

423392
# Users should build this target to get a failed build when typechecking fails
424393
native.filegroup(
425394
name = typecheck_target_name,
426-
srcs = [tsc_target_name],
395+
srcs = [name],
427396
output_group = "typecheck",
428397
**common_kwargs
429398
)
@@ -453,7 +422,7 @@ def ts_project(
453422
})
454423

455424
ts_project_rule(
456-
name = tsc_target_name,
425+
name = name,
457426
srcs = srcs,
458427
args = args,
459428
assets = assets,
@@ -485,6 +454,8 @@ def ts_project(
485454
tsc_worker = tsc_worker,
486455
transpile = -1 if not transpiler else int(transpiler == "tsc"),
487456
declaration_transpile = declaration_transpiler != None,
457+
pretranspiled_js = transpile_target_name,
458+
pretranspiled_dts = declarations_target_name,
488459
supports_workers = supports_workers,
489460
is_typescript_5_or_greater = is_typescript_5_or_greater,
490461
validate = validate,

ts/private/ts_lib.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more
7070
default = -1,
7171
values = [-1, 0, 1],
7272
),
73+
"pretranspiled_js": attr.label(
74+
doc = "Externally transpiled .js to be included in output providers",
75+
),
76+
"pretranspiled_dts": attr.label(
77+
doc = "Externally transpiled .d.ts to be included in output providers",
78+
),
7379
"declaration_transpile": attr.bool(
7480
doc = "Whether tsc should be used to produce .d.ts outputs",
7581
),

ts/private/ts_project.bzl

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
168168
common_args.extend(["--tsBuildInfoFile", to_output_relative_path(ctx.outputs.buildinfo_out)])
169169
outputs.append(ctx.outputs.buildinfo_out)
170170

171-
output_sources = js_outs + map_outs + assets_outs
171+
output_sources = js_outs + map_outs + assets_outs + ctx.files.pretranspiled_js
172172

173173
# Add JS inputs that collide with outputs (see #250).
174174
#
@@ -190,7 +190,7 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
190190
if len(outputs) > 0 and ctx.attr.transpile == -1 and not ctx.attr.emit_declaration_only and not options.default_to_tsc_transpiler:
191191
fail(transpiler_selection_required)
192192

193-
output_types = typings_outs + typing_maps_outs + typings_srcs
193+
output_types = typings_outs + typing_maps_outs + typings_srcs + ctx.files.pretranspiled_dts
194194

195195
# What tsc will be emitting
196196
use_tsc_for_js = len(js_outs) > 0
@@ -202,17 +202,8 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
202202
# - not invoking tsc for output files at all
203203
use_isolated_typecheck = ctx.attr.isolated_typecheck or not (use_tsc_for_js or use_tsc_for_dts)
204204

205-
# We don't produce any DefaultInfo outputs in this case, because we avoid running the tsc action
206-
# unless the output_types are requested.
207-
default_outputs = []
208-
209-
# Default outputs (DefaultInfo files) is what you see on the command-line for a built
210-
# library, and determines what files are used by a simple non-provider-aware downstream
211-
# library. Only the JavaScript outputs are intended for use in non-TS-aware dependents.
212-
if use_tsc_for_js:
213-
# Special case case where there are no source outputs and we don't have a custom
214-
# transpiler so we add output_types to the default outputs
215-
default_outputs = output_sources if len(output_sources) else output_types
205+
# Special case where there are no source outputs so we add output_types to the default outputs.
206+
default_outputs = output_sources if len(output_sources) else output_types
216207

217208
srcs_tsconfig_deps = ctx.attr.srcs + [ctx.attr.tsconfig] + ctx.attr.deps
218209

0 commit comments

Comments
 (0)