Skip to content

Commit

Permalink
feat: add ts_project(deps) validation ensuring all deps of declaratio…
Browse files Browse the repository at this point in the history
…ns (#199)

eat: add ts_project(deps) validation ensuring all deps of declarations

Fix #30
  • Loading branch information
jbedard authored Oct 31, 2022
1 parent ca39e49 commit d65732a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
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,
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 @@ -197,7 +197,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 @@ -339,6 +339,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:
for d in ctx.attr.deps:
if not d[JsInfo].declarations:
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))

# 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]),
"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

0 comments on commit d65732a

Please sign in to comment.