diff --git a/.gitignore b/.gitignore index 5826b1ad..57d74996 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,4 @@ bazel-bin bazel-out bazel-testlogs bazel-code +bazel-* diff --git a/crates/code-api/BUILD.bazel b/crates/code-api/BUILD.bazel index 6b2d0ba8..2021073c 100644 --- a/crates/code-api/BUILD.bazel +++ b/crates/code-api/BUILD.bazel @@ -34,6 +34,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -63,4 +64,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-commands/BUILD.bazel b/crates/code-commands/BUILD.bazel index 2affd3ff..88446eeb 100644 --- a/crates/code-commands/BUILD.bazel +++ b/crates/code-commands/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -49,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-core/BUILD.bazel b/crates/code-core/BUILD.bazel index 89c1f3c2..e4c0859e 100644 --- a/crates/code-core/BUILD.bazel +++ b/crates/code-core/BUILD.bazel @@ -26,6 +26,7 @@ sh_test( "test", ], local = 1, + timeout = "eternal", ) sh_test( @@ -55,4 +56,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-embeddings/BUILD.bazel b/crates/code-embeddings/BUILD.bazel index 3bb37844..0366902b 100644 --- a/crates/code-embeddings/BUILD.bazel +++ b/crates/code-embeddings/BUILD.bazel @@ -25,6 +25,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -54,4 +55,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-events/BUILD.bazel b/crates/code-events/BUILD.bazel index 1d3d0ec5..3730b5b1 100644 --- a/crates/code-events/BUILD.bazel +++ b/crates/code-events/BUILD.bazel @@ -25,6 +25,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -54,4 +55,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-harness/BUILD.bazel b/crates/code-harness/BUILD.bazel index bf7ba8dd..cb9182e2 100644 --- a/crates/code-harness/BUILD.bazel +++ b/crates/code-harness/BUILD.bazel @@ -50,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-incremental/BUILD.bazel b/crates/code-incremental/BUILD.bazel index 2363a7e9..f99ffa65 100644 --- a/crates/code-incremental/BUILD.bazel +++ b/crates/code-incremental/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -49,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-integrations/BUILD.bazel b/crates/code-integrations/BUILD.bazel index de6c085c..5a882b74 100644 --- a/crates/code-integrations/BUILD.bazel +++ b/crates/code-integrations/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -49,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-memory/BUILD.bazel b/crates/code-memory/BUILD.bazel index 3f3fe14f..de52be1d 100644 --- a/crates/code-memory/BUILD.bazel +++ b/crates/code-memory/BUILD.bazel @@ -25,6 +25,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -54,4 +55,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-orchestrator/BUILD.bazel b/crates/code-orchestrator/BUILD.bazel index 8f3c5843..e161ad9c 100644 --- a/crates/code-orchestrator/BUILD.bazel +++ b/crates/code-orchestrator/BUILD.bazel @@ -25,6 +25,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -54,4 +55,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-plugins/BUILD.bazel b/crates/code-plugins/BUILD.bazel index caffb516..974b9f02 100644 --- a/crates/code-plugins/BUILD.bazel +++ b/crates/code-plugins/BUILD.bazel @@ -30,6 +30,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -59,4 +60,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-providers/BUILD.bazel b/crates/code-providers/BUILD.bazel index 613af46e..af7758e1 100644 --- a/crates/code-providers/BUILD.bazel +++ b/crates/code-providers/BUILD.bazel @@ -50,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-repo/BUILD.bazel b/crates/code-repo/BUILD.bazel index 89fa0ab0..d9e802f7 100644 --- a/crates/code-repo/BUILD.bazel +++ b/crates/code-repo/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( diff --git a/crates/code-sandbox/BUILD.bazel b/crates/code-sandbox/BUILD.bazel index 0adf733b..89665949 100644 --- a/crates/code-sandbox/BUILD.bazel +++ b/crates/code-sandbox/BUILD.bazel @@ -25,6 +25,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -54,4 +55,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-server/BUILD.bazel b/crates/code-server/BUILD.bazel index 557fb95a..a0c8fcee 100644 --- a/crates/code-server/BUILD.bazel +++ b/crates/code-server/BUILD.bazel @@ -59,4 +59,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-store/BUILD.bazel b/crates/code-store/BUILD.bazel index bb4f3b08..f0998d24 100644 --- a/crates/code-store/BUILD.bazel +++ b/crates/code-store/BUILD.bazel @@ -50,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-telemetry/BUILD.bazel b/crates/code-telemetry/BUILD.bazel index 492ecf9e..955fd065 100644 --- a/crates/code-telemetry/BUILD.bazel +++ b/crates/code-telemetry/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -49,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-tools/BUILD.bazel b/crates/code-tools/BUILD.bazel index 448b7c71..50fc24d6 100644 --- a/crates/code-tools/BUILD.bazel +++ b/crates/code-tools/BUILD.bazel @@ -50,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-training/BUILD.bazel b/crates/code-training/BUILD.bazel index 80f0a4d8..289b060f 100644 --- a/crates/code-training/BUILD.bazel +++ b/crates/code-training/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -49,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-types/BUILD.bazel b/crates/code-types/BUILD.bazel index 064d3ab3..19488d92 100644 --- a/crates/code-types/BUILD.bazel +++ b/crates/code-types/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -49,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/crates/code-webhooks/BUILD.bazel b/crates/code-webhooks/BUILD.bazel index cc5cd760..a195d180 100644 --- a/crates/code-webhooks/BUILD.bazel +++ b/crates/code-webhooks/BUILD.bazel @@ -20,6 +20,7 @@ sh_test( "rust", "test", ], + timeout = "eternal", ) sh_test( @@ -49,4 +50,5 @@ sh_test( "lint", "rust", ], + timeout = "eternal", ) diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index 9d724f95..f2cf985d 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -13,6 +13,7 @@ filegroup( sh_test( name = "core_config_example", + timeout = "eternal", srcs = ["//tools:crate.sh"], data = ["//:cargo_workspace"], args = [ diff --git a/extensions/slack-agent/BUILD.bazel b/extensions/slack-agent/BUILD.bazel index 437c5271..63e9286b 100644 --- a/extensions/slack-agent/BUILD.bazel +++ b/extensions/slack-agent/BUILD.bazel @@ -33,7 +33,7 @@ sh_test( name = "lint", size = "small", srcs = ["//tools:extension.sh"], - data = [":srcs", ":config_srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore"], + data = [":srcs", ":config_srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "//tools:user_env.sh"], args = [ "extensions/slack-agent", "lint", @@ -47,7 +47,7 @@ sh_test( sh_test( name = "build", srcs = ["//tools:extension.sh"], - data = [":srcs", ":config_srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore"], + data = [":srcs", ":config_srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "//tools:user_env.sh"], args = [ "extensions/slack-agent", "build", @@ -61,7 +61,7 @@ sh_test( sh_test( name = "test", srcs = ["//tools:extension.sh"], - data = [":srcs", ":config_srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore"], + data = [":srcs", ":config_srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "//tools:user_env.sh"], args = [ "extensions/slack-agent", "test", diff --git a/extensions/vscode/BUILD.bazel b/extensions/vscode/BUILD.bazel index cf56632f..94e500dd 100644 --- a/extensions/vscode/BUILD.bazel +++ b/extensions/vscode/BUILD.bazel @@ -16,7 +16,7 @@ sh_test( name = "lint", size = "small", srcs = ["//tools:extension.sh"], - data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json"], + data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json", "//tools:user_env.sh"], args = [ "extensions/vscode", "lint", @@ -30,7 +30,7 @@ sh_test( sh_test( name = "build", srcs = ["//tools:extension.sh"], - data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json"], + data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json", "//tools:user_env.sh"], args = [ "extensions/vscode", "build", diff --git a/extensions/zed/BUILD.bazel b/extensions/zed/BUILD.bazel index fa1bed8c..962225fe 100644 --- a/extensions/zed/BUILD.bazel +++ b/extensions/zed/BUILD.bazel @@ -16,7 +16,7 @@ sh_test( name = "lint", size = "small", srcs = ["//tools:extension.sh"], - data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json"], + data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json", "//tools:user_env.sh"], args = [ "extensions/zed", "lint", @@ -30,7 +30,7 @@ sh_test( sh_test( name = "build", srcs = ["//tools:extension.sh"], - data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json"], + data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "package.json", "//tools:user_env.sh"], args = [ "extensions/zed", "build", diff --git a/npm/code-cli/BUILD.bazel b/npm/code-cli/BUILD.bazel index 0b4ab456..d6de2fba 100644 --- a/npm/code-cli/BUILD.bazel +++ b/npm/code-cli/BUILD.bazel @@ -18,9 +18,8 @@ filegroup( sh_test( name = "test", - local = True, srcs = ["//tools:cli.sh"], - data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore"], + data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "//tools:user_env.sh"], args = ["test"], tags = [ "test", @@ -32,7 +31,7 @@ sh_test( name = "lint", size = "small", srcs = ["//tools:cli.sh"], - data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore"], + data = [":srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore", "//tools:user_env.sh"], args = ["lint"], tags = [ "lint", @@ -43,9 +42,9 @@ sh_test( sh_test( name = "build", local = True, - timeout = "long", + timeout = "eternal", srcs = ["//tools:cli.sh"], - data = [":srcs", "//:cargo_workspace", "//:biome.json", "//:ultracite.json", "//:.gitignore"], + data = [":srcs", "//:cargo_workspace", "//:biome.json", "//:ultracite.json", "//:.gitignore", "//tools:user_env.sh"], args = ["build"], tags = [ "build", diff --git a/npm/code-cli/scripts/build.sh b/npm/code-cli/scripts/build.sh index 3fe6189c..dd85780e 100755 --- a/npm/code-cli/scripts/build.sh +++ b/npm/code-cli/scripts/build.sh @@ -27,7 +27,16 @@ case "$OS/$ARCH" in *) echo "ERROR: unsupported build platform: $OS/$ARCH" >&2; exit 1 ;; esac -SRC="$REPO_ROOT/target/release/code" +# Respect CARGO_TARGET_DIR (set by tools/user_env.sh under Bazel sandboxes) +# so the binary is found where cargo actually wrote it. Resolve relative +# values against REPO_ROOT (cargo's CWD during the build) so the cp finds +# the same directory cargo wrote to. +if [[ -n "${CARGO_TARGET_DIR:-}" && "${CARGO_TARGET_DIR:-}" != /* ]]; then + TARGET_DIR_CARGO="$REPO_ROOT/${CARGO_TARGET_DIR}" +else + TARGET_DIR_CARGO="${CARGO_TARGET_DIR:-$REPO_ROOT/target}" +fi +SRC="$TARGET_DIR_CARGO/release/code" DEST="$PKG_ROOT/vendor/$TARGET_DIR/bin/frontal-code" mkdir -p "$(dirname "$DEST")" cp "$SRC" "$DEST" diff --git a/sdks/typescript/BUILD.bazel b/sdks/typescript/BUILD.bazel index 90b925f1..b881dc57 100644 --- a/sdks/typescript/BUILD.bazel +++ b/sdks/typescript/BUILD.bazel @@ -19,8 +19,7 @@ filegroup( biome_lint( name = "lint", target = "sdks/typescript", - # bunx downloads Biome into a temp directory, which the test sandbox blocks. - tags = ["lint", "biome", "typescript", "no-sandbox"], + tags = ["lint", "biome", "typescript"], data = [":typescript_sdk_srcs", "//:biome.json", "//:ultracite.json", "//:.gitignore"], ) @@ -28,7 +27,7 @@ biome_lint( sh_test( name = "typecheck", srcs = ["//tools:sdk_ts.sh"], - data = [":typescript_sdk_srcs", "//extensions/zod:srcs", "//:biome.json", "//:ultracite.json"], + data = [":typescript_sdk_srcs", "//extensions/zod:srcs", "//:biome.json", "//:ultracite.json", "//tools:user_env.sh"], args = ["sdks/typescript", "typecheck"], tags = ["typescript", "test"], ) @@ -36,7 +35,7 @@ sh_test( sh_test( name = "build", srcs = ["//tools:sdk_ts.sh"], - data = [":typescript_sdk_srcs", "//extensions/zod:srcs", "//:biome.json", "//:ultracite.json"], + data = [":typescript_sdk_srcs", "//extensions/zod:srcs", "//:biome.json", "//:ultracite.json", "//tools:user_env.sh"], args = ["sdks/typescript", "build"], tags = ["typescript", "build"], ) @@ -45,7 +44,7 @@ sh_test( name = "test", srcs = ["//tools:sdk_ts.sh"], # The SDK depends on the zod schemas through a file: link, so the package must be in the sandbox. - data = [":typescript_sdk_srcs", "//extensions/zod:srcs"], + data = [":typescript_sdk_srcs", "//extensions/zod:srcs", "//tools:user_env.sh"], args = ["sdks/typescript", "test"], tags = ["typescript", "test"], ) diff --git a/tests/mock-gateway/BUILD.bazel b/tests/mock-gateway/BUILD.bazel index 741d3dc2..a6f2927c 100644 --- a/tests/mock-gateway/BUILD.bazel +++ b/tests/mock-gateway/BUILD.bazel @@ -19,6 +19,7 @@ sh_binary( sh_test( name = "test", + timeout = "eternal", srcs = ["//tools:crate.sh"], data = ["//:cargo_workspace"], args = [ @@ -48,6 +49,7 @@ sh_test( sh_test( name = "lint", + timeout = "eternal", srcs = ["//tools:crate.sh"], data = ["//:cargo_workspace"], args = [ diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel index eb6cb638..b0724fc6 100644 --- a/tools/BUILD.bazel +++ b/tools/BUILD.bazel @@ -23,6 +23,7 @@ exports_files([ "lint.bzl", "sdk_rust.sh", "sdk_ts.sh", + "user_env.sh", ]) # TODO(bazel): once rules_rust + crate_universe land (see docs/bazel/ROADMAP.md), diff --git a/tools/cli.sh b/tools/cli.sh index 267d718f..3e6b5892 100755 --- a/tools/cli.sh +++ b/tools/cli.sh @@ -8,6 +8,27 @@ if [[ -z "$REPO_ROOT" ]]; then REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" fi fi +# Redirect toolchain caches into TEST_TMPDIR so Bazel sandboxes can +# download and resolve dependencies (see tools/user_env.sh). +if [[ -n "${TEST_TMPDIR:-}" ]]; then + # user_env.sh is a source file next to this wrapper. The wrapper may be + # staged into the test's runfiles as a symlink to the source tree (which + # the sandbox can read), so resolve it from the wrapper's symlinked + # directory first; fall back to the runfiles tree and the REPO_ROOT copy + # for layouts where the wrapper's directory does not contain it. + USER_ENV="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/user_env.sh" + if [[ ! -f "$USER_ENV" && -n "${TEST_SRCDIR:-}" ]]; then + USER_ENV="$TEST_SRCDIR/$TEST_WORKSPACE/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + USER_ENV="$REPO_ROOT/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + echo "ERROR: user_env.sh not found (tried $USER_ENV)" >&2 + exit 1 + fi + source "$USER_ENV" +fi cd "$REPO_ROOT" ACTION="${1:-test}" cd npm/code-cli diff --git a/tools/crate.sh b/tools/crate.sh index 6b9f7c05..97da47ac 100755 --- a/tools/crate.sh +++ b/tools/crate.sh @@ -8,6 +8,27 @@ if [[ -z "$REPO_ROOT" ]]; then REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" fi fi +# Redirect toolchain caches into TEST_TMPDIR so Bazel sandboxes can +# download and resolve dependencies (see tools/user_env.sh). +if [[ -n "${TEST_TMPDIR:-}" ]]; then + # user_env.sh is a source file next to this wrapper. The wrapper may be + # staged into the test's runfiles as a symlink to the source tree (which + # the sandbox can read), so resolve it from the wrapper's symlinked + # directory first; fall back to the runfiles tree and the REPO_ROOT copy + # for layouts where the wrapper's directory does not contain it. + USER_ENV="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/user_env.sh" + if [[ ! -f "$USER_ENV" && -n "${TEST_SRCDIR:-}" ]]; then + USER_ENV="$TEST_SRCDIR/$TEST_WORKSPACE/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + USER_ENV="$REPO_ROOT/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + echo "ERROR: user_env.sh not found (tried $USER_ENV)" >&2 + exit 1 + fi + source "$USER_ENV" +fi if [[ -n "${TEST_TMPDIR:-}" ]]; then WORKSPACE_DIR="$TEST_TMPDIR/cargo-workspace" rm -rf "$WORKSPACE_DIR" diff --git a/tools/extension.sh b/tools/extension.sh index 918e2a44..ec831843 100755 --- a/tools/extension.sh +++ b/tools/extension.sh @@ -8,6 +8,27 @@ if [[ -z "$REPO_ROOT" ]]; then REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" fi fi +# Redirect toolchain caches into TEST_TMPDIR so Bazel sandboxes can +# download and resolve dependencies (see tools/user_env.sh). +if [[ -n "${TEST_TMPDIR:-}" ]]; then + # user_env.sh is a source file next to this wrapper. The wrapper may be + # staged into the test's runfiles as a symlink to the source tree (which + # the sandbox can read), so resolve it from the wrapper's symlinked + # directory first; fall back to the runfiles tree and the REPO_ROOT copy + # for layouts where the wrapper's directory does not contain it. + USER_ENV="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/user_env.sh" + if [[ ! -f "$USER_ENV" && -n "${TEST_SRCDIR:-}" ]]; then + USER_ENV="$TEST_SRCDIR/$TEST_WORKSPACE/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + USER_ENV="$REPO_ROOT/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + echo "ERROR: user_env.sh not found (tried $USER_ENV)" >&2 + exit 1 + fi + source "$USER_ENV" +fi EXTENSION="$1" ACTION="${2:-lint}" shift 2 diff --git a/tools/lint.bzl b/tools/lint.bzl index 5b0fc8f7..ddf1e6fe 100644 --- a/tools/lint.bzl +++ b/tools/lint.bzl @@ -9,7 +9,7 @@ def biome_lint(name, target = ".", data = [], tags = None, **kwargs): native.sh_test( name = name, srcs = ["//tools:lint.sh"], - data = data + ["//tools:lint.sh"], + data = data + ["//tools:lint.sh", "//tools:user_env.sh"], args = [target], size = "small", tags = tags or ["lint", "biome", "typescript"], @@ -22,7 +22,7 @@ def rust_lint(name, crate = None, data = [], tags = None, **kwargs): native.sh_test( name = name, srcs = ["//tools:crate.sh"], - data = data + ["//:cargo_workspace"], + data = data + ["//:cargo_workspace", "//tools:user_env.sh"], args = [crate_arg, "clippy"], tags = tags or ["lint", "rust"], **kwargs @@ -33,7 +33,7 @@ def rust_format(name, tags = None, **kwargs): native.sh_test( name = name, srcs = ["//tools:format.sh"], - data = ["//:cargo_workspace"], + data = ["//:cargo_workspace", "//tools:user_env.sh"], tags = tags or ["fmt", "rust"], **kwargs ) diff --git a/tools/lint.sh b/tools/lint.sh index ce3acfcf..861e57d4 100755 --- a/tools/lint.sh +++ b/tools/lint.sh @@ -13,6 +13,27 @@ if [[ -z "$REPO_ROOT" ]]; then REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" fi fi +# Redirect toolchain caches into TEST_TMPDIR so Bazel sandboxes can +# download and resolve dependencies (see tools/user_env.sh). +if [[ -n "${TEST_TMPDIR:-}" ]]; then + # user_env.sh is a source file next to this wrapper. The wrapper may be + # staged into the test's runfiles as a symlink to the source tree (which + # the sandbox can read), so resolve it from the wrapper's symlinked + # directory first; fall back to the runfiles tree and the REPO_ROOT copy + # for layouts where the wrapper's directory does not contain it. + USER_ENV="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/user_env.sh" + if [[ ! -f "$USER_ENV" && -n "${TEST_SRCDIR:-}" ]]; then + USER_ENV="$TEST_SRCDIR/$TEST_WORKSPACE/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + USER_ENV="$REPO_ROOT/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + echo "ERROR: user_env.sh not found (tried $USER_ENV)" >&2 + exit 1 + fi + source "$USER_ENV" +fi TARGET="${1:-.}" cd "$REPO_ROOT" diff --git a/tools/sdk_ts.sh b/tools/sdk_ts.sh index 444cc25a..e91e6dd6 100755 --- a/tools/sdk_ts.sh +++ b/tools/sdk_ts.sh @@ -10,6 +10,27 @@ if [[ -z "$REPO_ROOT" ]]; then REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" fi fi +# Redirect toolchain caches into TEST_TMPDIR so Bazel sandboxes can +# download and resolve dependencies (see tools/user_env.sh). +if [[ -n "${TEST_TMPDIR:-}" ]]; then + # user_env.sh is a source file next to this wrapper. The wrapper may be + # staged into the test's runfiles as a symlink to the source tree (which + # the sandbox can read), so resolve it from the wrapper's symlinked + # directory first; fall back to the runfiles tree and the REPO_ROOT copy + # for layouts where the wrapper's directory does not contain it. + USER_ENV="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/user_env.sh" + if [[ ! -f "$USER_ENV" && -n "${TEST_SRCDIR:-}" ]]; then + USER_ENV="$TEST_SRCDIR/$TEST_WORKSPACE/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + USER_ENV="$REPO_ROOT/tools/user_env.sh" + fi + if [[ ! -f "$USER_ENV" ]]; then + echo "ERROR: user_env.sh not found (tried $USER_ENV)" >&2 + exit 1 + fi + source "$USER_ENV" +fi DIR="$1" ACTION="${2:-test}" PKG_DIR="$REPO_ROOT/$DIR" diff --git a/tools/user_env.sh b/tools/user_env.sh new file mode 100644 index 00000000..163d68b6 --- /dev/null +++ b/tools/user_env.sh @@ -0,0 +1,99 @@ +# Shared environment for toolchain wrappers (cargo, bun, npm) running inside +# Bazel test sandboxes. +# +# Bazel sandboxes block writes to paths outside the sandbox, but cargo, bun and +# npm need writable state: cargo's registry/git checkout under ~/.cargo, bun's +# install cache under ~/.bun, and npm's cacache under ~/.npm. Without this +# every target that downloads a dependency fails with +# "Operation not permitted (os error 1)" or +# "bun is unable to write files to tempdir: PermissionDenied". +# +# bun's `bunx` additionally writes its tempdir under $HOME (not $TMPDIR), +# so HOME itself must also be redirected into the sandbox. +# +# This script redirects all three caches (and HOME for bun) into $TEST_TMPDIR +# for the duration of the test. It is a no-op outside Bazel tests (no +# TEST_TMPDIR), and any value the user or CI already set (via .bazelrc.project, +# --action_env, or the environment) always wins. +# +# Usage: +# source tools/user_env.sh +set -uo pipefail + +if [[ -z "${TEST_TMPDIR:-}" ]]; then + return 0 2>/dev/null || true +fi + +# --- HOME (bun writes its tempdir under $HOME, not $TMPDIR) ------------------ +# bunx resolves its tempdir relative to $HOME. The real $HOME lives outside +# the sandbox and writes there are denied, which surfaces as +# "bun is unable to write files to tempdir: PermissionDenied". Redirect HOME +# into the sandbox for the duration of the test. This also isolates any +# home-relative install state (~/.bun, ~/.npm, etc.) per test. +# +# We only set HOME if it was not already redirected into the sandbox by a +# higher-level wrapper, so a user who intentionally set HOME is respected. +if [[ -z "${HOME:-}" || "$HOME" != "$TEST_TMPDIR/"* ]]; then + # Keep the real home for seeding the cargo cache below and for resolving + # RUSTUP_HOME so the rustup toolchain stays reachable after HOME moves. + REAL_HOME="${HOME:-}" + if [[ -n "$REAL_HOME" && -z "${RUSTUP_HOME:-}" && -d "$REAL_HOME/.rustup" ]]; then + export RUSTUP_HOME="$REAL_HOME/.rustup" + fi + export HOME="$TEST_TMPDIR/home" + mkdir -p "$HOME" +fi + +# --- TMPDIR (bun writes its tempdir here) ----------------------------------- +export TMPDIR="${TEST_TMPDIR}" + +# --- cargo ------------------------------------------------------------------- +# A fresh, per-test CARGO_HOME. Writes (registry index, git checkouts) stay +# inside the sandbox; reads of the global toolchain (~/.cargo/bin on PATH) are +# unaffected because the toolchain itself is not under CARGO_HOME in a rustup +# layout. +if [[ -z "${CARGO_HOME:-}" ]]; then + export CARGO_HOME="$TEST_TMPDIR/cargo-home" + mkdir -p "$CARGO_HOME" + # git-fetch-with-cli lets cargo shell out to the system git instead of using + # libgit2, which is more robust under the sandbox's filesystem restrictions. + printf '[net]\ngit-fetch-with-cli = true\n' > "$CARGO_HOME/config.toml" + + # Seed the per-test registry cache from the global one when present. Reading + # the global cache is allowed; copying .crate files into the sandbox cache + # means `cargo test` resolves from cache instead of re-downloading the whole + # world on every run. + GLOBAL_CARGO_HOME="${REAL_HOME:-${HOME:-}}/.cargo" + if [[ -d "$GLOBAL_CARGO_HOME/registry/cache" ]]; then + mkdir -p "$CARGO_HOME/registry" + for shard in "$GLOBAL_CARGO_HOME/registry/cache"/*/; do + [[ -d "$shard" ]] || continue + shard_name="$(basename "$shard")" + mkdir -p "$CARGO_HOME/registry/cache/$shard_name" + cp -n "$shard"*.crate "$CARGO_HOME/registry/cache/$shard_name"/ 2>/dev/null || true + done + # Copy the index if present (read-only usage is fine). + if [[ -d "$GLOBAL_CARGO_HOME/registry/index" ]]; then + cp -Rn "$GLOBAL_CARGO_HOME/registry/index" "$CARGO_HOME/registry/index" 2>/dev/null || true + fi + fi +fi + +# Keep build artifacts inside the sandbox too (crate.sh already does this; +# belt and braces for any other wrapper that invokes cargo). +if [[ -z "${CARGO_TARGET_DIR:-}" ]]; then + export CARGO_TARGET_DIR="$TEST_TMPDIR/cargo-target" +fi + +# --- bun --------------------------------------------------------------------- +# bun's install cache (bunx binaries, bun install packages, tempdir writes). +if [[ -z "${BUN_CONFIG_CACHE_DIR:-}" ]]; then + export BUN_CONFIG_CACHE_DIR="$TEST_TMPDIR/bun-cache" + mkdir -p "$BUN_CONFIG_CACHE_DIR" +fi + +# --- npm --------------------------------------------------------------------- +if [[ -z "${npm_config_cache:-}" ]]; then + export npm_config_cache="$TEST_TMPDIR/npm-cache" + mkdir -p "$npm_config_cache" +fi