Skip to content

Commit

Permalink
fix: always declare ts_project_rule as primary ts_project target (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored Oct 10, 2024
1 parent b04439b commit 5e25e91
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 50 deletions.
8 changes: 5 additions & 3 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 5 additions & 34 deletions ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ inputs and produces JavaScript or declaration (.d.ts) outputs.
"""

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

# Target names for tsc, dts+js transpilers
tsc_target_name = name
declarations_target_name = None
transpile_target_name = None

Expand Down Expand Up @@ -368,35 +366,6 @@ def ts_project(
transpile_target_name = "%s_transpile" % name
_invoke_custom_transpiler("transpiler", transpiler, transpile_target_name, srcs, common_kwargs)

# Default target produced by the macro gives the js, dts and map outs,
# with the transitive dependencies.
if transpile_target_name or declarations_target_name:
tsc_target_name = "%s_tsc" % name

# Always include tsc target since even if it doesn't output js+dts, it may
# still be needed for JSON files to be included in the sources. Downstream
# dependencies of the js_library won't inadvertently cause this target to be
# typechecked when both transpiler targets for js+dts are present because
# the js_library doesn't include the ".typecheck" file which is only in the
# "typecheck" output group.
lib_srcs = [tsc_target_name]

# Include the transpiler targets for both js+dts if they exist.
if transpile_target_name:
lib_srcs.append(transpile_target_name)
if declarations_target_name:
lib_srcs.append(declarations_target_name)

# Include direct & transitive deps in addition to transpiled sources so
# that this js_library can be a valid dep for downstream ts_project or other rules_js derivative rules.
js_library(
name = name,
srcs = lib_srcs + assets,
deps = deps,
data = data,
**common_kwargs
)

# If the primary target does not output dts files then type-checking has a separate target.
if not emit_tsc_js or not emit_tsc_dts:
types_target_name = "%s_types" % name
Expand All @@ -415,15 +384,15 @@ def ts_project(
# tsc outputs the types and must be extracted via output_group
native.filegroup(
name = types_target_name,
srcs = [tsc_target_name],
srcs = [name],
output_group = "types",
**common_kwargs
)

# Users should build this target to get a failed build when typechecking fails
native.filegroup(
name = typecheck_target_name,
srcs = [tsc_target_name],
srcs = [name],
output_group = "typecheck",
**common_kwargs
)
Expand Down Expand Up @@ -453,7 +422,7 @@ def ts_project(
})

ts_project_rule(
name = tsc_target_name,
name = name,
srcs = srcs,
args = args,
assets = assets,
Expand Down Expand Up @@ -485,6 +454,8 @@ def ts_project(
tsc_worker = tsc_worker,
transpile = -1 if not transpiler else int(transpiler == "tsc"),
declaration_transpile = declaration_transpiler != None,
pretranspiled_js = transpile_target_name,
pretranspiled_dts = declarations_target_name,
supports_workers = supports_workers,
is_typescript_5_or_greater = is_typescript_5_or_greater,
validate = validate,
Expand Down
6 changes: 6 additions & 0 deletions ts/private/ts_lib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ https://docs.aspect.build/rulesets/aspect_rules_js/docs/js_library#deps for more
default = -1,
values = [-1, 0, 1],
),
"pretranspiled_js": attr.label(
doc = "Externally transpiled .js to be included in output providers",
),
"pretranspiled_dts": attr.label(
doc = "Externally transpiled .d.ts to be included in output providers",
),
"declaration_transpile": attr.bool(
doc = "Whether tsc should be used to produce .d.ts outputs",
),
Expand Down
17 changes: 4 additions & 13 deletions ts/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ See https://github.com/aspect-build/rules_ts/issues/361 for more details.
common_args.extend(["--tsBuildInfoFile", to_output_relative_path(ctx.outputs.buildinfo_out)])
outputs.append(ctx.outputs.buildinfo_out)

output_sources = js_outs + map_outs + assets_outs
output_sources = js_outs + map_outs + assets_outs + ctx.files.pretranspiled_js

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

output_types = typings_outs + typing_maps_outs + typings_srcs
output_types = typings_outs + typing_maps_outs + typings_srcs + ctx.files.pretranspiled_dts

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

# We don't produce any DefaultInfo outputs in this case, because we avoid running the tsc action
# unless the output_types are requested.
default_outputs = []

# Default outputs (DefaultInfo files) is what you see on the command-line for a built
# library, and determines what files are used by a simple non-provider-aware downstream
# library. Only the JavaScript outputs are intended for use in non-TS-aware dependents.
if use_tsc_for_js:
# Special case case where there are no source outputs and we don't have a custom
# transpiler so we add output_types to the default outputs
default_outputs = output_sources if len(output_sources) else output_types
# Special case where there are no source outputs so we add output_types to the default outputs.
default_outputs = output_sources if len(output_sources) else output_types

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

Expand Down

0 comments on commit 5e25e91

Please sign in to comment.