Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ts_project(deps) validation ensuring all deps of declarations #199

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/rules.md

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

1 change: 1 addition & 0 deletions examples/path_mappings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ts_project(
srcs = ["foo.ts"],
declaration = True,
tsconfig = "tsconfig.json",
validate = False,
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
deps = _DEPS,
)

Expand Down
3 changes: 2 additions & 1 deletion ts/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def ts_project(

Read more: https://blog.aspect.dev/typescript-speedup

validate: Whether to check that the tsconfig JSON settings match the attributes on this target.
validate: Whether to check that the dependencies are valid and the tsconfig JSON settings match the attributes on this target.
Set this to `False` to skip running our validator, in case you have a legitimate reason for these to differ,
e.g. you have a setting enabled just for the editor but you want different behavior when Bazel runs `tsc`.

Expand Down Expand Up @@ -342,6 +342,7 @@ def ts_project(
allow_js = allow_js,
tsconfig = tsconfig,
extends = extends,
deps = deps,
validator = validator,
**common_kwargs
)
Expand Down
16 changes: 16 additions & 0 deletions ts/private/ts_validate_options.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
load(":ts_config.bzl", "TsConfigInfo")
load(":ts_lib.bzl", "COMPILER_OPTION_ATTRS", "ValidOptionsInfo")
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_files_to_bin_actions")
load("@aspect_rules_js//js:providers.bzl", "JsInfo")

def _tsconfig_inputs(ctx):
"""Returns all transitively referenced tsconfig files from "tsconfig" and "extends" attributes."""
Expand All @@ -23,6 +24,20 @@ def _validate_options_impl(ctx):
# We make it a .d.ts file so we can plumb it to the deps of the ts_project compile.
marker = ctx.actions.declare_file("%s.optionsvalid.d.ts" % ctx.label.name)

# Provider validation
if not ctx.attr.allow_js:
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
for d in ctx.attr.deps:
if not d[JsInfo].declarations:
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
fail("""\
ts_project '{1}' dependency '{0}' does does not contain any declarations (.d.ts or other type-check files).
Generally, targets which produce no declarations aren't useful as dependencies to the TypeScript type-checker.
This likely means you forgot to set 'declaration = true' in the compilerOptions for that target.

To disable this check, set the validate attribute to False:
npx @bazel/buildozer 'set validate False' {1}
""".format(d.label, ctx.attr.target))
alexeagle marked this conversation as resolved.
Show resolved Hide resolved

# External validation
arguments = ctx.actions.args()
config = struct(
allow_js = ctx.attr.allow_js,
Expand Down Expand Up @@ -56,6 +71,7 @@ def _validate_options_impl(ctx):
]

_ATTRS = dict(COMPILER_OPTION_ATTRS, **{
"deps": attr.label_list(mandatory = True, providers = [JsInfo]),
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
"target": attr.string(),
"ts_build_info_file": attr.string(),
"tsconfig": attr.label(mandatory = True, allow_single_file = [".json"]),
Expand Down
6 changes: 3 additions & 3 deletions ts/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def rules_ts_dependencies(ts_version_from = None, ts_version = None, ts_integrit
maybe(
http_archive,
name = "aspect_rules_js",
sha256 = "9d80f28eb59df0486cc1e8e82868e97d8167429ea309a7ae96dfac64ff73275b",
strip_prefix = "rules_js-1.4.0",
url = "https://github.com/aspect-build/rules_js/archive/refs/tags/v1.4.0.tar.gz",
sha256 = "d8eabcd1e05d93147505ea806fa21089926b771d8813f01b92af5dec36617033",
strip_prefix = "rules_js-1.6.3",
url = "https://github.com/aspect-build/rules_js/archive/refs/tags/v1.6.3.tar.gz",
)

maybe(
Expand Down