feat: new bun_build and bun_compile, extend bun_install

This commit is contained in:
eric
2026-03-15 00:11:55 +01:00
parent c446f23a35
commit a0bc998bd2
58 changed files with 1845 additions and 191 deletions

View File

@@ -71,3 +71,25 @@ sh_test(
args = ["$(location :env_parent_cwd_bin)"],
data = [":env_parent_cwd_bin"],
)
bun_binary(
name = "runtime_flag_bin",
entry_point = "flag_probe.ts",
args = ["one", "two"],
preload = ["preload.ts"],
env_files = ["runtime.env"],
)
sh_test(
name = "bun_binary_runtime_flags_test",
srcs = ["run_flag_binary.sh"],
args = ["$(location :runtime_flag_bin)"],
data = [":runtime_flag_bin"],
)
sh_test(
name = "bun_binary_runtime_flags_shape_test",
srcs = ["verify_runtime_flags_shape.sh"],
args = ["$(location :runtime_flag_bin)"],
data = [":runtime_flag_bin"],
)

View File

@@ -0,0 +1,7 @@
const state = globalThis as typeof globalThis & { __rules_bun_preloaded?: string };
console.log(JSON.stringify({
preloaded: state.__rules_bun_preloaded ?? null,
env: process.env.RUNTIME_FLAG_TEST ?? null,
argv: process.argv.slice(2),
}));

View File

@@ -0,0 +1 @@
(globalThis as typeof globalThis & { __rules_bun_preloaded?: string }).__rules_bun_preloaded = "yes";

View File

@@ -5,7 +5,7 @@ binary="$1"
expected="$2"
output="$(${binary})"
if [[ "${output}" != "${expected}" ]]; then
if [[ ${output} != "${expected}" ]]; then
echo "Unexpected output from ${binary}: ${output}" >&2
exit 1
fi

View File

@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail
binary="$1"
output="$(${binary})"
expected='{"preloaded":"yes","env":"from-env-file","argv":["one","two"]}'
if [[ ${output} != "${expected}" ]]; then
echo "Unexpected output from ${binary}: ${output}" >&2
exit 1
fi

View File

@@ -0,0 +1 @@
RUNTIME_FLAG_TEST=from-env-file

View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
binary="$1"
grep -Fq -- '--no-install' "${binary}"
grep -Fq -- '--preload' "${binary}"
grep -Fq -- '--env-file' "${binary}"

View File

