From 59be5e4415ffc1d1603704debd28facf4f1b58e6 Mon Sep 17 00:00:00 2001
From: Greg Magolan <greg@aspect.dev>
Date: Sat, 1 Jun 2024 11:17:21 -0700
Subject: [PATCH] build(bazel): upgrade to rules_js 2.0 RC

---
 WORKSPACE       |  42 ++++++----------
 dev/esbuild.bzl |   4 ++
 dev/eslint.bzl  |  12 ++---
 dev/js_lib.bzl  | 126 ------------------------------------------------
 dev/mocha.bzl   |   2 +
 5 files changed, 27 insertions(+), 159 deletions(-)
 delete mode 100644 dev/js_lib.bzl

diff --git a/WORKSPACE b/WORKSPACE
index 0a50220afda9..0f4fe704049e 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -33,33 +33,25 @@ http_archive(
     url = "https://github.com/aspect-build/bazel-lib/releases/download/v2.7.7/bazel-lib-v2.7.7.tar.gz",
 )
 
-# rules_js defines an older rules_nodejs, so we override it here
-http_archive(
-    name = "rules_nodejs",
-    sha256 = "3e8369256ad63197959d2253c473a9dcc57c2841d176190e59b91d25d4fe9e67",
-    strip_prefix = "rules_nodejs-6.1.1",
-    url = "https://github.com/bazelbuild/rules_nodejs/releases/download/v6.1.1/rules_nodejs-v6.1.1.tar.gz",
-)
-
 http_archive(
     name = "aspect_rules_js",
-    sha256 = "2cfb3875e1231cefd3fada6774f2c0c5a99db0070e0e48ea398acbff7c6c765b",
-    strip_prefix = "rules_js-1.42.3",
-    url = "https://github.com/aspect-build/rules_js/releases/download/v1.42.3/rules_js-v1.42.3.tar.gz",
+    sha256 = "25e06ac98ce2dd44d74e728e63e1c88e707d0972db20d7e7339c8e458335b4e3",
+    strip_prefix = "rules_js-2.0.0-rc2",
+    url = "https://github.com/aspect-build/rules_js/releases/download/v2.0.0-rc2/rules_js-v2.0.0-rc2.tar.gz",
 )
 
 http_archive(
     name = "aspect_rules_ts",
-    sha256 = "da6620683ab2c28014e9c82e8a8fdbb724cd67f6a1d27317f42a8ceb14048b9b",
-    strip_prefix = "rules_ts-2.4.1",
-    url = "https://github.com/aspect-build/rules_ts/releases/download/v2.4.1/rules_ts-v2.4.1.tar.gz",
+    sha256 = "3ea5cdb825d5dbffe286b3d9c5197a2648cf04b5e6bd8b913a45823cdf0ae960",
+    strip_prefix = "rules_ts-3.0.0-rc0",
+    url = "https://github.com/aspect-build/rules_ts/releases/download/v3.0.0-rc0/rules_ts-v3.0.0-rc0.tar.gz",
 )
 
 http_archive(
     name = "aspect_rules_swc",
-    sha256 = "1908691bde56321423c3f3beaf37f5fc21c51614869572e5f626cea058649373",
-    strip_prefix = "rules_swc-1.2.3",
-    url = "https://github.com/aspect-build/rules_swc/releases/download/v1.2.3/rules_swc-v1.2.3.tar.gz",
+    sha256 = "c085647585c3d01bee3966eb9ba433a1efbb0ee79bb1b8c67882a81d82a9b37f",
+    strip_prefix = "rules_swc-2.0.0-rc0",
+    url = "https://github.com/aspect-build/rules_swc/releases/download/v2.0.0-rc0/rules_swc-v2.0.0-rc0.tar.gz",
 )
 
 http_archive(
@@ -207,16 +199,12 @@ load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies")
 
 rules_js_dependencies()
 
-# node toolchain setup ==========================
-load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
+load("@aspect_rules_js//js:toolchains.bzl", "rules_js_register_toolchains")
 
-nodejs_register_toolchains(
-    name = "nodejs",
-    node_version = "20.8.0",
-)
+rules_js_register_toolchains(node_version = "20.8.0")
 
 # rules_js npm setup ============================
-load("@aspect_rules_js//npm:npm_import.bzl", "npm_translate_lock")
+load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")
 
 npm_translate_lock(
     name = "npm",
@@ -270,9 +258,9 @@ swc_register_toolchains(
 # rules_esbuild setup ===========================
 http_archive(
     name = "aspect_rules_esbuild",
-    sha256 = "46aab76044f040c1c0bd97672d56324619af4913cb9e96606ec37ddd4605831d",
-    strip_prefix = "rules_esbuild-0.16.0",
-    url = "https://github.com/aspect-build/rules_esbuild/releases/download/v0.16.0/rules_esbuild-v0.16.0.tar.gz",
+    sha256 = "ef7163a2e8e319f8a9a70560788dd899126aebf3538c76f8bc1f0b4b52ba4b56",
+    strip_prefix = "rules_esbuild-0.21.0-rc1",
+    url = "https://github.com/aspect-build/rules_esbuild/releases/download/v0.21.0-rc1/rules_esbuild-v0.21.0-rc1.tar.gz",
 )
 
 load("@aspect_rules_esbuild//esbuild:dependencies.bzl", "rules_esbuild_dependencies")
diff --git a/dev/esbuild.bzl b/dev/esbuild.bzl
index 1266116b7c82..308a7d820b7a 100644
--- a/dev/esbuild.bzl
+++ b/dev/esbuild.bzl
@@ -4,6 +4,8 @@ load("@aspect_rules_esbuild//esbuild:defs.bzl", _esbuild = "esbuild")
 def esbuild(name, **kwargs):
     _esbuild(
         name,
+        # TODO: work through build failures when sandbox plugin is enabled so that bundling is hermetic
+        bazel_sandbox_plugin = False,
         **kwargs
     )
 
@@ -12,6 +14,8 @@ def esbuild_web_app(name, **kwargs):
 
     _esbuild(
         name = bundle_name,
+        # TODO: work through build failures when sandbox plugin is enabled so that bundling is hermetic
+        bazel_sandbox_plugin = False,
         **kwargs
     )
 
diff --git a/dev/eslint.bzl b/dev/eslint.bzl
index f6dbba16a5e2..b3f0e8c40b45 100644
--- a/dev/eslint.bzl
+++ b/dev/eslint.bzl
@@ -1,7 +1,7 @@
 load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS", "copy_files_to_bin_actions")
 load("@aspect_rules_js//js:defs.bzl", "js_library")
+load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
 load("@aspect_rules_js//js:providers.bzl", "JsInfo")
-load("//dev:js_lib.bzl", "gather_files_from_js_providers", "gather_runfiles")
 
 def eslint_config_and_lint_root(name = "eslint_config", config_deps = [], root_js_deps = []):
     """
@@ -58,17 +58,17 @@ def _custom_eslint_impl(ctx):
 
     inputs_depset = depset(
         copied_srcs + [ctx.executable.binary],
-        transitive = [gather_files_from_js_providers(
+        transitive = [js_lib_helpers.gather_files_from_js_infos(
             targets = [ctx.attr.config] + ctx.attr.deps,
             include_sources = False,
+            include_types = True,  # we have to include types because we need to lint the types.
             include_transitive_sources = False,
-            # We have to include declarations because we need to lint the types.
-            include_declarations = True,
-            include_npm_linked_packages = True,
+            include_transitive_types = True,  # we have to include types because we need to lint the types.
+            include_npm_sources = True,
         )],
     )
 
-    runfiles = gather_runfiles(
+    runfiles = js_lib_helpers.gather_runfiles(
         ctx = ctx,
         sources = [],
         data = [ctx.attr.config],
diff --git a/dev/js_lib.bzl b/dev/js_lib.bzl
deleted file mode 100644
index 0ac09265d59e..000000000000
--- a/dev/js_lib.bzl
+++ /dev/null
@@ -1,126 +0,0 @@
-"""`js_binary` helper functions. Copied from rules_js internals.
-"""
-
-load("@aspect_rules_js//js:providers.bzl", "JsInfo")
-load("@aspect_rules_js//npm:providers.bzl", "NpmPackageStoreInfo")
-
-def gather_files_from_js_providers(
-        targets,
-        include_sources,
-        include_transitive_sources,
-        include_declarations,
-        include_npm_linked_packages):
-    """Gathers files from JsInfo and NpmPackageStoreInfo providers.
-
-    Args:
-        targets: list of target to gather from
-        include_sources: see js_filegroup documentation
-        include_transitive_sources: see js_filegroup documentation
-        include_declarations: see js_filegroup documentation
-        include_npm_linked_packages: see js_filegroup documentation
-
-    Returns:
-        A depset of files
-    """
-
-    files_depsets = []
-
-    files_depsets.extend([
-        target[DefaultInfo].default_runfiles.files
-        for target in targets
-        if DefaultInfo in target and hasattr(target[DefaultInfo], "default_runfiles")
-    ])
-
-    if include_sources:
-        files_depsets.extend([
-            target[JsInfo].sources
-            for target in targets
-            if JsInfo in target and hasattr(target[JsInfo], "sources")
-        ])
-
-    if include_transitive_sources:
-        files_depsets.extend([
-            target[JsInfo].transitive_sources
-            for target in targets
-            if JsInfo in target and hasattr(target[JsInfo], "transitive_sources")
-        ])
-
-    if include_declarations:
-        files_depsets.extend([
-            target[JsInfo].transitive_declarations
-            for target in targets
-            if JsInfo in target and hasattr(target[JsInfo], "transitive_declarations")
-        ])
-
-    if include_npm_linked_packages:
-        files_depsets.extend([
-            target[JsInfo].transitive_npm_linked_package_files
-            for target in targets
-            if JsInfo in target and hasattr(target[JsInfo], "transitive_npm_linked_package_files")
-        ])
-        files_depsets.extend([
-            target[NpmPackageStoreInfo].transitive_files
-            for target in targets
-            if NpmPackageStoreInfo in target and hasattr(target[NpmPackageStoreInfo], "transitive_files")
-        ])
-
-    # print(files_depsets)
-
-    return depset([], transitive = files_depsets)
-
-def gather_runfiles(ctx, sources, data, deps):
-    """Creates a runfiles object containing files in `sources`, default outputs from `data` and transitive runfiles from `data` & `deps`.
-
-    As a defense in depth against `data` & `deps` targets not supplying all required runfiles, also
-    gathers the transitive sources & transitive npm linked packages from the `JsInfo` &
-    `NpmPackageStoreInfo` providers of `data` & `deps` targets.
-
-    See https://bazel.build/extending/rules#runfiles for more info on providing runfiles in build rules.
-
-    Args:
-        ctx: the rule context
-
-        sources: list or depset of files which should be included in runfiles
-
-        data: list of data targets; default outputs and transitive runfiles are gather from these targets
-
-            See https://bazel.build/reference/be/common-definitions#typical.data and
-            https://bazel.build/concepts/dependencies#data-dependencies for more info and guidance
-            on common usage of the `data` attribute in build rules.
-
-        deps: list of dependency targets; only transitive runfiles are gather from these targets
-
-    Returns:
-        A [runfiles](https://bazel.build/rules/lib/runfiles) object created with [ctx.runfiles](https://bazel.build/rules/lib/ctx#runfiles).
-    """
-
-    # Includes sources
-    if type(sources) == "list":
-        sources = depset(sources)
-    transitive_files_depsets = [sources]
-
-    # Gather the default outputs of data targets
-    transitive_files_depsets.extend([
-        target[DefaultInfo].files
-        for target in data
-    ])
-
-    # Gather the transitive sources & transitive npm linked packages from the JsInfo &
-    # NpmPackageStoreInfo providers of data & deps targets.
-    transitive_files_depsets.append(gather_files_from_js_providers(
-        targets = data + deps,
-        include_sources = True,
-        include_transitive_sources = True,
-        include_declarations = False,
-        include_npm_linked_packages = True,
-    ))
-
-    # Merge the above with the transitive runfiles of data & deps.
-    runfiles = ctx.runfiles(
-        transitive_files = depset(transitive = transitive_files_depsets),
-    ).merge_all([
-        target[DefaultInfo].default_runfiles
-        for target in data + deps
-    ])
-
-    return runfiles
diff --git a/dev/mocha.bzl b/dev/mocha.bzl
index 8f5bb827b6b3..a43d21b043bf 100644
--- a/dev/mocha.bzl
+++ b/dev/mocha.bzl
@@ -49,6 +49,8 @@ def mocha_test(name, tests, deps = [], args = [], data = [], env = {}, is_percy_
                 ".node": "copy",
             },
         },
+        # TODO: work through build failures when sandbox plugin is enabled so that bundling is hermetic
+        bazel_sandbox_plugin = False,
     )
 
     args = [