diff --git a/docs/js_binary.md b/docs/js_binary.md index e8f06c05a..47a9c67c4 100644 --- a/docs/js_binary.md +++ b/docs/js_binary.md @@ -23,9 +23,9 @@ js_binary(
 js_binary(name, chdir, copy_data_to_bin, data, enable_runfiles, entry_point, env,
-          expected_exit_code, fixed_args, include_declarations, include_npm, include_npm_sources,
-          include_transitive_sources, log_level, no_copy_to_bin, node_options, node_toolchain,
-          patch_node_fs, preserve_symlinks_main)
+          expected_exit_code, fixed_args, include_npm, include_npm_sources,
+          include_transitive_sources, include_types, log_level, no_copy_to_bin, node_options,
+          node_toolchain, patch_node_fs, preserve_symlinks_main)
 
Execute a program in the Node.js runtime. @@ -76,10 +76,10 @@ The following environment variables are made available to the Node.js runtime ba | env | Environment variables of the action.

Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution. | Dictionary: String -> String | optional | {} | | expected_exit_code | The expected exit code.

Can be used to write tests that are expected to fail. | Integer | optional | 0 | | fixed_args | Fixed command line arguments to pass to the Node.js when this binary target is executed.

Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution.

Unlike the built-in args, which are only passed to the target when it is executed either by the bazel run command or as a test, fixed_args are baked into the generated launcher script so are always passed even when the binary target is run outside of Bazel directly from the launcher script.

fixed_args are passed before the ones specified in args and before ones that are specified on the bazel run or bazel test command line.

See https://bazel.build/reference/be/common-definitions#common-attributes-binaries for more info on the built-in args attribute. | List of strings | optional | [] | -| include_declarations | When True, declarations and transitive_declarations from JsInfo providers in data targets are included in the runfiles of the target.

Defaults to false since declarations are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | | include_npm | When True, npm is included in the runfiles of the target.

An npm binary is also added on the PATH so tools can spawn npm processes. This is a bash script on Linux and MacOS and a batch script on Windows.

A minimum of rules_nodejs version 5.7.0 is required which contains the Node.js toolchain changes to use npm. | Boolean | optional | False | | include_npm_sources | When True, files in npm_sources from JsInfo providers in data targets are included in the runfiles of the target.

transitive_files from NpmPackageStoreInfo providers in data targets are also included in the runfiles of the target. | Boolean | optional | True | | include_transitive_sources | When True, transitive_sources from JsInfo providers in data targets are included in the runfiles of the target. | Boolean | optional | True | +| include_types | When True, types and transitive_types from JsInfo providers in data targets are included in the runfiles of the target.

Defaults to false since types are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | | log_level | Set the logging level.

Log from are written to stderr. They will be supressed on success when running as the tool of a js_run_binary when silent_on_success is True. In that case, they will be shown only on a build failure along with the stdout & stderr of the node tool being run.

Log levels: fatal, error, warn, info, debug | String | optional | "error" | | no_copy_to_bin | List of files to not copy to the Bazel output tree when copy_data_to_bin is True.

This is useful for exceptional cases where a copy_to_bin is not possible or not suitable for an input file such as a file in an external repository. In most cases, this option is not needed. See copy_data_to_bin docstring for more info. | List of labels | optional | [] | | node_options | Options to pass to the node invocation on the command line.

https://nodejs.org/api/cli.html

These options are passed directly to the node invocation on the command line. Options passed here will take precendence over options passed via the NODE_OPTIONS environment variable. Options passed here are not added to the NODE_OPTIONS environment variable so will not be automatically picked up by child processes that inherit that enviroment variable. | List of strings | optional | [] | @@ -94,9 +94,9 @@ The following environment variables are made available to the Node.js runtime ba
 js_test(name, chdir, copy_data_to_bin, data, enable_runfiles, entry_point, env, expected_exit_code,
-        fixed_args, include_declarations, include_npm, include_npm_sources,
-        include_transitive_sources, log_level, no_copy_to_bin, node_options, node_toolchain,
-        patch_node_fs, preserve_symlinks_main)
+        fixed_args, include_npm, include_npm_sources, include_transitive_sources, include_types,
+        log_level, no_copy_to_bin, node_options, node_toolchain, patch_node_fs,
+        preserve_symlinks_main)
 
Identical to js_binary, but usable under `bazel test`. @@ -135,10 +135,10 @@ the contract between Bazel and a test runner. | env | Environment variables of the action.

Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution. | Dictionary: String -> String | optional | {} | | expected_exit_code | The expected exit code.

Can be used to write tests that are expected to fail. | Integer | optional | 0 | | fixed_args | Fixed command line arguments to pass to the Node.js when this binary target is executed.

