-
-
Notifications
You must be signed in to change notification settings - Fork 112
WIP: prototype of wasm host as run_binary wrapper #1210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexeagle
wants to merge
2
commits into
main
Choose a base branch
from
wasm_launcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The run_binary action now executes only
ctx.executable.tool_launcherand never referencesctx.executable.tool. Because thetoolattribute is no longer added to the action’s inputs or invoked, everyrun_binarytarget 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 ofrun_binaryand breaks all existing usages of the rule.Useful? React with 👍 / 👎.