@@ -11,6 +11,27 @@ bun_test(
srcs = ["failing.test.ts"],
)
bun_test(
name = "configured_suite",
srcs = ["passing.test.ts"],
preload = ["preload.ts"],
env_files = ["test.env"],
no_env_file = True,
timeout_ms = 250,
update_snapshots = True,
rerun_each = 2,
retry = 3,
concurrent = True,
randomize = True,
seed = 7,
bail = 1,
reporter = "junit",
max_concurrency = 4,
coverage = True,
coverage_reporters = ["lcov"],
test_flags = ["--only-failures"],
)
sh_test(
name = "bun_test_failing_suite_test",
srcs = ["failing_suite_shape.sh"],
@@ -38,3 +59,10 @@ sh_test(
args = ["$(location //internal:bun_test.bzl)"],
data = ["//internal:bun_test.bzl"],
)
sh_test(
name = "bun_test_configured_suite_shape_test",
srcs = ["configured_suite_shape.sh"],
args = ["$(location :configured_suite)"],
data = [":configured_suite"],
)

View File

@@ -3,6 +3,5 @@ set -euo pipefail
rule_file="$1"
grep -Fq 'set -euo pipefail' "${rule_file}"
grep -Fq 'src_args = " ".join([_shell_quote(src.short_path) for src in ctx.files.srcs])' "${rule_file}"
grep -Fq 'exec "${{bun_bin}}" --bun test {src_args} "$@"' "${rule_file}"
grep -Fq 'launcher_lines = [render_shell_array("bun_args", ["--bun", "test"])]' "${rule_file}"
grep -Fq 'exec "${bun_bin}" "${bun_args[@]}" "$@"' "${rule_file}"

View File

@@ -3,5 +3,6 @@ set -euo pipefail
rule_file="$1"
grep -Eq 'files = \[bun_bin\] \+ ctx\.files\.srcs \+ ctx\.files\.data' "${rule_file}"
grep -Fq 'extra_files = ctx.files.srcs + ctx.files.data + ctx.files.preload + ctx.files.env_files + [bun_bin]' "${rule_file}"
grep -Eq '"srcs": attr\.label_list\(' "${rule_file}"
grep -Eq '"coverage": attr\.bool\(' "${rule_file}"

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail
launcher="$1"
grep -Fq -- '--no-install' "${launcher}"
grep -Fq -- '--preload' "${launcher}"
grep -Fq -- '--env-file' "${launcher}"
grep -Fq -- '--no-env-file' "${launcher}"
grep -Fq -- '--timeout' "${launcher}"
grep -Fq -- '--update-snapshots' "${launcher}"
grep -Fq -- '--rerun-each' "${launcher}"
grep -Fq -- '--retry' "${launcher}"
grep -Fq -- '--concurrent' "${launcher}"
grep -Fq -- '--randomize' "${launcher}"
grep -Fq -- '--seed' "${launcher}"
grep -Fq -- '--bail' "${launcher}"
grep -Fq -- '--max-concurrency' "${launcher}"
grep -Fq -- '--reporter' "${launcher}"
grep -Fq -- '--reporter-outfile' "${launcher}"
grep -Fq -- '--coverage' "${launcher}"
grep -Fq -- '--coverage-dir' "${launcher}"
grep -Fq -- '--coverage-reporter' "${launcher}"

View File

@@ -3,5 +3,5 @@ set -euo pipefail
rule_file="$1"
grep -Fq 'exec "${{bun_bin}}" --bun test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" "$@"' "${rule_file}"
grep -Fq 'if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" ]]' "${rule_file}"
grep -Fq 'reporter_out="${XML_OUTPUT_FILE:-${runtime_workspace}/junit.xml}"' "${rule_file}"
grep -Fq 'bun_args+=("--reporter" "junit" "--reporter-outfile" "${reporter_out}")' "${rule_file}"

View File

@@ -0,0 +1 @@
(globalThis as typeof globalThis & { __rules_bun_test_preloaded?: boolean }).__rules_bun_test_preloaded = true;

View File

@@ -0,0 +1 @@
TEST_FROM_ENV=1

View File

@@ -1,4 +1,4 @@
load("//bun:defs.bzl", "bun_bundle")
load("//bun:defs.bzl", "bun_build", "bun_bundle", "bun_compile")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
bun_bundle(
@@ -18,6 +18,32 @@ bun_bundle(
external = ["left-pad"],
)
bun_build(
name = "site_build",
entry_points = ["site/index.html"],
data = [
"site/main.ts",
"site/styles.css",
],
splitting = True,
)
bun_build(
name = "site_build_with_meta",
entry_points = ["site/index.html"],
data = [
"site/main.ts",
"site/styles.css",
],
metafile = True,
metafile_md = True,
)
bun_compile(
name = "compiled_cli",
entry_point = "cli.ts",
)
sh_test(
name = "bundle_output_test",
srcs = ["verify_bundle.sh"],
@@ -57,3 +83,24 @@ sh_test(
"//tests/bundle_test:BUILD.bazel",
],
)
sh_test(
name = "bun_build_site_output_test",
srcs = ["verify_site_build.sh"],
args = ["$(location :site_build)"],
data = [":site_build"],
)
sh_test(
name = "bun_build_site_meta_test",
srcs = ["verify_site_build_meta.sh"],
args = ["$(locations :site_build_with_meta)"],
data = [":site_build_with_meta"],
)
sh_test(
name = "bun_compile_output_test",
srcs = ["run_compiled_binary.sh"],
args = ["$(location :compiled_cli)"],
data = [":compiled_cli"],
)

1
tests/bundle_test/cli.ts Normal file
View File

@@ -0,0 +1 @@
console.log("compiled-cli");

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
binary="$1"
output="$(${binary})"
if [[ ${output} != "compiled-cli" ]]; then
echo "Unexpected output from compiled binary ${binary}: ${output}" >&2
exit 1
fi

View File

@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rules_bun site build</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
<div id="app">rules_bun</div>
<script type="module" src="./main.ts"></script>
</body>
</html>

View File

@@ -0,0 +1 @@
document.getElementById("app")?.setAttribute("data-built", "yes");

View File

@@ -0,0 +1,4 @@
body {
background: #f5efe2;
color: #1f1b14;
}

View File

@@ -3,12 +3,12 @@ set -euo pipefail
bundle="$1"
if [[ ! -f "${bundle}" ]]; then
if [[ ! -f ${bundle} ]]; then
echo "Bundle output not found: ${bundle}" >&2
exit 1
fi
if [[ ! -s "${bundle}" ]]; then
if [[ ! -s ${bundle} ]]; then
echo "Bundle output is empty: ${bundle}" >&2
exit 1
fi

View File

@@ -4,10 +4,10 @@ set -euo pipefail
bundle="$1"
minified="$2"
bundle_size="$(wc -c < "${bundle}")"
minified_size="$(wc -c < "${minified}")"
bundle_size="$(wc -c <"${bundle}")"
minified_size="$(wc -c <"${minified}")"
if (( minified_size >= bundle_size )); then
if ((minified_size >= bundle_size)); then
echo "Expected minified bundle (${minified_size}) to be smaller than regular bundle (${bundle_size})" >&2
exit 1
fi

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
output_dir="$1"
if [[ ! -d ${output_dir} ]]; then
echo "Expected output directory: ${output_dir}" >&2
exit 1
fi
if ! find -L "${output_dir}" -type f \( -name '*.js' -o -name '*.css' \) | grep -q .; then
echo "Expected Bun build assets in ${output_dir}" >&2
exit 1
fi

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
set -euo pipefail
output_dir=""
meta_json=""
meta_md=""
for path in "$@"; do
case "${path}" in
*.meta.json) meta_json="${path}" ;;
*.meta.md) meta_md="${path}" ;;
*) output_dir="${path}" ;;
esac
done
if [[ ! -d ${output_dir} ]]; then
echo "Expected directory output, got: ${output_dir}" >&2
exit 1
fi
if [[ ! -f ${meta_json} ]]; then
echo "Expected JSON metafile output" >&2
exit 1
fi
if [[ ! -f ${meta_md} ]]; then
echo "Expected markdown metafile output" >&2
exit 1
fi

