From ee2b155cbce2e7feb7855a67181baaa6924051da Mon Sep 17 00:00:00 2001 From: Andrius Bankauskas Date: Thu, 28 Apr 2022 04:52:30 +0300 Subject: [PATCH] refactor: fix TsConfigInfo to use depset and deduplicate files (#3430) (#3431) --- nodejs/private/ts_config.bzl | 5 +++-- nodejs/private/ts_project.bzl | 18 ++++++++---------- nodejs/private/ts_validate_options.bzl | 10 +++++----- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/nodejs/private/ts_config.bzl b/nodejs/private/ts_config.bzl index 4c2f04715c..6d992a7ef8 100644 --- a/nodejs/private/ts_config.bzl +++ b/nodejs/private/ts_config.bzl @@ -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( diff --git a/nodejs/private/ts_project.bzl b/nodejs/private/ts_project.bzl index 6d5c6a5d7f..a4b67670d2 100644 --- a/nodejs/private/ts_project.bzl +++ b/nodejs/private/ts_project.bzl @@ -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.) @@ -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", @@ -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 diff --git a/nodejs/private/ts_validate_options.bzl b/nodejs/private/ts_validate_options.bzl index 92bf35770a..119662dfab 100644 --- a/nodejs/private/ts_validate_options.bzl +++ b/nodejs/private/ts_validate_options.bzl @@ -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