Skip to content

Commit

Permalink
fix(typescript): add build_test to ensure typecheck is run under --bu…
Browse files Browse the repository at this point in the history
…ild_tests_only (#3196)
  • Loading branch information
alexeagle authored Jan 8, 2022
1 parent 4c1401e commit 2a966ad
Show file tree
Hide file tree
Showing 5 changed files with 439 additions and 18 deletions.
24 changes: 15 additions & 9 deletions docs/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,15 +758,21 @@ See the packages/typescript/test/ts_project/swc directory for an example.
When a custom transpiler is used, then the `ts_project` macro expands to these targets:
- `[name]` - the default target is a `js_library` which can be included in the `deps` of downstream rules.
Note that it will successfully build *even if there are typecheck failures* because the `tsc` binary
is not needed to produce the default outputs.
This is considered a feature, as it allows you to have a faster development mode where type-checking
is not on the critical path.
- `[name]_typecheck` - this target will fail to build if the type-checking fails, useful for CI.
- `[name]_typings` - internal target which runs the binary from the `tsc` attribute
- Any additional target(s) the custom transpiler rule/macro produces.
Some rules produce one target per TypeScript input file.
- `[name]` - the default target is a `js_library` which can be included in the `deps` of downstream rules.
Note that it will successfully build *even if there are typecheck failures* because the `tsc` binary
is not needed to produce the default outputs.
This is considered a feature, as it allows you to have a faster development mode where type-checking
is not on the critical path.
- `[name]_typecheck` - provides typings (`.d.ts` files) as the default output,
therefore building this target always causes the typechecker to run.
- `[name]_typecheck_test` - a
[`build_test`](https://github.com/bazelbuild/bazel-skylib/blob/main/rules/build_test.bzl)
target which simply depends on the `[name]_typecheck` target.
This ensures that typechecking will be run under `bazel test` with
[`--build_tests_only`](https://docs.bazel.build/versions/main/user-manual.html#flag--build_tests_only).
- `[name]_typings` - internal target which runs the binary from the `tsc` attribute
- Any additional target(s) the custom transpiler rule/macro produces.
Some rules produce one target per TypeScript input file.
By default, `ts_project` expects `.js` outputs to be written in the same action
that does the type-checking to produce `.d.ts` outputs.
Expand Down
33 changes: 24 additions & 9 deletions packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "ExternalNp
load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect")
load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//third_party/github.com/bazelbuild/bazel-skylib:lib/partial.bzl", "partial")
load("@build_bazel_rules_nodejs//third_party/github.com/bazelbuild/bazel-skylib:rules/build_test.bzl", "build_test")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load(":ts_config.bzl", "TsConfigInfo", "write_tsconfig")

Expand Down Expand Up @@ -568,15 +569,21 @@ def ts_project_macro(
When a custom transpiler is used, then the `ts_project` macro expands to these targets:
- `[name]` - the default target is a `js_library` which can be included in the `deps` of downstream rules.
Note that it will successfully build *even if there are typecheck failures* because the `tsc` binary
is not needed to produce the default outputs.
This is considered a feature, as it allows you to have a faster development mode where type-checking
is not on the critical path.
- `[name]_typecheck` - this target will fail to build if the type-checking fails, useful for CI.
- `[name]_typings` - internal target which runs the binary from the `tsc` attribute
- Any additional target(s) the custom transpiler rule/macro produces.
Some rules produce one target per TypeScript input file.
- `[name]` - the default target is a `js_library` which can be included in the `deps` of downstream rules.
Note that it will successfully build *even if there are typecheck failures* because the `tsc` binary
is not needed to produce the default outputs.
This is considered a feature, as it allows you to have a faster development mode where type-checking
is not on the critical path.
- `[name]_typecheck` - provides typings (`.d.ts` files) as the default output,
therefore building this target always causes the typechecker to run.
- `[name]_typecheck_test` - a
[`build_test`](https://github.com/bazelbuild/bazel-skylib/blob/main/rules/build_test.bzl)
target which simply depends on the `[name]_typecheck` target.
This ensures that typechecking will be run under `bazel test` with
[`--build_tests_only`](https://docs.bazel.build/versions/main/user-manual.html#flag--build_tests_only).
- `[name]_typings` - internal target which runs the binary from the `tsc` attribute
- Any additional target(s) the custom transpiler rule/macro produces.
Some rules produce one target per TypeScript input file.
By default, `ts_project` expects `.js` outputs to be written in the same action
that does the type-checking to produce `.d.ts` outputs.
Expand Down Expand Up @@ -817,6 +824,7 @@ def ts_project_macro(
tsc_target_name = "%s_typings" % name
transpile_target_name = "%s_transpile" % name
typecheck_target_name = "%s_typecheck" % name
test_target_name = "%s_typecheck_test" % name

common_kwargs = {
"tags": kwargs.get("tags", []),
Expand Down Expand Up @@ -852,6 +860,13 @@ def ts_project_macro(
**common_kwargs
)

# Ensures the target above gets built under `bazel test --build_tests_only`
build_test(
name = test_target_name,
targets = [typecheck_target_name],
**common_kwargs
)

# Default target produced by the macro gives the js and map outs, with the transitive dependencies.
js_library(
name = name,
Expand Down
43 changes: 43 additions & 0 deletions third_party/github.com/bazelbuild/bazel-skylib/lib/dicts.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Skylib module containing functions that operate on dictionaries."""

def _add(*dictionaries, **kwargs):
"""Returns a new `dict` that has all the entries of the given dictionaries.
If the same key is present in more than one of the input dictionaries, the
last of them in the argument list overrides any earlier ones.
This function is designed to take zero or one arguments as well as multiple
dictionaries, so that it follows arithmetic identities and callers can avoid
special cases for their inputs: the sum of zero dictionaries is the empty
dictionary, and the sum of a single dictionary is a copy of itself.
Args:
*dictionaries: Zero or more dictionaries to be added.
**kwargs: Additional dictionary passed as keyword args.
Returns:
A new `dict` that has all the entries of the given dictionaries.
"""
result = {}
for d in dictionaries:
result.update(d)
result.update(kwargs)
return result

dicts = struct(
add = _add,
)
243 changes: 243 additions & 0 deletions third_party/github.com/bazelbuild/bazel-skylib/lib/new_sets.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Skylib module containing common hash-set algorithms.
An empty set can be created using: `sets.make()`, or it can be created with some starting values
if you pass it an sequence: `sets.make([1, 2, 3])`. This returns a struct containing all of the
values as keys in a dictionary - this means that all passed in values must be hashable. The
values in the set can be retrieved using `sets.to_list(my_set)`.
An arbitrary object can be tested whether it is a set generated by `sets.make()` or not with the
`types.is_set()` method in types.bzl.
"""

load("//third_party/github.com/bazelbuild/bazel-skylib:lib/dicts.bzl", "dicts")

def _make(elements = None):
"""Creates a new set.
All elements must be hashable.
Args:
elements: Optional sequence to construct the set out of.
Returns:
A set containing the passed in values.
"""

# If you change the structure of a set, you need to also update the _is_set method
# in types.bzl.
elements = elements if elements else []
return struct(_values = {e: None for e in elements})

def _copy(s):
"""Creates a new set from another set.
Args:
s: A set, as returned by `sets.make()`.
Returns:
A new set containing the same elements as `s`.
"""
return struct(_values = dict(s._values))

def _to_list(s):
"""Creates a list from the values in the set.
Args:
s: A set, as returned by `sets.make()`.
Returns:
A list of values inserted into the set.
"""
return s._values.keys()

def _insert(s, e):
"""Inserts an element into the set.
Element must be hashable. This mutates the original set.
Args:
s: A set, as returned by `sets.make()`.
e: The element to be inserted.
Returns:
The set `s` with `e` included.
"""
s._values[e] = None
return s

def _remove(s, e):
"""Removes an element from the set.
Element must be hashable. This mutates the original set.
Args:
s: A set, as returned by `sets.make()`.
e: The element to be removed.
Returns:
The set `s` with `e` removed.
"""
s._values.pop(e)
return s

def _contains(a, e):
"""Checks for the existence of an element in a set.
Args:
a: A set, as returned by `sets.make()`.
e: The element to look for.
Returns:
True if the element exists in the set, False if the element does not.
"""
return e in a._values

def _get_shorter_and_longer(a, b):
"""Returns two sets in the order of shortest and longest.
Args:
a: A set, as returned by `sets.make()`.
b: A set, as returned by `sets.make()`.
Returns:
`a`, `b` if `a` is shorter than `b` - or `b`, `a` if `b` is shorter than `a`.
"""
if _length(a) < _length(b):
return a, b
return b, a

def _is_equal(a, b):
"""Returns whether two sets are equal.
Args:
a: A set, as returned by `sets.make()`.
b: A set, as returned by `sets.make()`.
Returns:
True if `a` is equal to `b`, False otherwise.
"""
return a._values == b._values

def _is_subset(a, b):
"""Returns whether `a` is a subset of `b`.
Args:
a: A set, as returned by `sets.make()`.
b: A set, as returned by `sets.make()`.
Returns:
True if `a` is a subset of `b`, False otherwise.
"""
for e in a._values.keys():
if e not in b._values:
return False
return True

def _disjoint(a, b):
"""Returns whether two sets are disjoint.
Two sets are disjoint if they have no elements in common.
Args:
a: A set, as returned by `sets.make()`.
b: A set, as returned by `sets.make()`.
Returns:
True if `a` and `b` are disjoint, False otherwise.
"""
shorter, longer = _get_shorter_and_longer(a, b)
for e in shorter._values.keys():
if e in longer._values:
return False
return True

def _intersection(a, b):
"""Returns the intersection of two sets.
Args:
a: A set, as returned by `sets.make()`.
b: A set, as returned by `sets.make()`.
Returns:
A set containing the elements that are in both `a` and `b`.
"""
shorter, longer = _get_shorter_and_longer(a, b)
return struct(_values = {e: None for e in shorter._values.keys() if e in longer._values})

def _union(*args):
"""Returns the union of several sets.
Args:
*args: An arbitrary number of sets.
Returns:
The set union of all sets in `*args`.
"""
return struct(_values = dicts.add(*[s._values for s in args]))

def _difference(a, b):
"""Returns the elements in `a` that are not in `b`.
Args:
a: A set, as returned by `sets.make()`.
b: A set, as returned by `sets.make()`.
Returns:
A set containing the elements that are in `a` but not in `b`.
"""
return struct(_values = {e: None for e in a._values.keys() if e not in b._values})

def _length(s):
"""Returns the number of elements in a set.
Args:
s: A set, as returned by `sets.make()`.
Returns:
An integer representing the number of elements in the set.
"""
return len(s._values)

def _repr(s):
"""Returns a string value representing the set.
Args:
s: A set, as returned by `sets.make()`.
Returns:
A string representing the set.
"""
return repr(s._values.keys())

sets = struct(
make = _make,
copy = _copy,
to_list = _to_list,
insert = _insert,
contains = _contains,
is_equal = _is_equal,
is_subset = _is_subset,
disjoint = _disjoint,
intersection = _intersection,
union = _union,
difference = _difference,
length = _length,
remove = _remove,
repr = _repr,
str = _repr,
# is_set is declared in types.bzl
)
Loading

0 comments on commit 2a966ad

Please sign in to comment.