|
| 1 | +load("@aspect_rules_js//js:providers.bzl", "JsInfo") |
| 2 | + |
| 3 | +# A custom provider to pass along the npm package name for linked npm packages |
| 4 | +NpmPackage = provider() |
| 5 | + |
| 6 | +def _npm_package_aspect_impl(target, ctx): |
| 7 | + if (ctx.rule.kind == "npm_link_package_store"): |
| 8 | + package_name = ctx.rule.attr.package |
| 9 | + |
| 10 | + # TODO: Determine how to include the package field information in locally built npm package targets |
| 11 | + if package_name == "": |
| 12 | + package_name = target[JsInfo].npm_package_store_infos.to_list()[0].package |
| 13 | + return [NpmPackage(name = package_name)] |
| 14 | + return [] |
| 15 | + |
| 16 | +# Aspect to include the npm package name for use in strict deps checking. |
| 17 | +_npm_package_aspect = aspect( |
| 18 | + implementation = _npm_package_aspect_impl, |
| 19 | + required_providers = [], |
| 20 | +) |
| 21 | + |
| 22 | +def _strict_deps_impl(ctx): |
| 23 | + sources = [] |
| 24 | + |
| 25 | + allowed_sources = [] |
| 26 | + allowed_module_names = [] |
| 27 | + test_files = [] |
| 28 | + |
| 29 | + # Whether or not the strict_deps check is expected to fail. |
| 30 | + expect_failure = "true" if ctx.attr.will_fail else "false" |
| 31 | + |
| 32 | + for dep in ctx.attr.deps: |
| 33 | + if JsInfo in dep: |
| 34 | + # Because each source maps to a corresponding type file, we can simply look at the type |
| 35 | + # files for the sources, this also allows for situations in which we only provide types. |
| 36 | + sources.append(dep[JsInfo].types) |
| 37 | + if NpmPackage in dep: |
| 38 | + allowed_module_names.append(dep[NpmPackage].name) |
| 39 | + |
| 40 | + for source in depset(transitive = sources).to_list(): |
| 41 | + allowed_sources.append(source.short_path) |
| 42 | + |
| 43 | + for file in ctx.files.srcs: |
| 44 | + allowed_sources.append(file.short_path) |
| 45 | + if file.is_source: |
| 46 | + test_files.append(file.short_path) |
| 47 | + |
| 48 | + manifest = ctx.actions.declare_file("%s_strict_deps_manifest.json" % ctx.attr.name) |
| 49 | + ctx.actions.write( |
| 50 | + output = manifest, |
| 51 | + content = json.encode({ |
| 52 | + # Note: Ensure this matches `StrictDepsManifest` from `manifest.mts` |
| 53 | + "testFiles": test_files, |
| 54 | + "allowedModuleNames": allowed_module_names, |
| 55 | + "allowedSources": allowed_sources, |
| 56 | + }), |
| 57 | + ) |
| 58 | + |
| 59 | + launcher = ctx.actions.declare_file("%s_launcher.sh" % ctx.attr.name) |
| 60 | + ctx.actions.write( |
| 61 | + output = launcher, |
| 62 | + is_executable = True, |
| 63 | + # Bash runfile library taken from: |
| 64 | + # https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash. |
| 65 | + content = """ |
| 66 | + #!/usr/bin/env bash |
| 67 | +
|
| 68 | + # --- begin runfiles.bash initialization v3 --- |
| 69 | + # Copy-pasted from the Bazel Bash runfiles library v3. |
| 70 | + set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash |
| 71 | + # shellcheck disable=SC1090 |
| 72 | + source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ |
| 73 | + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ |
| 74 | + source "$0.runfiles/$f" 2>/dev/null || \ |
| 75 | + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ |
| 76 | + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ |
| 77 | + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e |
| 78 | + # --- end runfiles.bash initialization v3 --- |
| 79 | +
|
| 80 | + exec $(rlocation %s) $(rlocation %s) %s |
| 81 | + """ % ( |
| 82 | + "%s/%s" % (ctx.workspace_name, ctx.files._bin[0].short_path), |
| 83 | + "%s/%s" % (ctx.workspace_name, manifest.short_path), |
| 84 | + expect_failure, |
| 85 | + ), |
| 86 | + ) |
| 87 | + |
| 88 | + bin_runfiles = ctx.attr._bin[DefaultInfo].default_runfiles |
| 89 | + |
| 90 | + return [ |
| 91 | + DefaultInfo( |
| 92 | + executable = launcher, |
| 93 | + runfiles = ctx.runfiles( |
| 94 | + files = ctx.files._runfiles_lib + ctx.files.srcs + [manifest], |
| 95 | + ).merge(bin_runfiles), |
| 96 | + ), |
| 97 | + ] |
| 98 | + |
| 99 | +_strict_deps_test = rule( |
| 100 | + implementation = _strict_deps_impl, |
| 101 | + test = True, |
| 102 | + doc = "Rule to verify that specified TS files only import from explicitly listed deps.", |
| 103 | + attrs = { |
| 104 | + "deps": attr.label_list( |
| 105 | + aspects = [_npm_package_aspect], |
| 106 | + doc = "Direct dependencies that are allowed", |
| 107 | + default = [], |
| 108 | + ), |
| 109 | + "srcs": attr.label_list( |
| 110 | + doc = "TS files to be checked", |
| 111 | + allow_files = True, |
| 112 | + mandatory = True, |
| 113 | + ), |
| 114 | + "will_fail": attr.bool( |
| 115 | + doc = "Whether the test is expected to fail", |
| 116 | + default = False, |
| 117 | + ), |
| 118 | + "_runfiles_lib": attr.label( |
| 119 | + default = "@bazel_tools//tools/bash/runfiles", |
| 120 | + ), |
| 121 | + "_bin": attr.label( |
| 122 | + default = "@devinfra//bazel/ts_project/strict_deps:bin", |
| 123 | + executable = True, |
| 124 | + cfg = "exec", |
| 125 | + ), |
| 126 | + }, |
| 127 | +) |
| 128 | + |
| 129 | +def strict_deps_test(**kwargs): |
| 130 | + kwargs["will_fail"] = False |
| 131 | + _strict_deps_test(**kwargs) |
| 132 | + |
| 133 | +def invalid_strict_deps_test(**kwargs): |
| 134 | + kwargs["will_fail"] = True |
| 135 | + _strict_deps_test(**kwargs) |
0 commit comments