Skip to content

Commit

Permalink
refactor: fix TsConfigInfo to use depset and deduplicate files (#3430) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrius-B authored Apr 28, 2022
1 parent e45c471 commit ee2b155
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
5 changes: 3 additions & 2 deletions nodejs/private/ts_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ def _ts_config_impl(ctx):
transitive_deps = []
for dep in ctx.attr.deps:
if TsConfigInfo in dep:
transitive_deps.extend(dep[TsConfigInfo].deps)
transitive_deps.append(dep[TsConfigInfo].deps)
transitive_deps.append(depset(ctx.files.deps))
return [
DefaultInfo(files = files),
TsConfigInfo(deps = [ctx.file.src] + ctx.files.deps + transitive_deps),
TsConfigInfo(deps = depset([ctx.file.src], transitive = transitive_deps)),
]

ts_config = rule(
Expand Down
18 changes: 8 additions & 10 deletions nodejs/private/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,23 @@ def _ts_project_impl(ctx, run_action = None, ExternalNpmPackageInfo = None):
"--extendedDiagnostics",
])

deps_depsets = []
transitive_inputs = []
inputs = ctx.files.srcs[:]
for dep in ctx.attr.deps:
if TsConfigInfo in dep:
deps_depsets.append(dep[TsConfigInfo].deps)
transitive_inputs.append(dep[TsConfigInfo].deps)
if ExternalNpmPackageInfo != None and ExternalNpmPackageInfo in dep:
# TODO: we could maybe filter these to be tsconfig.json or *.d.ts only
# we don't expect tsc wants to read any other files from npm packages.
deps_depsets.append(dep[ExternalNpmPackageInfo].sources)
transitive_inputs.append(dep[ExternalNpmPackageInfo].sources)
if DeclarationInfo in dep:
deps_depsets.append(dep[DeclarationInfo].transitive_declarations)
transitive_inputs.append(dep[DeclarationInfo].transitive_declarations)
if ValidOptionsInfo in dep:
inputs.append(dep[ValidOptionsInfo].marker)

inputs.extend(depset(transitive = deps_depsets).to_list())
transitive_inputs.append(depset([dep[ValidOptionsInfo].marker]))

# Gather TsConfig info from both the direct (tsconfig) and indirect (extends) attribute
tsconfig_inputs = _validate_lib.tsconfig_inputs(ctx)
inputs.extend(tsconfig_inputs)
transitive_inputs.append(tsconfig_inputs)

# We do not try to predeclare json_outs, because their output locations generally conflict with their path in the source tree.
# (The exception is when out_dir is used, then the .json output is a different path than the input.)
Expand Down Expand Up @@ -155,7 +153,7 @@ This is an error because Bazel does not run actions unless their outputs are nee

if len(outputs) > 0:
run_action_kwargs = {
"inputs": inputs,
"inputs": depset(inputs, transitive = transitive_inputs),
"arguments": [arguments],
"outputs": outputs,
"mnemonic": "TsProject",
Expand Down Expand Up @@ -198,7 +196,7 @@ This is an error because Bazel does not run actions unless their outputs are nee
sources = depset(runtime_outputs),
deps = ctx.attr.deps,
),
TsConfigInfo(deps = depset(tsconfig_inputs, transitive = [
TsConfigInfo(deps = depset(transitive = [tsconfig_inputs] + [
dep[TsConfigInfo].deps
for dep in ctx.attr.deps
if TsConfigInfo in dep
Expand Down
10 changes: 5 additions & 5 deletions nodejs/private/ts_validate_options.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ def _tsconfig_inputs(ctx):
"""Returns all transitively referenced tsconfig files from "tsconfig" and "extends" attributes."""
inputs = []
if TsConfigInfo in ctx.attr.tsconfig:
inputs.extend(ctx.attr.tsconfig[TsConfigInfo].deps)
inputs.append(ctx.attr.tsconfig[TsConfigInfo].deps)
else:
inputs.append(ctx.file.tsconfig)
inputs.append(depset([ctx.file.tsconfig]))
if hasattr(ctx.attr, "extends") and ctx.attr.extends:
if TsConfigInfo in ctx.attr.extends:
inputs.extend(ctx.attr.extends[TsConfigInfo].deps)
inputs.append(ctx.attr.extends[TsConfigInfo].deps)
else:
inputs.extend(ctx.attr.extends.files.to_list())
return inputs
inputs.append(ctx.attr.extends.files)
return depset(transitive = inputs)

def _validate_options_impl(ctx, run_action = None):
# Bazel won't run our action unless its output is needed, so make a marker file
Expand Down

0 comments on commit ee2b155

Please sign in to comment.