feat: add option for process cwd (.env support)
This commit is contained in:
4
MODULE.bazel.lock
generated
4
MODULE.bazel.lock
generated
@@ -189,8 +189,8 @@
|
|||||||
"moduleExtensions": {
|
"moduleExtensions": {
|
||||||
"//bun:extensions.bzl%bun": {
|
"//bun:extensions.bzl%bun": {
|
||||||
"general": {
|
"general": {
|
||||||
"bzlTransitiveDigest": "Q0uQOwFAgAU+etePCZ4TUDO+adLX7Z0EmRLaEsKgncw=",
|
"bzlTransitiveDigest": "oLR98WtKDCc+zh7Tvu9jtakNg8q/T1IPE38QR1FEQtI=",
|
||||||
"usagesDigest": "UC4zk8kEwWRiDG5FVQOCFysXcrZ757Jehf3sZgG893w=",
|
"usagesDigest": "9s+/cJR+wTifjBrio71E10Knd41B08/GmW4I9zJmU64=",
|
||||||
"recordedInputs": [
|
"recordedInputs": [
|
||||||
"REPO_MAPPING:,bazel_tools bazel_tools"
|
"REPO_MAPPING:,bazel_tools bazel_tools"
|
||||||
],
|
],
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -103,6 +103,8 @@ Run one of your bun-backed targets, for example:
|
|||||||
bazel test //path/to:your_bun_test
|
bazel test //path/to:your_bun_test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
All `rules_bun` rule-driven Bun invocations pass `--bun`.
|
||||||
|
|
||||||
## Development mode (`bun_dev`)
|
## Development mode (`bun_dev`)
|
||||||
|
|
||||||
Use `bun_dev` for long-running local development with Bun watch mode.
|
Use `bun_dev` for long-running local development with Bun watch mode.
|
||||||
@@ -113,6 +115,8 @@ load("@rules_bun//bun:defs.bzl", "bun_dev")
|
|||||||
bun_dev(
|
bun_dev(
|
||||||
name = "web_dev",
|
name = "web_dev",
|
||||||
entry_point = "src/main.ts",
|
entry_point = "src/main.ts",
|
||||||
|
# Optional: run from the entry point directory so Bun auto-loads colocated .env files.
|
||||||
|
# working_dir = "entry_point",
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -127,6 +131,16 @@ bazel run //path/to:web_dev
|
|||||||
- `watch_mode = "watch"` (default) for `bun --watch`
|
- `watch_mode = "watch"` (default) for `bun --watch`
|
||||||
- `watch_mode = "hot"` for `bun --hot`
|
- `watch_mode = "hot"` for `bun --hot`
|
||||||
- `restart_on = [...]` to force full process restarts when specific files change
|
- `restart_on = [...]` to force full process restarts when specific files change
|
||||||
|
- `working_dir = "workspace" | "entry_point"` (default: `workspace`)
|
||||||
|
|
||||||
|
## Runtime working directory (`bun_binary`, `bun_dev`)
|
||||||
|
|
||||||
|
`bun_binary` and `bun_dev` support `working_dir`:
|
||||||
|
|
||||||
|
- `"workspace"` (default): runs from the Bazel runfiles workspace root.
|
||||||
|
- `"entry_point"`: runs from the entry point file's directory.
|
||||||
|
|
||||||
|
Use `"entry_point"` when Bun should resolve local files such as colocated `.env` files relative to the program directory.
|
||||||
|
|
||||||
### Hybrid Go + Bun + protobuf workflow
|
### Hybrid Go + Bun + protobuf workflow
|
||||||
|
|
||||||
|
|||||||
@@ -16,3 +16,4 @@ js_library = _js_library
|
|||||||
ts_library = _ts_library
|
ts_library = _ts_library
|
||||||
BunToolchainInfo = _BunToolchainInfo
|
BunToolchainInfo = _BunToolchainInfo
|
||||||
bun_toolchain = _bun_toolchain
|
bun_toolchain = _bun_toolchain
|
||||||
|
|
||||||
@@ -17,10 +17,18 @@ runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
|
|||||||
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
|
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
|
||||||
entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
|
entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
|
||||||
|
|
||||||
exec "${{bun_bin}}" run "${{entry_point}}" "$@"
|
working_dir="{working_dir}"
|
||||||
|
if [[ "${{working_dir}}" == "entry_point" ]]; then
|
||||||
|
cd "$(dirname "${{entry_point}}")"
|
||||||
|
else
|
||||||
|
cd "${{runfiles_dir}}/_main"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec "${{bun_bin}}" --bun run "${{entry_point}}" "$@"
|
||||||
""".format(
|
""".format(
|
||||||
bun_short_path = bun_bin.short_path,
|
bun_short_path = bun_bin.short_path,
|
||||||
entry_short_path = entry_point.short_path,
|
entry_short_path = entry_point.short_path,
|
||||||
|
working_dir = ctx.attr.working_dir,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,6 +58,10 @@ bun_binary = rule(
|
|||||||
),
|
),
|
||||||
"node_modules": attr.label(),
|
"node_modules": attr.label(),
|
||||||
"data": attr.label_list(allow_files = True),
|
"data": attr.label_list(allow_files = True),
|
||||||
|
"working_dir": attr.string(
|
||||||
|
default = "workspace",
|
||||||
|
values = ["workspace", "entry_point"],
|
||||||
|
),
|
||||||
},
|
},
|
||||||
executable = True,
|
executable = True,
|
||||||
toolchains = ["//bun:toolchain_type"],
|
toolchains = ["//bun:toolchain_type"],
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ def _bun_bundle_impl(ctx):
|
|||||||
outputs.append(output)
|
outputs.append(output)
|
||||||
|
|
||||||
args = ctx.actions.args()
|
args = ctx.actions.args()
|
||||||
|
args.add("--bun")
|
||||||
args.add("build")
|
args.add("build")
|
||||||
args.add(entry.path)
|
args.add(entry.path)
|
||||||
args.add("--outfile")
|
args.add("--outfile")
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"""Rule for running JS/TS scripts with Bun in watch mode for development."""
|
"""Rule for running JS/TS scripts with Bun in watch mode for development."""
|
||||||
|
|
||||||
|
|
||||||
def _bun_dev_impl(ctx):
|
def _bun_dev_impl(ctx):
|
||||||
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
toolchain = ctx.toolchains["//bun:toolchain_type"]
|
||||||
bun_bin = toolchain.bun.bun_bin
|
bun_bin = toolchain.bun.bun_bin
|
||||||
@@ -18,7 +17,13 @@ set -euo pipefail
|
|||||||
runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
|
runfiles_dir="${{RUNFILES_DIR:-$0.runfiles}}"
|
||||||
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
|
bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
|
||||||
entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
|
entry_point="${{runfiles_dir}}/_main/{entry_short_path}"
|
||||||
cd "${{runfiles_dir}}/_main"
|
|
||||||
|
working_dir="{working_dir}"
|
||||||
|
if [[ "${{working_dir}}" == "entry_point" ]]; then
|
||||||
|
cd "$(dirname "${{entry_point}}")"
|
||||||
|
else
|
||||||
|
cd "${{runfiles_dir}}/_main"
|
||||||
|
fi
|
||||||
|
|
||||||
watch_mode="{watch_mode}"
|
watch_mode="{watch_mode}"
|
||||||
if [[ "${{watch_mode}}" == "hot" ]]; then
|
if [[ "${{watch_mode}}" == "hot" ]]; then
|
||||||
@@ -28,7 +33,7 @@ else
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
run_dev() {{
|
run_dev() {{
|
||||||
exec "${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@"
|
exec "${{bun_bin}}" --bun "${{dev_flag}}" run "${{entry_point}}" "$@"
|
||||||
}}
|
}}
|
||||||
|
|
||||||
if [[ {restart_count} -eq 0 ]]; then
|
if [[ {restart_count} -eq 0 ]]; then
|
||||||
@@ -50,8 +55,9 @@ file_mtime() {{
|
|||||||
|
|
||||||
declare -A mtimes
|
declare -A mtimes
|
||||||
for rel in "${{restart_paths[@]}}"; do
|
for rel in "${{restart_paths[@]}}"; do
|
||||||
if [[ -e "${{rel}}" ]]; then
|
path="${{runfiles_dir}}/_main/${{rel}}"
|
||||||
mtimes["${{rel}}"]="$(file_mtime "${{rel}}")"
|
if [[ -e "${{path}}" ]]; then
|
||||||
|
mtimes["${{rel}}"]="$(file_mtime "${{path}}")"
|
||||||
else
|
else
|
||||||
mtimes["${{rel}}"]="missing"
|
mtimes["${{rel}}"]="missing"
|
||||||
fi
|
fi
|
||||||
@@ -63,7 +69,7 @@ restart_child() {{
|
|||||||
kill "${{child_pid}}"
|
kill "${{child_pid}}"
|
||||||
wait "${{child_pid}}" || true
|
wait "${{child_pid}}" || true
|
||||||
fi
|
fi
|
||||||
"${{bun_bin}}" "${{dev_flag}}" run "${{entry_point}}" "$@" &
|
"${{bun_bin}}" --bun "${{dev_flag}}" run "${{entry_point}}" "$@" &
|
||||||
child_pid=$!
|
child_pid=$!
|
||||||
}}
|
}}
|
||||||
|
|
||||||
@@ -82,8 +88,9 @@ while true; do
|
|||||||
sleep 1
|
sleep 1
|
||||||
changed=0
|
changed=0
|
||||||
for rel in "${{restart_paths[@]}}"; do
|
for rel in "${{restart_paths[@]}}"; do
|
||||||
if [[ -e "${{rel}}" ]]; then
|
path="${{runfiles_dir}}/_main/${{rel}}"
|
||||||
current="$(file_mtime "${{rel}}")"
|
if [[ -e "${{path}}" ]]; then
|
||||||
|
current="$(file_mtime "${{path}}")"
|
||||||
else
|
else
|
||||||
current="missing"
|
current="missing"
|
||||||
fi
|
fi
|
||||||
@@ -100,6 +107,7 @@ done
|
|||||||
bun_short_path = bun_bin.short_path,
|
bun_short_path = bun_bin.short_path,
|
||||||
entry_short_path = entry_point.short_path,
|
entry_short_path = entry_point.short_path,
|
||||||
watch_mode = ctx.attr.watch_mode,
|
watch_mode = ctx.attr.watch_mode,
|
||||||
|
working_dir = ctx.attr.working_dir,
|
||||||
restart_count = len(ctx.files.restart_on),
|
restart_count = len(ctx.files.restart_on),
|
||||||
restart_watch_paths = restart_watch_paths,
|
restart_watch_paths = restart_watch_paths,
|
||||||
),
|
),
|
||||||
@@ -121,7 +129,6 @@ done
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
bun_dev = rule(
|
bun_dev = rule(
|
||||||
implementation = _bun_dev_impl,
|
implementation = _bun_dev_impl,
|
||||||
attrs = {
|
attrs = {
|
||||||
@@ -136,6 +143,10 @@ bun_dev = rule(
|
|||||||
"restart_on": attr.label_list(allow_files = True),
|
"restart_on": attr.label_list(allow_files = True),
|
||||||
"node_modules": attr.label(),
|
"node_modules": attr.label(),
|
||||||
"data": attr.label_list(allow_files = True),
|
"data": attr.label_list(allow_files = True),
|
||||||
|
"working_dir": attr.string(
|
||||||
|
default = "workspace",
|
||||||
|
values = ["workspace", "entry_point"],
|
||||||
|
),
|
||||||
},
|
},
|
||||||
executable = True,
|
executable = True,
|
||||||
toolchains = ["//bun:toolchain_type"],
|
toolchains = ["//bun:toolchain_type"],
|
||||||
|
|||||||
@@ -36,14 +36,14 @@ def _bun_install_repository_impl(repository_ctx):
|
|||||||
repository_ctx.symlink(bun_lockfile, "bun.lockb")
|
repository_ctx.symlink(bun_lockfile, "bun.lockb")
|
||||||
|
|
||||||
result = repository_ctx.execute(
|
result = repository_ctx.execute(
|
||||||
[str(bun_bin), "install", "--frozen-lockfile", "--no-progress"],
|
[str(bun_bin), "--bun", "install", "--frozen-lockfile", "--no-progress"],
|
||||||
timeout = 600,
|
timeout = 600,
|
||||||
quiet = False,
|
quiet = False,
|
||||||
environment = {"HOME": str(repository_ctx.path("."))},
|
environment = {"HOME": str(repository_ctx.path("."))},
|
||||||
)
|
)
|
||||||
|
|
||||||
if result.return_code:
|
if result.return_code:
|
||||||
fail("""bun_install failed running `bun install --frozen-lockfile`.
|
fail("""bun_install failed running `bun --bun install --frozen-lockfile`.
|
||||||
stdout:
|
stdout:
|
||||||
{}
|
{}
|
||||||
stderr:
|
stderr:
|
||||||
|
|||||||
@@ -24,15 +24,15 @@ bun_bin="${{runfiles_dir}}/_main/{bun_short_path}"
|
|||||||
cd "${{runfiles_dir}}/_main"
|
cd "${{runfiles_dir}}/_main"
|
||||||
|
|
||||||
if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" && -n "${{COVERAGE_DIR:-}}" ]]; then
|
if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" && -n "${{COVERAGE_DIR:-}}" ]]; then
|
||||||
exec "${{bun_bin}}" test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" --coverage "$@"
|
exec "${{bun_bin}}" --bun test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" --coverage "$@"
|
||||||
fi
|
fi
|
||||||
if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" ]]; then
|
if [[ -n "${{TESTBRIDGE_TEST_ONLY:-}}" ]]; then
|
||||||
exec "${{bun_bin}}" test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" "$@"
|
exec "${{bun_bin}}" --bun test {src_args} --test-name-pattern "${{TESTBRIDGE_TEST_ONLY}}" "$@"
|
||||||
fi
|
fi
|
||||||
if [[ -n "${{COVERAGE_DIR:-}}" ]]; then
|
if [[ -n "${{COVERAGE_DIR:-}}" ]]; then
|
||||||
exec "${{bun_bin}}" test {src_args} --coverage "$@"
|
exec "${{bun_bin}}" --bun test {src_args} --coverage "$@"
|
||||||
fi
|
fi
|
||||||
exec "${{bun_bin}}" test {src_args} "$@"
|
exec "${{bun_bin}}" --bun test {src_args} "$@"
|
||||||
""".format(
|
""".format(
|
||||||
bun_short_path = bun_bin.short_path,
|
bun_short_path = bun_bin.short_path,
|
||||||
src_args = src_args,
|
src_args = src_args,
|
||||||
|
|||||||
@@ -43,3 +43,17 @@ sh_test(
|
|||||||
"//tests/binary_test:BUILD.bazel",
|
"//tests/binary_test:BUILD.bazel",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
bun_binary(
|
||||||
|
name = "env_cwd_bin",
|
||||||
|
entry_point = "env.ts",
|
||||||
|
data = [".env"],
|
||||||
|
working_dir = "entry_point",
|
||||||
|
)
|
||||||
|
|
||||||
|
sh_test(
|
||||||
|
name = "bun_binary_env_cwd_test",
|
||||||
|
srcs = ["run_env_binary.sh"],
|
||||||
|
args = ["$(location :env_cwd_bin)"],
|
||||||
|
data = [":env_cwd_bin"],
|
||||||
|
)
|
||||||
|
|||||||
2
tests/binary_test/env.ts
Normal file
2
tests/binary_test/env.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
const value = process.env.BUN_ENV_CWD_TEST ?? "missing";
|
||||||
|
console.log(value);
|
||||||
10
tests/binary_test/run_env_binary.sh
Executable file
10
tests/binary_test/run_env_binary.sh
Executable file
@@ -0,0 +1,10 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
binary="$1"
|
||||||
|
output="$(${binary})"
|
||||||
|
|
||||||
|
if [[ ${output} != "from-dotenv" ]]; then
|
||||||
|
echo "Expected .env value from entry-point directory, got: ${output}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user