Subject to [$(location)](https://bazel.build/reference/be/make-variables#predefined_label_variables) and ["Make variable"](https://bazel.build/reference/be/make-variables) substitution.

Unlike the built-in args, which are only passed to the target when it is executed either by the bazel run command or as a test, fixed_args are baked into the generated launcher script so are always passed even when the binary target is run outside of Bazel directly from the launcher script.

fixed_args are passed before the ones specified in args and before ones that are specified on the bazel run or bazel test command line.

See https://bazel.build/reference/be/common-definitions#common-attributes-binaries for more info on the built-in args attribute. | List of strings | optional | [] | -| include_declarations | When True, declarations and transitive_declarations from JsInfo providers in data targets are included in the runfiles of the target.

Defaults to false since declarations are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | | include_npm | When True, npm is included in the runfiles of the target.

An npm binary is also added on the PATH so tools can spawn npm processes. This is a bash script on Linux and MacOS and a batch script on Windows.

A minimum of rules_nodejs version 5.7.0 is required which contains the Node.js toolchain changes to use npm. | Boolean | optional | False | | include_npm_sources | When True, files in npm_sources from JsInfo providers in data targets are included in the runfiles of the target.

transitive_files from NpmPackageStoreInfo providers in data targets are also included in the runfiles of the target. | Boolean | optional | True | | include_transitive_sources | When True, transitive_sources from JsInfo providers in data targets are included in the runfiles of the target. | Boolean | optional | True | +| include_types | When True, types and transitive_types from JsInfo providers in data targets are included in the runfiles of the target.

Defaults to false since types are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | | log_level | Set the logging level.

Log from are written to stderr. They will be supressed on success when running as the tool of a js_run_binary when silent_on_success is True. In that case, they will be shown only on a build failure along with the stdout & stderr of the node tool being run.

Log levels: fatal, error, warn, info, debug | String | optional | "error" | | no_copy_to_bin | List of files to not copy to the Bazel output tree when copy_data_to_bin is True.

This is useful for exceptional cases where a copy_to_bin is not possible or not suitable for an input file such as a file in an external repository. In most cases, this option is not needed. See copy_data_to_bin docstring for more info. | List of labels | optional | [] | | node_options | Options to pass to the node invocation on the command line.

https://nodejs.org/api/cli.html

These options are passed directly to the node invocation on the command line. Options passed here will take precendence over options passed via the NODE_OPTIONS environment variable. Options passed here are not added to the NODE_OPTIONS environment variable so will not be automatically picked up by child processes that inherit that enviroment variable. | List of strings | optional | [] | diff --git a/docs/js_info_files.md b/docs/js_info_files.md index 35628baca..a30c90c22 100644 --- a/docs/js_info_files.md +++ b/docs/js_info_files.md @@ -7,8 +7,8 @@ Helper rule to gather files from JsInfo providers of targets and provide them as ## js_info_files
-js_info_files(name, include_declarations, include_npm_sources, include_sources,
-              include_transitive_declarations, include_transitive_sources, srcs)
+js_info_files(name, include_npm_sources, include_sources, include_transitive_sources,
+              include_transitive_types, include_types, srcs)
 
Gathers files from the JsInfo providers from targets in srcs and provides them as default outputs. @@ -22,11 +22,11 @@ This helper rule is used by the `js_run_binary` macro. | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| include_declarations | When True, declarations from JsInfo providers in srcs targets are included in the default outputs of the target.

Defaults to False since declarations are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | | include_npm_sources | When True, files in npm_sources from JsInfo providers in srcs targets are included in the default outputs of the target.

transitive_files from NpmPackageStoreInfo providers in data targets are also included in the default outputs of the target. | Boolean | optional | True | | include_sources | When True, sources from JsInfo providers in srcs targets are included in the default outputs of the target. | Boolean | optional | True | -| include_transitive_declarations | When True, transitive_declarations from JsInfo providers in srcs targets are included in the default outputs of the target.

Defaults to False since declarations are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | | include_transitive_sources | When True, transitive_sources from JsInfo providers in srcs targets are included in the default outputs of the target. | Boolean | optional | True | +| include_transitive_types | When True, transitive_types from JsInfo providers in srcs targets are included in the default outputs of the target.

Defaults to False since types are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | +| include_types | When True, types from JsInfo providers in srcs targets are included in the default outputs of the target.

Defaults to False since types are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes. | Boolean | optional | False | | srcs | List of targets to gather files from. | List of labels | optional | [] | diff --git a/docs/js_library.md b/docs/js_library.md index 94c4c1759..8cec1275d 100644 --- a/docs/js_library.md +++ b/docs/js_library.md @@ -30,7 +30,7 @@ js_library( ## js_library
-js_library(name, copy_data_to_bin, data, declarations, deps, no_copy_to_bin, srcs)
+js_library(name, copy_data_to_bin, data, deps, no_copy_to_bin, srcs, types)
 
A library of JavaScript sources. Provides JsInfo, the primary provider used in rules_js @@ -38,7 +38,7 @@ and derivative rule sets. Declaration files are handled separately from sources since they are generally not needed at runtime and build rules, such as ts_project, are optimal in their build graph if they only depend -on declarations from `deps` since these they don't need the JavaScript source files from deps to +on types from `deps` since these they don't need the JavaScript source files from deps to typecheck. Linked npm dependences are also handled separately from sources since not all rules require them and it @@ -56,10 +56,10 @@ for more context on why we do this. | name | A unique name for this target. | Name | required | | | copy_data_to_bin | When True, data files are copied to the Bazel output tree before being passed as inputs to runfiles. | Boolean | optional | True | | data | Runtime dependencies to include in binaries/tests that depend on this target.

The transitive npm dependencies, transitive sources, default outputs and runfiles of targets in the data attribute are added to the runfiles of this target. They should appear in the '*.runfiles' area of any executable which has a runtime dependency on this target.

If this list contains linked npm packages, npm package store targets or other targets that provide JsInfo, NpmPackageStoreInfo providers are gathered from JsInfo. This is done directly from the npm_package_store_infos field of these. For linked npm package targets, the underlying npm_package_store target(s) that back the links is used. Gathered NpmPackageStoreInfo providers are propagated to the direct dependencies of downstream linked npm_package targets.

NB: Linked npm package targets that are "dev" dependencies do not forward their underlying npm_package_store target(s) through npm_package_store_infos and will therefore not be propagated to the direct dependencies of downstream linked npm_package targets. npm packages that come in from npm_translate_lock are considered "dev" dependencies if they are have dev: true set in the pnpm lock file. This should be all packages that are only listed as "devDependencies" in all package.json files within the pnpm workspace. This behavior is intentional to mimic how devDependencies work in published npm packages. | List of labels | optional | [] | -| declarations | Same as srcs except all files are also provided as "declarations" available to downstream rules for type checking.

For example, a js_library with only .js files that are intended to be imported as .js files by downstream type checking rules such as ts_project would list those files in declarations:

 js_library(     name = "js_lib",     declarations = ["index.js"], ) 
| List of labels | optional | [] | | deps | Dependencies of this target.

This may include other js_library targets or other targets that provide JsInfo

The transitive npm dependencies, transitive sources & runfiles of targets in the deps attribute are added to the runfiles of this target. They should appear in the '*.runfiles' area of any executable which is output by or has a runtime dependency on this target.

If this list contains linked npm packages, npm package store targets or other targets that provide JsInfo, NpmPackageStoreInfo providers are gathered from JsInfo. This is done directly from the npm_package_store_infos field of these. For linked npm package targets, the underlying npm_package_store target(s) that back the links is used. Gathered NpmPackageStoreInfo providers are propagated to the direct dependencies of downstream linked npm_package targets.

NB: Linked npm package targets that are "dev" dependencies do not forward their underlying npm_package_store target(s) through npm_package_store_infos and will therefore not be propagated to the direct dependencies of downstream linked npm_package targets. npm packages that come in from npm_translate_lock are considered "dev" dependencies if they are have dev: true set in the pnpm lock file. This should be all packages that are only listed as "devDependencies" in all package.json files within the pnpm workspace. This behavior is intentional to mimic how devDependencies work in published npm packages. | List of labels | optional | [] | | no_copy_to_bin | List of files to not copy to the Bazel output tree when copy_data_to_bin is True.

This is useful for exceptional cases where a copy_to_bin is not possible or not suitable for an input file such as a file in an external repository. In most cases, this option is not needed. See copy_data_to_bin docstring for more info. | List of labels | optional | [] | -| srcs | Source files that are included in this library.

This includes all your checked-in code and any generated source files.

The transitive npm dependencies, transitive sources & runfiles of targets in the srcs attribute are added to the runfiles of this target. They should appear in the '*.runfiles' area of any executable which is output by or has a runtime dependency on this target.

Source files that are JSON files, declaration files or directory artifacts will be automatically provided as "declarations" available to downstream rules for type checking. To explicitly provide source files as "declarations" available to downstream rules for type checking that do not match these criteria, move those files to the declarations attribute instead. | List of labels | optional | [] | +| srcs | Source files that are included in this library.

This includes all your checked-in code and any generated source files.

The transitive npm dependencies, transitive sources & runfiles of targets in the srcs attribute are added to the runfiles of this target. They should appear in the '*.runfiles' area of any executable which is output by or has a runtime dependency on this target.

Source files that are JSON files, declaration files or directory artifacts will be automatically provided as "types" available to downstream rules for type checking. To explicitly provide source files as "types" available to downstream rules for type checking that do not match these criteria, move those files to the types attribute instead. | List of labels | optional | [] | +| types | Same as srcs except all files are also provided as "types" available to downstream rules for type checking.

For example, a js_library with only .js files that are intended to be imported as .js files by downstream type checking rules such as ts_project would list those files in types:

 js_library(     name = "js_lib",     types = ["index.js"], ) 
| List of labels | optional | [] | diff --git a/docs/js_run_binary.md b/docs/js_run_binary.md index 8be29bfd0..2cab1b0f7 100644 --- a/docs/js_run_binary.md +++ b/docs/js_run_binary.md @@ -19,8 +19,8 @@ load("@aspect_rules_js//js:defs.bzl", "js_run_binary")
 js_run_binary(name, tool, env, srcs, outs, out_dirs, args, chdir, stdout, stderr, exit_code_out,
               silent_on_success, use_execroot_entry_point, copy_srcs_to_bin,
-              include_transitive_sources, include_declarations, include_npm_sources, log_level,
-              mnemonic, progress_message, execution_requirements, stamp, patch_node_fs,
+              include_transitive_sources, include_types, include_npm_sources, log_level, mnemonic,
+              progress_message, execution_requirements, stamp, patch_node_fs,
               allow_execroot_entry_point_with_no_copy_data_to_bin, use_default_shell_env, kwargs)
 
@@ -63,7 +63,7 @@ The following environment variables are made available to the Node.js runtime ba | use_execroot_entry_point | Use the entry_point script of the js_binary tool that is in the execroot output tree instead of the copy that is in runfiles.

Runfiles of tool are all hoisted to srcs of the underlying run_binary so they are included as execroot inputs to the action.

Using the entry point script that is in the execroot output tree means that there will be no conflicting runfiles node_modules in the node_modules resolution path which can confuse npm packages such as next and react that don't like being resolved in multiple node_modules trees. This more closely emulates the environment that tools such as Next.js see when they are run outside of Bazel.

When True, the js_binary tool must have copy_data_to_bin set to True (the default) so that all data files needed by the binary are available in the execroot output tree. This requirement can be turned off with by setting allow_execroot_entry_point_with_no_copy_data_to_bin to True. | True | | copy_srcs_to_bin | When True, all srcs files are copied to the output tree that are not already there. | True | | include_transitive_sources | see js_info_files documentation | True | -| include_declarations | see js_info_files documentation | False | +| include_types | see js_info_files documentation | False | | include_npm_sources | see js_info_files documentation | True | | log_level | Set the logging level of the js_binary tool.

This overrides the log level set on the js_binary tool target. | None | | mnemonic | A one-word description of the action, for example, CppCompile or GoLink. | "JsRunBinary" | diff --git a/docs/npm_package.md b/docs/npm_package.md index ecd42c4d9..c67f7d949 100644 --- a/docs/npm_package.md +++ b/docs/npm_package.md @@ -18,8 +18,8 @@ load("@aspect_rules_js//npm:defs.bzl", "npm_package") npm_package(name, srcs, data, args, out, package, version, root_paths, include_external_repositories, include_srcs_packages, exclude_srcs_packages, include_srcs_patterns, exclude_srcs_patterns, replace_prefixes, allow_overwrites, - include_sources, include_transitive_sources, include_declarations, - include_transitive_declarations, include_runfiles, hardlink, verbose, kwargs) + include_sources, include_transitive_sources, include_types, include_transitive_types, + include_runfiles, hardlink, verbose, kwargs) A macro that packages sources into a directory (a tree artifact) and provides an `NpmPackageInfo`. @@ -60,14 +60,14 @@ for more information on supported globbing patterns. `npm_package` makes use of `copy_to_directory` (https://docs.aspect.build/rules/aspect_bazel_lib/docs/copy_to_directory) under the hood, adopting its API and its copy action using composition. However, unlike `copy_to_directory`, -`npm_package` includes `transitive_sources` and `transitive_declarations` files from `JsInfo` providers in srcs -by default. The behavior of including sources and declarations from `JsInfo` can be configured -using the `include_sources`, `include_transitive_sources`, `include_declarations`, `include_transitive_declarations` +`npm_package` includes `transitive_sources` and `transitive_types` files from `JsInfo` providers in srcs +by default. The behavior of including sources and types from `JsInfo` can be configured +using the `include_sources`, `include_transitive_sources`, `include_types`, `include_transitive_types` attributes. -The two `include*_declarations` options may cause type-check actions to run, which slows down your +The two `include*_types` options may cause type-check actions to run, which slows down your development round-trip. -You can pass the Bazel option `--@aspect_rules_js//npm:exclude_declarations_from_npm_packages` +You can pass the Bazel option `--@aspect_rules_js//npm:exclude_types_from_npm_packages` to override these two attributes for an individual `bazel` invocation, avoiding the type-check. `npm_package` also includes default runfiles from `srcs` by default which `copy_to_directory` does not. This behavior @@ -105,9 +105,9 @@ To stamp the current git tag as the "version" in the package.json file, see | allow_overwrites | If True, allow files to be overwritten if the same output file is copied to twice.

The order of srcs matters as the last copy of a particular file will win when overwriting. Performance of npm_package will be slightly degraded when allow_overwrites is True since copies cannot be parallelized out as they are calculated. Instead all copy paths must be calculated before any copies can be started. | False | | include_sources | When True, sources from JsInfo providers in data targets are included in the list of available files to copy. | True | | include_transitive_sources | When True, transitive_sources from JsInfo providers in data targets are included in the list of available files to copy. | True | -| include_declarations | When True, declarations from JsInfo providers in data targets are included in the list of available files to copy. | True | -| include_transitive_declarations | When True, transitive_declarations from JsInfo providers in data targets are included in the list of available files to copy. | True | -| include_runfiles | When True, default runfiles from srcs targets are included in the list of available files to copy.

This may be needed in a few cases:

- to work-around issues with rules that don't provide everything needed in sources, transitive_sources, declarations & transitive_declarations - to depend on the runfiles targets that don't use JsInfo

NB: The default value will be flipped to False in the next major release as runfiles are not needed in the general case and adding them to the list of files available to copy can add noticeable overhead to the analysis phase in a large repository with many npm_package targets. | False | +| include_types | When True, types from JsInfo providers in data targets are included in the list of available files to copy. | True | +| include_transitive_types | When True, transitive_types from JsInfo providers in data targets are included in the list of available files to copy. | True | +| include_runfiles | When True, default runfiles from srcs targets are included in the list of available files to copy.

This may be needed in a few cases:

- to work-around issues with rules that don't provide everything needed in sources, transitive_sources, types & transitive_types - to depend on the runfiles targets that don't use JsInfo

NB: The default value will be flipped to False in the next major release as runfiles are not needed in the general case and adding them to the list of files available to copy can add noticeable overhead to the analysis phase in a large repository with many npm_package targets. | False | | hardlink | Controls when to use hardlinks to files instead of making copies.

Creating hardlinks is much faster than making copies of files with the caveat that hardlinks share file permissions with their source.

Since Bazel removes write permissions on files in the output tree after an action completes, hardlinks to source files are not recommended since write permissions will be inadvertently removed from sources files.

- auto: hardlinks are used for generated files already in the output tree - off: all files are copied - on: hardlinks are used for all files (not recommended) | "auto" | | verbose | If true, prints out verbose logs to stdout | False | | kwargs | Additional attributes such as tags and visibility | none | diff --git a/examples/js_library/two/BUILD.bazel b/examples/js_library/two/BUILD.bazel index 342af2246..fc14d26d0 100644 --- a/examples/js_library/two/BUILD.bazel +++ b/examples/js_library/two/BUILD.bazel @@ -13,7 +13,7 @@ tsc( outs = [ "two.js", ], - include_declarations = True, + include_types = True, ) js_test( diff --git a/js/defs.bzl b/js/defs.bzl index 629f8271d..60e2399de 100644 --- a/js/defs.bzl +++ b/js/defs.bzl @@ -29,6 +29,7 @@ load( # buildifier: disable=function-docstring def js_binary(**kwargs): include_npm_sources = kwargs.pop("include_npm_sources", True) + include_types = kwargs.pop("include_types", False) # For backward compat # TODO(3.0): remove backward compat handling @@ -39,8 +40,18 @@ def js_binary(**kwargs): WARNING: js_binary 'include_npm_linked_packages' is deprecated. Use 'include_npm_sources' instead.""") include_npm_sources = include_npm_linked_packages + # For backward compat + # TODO(3.0): remove backward compat handling + include_declarations = kwargs.pop("include_declarations", False) + if include_declarations: + # buildifier: disable=print + print(""" +WARNING: js_binary 'include_declarations' is deprecated. Use 'include_types' instead.""") + include_types = include_declarations + _js_binary( include_npm_sources = include_npm_sources, + include_types = include_types, enable_runfiles = select({ Label("@aspect_bazel_lib//lib:enable_runfiles"): True, "//conditions:default": False, @@ -51,6 +62,7 @@ WARNING: js_binary 'include_npm_linked_packages' is deprecated. Use 'include_npm # buildifier: disable=function-docstring def js_test(**kwargs): include_npm_sources = kwargs.pop("include_npm_sources", True) + include_types = kwargs.pop("include_types", False) # For backward compat # TODO(3.0): remove backward compat handling @@ -61,8 +73,18 @@ def js_test(**kwargs): WARNING: js_test 'include_npm_linked_packages' is deprecated. Use 'include_npm_sources' instead.""") include_npm_sources = include_npm_linked_packages + # For backward compat + # TODO(3.0): remove backward compat handling + include_declarations = kwargs.pop("include_declarations", False) + if include_declarations: + # buildifier: disable=print + print(""" +WARNING: js_test 'include_declarations' is deprecated. Use 'include_types' instead.""") + include_types = include_declarations + _js_test( include_npm_sources = include_npm_sources, + include_types = include_types, enable_runfiles = select({ Label("@aspect_bazel_lib//lib:enable_runfiles"): True, "//conditions:default": False, @@ -70,8 +92,25 @@ WARNING: js_test 'include_npm_linked_packages' is deprecated. Use 'include_npm_s **kwargs ) +# buildifier: disable=function-docstring +def js_library(**kwargs): + types = kwargs.pop("types", []) + + # For backward compat + # TODO(3.0): remove backward compat handling + declarations = kwargs.pop("declarations", None) + if declarations: + # buildifier: disable=print + print(""" +WARNING: js_library 'declarations' is deprecated. Use 'types' instead.""") + types.extend(declarations) + + _js_library( + types = types, + **kwargs + ) + js_run_devserver = _js_run_devserver js_info_files = _js_info_files -js_library = _js_library js_run_binary = _js_run_binary js_image_layer = _js_image_layer diff --git a/js/libs.bzl b/js/libs.bzl index a746bbadb..6e7fcce6a 100644 --- a/js/libs.bzl +++ b/js/libs.bzl @@ -18,8 +18,8 @@ load( _gather_npm_package_store_infos = "gather_npm_package_store_infos", _gather_npm_sources = "gather_npm_sources", _gather_runfiles = "gather_runfiles", - _gather_transitive_declarations = "gather_transitive_declarations", _gather_transitive_sources = "gather_transitive_sources", + _gather_transitive_types = "gather_transitive_types", ) js_binary_lib = _js_binary_lib @@ -31,7 +31,7 @@ js_lib_helpers = struct( gather_npm_sources = _gather_npm_sources, gather_npm_package_store_infos = _gather_npm_package_store_infos, gather_runfiles = _gather_runfiles, - gather_transitive_declarations = _gather_transitive_declarations, + gather_transitive_types = _gather_transitive_types, gather_transitive_sources = _gather_transitive_sources, JS_LIBRARY_DATA_ATTR = _JS_LIBRARY_DATA_ATTR, DOWNSTREAM_LINKED_NPM_DEPS_DOCSTRING = _DOWNSTREAM_LINKED_NPM_DEPS_DOCSTRING, diff --git a/js/private/js_binary.bzl b/js/private/js_binary.bzl index 33ea3b4eb..2c5607ae6 100644 --- a/js/private/js_binary.bzl +++ b/js/private/js_binary.bzl @@ -191,10 +191,10 @@ _ATTRS = { doc = """When True, `transitive_sources` from `JsInfo` providers in data targets are included in the runfiles of the target.""", default = True, ), - "include_declarations": attr.bool( - doc = """When True, `declarations` and `transitive_declarations` from `JsInfo` providers in data targets are included in the runfiles of the target. + "include_types": attr.bool( + doc = """When True, `types` and `transitive_types` from `JsInfo` providers in data targets are included in the runfiles of the target. - Defaults to false since declarations are generally not needed at runtime and introducing them could slow down developer round trip + Defaults to false since types are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes.""", default = False, ), @@ -516,7 +516,7 @@ def _create_launcher(ctx, log_prefix_rule_set, log_prefix_rule, fixed_args = [], copy_data_files_to_bin = ctx.attr.copy_data_to_bin, no_copy_to_bin = ctx.files.no_copy_to_bin, include_transitive_sources = ctx.attr.include_transitive_sources, - include_declarations = ctx.attr.include_declarations, + include_types = ctx.attr.include_types, include_npm_sources = ctx.attr.include_npm_sources, ).merge(ctx.runfiles( files = launcher_files, diff --git a/js/private/js_helpers.bzl b/js/private/js_helpers.bzl index 11093968a..96a9aef07 100644 --- a/js/private/js_helpers.bzl +++ b/js/private/js_helpers.bzl @@ -54,24 +54,24 @@ def gather_transitive_sources(sources, targets): ] return depset([], transitive = [sources] + transitive) -def gather_transitive_declarations(declarations, targets): - """Gathers transitive declarations from a list of direct declarations and targets +def gather_transitive_types(types, targets): + """Gathers transitive types from a list of direct types and targets Args: - declarations: list or depset of direct sources which should be included in `transitive_declarations` - targets: list of targets to gather `transitive_declarations` from `JsInfo` + types: list or depset of direct sources which should be included in `transitive_types` + targets: list of targets to gather `transitive_types` from `JsInfo` Returns: A depset of transitive sources """ - if type(declarations) == "list": - declarations = depset(declarations) + if type(types) == "list": + types = depset(types) transitive = [ - target[JsInfo].transitive_declarations + target[JsInfo].transitive_types for target in targets - if JsInfo in target and hasattr(target[JsInfo], "transitive_declarations") + if JsInfo in target and hasattr(target[JsInfo], "transitive_types") ] - return depset([], transitive = [declarations] + transitive) + return depset([], transitive = [types] + transitive) def gather_npm_sources(srcs, deps): """Gathers npm sources from a list of srcs and deps targets @@ -155,8 +155,8 @@ def gather_runfiles( no_copy_to_bin = [], include_sources = True, include_transitive_sources = True, - include_declarations = False, - include_transitive_declarations = False, + include_types = False, + include_transitive_types = False, include_npm_sources = True): """Creates a runfiles object containing files in `sources`, default outputs from `data` and transitive runfiles from `data` & `deps`. @@ -197,9 +197,9 @@ def gather_runfiles( include_transitive_sources: see js_info_files documentation - include_declarations: see js_info_files documentation + include_types: see js_info_files documentation - include_transitive_declarations: see js_info_files documentation + include_transitive_types: see js_info_files documentation include_npm_sources: see js_info_files documentation @@ -223,8 +223,8 @@ def gather_runfiles( targets = data + deps, include_sources = include_sources, include_transitive_sources = include_transitive_sources, - include_declarations = include_declarations, - include_transitive_declarations = include_transitive_declarations, + include_types = include_types, + include_transitive_types = include_transitive_types, include_npm_sources = include_npm_sources, )) @@ -286,8 +286,8 @@ def gather_files_from_js_info( targets, include_sources, include_transitive_sources, - include_declarations, - include_transitive_declarations, + include_types, + include_transitive_types, include_npm_sources): """Gathers files from JsInfo and NpmPackageStoreInfo providers. @@ -295,8 +295,8 @@ def gather_files_from_js_info( targets: list of target to gather from include_sources: see js_info_files documentation include_transitive_sources: see js_info_files documentation - include_declarations: see js_info_files documentation - include_transitive_declarations: see js_info_files documentation + include_types: see js_info_files documentation + include_transitive_types: see js_info_files documentation include_npm_sources: see js_info_files documentation Returns: @@ -315,17 +315,17 @@ def gather_files_from_js_info( for target in targets if JsInfo in target and hasattr(target[JsInfo], "transitive_sources") ]) - if include_declarations: + if include_types: files_depsets.extend([ - target[JsInfo].declarations + target[JsInfo].types for target in targets - if JsInfo in target and hasattr(target[JsInfo], "declarations") + if JsInfo in target and hasattr(target[JsInfo], "types") ]) - if include_transitive_declarations: + if include_transitive_types: files_depsets.extend([ - target[JsInfo].transitive_declarations + target[JsInfo].transitive_types for target in targets - if JsInfo in target and hasattr(target[JsInfo], "transitive_declarations") + if JsInfo in target and hasattr(target[JsInfo], "transitive_types") ]) if include_npm_sources: files_depsets.extend([ diff --git a/js/private/js_info.bzl b/js/private/js_info.bzl index cde5e6dd6..0a756a67c 100644 --- a/js/private/js_info.bzl +++ b/js/private/js_info.bzl @@ -3,53 +3,53 @@ JsInfo = provider( doc = "Encapsulates information provided by rules in rules_js and derivative rule sets", fields = { - "declarations": "A depset of declaration files produced by the target", + "types": "A depset of typings files produced by the target", "npm_sources": "A depset of files in npm package dependencies of the target and the target's transitive deps", "npm_package_store_infos": "A depset of NpmPackageStoreInfo providers from non-dev npm dependencies of the target and the target's transitive dependencies to use as direct dependencies when linking downstream npm_package targets with npm_link_package", "sources": "A depset of source files produced by the target", - "transitive_declarations": "A depset of declaration files produced by the target and the target's transitive deps", + "transitive_types": "A depset of declaration files produced by the target and the target's transitive deps", "transitive_sources": "A depset of source files produced by the target and the target's transitive deps", }, ) def js_info( - declarations = depset(), + types = depset(), npm_sources = depset(), npm_package_store_infos = depset(), sources = depset(), - transitive_declarations = depset(), + transitive_types = depset(), transitive_sources = depset()): """Construct a JsInfo. Args: - declarations: See JsInfo documentation + types: See JsInfo documentation npm_sources: See JsInfo documentation npm_package_store_infos: See JsInfo documentation sources: See JsInfo documentation - transitive_declarations: See JsInfo documentation + transitive_types: See JsInfo documentation transitive_sources: See JsInfo documentation Returns: A JsInfo provider """ - if type(declarations) != "depset": - fail("Expected declarations to be a depset") + if type(types) != "depset": + fail("Expected types to be a depset") if type(npm_sources) != "depset": fail("Expected npm_sources to be a depset") if type(npm_package_store_infos) != "depset": fail("Expected npm_package_store_infos to be a depset") if type(sources) != "depset": fail("Expected sources to be a depset") - if type(transitive_declarations) != "depset": - fail("Expected transitive_declarations to be a depset") + if type(transitive_types) != "depset": + fail("Expected transitive_types to be a depset") if type(transitive_sources) != "depset": fail("Expected transitive_sources to be a depset") return JsInfo( - declarations = declarations, + types = types, npm_sources = npm_sources, npm_package_store_infos = npm_package_store_infos, sources = sources, - transitive_declarations = transitive_declarations, + transitive_types = transitive_types, transitive_sources = transitive_sources, ) diff --git a/js/private/js_info_files.bzl b/js/private/js_info_files.bzl index e1e396de3..400d8cf23 100644 --- a/js/private/js_info_files.bzl +++ b/js/private/js_info_files.bzl @@ -12,8 +12,8 @@ def _js_info_files_impl(ctx): targets = ctx.attr.srcs, include_sources = ctx.attr.include_sources, include_transitive_sources = ctx.attr.include_transitive_sources, - include_declarations = ctx.attr.include_declarations, - include_transitive_declarations = ctx.attr.include_transitive_declarations, + include_types = ctx.attr.include_types, + include_transitive_types = ctx.attr.include_transitive_types, include_npm_sources = ctx.attr.include_npm_sources, )) @@ -33,17 +33,17 @@ js_info_files = rule( doc = """When True, `transitive_sources` from `JsInfo` providers in `srcs` targets are included in the default outputs of the target.""", default = True, ), - "include_declarations": attr.bool( - doc = """When True, `declarations` from `JsInfo` providers in srcs targets are included in the default outputs of the target. + "include_types": attr.bool( + doc = """When True, `types` from `JsInfo` providers in srcs targets are included in the default outputs of the target. - Defaults to False since declarations are generally not needed at runtime and introducing them could slow down developer round trip + Defaults to False since types are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes.""", default = False, ), - "include_transitive_declarations": attr.bool( - doc = """When True, `transitive_declarations` from `JsInfo` providers in srcs targets are included in the default outputs of the target. + "include_transitive_types": attr.bool( + doc = """When True, `transitive_types` from `JsInfo` providers in srcs targets are included in the default outputs of the target. - Defaults to False since declarations are generally not needed at runtime and introducing them could slow down developer round trip + Defaults to False since types are generally not needed at runtime and introducing them could slow down developer round trip time due to having to generate typings on source file changes.""", default = False, ), diff --git a/js/private/js_library.bzl b/js/private/js_library.bzl index cff8c0e73..98b0a3cbe 100644 --- a/js/private/js_library.bzl +++ b/js/private/js_library.bzl @@ -24,7 +24,7 @@ js_library( """ load(":js_info.bzl", "JsInfo", "js_info") -load(":js_helpers.bzl", "DOWNSTREAM_LINKED_NPM_DEPS_DOCSTRING", "JS_LIBRARY_DATA_ATTR", "copy_js_file_to_bin_action", "gather_npm_package_store_infos", "gather_npm_sources", "gather_runfiles", "gather_transitive_declarations", "gather_transitive_sources") +load(":js_helpers.bzl", "DOWNSTREAM_LINKED_NPM_DEPS_DOCSTRING", "JS_LIBRARY_DATA_ATTR", "copy_js_file_to_bin_action", "gather_npm_package_store_infos", "gather_npm_sources", "gather_runfiles", "gather_transitive_sources", "gather_transitive_types") load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS") _DOC = """A library of JavaScript sources. Provides JsInfo, the primary provider used in rules_js @@ -32,7 +32,7 @@ and derivative rule sets. Declaration files are handled separately from sources since they are generally not needed at runtime and build rules, such as ts_project, are optimal in their build graph if they only depend -on declarations from `deps` since these they don't need the JavaScript source files from deps to +on types from `deps` since these they don't need the JavaScript source files from deps to typecheck. Linked npm dependences are also handled separately from sources since not all rules require them and it @@ -53,22 +53,22 @@ runfiles of this target. They should appear in the '*.runfiles' area of any exec runtime dependency on this target. Source files that are JSON files, declaration files or directory artifacts will be automatically provided as -"declarations" available to downstream rules for type checking. To explicitly provide source files as "declarations" -available to downstream rules for type checking that do not match these criteria, move those files to the `declarations` +"types" available to downstream rules for type checking. To explicitly provide source files as "types" +available to downstream rules for type checking that do not match these criteria, move those files to the `types` attribute instead. """, allow_files = True, ), - "declarations": attr.label_list( - doc = """Same as `srcs` except all files are also provided as "declarations" available to downstream rules for type checking. + "types": attr.label_list( + doc = """Same as `srcs` except all files are also provided as "types" available to downstream rules for type checking. For example, a js_library with only `.js` files that are intended to be imported as `.js` files by downstream type checking -rules such as `ts_project` would list those files in `declarations`: +rules such as `ts_project` would list those files in `types`: ``` js_library( name = "js_lib", - declarations = ["index.js"], + types = ["index.js"], ) ``` """, @@ -103,33 +103,33 @@ runtime dependency on this target. ), } -def _gather_sources_and_declarations(ctx, targets, files): - """Gathers sources and declarations from a list of targets +def _gather_sources_and_types(ctx, targets, files): + """Gathers sources and types from a list of targets Args: ctx: the rule context - targets: List of targets to gather sources and declarations from their JsInfo providers. + targets: List of targets to gather sources and types from their JsInfo providers. These typically come from the `srcs` and/or `data` attributes of a rule - files: List of files to gather as sources and declarations. + files: List of files to gather as sources and types. These typically come from the `srcs` and/or `data` attributes of a rule Returns: - Sources & declaration files depsets in the sequence (sources, declarations) + Sources & declaration files depsets in the sequence (sources, types) """ sources = [] - declarations = [] + types = [] for file in files: if file.is_source: file = copy_js_file_to_bin_action(ctx, file) if file.is_directory: - # assume a directory contains declarations since we can't know that it doesn't - declarations.append(file) + # assume a directory contains types since we can't know that it doesn't + types.append(file) sources.append(file) elif ( file.path.endswith(".d.ts") or @@ -139,11 +139,11 @@ def _gather_sources_and_declarations(ctx, targets, files): file.path.endswith(".d.cts") or file.path.endswith(".d.cts.map") ): - declarations.append(file) + types.append(file) elif file.path.endswith(".json"): # Any .json can produce types: https://www.typescriptlang.org/tsconfig/#resolveJsonModule - # package.json may be required to resolve declarations with the "typings" key - declarations.append(file) + # package.json may be required to resolve types with the "typings" key + types.append(file) sources.append(file) else: sources.append(file) @@ -155,43 +155,43 @@ def _gather_sources_and_declarations(ctx, targets, files): if JsInfo in target and hasattr(target[JsInfo], "sources") ]) - # declarations as depset - declarations = depset(declarations, transitive = [ - target[JsInfo].declarations + # types as depset + types = depset(types, transitive = [ + target[JsInfo].types for target in targets - if JsInfo in target and hasattr(target[JsInfo], "declarations") + if JsInfo in target and hasattr(target[JsInfo], "types") ]) - return (sources, declarations) + return (sources, types) def _js_library_impl(ctx): - sources, declarations = _gather_sources_and_declarations( + sources, types = _gather_sources_and_types( ctx = ctx, targets = ctx.attr.srcs, files = ctx.files.srcs, ) - additional_sources, additional_declarations = _gather_sources_and_declarations( + additional_sources, additional_types = _gather_sources_and_types( ctx = ctx, - targets = ctx.attr.declarations, - files = ctx.files.declarations, + targets = ctx.attr.types, + files = ctx.files.types, ) sources = depset(transitive = [sources, additional_sources]) - declarations = depset(transitive = [declarations, additional_sources, additional_declarations]) + types = depset(transitive = [types, additional_sources, additional_types]) transitive_sources = gather_transitive_sources( sources = sources, - targets = ctx.attr.srcs + ctx.attr.declarations + ctx.attr.deps, + targets = ctx.attr.srcs + ctx.attr.types + ctx.attr.deps, ) - transitive_declarations = gather_transitive_declarations( - declarations = declarations, - targets = ctx.attr.srcs + ctx.attr.declarations + ctx.attr.deps, + transitive_types = gather_transitive_types( + types = types, + targets = ctx.attr.srcs + ctx.attr.types + ctx.attr.deps, ) npm_sources = gather_npm_sources( - srcs = ctx.attr.srcs + ctx.attr.declarations, + srcs = ctx.attr.srcs + ctx.attr.types, deps = ctx.attr.deps, ) @@ -203,22 +203,22 @@ def _js_library_impl(ctx): ctx = ctx, sources = transitive_sources, data = ctx.attr.data, - deps = ctx.attr.srcs + ctx.attr.declarations + ctx.attr.deps, + deps = ctx.attr.srcs + ctx.attr.types + ctx.attr.deps, data_files = ctx.files.data, copy_data_files_to_bin = ctx.attr.copy_data_to_bin, no_copy_to_bin = ctx.files.no_copy_to_bin, include_transitive_sources = True, - include_declarations = False, + include_types = False, include_npm_sources = True, ) return [ js_info( - declarations = declarations, + types = types, npm_sources = npm_sources, npm_package_store_infos = npm_package_store_infos, sources = sources, - transitive_declarations = transitive_declarations, + transitive_types = transitive_types, transitive_sources = transitive_sources, ), DefaultInfo( @@ -226,7 +226,7 @@ def _js_library_impl(ctx): runfiles = runfiles, ), OutputGroupInfo( - types = declarations, + types = types, runfiles = runfiles.files, ), ] diff --git a/js/private/js_run_binary.bzl b/js/private/js_run_binary.bzl index e1959e708..950d9d82e 100644 --- a/js/private/js_run_binary.bzl +++ b/js/private/js_run_binary.bzl @@ -34,7 +34,7 @@ def js_run_binary( use_execroot_entry_point = True, copy_srcs_to_bin = True, include_transitive_sources = True, - include_declarations = False, + include_types = False, include_npm_sources = True, log_level = None, mnemonic = "JsRunBinary", @@ -152,7 +152,7 @@ def js_run_binary( include_transitive_sources: see `js_info_files` documentation - include_declarations: see `js_info_files` documentation + include_types: see `js_info_files` documentation include_npm_sources: see `js_info_files` documentation @@ -251,11 +251,20 @@ def js_run_binary( WARNING: js_run_binary 'include_npm_linked_packages' is deprecated. Use 'include_npm_sources' instead.""") include_npm_sources = include_npm_linked_packages + # For backward compat + # TODO(3.0): remove backward compat handling + include_declarations = kwargs.pop("include_declarations", False) + if include_declarations: + # buildifier: disable=print + print(""" +WARNING: js_library 'include_declarations' is deprecated. Use 'include_types' instead.""") + include_types = include_declarations + extra_srcs = [] # Hoist js provider files to DefaultInfo make_js_info_files_target = (include_transitive_sources or - include_declarations or + include_types or include_npm_sources) if make_js_info_files_target: js_info_files_name = "{}_js_info_files".format(name) @@ -263,7 +272,7 @@ WARNING: js_run_binary 'include_npm_linked_packages' is deprecated. Use 'include name = js_info_files_name, srcs = srcs, include_transitive_sources = include_transitive_sources, - include_declarations = include_declarations, + include_types = include_types, include_npm_sources = include_npm_sources, # Always tag the target manual since we should only build it when the final target is built. tags = kwargs.get("tags", []) + ["manual"], diff --git a/js/private/js_run_devserver.bzl b/js/private/js_run_devserver.bzl index 4fac64482..4ca6a165c 100644 --- a/js/private/js_run_devserver.bzl +++ b/js/private/js_run_devserver.bzl @@ -44,8 +44,8 @@ def _js_run_devserver_impl(ctx): targets = ctx.attr.data, include_sources = True, include_transitive_sources = ctx.attr.include_transitive_sources, - include_declarations = ctx.attr.include_declarations, - include_transitive_declarations = ctx.attr.include_declarations, + include_types = ctx.attr.include_types, + include_transitive_types = ctx.attr.include_types, include_npm_sources = ctx.attr.include_npm_sources, )] diff --git a/js/private/test/create_launcher/custom_test.bzl b/js/private/test/create_launcher/custom_test.bzl index 498f345ea..7ec7e587d 100644 --- a/js/private/test/create_launcher/custom_test.bzl +++ b/js/private/test/create_launcher/custom_test.bzl @@ -23,8 +23,8 @@ def _custom_test_impl(ctx): targets = ctx.attr.data, include_sources = True, include_transitive_sources = ctx.attr.include_transitive_sources, - include_declarations = ctx.attr.include_declarations, - include_transitive_declarations = ctx.attr.include_declarations, + include_types = ctx.attr.include_types, + include_transitive_types = ctx.attr.include_types, include_npm_sources = ctx.attr.include_npm_sources, ), ).merge(launcher.runfiles).merge_all([ diff --git a/js/private/test/js_library_test.bzl b/js/private/test/js_library_test.bzl index 8c40a179a..c32087bb3 100644 --- a/js/private/test/js_library_test.bzl +++ b/js/private/test/js_library_test.bzl @@ -27,69 +27,69 @@ def _js_library_test_suite_data(): ) # Tests -def _declarations_test_impl(ctx): +def _types_test_impl(ctx): env = analysistest.begin(ctx) target_under_test = analysistest.target_under_test(env) - # declarations should only have the source declarations - declarations = target_under_test[JsInfo].declarations.to_list() - asserts.equals(env, 2, len(declarations)) - asserts.true(env, declarations[0].path.find("/importing.d.ts") != -1) - asserts.true(env, declarations[1].path.find("/data.json") != -1) + # types should only have the source types + types = target_under_test[JsInfo].types.to_list() + asserts.equals(env, 2, len(types)) + asserts.true(env, types[0].path.find("/importing.d.ts") != -1) + asserts.true(env, types[1].path.find("/data.json") != -1) - # transitive_declarations should have the source declarations and transitive declarations - transitive_declarations = target_under_test[JsInfo].transitive_declarations.to_list() - asserts.true(env, len(transitive_declarations) == 2) - asserts.true(env, transitive_declarations[0].path.find("/importing.d.ts") != -1) - asserts.true(env, transitive_declarations[1].path.find("/data.json") != -1) + # transitive_types should have the source types and transitive types + transitive_types = target_under_test[JsInfo].transitive_types.to_list() + asserts.true(env, len(transitive_types) == 2) + asserts.true(env, transitive_types[0].path.find("/importing.d.ts") != -1) + asserts.true(env, transitive_types[1].path.find("/data.json") != -1) - # types OutputGroupInfo should be the same as direct declarations - asserts.equals(env, declarations, target_under_test[OutputGroupInfo].types.to_list()) + # types OutputGroupInfo should be the same as direct types + asserts.equals(env, types, target_under_test[OutputGroupInfo].types.to_list()) return analysistest.end(env) -def _explicit_declarations_test_impl(ctx): +def _explicit_types_test_impl(ctx): env = analysistest.begin(ctx) target_under_test = analysistest.target_under_test(env) - # declarations should only have the source declarations - declarations = target_under_test[JsInfo].declarations.to_list() - asserts.equals(env, 2, len(declarations)) - asserts.true(env, declarations[0].path.find("/data.json") != -1) - asserts.true(env, declarations[1].path.find("/index.js") != -1) + # types should only have the source types + types = target_under_test[JsInfo].types.to_list() + asserts.equals(env, 2, len(types)) + asserts.true(env, types[0].path.find("/data.json") != -1) + asserts.true(env, types[1].path.find("/index.js") != -1) - # transitive_declarations should have the source declarations and transitive declarations - transitive_declarations = target_under_test[JsInfo].transitive_declarations.to_list() - asserts.true(env, len(transitive_declarations) == 2) - asserts.true(env, transitive_declarations[0].path.find("/data.json") != -1) - asserts.true(env, transitive_declarations[1].path.find("/index.js") != -1) + # transitive_types should have the source types and transitive types + transitive_types = target_under_test[JsInfo].transitive_types.to_list() + asserts.true(env, len(transitive_types) == 2) + asserts.true(env, transitive_types[0].path.find("/data.json") != -1) + asserts.true(env, transitive_types[1].path.find("/index.js") != -1) - # types OutputGroupInfo should be the same as direct declarations - asserts.equals(env, declarations, target_under_test[OutputGroupInfo].types.to_list()) + # types OutputGroupInfo should be the same as direct types + asserts.equals(env, types, target_under_test[OutputGroupInfo].types.to_list()) return analysistest.end(env) -def _declarations_empty_srcs_test_impl(ctx): +def _types_empty_srcs_test_impl(ctx): env = analysistest.begin(ctx) target_under_test = analysistest.target_under_test(env) - # declarations should only have the source declarations, in this case 0 - declarations = target_under_test[JsInfo].declarations.to_list() - asserts.equals(env, 0, len(declarations)) + # types should only have the source types, in this case 0 + types = target_under_test[JsInfo].types.to_list() + asserts.equals(env, 0, len(types)) - # transitive_declarations should contain additional indirect deps - transitive_declarations = target_under_test[JsInfo].transitive_declarations.to_list() - asserts.true(env, len(transitive_declarations) > len(declarations)) + # transitive_types should contain additional indirect deps + transitive_types = target_under_test[JsInfo].transitive_types.to_list() + asserts.true(env, len(transitive_types) > len(types)) - # types OutputGroupInfo should be the same as direct declarations - asserts.equals(env, declarations, target_under_test[OutputGroupInfo].types.to_list()) + # types OutputGroupInfo should be the same as direct types + asserts.equals(env, types, target_under_test[OutputGroupInfo].types.to_list()) return analysistest.end(env) -# Test declarations -_declarations_test = analysistest.make(_declarations_test_impl) -_explicit_declarations_test = analysistest.make(_explicit_declarations_test_impl) -_declarations_empty_srcs_test = analysistest.make(_declarations_empty_srcs_test_impl) +# Test types +_types_test = analysistest.make(_types_test_impl) +_explicit_types_test = analysistest.make(_explicit_types_test_impl) +_types_empty_srcs_test = analysistest.make(_types_empty_srcs_test_impl) def js_library_test_suite(name): """Test suite including all tests and data @@ -108,33 +108,33 @@ def js_library_test_suite(name): ], tags = ["manual"], ) - _declarations_test( + _types_test( name = "transitive_type_deps_test", target_under_test = "transitive_type_deps", ) - # Explicit declarations + # Explicit types js_library( - name = "explicit_declarations", + name = "explicit_types", srcs = ["data.json"], - declarations = ["index.js"], + types = ["index.js"], deps = [ "//:node_modules/@types/node", ], tags = ["manual"], ) - _explicit_declarations_test( - name = "explicit_declarations_test", - target_under_test = "explicit_declarations", + _explicit_types_test( + name = "explicit_types_test", + target_under_test = "explicit_types", ) - # Empty srcs, declarations in deps + # Empty srcs, types in deps js_library( name = "transitive_type_deps_empty_srcs", deps = [":transitive_type_deps"], tags = ["manual"], ) - _declarations_empty_srcs_test( + _types_empty_srcs_test( name = "transitive_type_deps_empty_srcs_test", target_under_test = "transitive_type_deps_empty_srcs", ) @@ -143,7 +143,7 @@ def js_library_test_suite(name): name = name, tests = [ ":transitive_type_deps_test", - ":explicit_declarations_test", + ":explicit_types_test", ":transitive_type_deps_empty_srcs_test", ], ) diff --git a/npm/BUILD.bazel b/npm/BUILD.bazel index 8e22606e0..6be5b2d45 100644 --- a/npm/BUILD.bazel +++ b/npm/BUILD.bazel @@ -11,14 +11,21 @@ exports_files( # Allows for faster dev roundtrip by excluding type-checking from libraries that are # internally used via npm_package bool_flag( - name = "exclude_declarations_from_npm_packages", + name = "exclude_types_from_npm_packages", build_setting_default = False, visibility = ["//visibility:public"], ) +# For backward compat +# TODO(3.0): remove this backward compat flag +alias( + name = "exclude_declarations_from_npm_packages", + actual = ":exclude_types_from_npm_packages", +) + config_setting( - name = "exclude_declarations_from_npm_packages_flag", - flag_values = {":exclude_declarations_from_npm_packages": "true"}, + name = "exclude_types_from_npm_packages_flag", + flag_values = {":exclude_types_from_npm_packages": "true"}, visibility = ["//visibility:public"], ) diff --git a/npm/private/npm_package.bzl b/npm/private/npm_package.bzl index 931230b7d..0196de865 100644 --- a/npm/private/npm_package.bzl +++ b/npm/private/npm_package.bzl @@ -34,10 +34,10 @@ _NPM_PACKAGE_ATTRS = dicts.add(copy_to_directory_lib_attrs, { }) _NPM_PACKAGE_FILES_ATTRS = { - "include_declarations": attr.bool(), + "include_types": attr.bool(), "include_runfiles": attr.bool(), "include_sources": attr.bool(), - "include_transitive_declarations": attr.bool(), + "include_transitive_types": attr.bool(), "include_transitive_sources": attr.bool(), "srcs": attr.label_list(allow_files = True), } @@ -60,19 +60,19 @@ def _npm_package_files_impl(ctx): if JsInfo in target and hasattr(target[JsInfo], "sources") ]) - if ctx.attr.include_transitive_declarations: - # include all transitive declarations (this includes direct declarations) + if ctx.attr.include_transitive_types: + # include all transitive types (this includes direct types) files_depsets.extend([ - target[JsInfo].transitive_declarations + target[JsInfo].transitive_types for target in ctx.attr.srcs - if JsInfo in target and hasattr(target[JsInfo], "transitive_declarations") + if JsInfo in target and hasattr(target[JsInfo], "transitive_types") ]) - elif ctx.attr.include_declarations: - # include only direct declarations + elif ctx.attr.include_types: + # include only direct types files_depsets.extend([ - target[JsInfo].declarations + target[JsInfo].types for target in ctx.attr.srcs - if JsInfo in target and hasattr(target[JsInfo], "declarations") + if JsInfo in target and hasattr(target[JsInfo], "types") ]) if ctx.attr.include_runfiles: @@ -169,8 +169,8 @@ def npm_package( allow_overwrites = False, include_sources = True, include_transitive_sources = True, - include_declarations = True, - include_transitive_declarations = True, + include_types = True, + include_transitive_types = True, include_runfiles = False, hardlink = "auto", verbose = False, @@ -213,14 +213,14 @@ def npm_package( `npm_package` makes use of `copy_to_directory` (https://docs.aspect.build/rules/aspect_bazel_lib/docs/copy_to_directory) under the hood, adopting its API and its copy action using composition. However, unlike `copy_to_directory`, - `npm_package` includes `transitive_sources` and `transitive_declarations` files from `JsInfo` providers in srcs - by default. The behavior of including sources and declarations from `JsInfo` can be configured - using the `include_sources`, `include_transitive_sources`, `include_declarations`, `include_transitive_declarations` + `npm_package` includes `transitive_sources` and `transitive_types` files from `JsInfo` providers in srcs + by default. The behavior of including sources and types from `JsInfo` can be configured + using the `include_sources`, `include_transitive_sources`, `include_types`, `include_transitive_types` attributes. - The two `include*_declarations` options may cause type-check actions to run, which slows down your + The two `include*_types` options may cause type-check actions to run, which slows down your development round-trip. - You can pass the Bazel option `--@aspect_rules_js//npm:exclude_declarations_from_npm_packages` + You can pass the Bazel option `--@aspect_rules_js//npm:exclude_types_from_npm_packages` to override these two attributes for an individual `bazel` invocation, avoiding the type-check. `npm_package` also includes default runfiles from `srcs` by default which `copy_to_directory` does not. This behavior @@ -407,15 +407,15 @@ def npm_package( include_transitive_sources: When True, `transitive_sources` from `JsInfo` providers in data targets are included in the list of available files to copy. - include_declarations: When True, `declarations` from `JsInfo` providers in data targets are included in the list of available files to copy. + include_types: When True, `types` from `JsInfo` providers in data targets are included in the list of available files to copy. - include_transitive_declarations: When True, `transitive_declarations` from `JsInfo` providers in data targets are included in the list of available files to copy. + include_transitive_types: When True, `transitive_types` from `JsInfo` providers in data targets are included in the list of available files to copy. include_runfiles: When True, default runfiles from `srcs` targets are included in the list of available files to copy. This may be needed in a few cases: - - to work-around issues with rules that don't provide everything needed in sources, transitive_sources, declarations & transitive_declarations + - to work-around issues with rules that don't provide everything needed in sources, transitive_sources, types & transitive_types - to depend on the runfiles targets that don't use JsInfo NB: The default value will be flipped to False in the next major release as runfiles are not needed in the general case @@ -440,20 +440,20 @@ def npm_package( **kwargs: Additional attributes such as `tags` and `visibility` """ - if include_sources or include_transitive_sources or include_declarations or include_transitive_declarations or include_runfiles: + if include_sources or include_transitive_sources or include_types or include_transitive_types or include_runfiles: files_target = "{}_files".format(name) _npm_package_files( name = files_target, srcs = srcs, include_sources = include_sources, include_transitive_sources = include_transitive_sources, - include_declarations = select({ - "@aspect_rules_js//npm:exclude_declarations_from_npm_packages_flag": False, - "//conditions:default": include_declarations, + include_types = select({ + "@aspect_rules_js//npm:exclude_types_from_npm_packages_flag": False, + "//conditions:default": include_types, }), - include_transitive_declarations = select({ - "@aspect_rules_js//npm:exclude_declarations_from_npm_packages_flag": False, - "//conditions:default": include_transitive_declarations, + include_transitive_types = select({ + "@aspect_rules_js//npm:exclude_types_from_npm_packages_flag": False, + "//conditions:default": include_transitive_types, }), include_runfiles = include_runfiles, # Always tag the target manual since we should only build it when the final target is built.