Skip to content

Commit

Permalink
fix: tsconfig validator fails on chained tsconfig references (#2512)
Browse files Browse the repository at this point in the history
  • Loading branch information
bduffany authored Mar 17, 2021
1 parent 09be982 commit bfd74e5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
31 changes: 16 additions & 15 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,7 @@ def _ts_project_impl(ctx):
inputs.extend(depset(transitive = deps_depsets).to_list())

# Gather TsConfig info from both the direct (tsconfig) and indirect (extends) attribute
tsconfig_inputs = []
if TsConfigInfo in ctx.attr.tsconfig:
tsconfig_inputs.extend(ctx.attr.tsconfig[TsConfigInfo].deps)
else:
tsconfig_inputs.append(ctx.file.tsconfig)
if hasattr(ctx.attr, "extends") and ctx.attr.extends:
if TsConfigInfo in ctx.attr.extends:
tsconfig_inputs.extend(ctx.attr.extends[TsConfigInfo].deps)
else:
tsconfig_inputs.extend(ctx.attr.extends.files.to_list())
tsconfig_inputs = _tsconfig_inputs(ctx)
inputs.extend(tsconfig_inputs)

# We do not try to predeclare json_outs, because their output locations generally conflict with their path in the source tree.
Expand Down Expand Up @@ -249,6 +240,20 @@ def _ts_project_impl(ctx):

return providers

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)
else:
inputs.append(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)
else:
inputs.extend(ctx.attr.extends.files.to_list())
return inputs

ts_project = rule(
implementation = _ts_project_impl,
attrs = dict(_ATTRS, **_OUTPUTS),
Expand All @@ -271,11 +276,7 @@ def _validate_options_impl(ctx):
ts_build_info_file = ctx.attr.ts_build_info_file,
).to_json()])

inputs = ctx.files.extends[:]
if TsConfigInfo in ctx.attr.tsconfig:
inputs.extend(ctx.attr.tsconfig[TsConfigInfo].deps)
else:
inputs.append(ctx.file.tsconfig)
inputs = _tsconfig_inputs(ctx)

run_node(
ctx,
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript/test/ts_project/extends_chain/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//packages/typescript:index.bzl", "ts_config")

ts_config(
name = "tsconfig_node",
src = "tsconfig.node.json",
visibility = [":__subpackages__"],
deps = ["tsconfig.base.json"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//packages/typescript:index.bzl", "ts_project")

ts_project(
name = "main",
srcs = ["main.ts"],
extends = "//packages/typescript/test/ts_project/extends_chain:tsconfig_node",
tsconfig = ":tsconfig.json",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello world!');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.node.json"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"noImplicitAny": true,
"types": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"moduleResolution": "Node",
"module": "commonjs"
}
}

0 comments on commit bfd74e5

Please sign in to comment.