"""Rule for running package.json scripts with Bun.""" load("//internal:bun_command.bzl", "append_flag", "append_flag_value", "append_flag_values", "append_install_mode", "append_raw_flags") load("//internal:runtime_launcher.bzl", "declare_runtime_wrapper", "runfiles_path", "runtime_launcher_attrs", "write_launcher_spec") load("//internal:workspace.bzl", "create_bun_workspace_info", "workspace_runfiles") def _bun_script_impl(ctx): toolchain = ctx.toolchains["//bun:toolchain_type"] bun_bin = toolchain.bun.bun_bin package_json = ctx.file.package_json workspace_info = create_bun_workspace_info( ctx, extra_files = ctx.files.data + ctx.files.preload + ctx.files.env_files + [bun_bin], package_dir_hint = package_json.dirname or ".", package_json = package_json, primary_file = package_json, ) argv = ["--bun", "run"] append_install_mode(argv, ctx.attr.install_mode) append_flag(argv, "--no-env-file", ctx.attr.no_env_file) append_flag(argv, "--smol", ctx.attr.smol) append_flag_values(argv, "--conditions", ctx.attr.conditions) append_flag(argv, "--workspaces", ctx.attr.workspaces) append_flag_values(argv, "--filter", ctx.attr.filters) if ctx.attr.execution_mode == "parallel": append_flag(argv, "--parallel", True) elif ctx.attr.execution_mode == "sequential": append_flag(argv, "--sequential", True) append_flag(argv, "--no-exit-on-error", ctx.attr.no_exit_on_error) append_flag_value(argv, "--shell", ctx.attr.shell) append_flag(argv, "--silent", ctx.attr.silent) append_raw_flags(argv, ctx.attr.run_flags) spec_file = write_launcher_spec(ctx, { "version": 1, "kind": "bun_run", "bun_short_path": runfiles_path(bun_bin), "primary_source_short_path": "", "package_json_short_path": runfiles_path(package_json), "install_metadata_short_path": runfiles_path(workspace_info.install_metadata_file) if workspace_info.install_metadata_file else "", "install_repo_runfiles_path": workspace_info.install_repo_runfiles_path, "node_modules_roots": workspace_info.node_modules_roots, "package_dir_hint": package_json.dirname or ".", "working_dir_mode": ctx.attr.working_dir, "inherit_host_path": ctx.attr.inherit_host_path, "argv": argv, "args": [ctx.attr.script] + ctx.attr.args, "passthrough_args": True, "tool_short_path": "", "restart_on": [], "watch_mode": "", "reporter": "", "coverage": False, "coverage_reporters": [], "preload_short_paths": [runfiles_path(file) for file in ctx.files.preload], "env_file_short_paths": [runfiles_path(file) for file in ctx.files.env_files], "test_short_paths": [], }) launcher = declare_runtime_wrapper(ctx, bun_bin, spec_file) return [ workspace_info, DefaultInfo( executable = launcher.executable, runfiles = workspace_runfiles( ctx, workspace_info, direct_files = [launcher.executable, launcher.runner, spec_file], ), ), ] _BUN_SCRIPT_ATTRS = runtime_launcher_attrs() _BUN_SCRIPT_ATTRS.update({ "script": attr.string( mandatory = True, doc = "Name of the `package.json` script to execute via `bun run