diff --git a/packages/typescript/BUILD.bazel b/packages/typescript/BUILD.bazel index af278ec4e6..1408d2a2cf 100644 --- a/packages/typescript/BUILD.bazel +++ b/packages/typescript/BUILD.bazel @@ -26,7 +26,9 @@ bzl_library( srcs = glob(["*.bzl"]), deps = [ "//packages/typescript/internal:bzl", + "@bazel_skylib//lib:partial", "@bazel_skylib//lib:types", + "@bazel_skylib//rules:build_test", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@build_bazel_rules_nodejs//internal/node:bzl", diff --git a/packages/typescript/index.bzl b/packages/typescript/index.bzl index 64f74f1b67..800c1d7920 100644 --- a/packages/typescript/index.bzl +++ b/packages/typescript/index.bzl @@ -22,8 +22,8 @@ load("//packages/typescript/internal:ts_config.bzl", "write_tsconfig", _ts_confi load("//packages/typescript/internal:ts_project.bzl", _lib = "lib", _ts_project = "ts_project") load("//packages/typescript/internal:validate_options.bzl", "validate_options") load("@build_bazel_rules_nodejs//:index.bzl", "js_library") -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("@bazel_skylib//lib:partial.bzl", "partial") +load("@bazel_skylib//rules:build_test.bzl", "build_test") # If adding rules here also add to index.docs.bzl diff --git a/third_party/github.com/bazelbuild/bazel-skylib/lib/dicts.bzl b/third_party/github.com/bazelbuild/bazel-skylib/lib/dicts.bzl deleted file mode 100644 index 3f8e661a28..0000000000 --- a/third_party/github.com/bazelbuild/bazel-skylib/lib/dicts.bzl +++ /dev/null @@ -1,43 +0,0 @@ -# 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, -) diff --git a/third_party/github.com/bazelbuild/bazel-skylib/lib/new_sets.bzl b/third_party/github.com/bazelbuild/bazel-skylib/lib/new_sets.bzl deleted file mode 100644 index 02510f2293..0000000000 --- a/third_party/github.com/bazelbuild/bazel-skylib/lib/new_sets.bzl +++ /dev/null @@ -1,243 +0,0 @@ -# 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 -) diff --git a/third_party/github.com/bazelbuild/bazel-skylib/lib/partial.bzl b/third_party/github.com/bazelbuild/bazel-skylib/lib/partial.bzl deleted file mode 100644 index a642d96ec4..0000000000 --- a/third_party/github.com/bazelbuild/bazel-skylib/lib/partial.bzl +++ /dev/null @@ -1,176 +0,0 @@ -# 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. - -"""Starlark module for working with partial function objects. - -Partial function objects allow some parameters are bound before the call. - -Similar to https://docs.python.org/3/library/functools.html#functools.partial. -""" - -# create instance singletons to avoid unnecessary allocations -_a_dict_type = type({}) -_a_tuple_type = type(()) -_a_struct_type = type(struct()) - -def _call(partial, *args, **kwargs): - """Calls a partial created using `make`. - - Args: - partial: The partial to be called. - *args: Additional positional arguments to be appended to the ones given to - make. - **kwargs: Additional keyword arguments to augment and override the ones - given to make. - - Returns: - Whatever the function in the partial returns. - """ - function_args = partial.args + args - function_kwargs = dict(partial.kwargs) - function_kwargs.update(kwargs) - return partial.function(*function_args, **function_kwargs) - -def _make(func, *args, **kwargs): - """Creates a partial that can be called using `call`. - - A partial can have args assigned to it at the make site, and can have args - passed to it at the call sites. - - A partial 'function' can be defined with positional args and kwargs: - - # function with no args - ``` - def function1(): - ... - ``` - - # function with 2 args - ``` - def function2(arg1, arg2): - ... - ``` - - # function with 2 args and keyword args - ``` - def function3(arg1, arg2, x, y): - ... - ``` - - The positional args passed to the function are the args passed into make - followed by any additional positional args given to call. The below example - illustrates a function with two positional arguments where one is supplied by - make and the other by call: - - # function demonstrating 1 arg at make site, and 1 arg at call site - ``` - def _foo(make_arg1, func_arg1): - print(make_arg1 + " " + func_arg1 + "!") - ``` - - For example: - - ``` - hi_func = partial.make(_foo, "Hello") - bye_func = partial.make(_foo, "Goodbye") - partial.call(hi_func, "Jennifer") - partial.call(hi_func, "Dave") - partial.call(bye_func, "Jennifer") - partial.call(bye_func, "Dave") - ``` - - prints: - - ``` - "Hello, Jennifer!" - "Hello, Dave!" - "Goodbye, Jennifer!" - "Goodbye, Dave!" - ``` - - The keyword args given to the function are the kwargs passed into make - unioned with the keyword args given to call. In case of a conflict, the - keyword args given to call take precedence. This allows you to set a default - value for keyword arguments and override it at the call site. - - Example with a make site arg, a call site arg, a make site kwarg and a - call site kwarg: - - ``` - def _foo(make_arg1, call_arg1, make_location, call_location): - print(make_arg1 + " is from " + make_location + " and " + - call_arg1 + " is from " + call_location + "!") - - func = partial.make(_foo, "Ben", make_location="Hollywood") - partial.call(func, "Jennifer", call_location="Denver") - ``` - - Prints "Ben is from Hollywood and Jennifer is from Denver!". - - ``` - partial.call(func, "Jennifer", make_location="LA", call_location="Denver") - ``` - - Prints "Ben is from LA and Jennifer is from Denver!". - - Note that keyword args may not overlap with positional args, regardless of - whether they are given during the make or call step. For instance, you can't - do: - - ``` - def foo(x): - pass - - func = partial.make(foo, 1) - partial.call(func, x=2) - ``` - - Args: - func: The function to be called. - *args: Positional arguments to be passed to function. - **kwargs: Keyword arguments to be passed to function. Note that these can - be overridden at the call sites. - - Returns: - A new `partial` that can be called using `call` - """ - return struct(function = func, args = args, kwargs = kwargs) - -def _is_instance(v): - """Returns True if v is a partial created using `make`. - - Args: - v: The value to check. - - Returns: - True if v was created by `make`, False otherwise. - """ - - # Note that in bazel 3.7.0 and earlier, type(v.function) is the same - # as the type of a function even if v.function is a rule. But we - # cannot rely on this in later bazels due to breaking change - # https://github.com/bazelbuild/bazel/commit/e379ece1908aafc852f9227175dd3283312b4b82 - # - # Since this check is heuristic anyway, we simply check for the - # presence of a "function" attribute without checking its type. - return type(v) == _a_struct_type and \ - hasattr(v, "function") and \ - hasattr(v, "args") and type(v.args) == _a_tuple_type and \ - hasattr(v, "kwargs") and type(v.kwargs) == _a_dict_type - -partial = struct( - make = _make, - call = _call, - is_instance = _is_instance, -) diff --git a/third_party/github.com/bazelbuild/bazel-skylib/rules/build_test.bzl b/third_party/github.com/bazelbuild/bazel-skylib/rules/build_test.bzl deleted file mode 100644 index 70b9355a94..0000000000 --- a/third_party/github.com/bazelbuild/bazel-skylib/rules/build_test.bzl +++ /dev/null @@ -1,114 +0,0 @@ -# Copyright 2019 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. - -"""A test verifying other targets build as part of a `bazel test`""" - -load("//third_party/github.com/bazelbuild/bazel-skylib:lib/new_sets.bzl", "sets") - -def _empty_test_impl(ctx): - extension = ".bat" if ctx.attr.is_windows else ".sh" - content = "exit 0" if ctx.attr.is_windows else "#!/usr/bin/env bash\nexit 0" - executable = ctx.actions.declare_file(ctx.label.name + extension) - ctx.actions.write( - output = executable, - is_executable = True, - content = content, - ) - - return [DefaultInfo( - files = depset([executable]), - executable = executable, - runfiles = ctx.runfiles(files = ctx.files.data), - )] - -_empty_test = rule( - implementation = _empty_test_impl, - attrs = { - "data": attr.label_list(allow_files = True), - "is_windows": attr.bool(mandatory = True), - }, - test = True, -) - -def build_test(name, targets, **kwargs): - """Test rule checking that other targets build. - - This works not by an instance of this test failing, but instead by - the targets it depends on failing to build, and hence failing - the attempt to run this test. - - Typical usage: - - ``` - load("@bazel_skylib//rules:build_test.bzl", "build_test") - build_test( - name = "my_build_test", - targets = [ - "//some/package:rule", - ], - ) - ``` - - Args: - name: The name of the test rule. - targets: A list of targets to ensure build. - **kwargs: The common attributes for tests. - """ - if len(targets) == 0: - fail("targets must be non-empty", "targets") - if kwargs.get("data", None): - fail("data is not supported on a build_test()", "data") - - # Remove any duplicate test targets. - targets = sets.to_list(sets.make(targets)) - - # Use a genrule to ensure the targets are built (works because it forces - # the outputs of the other rules on as data for the genrule) - - # Split into batches to hopefully avoid things becoming so large they are - # too much for a remote execution set up. - batch_size = max(1, len(targets) // 100) - - # Pull a few args over from the test to the genrule. - args_to_reuse = ["compatible_with", "restricted_to", "tags"] - genrule_args = {k: kwargs.get(k) for k in args_to_reuse if k in kwargs} - - # Pass an output from the genrules as data to a shell test to bundle - # it all up in a test. - test_data = [] - - for idx, batch in enumerate([targets[i:i + batch_size] for i in range(0, len(targets), batch_size)]): - full_name = "{name}_{idx}__deps".format(name = name, idx = idx) - test_data.append(full_name) - native.genrule( - name = full_name, - srcs = batch, - outs = [full_name + ".out"], - testonly = 1, - visibility = ["//visibility:private"], - cmd = "touch $@", - cmd_bat = "type nul > $@", - **genrule_args - ) - - _empty_test( - name = name, - data = test_data, - size = kwargs.pop("size", "small"), # Default to small for test size - is_windows = select({ - "@bazel_tools//src/conditions:host_windows": True, - "//conditions:default": False, - }), - **kwargs - )