From d04f489b0e8e6c0ad90764ddf48bc9a8987a810c Mon Sep 17 00:00:00 2001 From: eric Date: Sat, 7 Mar 2026 00:00:00 +0100 Subject: [PATCH] fix: bun install symlinks --- .pre-commit-config.yaml | 2 +- MODULE.bazel.lock | 14 ++++++++--- examples/vite_monorepo/BUILD.bazel | 10 ++++++++ flake.nix | 3 +-- internal/bun_script.bzl | 35 ++++++++++++++++++++++++-- tests/install_test/workspace_parity.sh | 12 ++++----- 6 files changed, 61 insertions(+), 15 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f5b4931..26e1090 120000 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1 +1 @@ -/nix/store/5lbgsnb4r2d7gdjykl2hiqny0ya64baa-pre-commit-config.json \ No newline at end of file +/nix/store/fj7p8m3p4s7cx9kqvx2h0glk2ygknmvl-pre-commit-config.json \ No newline at end of file diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 7d3e728..d9cea6e 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -253,7 +253,7 @@ }, "//bun:extensions.bzl%bun_install": { "general": { - "bzlTransitiveDigest": "xKWhaSNvthtzN2J8BNWSLHTqQ5ZJHwyCTQAdUQyOFXE=", + "bzlTransitiveDigest": "64B4fTkEHdAlieIOkE/Wi2M/R9lMNZhFxeI1eXEFHRs=", "usagesDigest": "d+DGTyl4FpB6Ygb/R/V5knxm9bGYZKO223wMX1Q6R6w=", "recordedInputs": [ "REPO_MAPPING:,bazel_tools bazel_tools" @@ -263,21 +263,27 @@ "repoRuleId": "@@//internal:bun_install.bzl%bun_install_repository", "attributes": { "package_json": "@@//tests/script_test:vite_app/package.json", - "bun_lockfile": "@@//tests/script_test:vite_app/bun.lock" + "bun_lockfile": "@@//tests/script_test:vite_app/bun.lock", + "install_inputs": [], + "isolated_home": true } }, "script_test_vite_monorepo_node_modules": { "repoRuleId": "@@//internal:bun_install.bzl%bun_install_repository", "attributes": { "package_json": "@@//tests/script_test:vite_monorepo/package.json", - "bun_lockfile": "@@//tests/script_test:vite_monorepo/bun.lock" + "bun_lockfile": "@@//tests/script_test:vite_monorepo/bun.lock", + "install_inputs": [], + "isolated_home": true } }, "examples_vite_monorepo_node_modules": { "repoRuleId": "@@//internal:bun_install.bzl%bun_install_repository", "attributes": { "package_json": "@@//examples/vite_monorepo:package.json", - "bun_lockfile": "@@//examples/vite_monorepo:bun.lock" + "bun_lockfile": "@@//examples/vite_monorepo:bun.lock", + "install_inputs": [], + "isolated_home": true } } } diff --git a/examples/vite_monorepo/BUILD.bazel b/examples/vite_monorepo/BUILD.bazel index 98f25b5..5d27d22 100644 --- a/examples/vite_monorepo/BUILD.bazel +++ b/examples/vite_monorepo/BUILD.bazel @@ -4,6 +4,16 @@ load("@rules_bun//bun:defs.bzl", "bun_script") load("@rules_multirun//:defs.bzl", "multirun") load("@rules_shell//shell:sh_test.bzl", "sh_test") +package(default_visibility = ["//visibility:public"]) + +exports_files([ + "README.md", + "package.json", + "bun.lock", + "apps/app-a/package.json", + "apps/app-b/package.json", +]) + multirun( name = "dev", commands = [ diff --git a/flake.nix b/flake.nix index b96176d..c3e7c65 100644 --- a/flake.nix +++ b/flake.nix @@ -66,8 +66,7 @@ tests = { enable = true; entry = '' - bazel test //tests/... --test_output=errors - tests/install_test/workspace_parity.sh "$(command -v bun)" + ${pkgs.bash}/bin/bash -ec 'bazel test //tests/... --test_output=errors && tests/install_test/workspace_parity.sh "$(command -v bun)"' ''; pass_filenames = false; stages = [ "pre-push" ]; diff --git a/internal/bun_script.bzl b/internal/bun_script.bzl index 140dd6d..fa1d891 100644 --- a/internal/bun_script.bzl +++ b/internal/bun_script.bzl @@ -92,9 +92,40 @@ find_node_modules() {{ return 1 }} -if [[ -n "${{install_repo_root}}" && -d "${{install_repo_root}}/${{package_rel_dir}}/node_modules" ]]; then +find_install_repo_node_modules() {{ + local repo_root="$1" + local rel_dir="$2" + local candidate="${{rel_dir}}" + + while [[ -n "${{candidate}}" ]]; do + if [[ -d "${{repo_root}}/${{candidate}}/node_modules" ]]; then + echo "${{repo_root}}/${{candidate}}/node_modules" + return 0 + fi + + if [[ "${{candidate}}" != */* ]]; then + break + fi + + candidate="${{candidate#*/}}" + done + + if [[ -d "${{repo_root}}/node_modules" ]]; then + echo "${{repo_root}}/node_modules" + return 0 + fi + + return 1 +}} + +resolved_install_node_modules="" +if [[ -n "${{install_repo_root}}" ]]; then + resolved_install_node_modules="$(find_install_repo_node_modules "${{install_repo_root}}" "${{package_rel_dir}}" || true)" +fi + +if [[ -n "${{resolved_install_node_modules}}" ]]; then rm -rf "${{runtime_package_dir}}/node_modules" - ln -s "${{install_repo_root}}/${{package_rel_dir}}/node_modules" "${{runtime_package_dir}}/node_modules" + ln -s "${{resolved_install_node_modules}}" "${{runtime_package_dir}}/node_modules" else resolved_node_modules="$(find_node_modules "${{runtime_package_dir}}" "${{runtime_workspace}}" || true)" if [[ -n "${{resolved_node_modules}}" && "${{resolved_node_modules}}" != "${{runtime_package_dir}}/node_modules" ]]; then diff --git a/tests/install_test/workspace_parity.sh b/tests/install_test/workspace_parity.sh index 76498bc..41b35f2 100755 --- a/tests/install_test/workspace_parity.sh +++ b/tests/install_test/workspace_parity.sh @@ -403,17 +403,17 @@ if ! diff -u "${plain_manifest}" "${bazel_manifest}"; then exit 1 fi -rm -rf "${plain_dir}/packages/web/dist" -"${bun_path}" run --cwd "${plain_dir}/packages/web" build -- --emptyOutDir >/dev/null +plain_dist_dir="${workdir}/plain-dist" +bazel_dist_dir="${workdir}/bazel-dist" + +rm -rf "${plain_dist_dir}" "${bazel_dist_dir}" +"${bun_path}" run --cwd "${plain_dir}/packages/web" build -- --emptyOutDir --outDir "${plain_dist_dir}" >/dev/null ( cd "${bazel_dir}" - bazel run //:web_build -- --emptyOutDir >/dev/null + bazel run //:web_build -- --emptyOutDir --outDir "${bazel_dist_dir}" >/dev/null ) -plain_dist_dir="${plain_dir}/packages/web/dist" -bazel_dist_dir="${bazel_dir}/bazel-bin/web_build.runfiles/_main/packages/web/dist" - if [[ ! -d ${plain_dist_dir} ]]; then echo "Plain Bun Vite build did not produce output" >&2 exit 1