View File

@@ -121,3 +121,10 @@ sh_test(
"//conditions:default": ["@bun_linux_x64//:bun"],
}),
)
sh_test(
name = "bun_install_install_flags_shape_test",
srcs = ["install_flags_shape.sh"],
args = ["$(location //internal:bun_install.bzl)"],
data = ["//internal:bun_install.bzl"],
)

View File

@@ -5,7 +5,7 @@ bun_path="$1"
workdir="$(mktemp -d)"
trap 'rm -rf "${workdir}"' EXIT
cat > "${workdir}/package.json" <<'JSON'
cat >"${workdir}/package.json" <<'JSON'
{
"name": "clean-install-test",
"version": "1.0.0"

View File

@@ -7,3 +7,4 @@ grep -Eq 'install_args = \[str\(bun_bin\), "--bun", "install", "--frozen-lockfil
grep -Eq 'if repository_ctx\.attr\.isolated_home:' "${rule_file}"
grep -Eq 'environment[[:space:]]*=[[:space:]]*\{"HOME":[[:space:]]*str\(repository_ctx\.path\("\."\)\)\}' "${rule_file}"
grep -Eq '"isolated_home": attr\.bool\(default = True\)' "${rule_file}"
grep -Eq '"install_flags": attr\.string_list\(\)' "${rule_file}"

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail
rule_file="$1"
grep -Fq 'repository_ctx.attr.production' "${rule_file}"
grep -Fq '"--production"' "${rule_file}"
grep -Fq 'for omit in repository_ctx.attr.omit' "${rule_file}"
grep -Fq '"--omit"' "${rule_file}"
grep -Fq 'repository_ctx.attr.linker' "${rule_file}"
grep -Fq '"--linker"' "${rule_file}"
grep -Fq 'repository_ctx.attr.backend' "${rule_file}"
grep -Fq '"--backend"' "${rule_file}"
grep -Fq 'repository_ctx.attr.ignore_scripts' "${rule_file}"
grep -Fq '"--ignore-scripts"' "${rule_file}"
grep -Fq 'repository_ctx.attr.install_flags' "${rule_file}"

View File

@@ -4,6 +4,6 @@ set -euo pipefail
build_file="$1"
readme_file="$2"
[[ -f "${build_file}" ]]
[[ -f "${readme_file}" ]]
[[ -f ${build_file} ]]
[[ -f ${readme_file} ]]
grep -Eq '^package\(default_visibility = \["//visibility:public"\]\)$' "${build_file}"

View File

@@ -3,7 +3,7 @@ set -euo pipefail
bundle="$1"
if [[ ! -s "${bundle}" ]]; then
if [[ ! -s ${bundle} ]]; then
echo "Expected bundled output to exist and be non-empty: ${bundle}" >&2
exit 1
fi

View File

@@ -143,3 +143,25 @@ sh_test(
":paraglide_monorepo_app_b_build",
],
)
bun_script(
name = "workspace_filtered_script",
script = "say",
package_json = "workspace_run/package.json",
data = [
"workspace_run/packages/pkg-a/package.json",
"workspace_run/packages/pkg-a/say.ts",
"workspace_run/packages/pkg-b/package.json",
"workspace_run/packages/pkg-b/say.ts",
],
filters = ["./packages/pkg-a"],
execution_mode = "sequential",
silent = True,
)
sh_test(
name = "bun_script_workspace_filter_test",
srcs = ["run_workspace_script.sh"],
args = ["$(location :workspace_filtered_script)"],
data = [":workspace_filtered_script"],
)

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail
script_bin="$1"
output="$(${script_bin})"
if [[ ${output} != *"pkg-a"* ]]; then
echo "Expected workspace run output to include pkg-a: ${output}" >&2
exit 1
fi
if [[ ${output} == *"pkg-b"* ]]; then
echo "Workspace filter unexpectedly included pkg-b: ${output}" >&2
exit 1
fi

View File

@@ -0,0 +1,7 @@
{
"name": "workspace-run-root",
"private": true,
"workspaces": [
"packages/*"
]
}

View File

@@ -0,0 +1,6 @@
{
"name": "pkg-a",
"scripts": {
"say": "bun ./say.ts"
}
}

View File

@@ -0,0 +1 @@
console.log("pkg-a");

View File

@@ -0,0 +1,6 @@
{
"name": "pkg-b",
"scripts": {
"say": "bun ./say.ts"
}
}

View File

@@ -0,0 +1 @@
console.log("pkg-b");

View File

@@ -4,7 +4,7 @@ set -euo pipefail
bun_path="$1"
version="$(${bun_path} --version)"
if [[ ! "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
if [[ ! ${version} =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then
echo "Unexpected bun version output: ${version}" >&2
exit 1
fi