From ed9d26c25260939e6b2ec7e98774a04963c95953 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 3 Apr 2023 08:03:40 -0700 Subject: [PATCH] feat: skipLibCheck take 2 (#350) * feat: skipLibCheck take 2 Fixes #348 * refactor: use bool flags, finish error msg * refactor: use string_flag instead of pair of bool_flag * Update ts/defs.bzl Co-authored-by: Sahin Yort * Update defs.bzl --------- Co-authored-by: Sahin Yort --- ts/BUILD.bazel | 26 ++++++++++++++++++++++++++ ts/defs.bzl | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/ts/BUILD.bazel b/ts/BUILD.bazel index e3dc1333..028b1b7d 100644 --- a/ts/BUILD.bazel +++ b/ts/BUILD.bazel @@ -1,10 +1,36 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") +load("@bazel_skylib//rules:common_settings.bzl", "string_flag") exports_files( glob(["*.bzl"]), visibility = ["//docs:__pkg__"], ) +# Users can enable with --@aspect_rules_ts//ts:skipLibCheck=always +string_flag( + # Note: this name is chosen to be searchable when users look for "skipLibCheck" in their repo, + # as that's the way it appears in tsconfig.json or tsc command line. + name = "skipLibCheck", + # TODO(2.0): change default to "unspecified" + build_setting_default = "honor_tsconfig", + values = [ + "always", + "honor_tsconfig", + "unspecified", + ], +) + +# Note, users could use a Transition to make a subgraph of their depgraph opt-in to skipLibCheck. +config_setting( + name = "skip_lib_check.always", + flag_values = {":skipLibCheck": "always"}, +) + +config_setting( + name = "skip_lib_check.honor_tsconfig", + flag_values = {":skipLibCheck": "honor_tsconfig"}, +) + bzl_library( name = "defs", srcs = ["defs.bzl"], diff --git a/ts/defs.bzl b/ts/defs.bzl index 356d62a5..d74ef730 100644 --- a/ts/defs.bzl +++ b/ts/defs.bzl @@ -16,6 +16,37 @@ load("//ts/private:ts_lib.bzl", _lib = "lib") ts_config = _ts_config TsConfigInfo = _TsConfigInfo +_skip_lib_check_selection_required = """ + +######## Required Typecheck Performance Selection ######## + +TypeScript's type-checking exposes a flag `--skipLibCheck`: +https://www.typescriptlang.org/tsconfig#skipLibCheck + +Using this flag saves substantial time during type-checking. +Rather than doing a full check of all d.ts files, TypeScript will only type-check the code you +specifically refer to in your app's source code. +We recommend this for most rules_ts users. + +HOWEVER this performance improvement comes at the expense of type-system accuracy. +For example, two packages could define two copies of the same type in an inconsistent way. +If you publish a library from your repository, your incorrect types may result in errors for your users. + +You must choose exactly one of the following flags: + +1. To choose the faster performance, put this in /.bazelrc: + + # passes an argument `--skipLibCheck` to *every* spawn of tsc + build --@aspect_rules_ts//ts:skipLibCheck=always + +2. To choose more correct typechecks, put this in /.bazelrc: + + # honor the setting of `skipLibCheck` in the tsconfig.json file + build --@aspect_rules_ts//ts:skipLibCheck=honor_tsconfig + +########################################################## +""" + validate_options = rule( doc = """Validates that some tsconfig.json properties match attributes on ts_project. See the documentation of [`ts_project`](#ts_project) for more information.""", @@ -400,7 +431,13 @@ def ts_project( ts_project_rule( name = tsc_target_name, srcs = srcs, - args = args, + args = args + select( + { + "@aspect_rules_ts//ts:skip_lib_check.always": ["--skipLibCheck"], + "@aspect_rules_ts//ts:skip_lib_check.honor_tsconfig": [], + }, + no_match_error = _skip_lib_check_selection_required, + ), assets = assets, data = data, deps = tsc_deps,