Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_bmatcuk_doublestar_v4",
"com_github_tetratelabs_wazero",
"org_golang_x_exp",
"org_golang_x_sys",
)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.24.6
require (
github.com/bazelbuild/rules_go v0.55.0
github.com/bmatcuk/doublestar/v4 v4.7.1
github.com/tetratelabs/wazero v1.10.1
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
golang.org/x/sys v0.30.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/bazelbuild/rules_go v0.55.0 h1:S8X/b/Oygw/Dtv7NuyW7ht0QwdynMEdXQqYigX
github.com/bazelbuild/rules_go v0.55.0/go.mod h1:T90Gpyq4HDFlsrvtQa2CBdHNJ2P4rAu/uUTmQbanzf0=
github.com/bmatcuk/doublestar/v4 v4.7.1 h1:fdDeAqgT47acgwd9bd9HxJRDmc9UAmPpc+2m0CXv75Q=
github.com/bmatcuk/doublestar/v4 v4.7.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/tetratelabs/wazero v1.10.1 h1:2DugeJf6VVk58KTPszlNfeeN8AhhpwcZqkJj2wwFuH8=
github.com/tetratelabs/wazero v1.10.1/go.mod h1:DRm5twOQ5Gr1AoEdSi0CLjDQF1J9ZAuyqFIjl1KKfQU=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
Expand Down
8 changes: 7 additions & 1 deletion lib/private/run_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Possible fixes:
ctx.actions.run(
outputs = outputs,
inputs = inputs,
executable = ctx.executable.tool,
executable = ctx.executable.tool_launcher,
arguments = [args],
Comment on lines 65 to 69

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep running the user-specified tool

The run_binary action now executes only ctx.executable.tool_launcher and never references ctx.executable.tool. Because the tool attribute is no longer added to the action’s inputs or invoked, every run_binary target will invoke the launcher binary regardless of what tool the rule declares, and the declared tool will not even be built when the action runs. This completely regresses the behavior of run_binary and breaks all existing usages of the rule.

Useful? React with 👍 / 👎.

resource_set = resource_set(ctx.attr),
mnemonic = ctx.attr.mnemonic if ctx.attr.mnemonic else None,
Expand All @@ -88,6 +88,12 @@ _run_binary = rule(
mandatory = True,
cfg = "exec",
),
"tool_launcher": attr.label(
default = Label("//tools/run_binary"),
executable = True,
allow_files = True,
cfg = "exec",
),
"env": attr.string_dict(),
"srcs": attr.label_list(
allow_files = True,
Expand Down
33 changes: 33 additions & 0 deletions path/to/package/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@bazel_lib//lib:run_binary.bzl", "run_binary")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")

sh_binary(
name = "tool",
srcs = ["tool.sh"],
visibility = ["//visibility:public"],
)

go_binary(
name = "wasm_tool",
srcs = ["tool.go"],
goarch = "wasm",
goos = "wasip1",
# build as a reactor
linkmode = "c-shared",
visibility = ["//visibility:public"],
)

run_binary(
name = "package",
outs = ["output.txt"],
args = ["$(location output.txt)"],
tool = ":fake_tool",
)

go_library(
name = "package_lib",
srcs = ["tool.go"],
importpath = "github.com/bazel-contrib/bazel-lib/path/to/package",
visibility = ["//visibility:private"],
)
23 changes: 23 additions & 0 deletions path/to/package/tool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This is a WASM tool that prints "Hello from a WASM tool!" to a file path passed in as an argument.
// It is compiled to WASM using Bazel.
package main

import (
"fmt"
"os"
)

//go:wasmexport spawn
func spawn() {
f, err := os.Create("bazel-out/darwin_arm64-fastbuild/bin/path/to/package/output.txt")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create file: %v\n", err)
os.Exit(1)
}
defer f.Close()
_, err = f.Write([]byte("Hello from a WASM tool!\n"))
}

func main() {
panic("Should be called by WASM runtime")
}
5 changes: 5 additions & 0 deletions path/to/package/tool.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

echo "Hello, World!" > "$1"
19 changes: 19 additions & 0 deletions tools/run_binary/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "run_binary_lib",
srcs = ["main.go"],
importpath = "github.com/bazel-contrib/bazel-lib/tools/run_binary",
visibility = ["//visibility:private"],
deps = [
"@com_github_tetratelabs_wazero//:wazero",
"@com_github_tetratelabs_wazero//imports/wasi_snapshot_preview1",
],
)

go_binary(
name = "run_binary",
embed = [":run_binary_lib"],
pure = "on",
visibility = ["//visibility:public"],
)
59 changes: 59 additions & 0 deletions tools/run_binary/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// This is a wrapper/host process for running tools under a Bazel action.
// The environment it runs in is specified by Bazel:
// - the working directory is the execution root
// - args/env are dropped since the tool is not run directly by the user under Bazel run
//
// It also supports WASM tools. It hosts a WASM runtime and loads the WASM module into it.
// This makes it easier to ship tools from Bazel rules without having to build lots of different target-triple pre-compiled versions.
//
// In the future, this tool can also strace the tool we run to collect telemetry data.
// That would be great for:
// - The tool wrote to a different output path than expected, where did it go?
// - What's making the tool slow? Maybe we can get eBPF timing data under Linux at least - and stitch the profiling into the Bazel profile.
//
// The tool we run can do things like:
// - write output files and output directories under bazel-bin/path/to/package
package main

import (
"context"
"fmt"
"os"

"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

func main() {
wasmCode, err := os.ReadFile("/Users/alexeagle/Projects/bazel-lib/bazel-bin/path/to/package/wasm_tool_/wasm_tool.wasm")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read wasm code: %v\n", err)
os.Exit(1)
}

// Choose the context to use for function calls.
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx)

wasi_snapshot_preview1.MustInstantiate(ctx, r)

module, err := r.Instantiate(ctx, wasmCode)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to instantiate module: %v\n", err)
os.Exit(1)
}

// Call the exported spawn function with the output path
// Go's wasmexport handles string parameter marshalling automatically
spawn := module.ExportedFunction("spawn")
if spawn == nil {
fmt.Fprintf(os.Stderr, "Failed to get spawn function\n")
os.Exit(1)
}
_, err = spawn.Call(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to call spawn: %v\n", err)
os.Exit(1)
}
}
Loading