Skip to content

Commit a411917

Browse files
committed
internal/wasmtools, all: refinements by @ydnar
.github/workflows: remove some (now) unneeded steps .gitignore: condense internal/wasmtools/.gitignore internal/wasmtools: use single quotes, add Rust edition Makefile: update order of wasm-tools.wasm.\* targets) internal/wasmtools: use gzipped wasm-tools.wasm.gz with sync.Once internal/wasmtools: rebuild wasm-tools.wasm.gz CHANGELOG: wordsmith the wasm-tools and Wazero update internal/wasmtools, wit: remove optional name arg Removing this did not seem to affect tests. internal/wasmtools: leave timeout to the caller Makefile: reorder gzip targets internal/wasmtools: oops Makefile: next try wit: remove check for wasm-tools in PATH internal/wasmtools, wit: swap key and value types for fsMap Not all fs.FS are hashable, but all strings are, so use map[string]fs.FS instead. wit/bindgen: use internal/wasmtools to run wasm-tools in WebAssembly internal/wasmtools: use wazero.CompilationCache This speeds up wit/bindgen tests 10x internal/wasmtools: fix TinyGo impl internal/wasmtools: remove Runner interface This wasn’t used anywhere, so removing. internal/wasmtools: change Run() to accept optional stdout and stderr internal/wasmtools: move args to varadic trailing arg in Run wit: (*testing.common).Errorf does not support error-wrapping directive %w internal/wasmtools, wit/bindgen: additional cleanups for TinyGo and WASI .github/workflows/test: TinyGo needs wasm-tools for -target=wasip2 .github/dependabot: add internal/wasmtools for Cargo updates cmd/wit-bindgen-go, internal/{module,witcli}: extract module helper into new package internal/wasmtools: try to use disk-based compilation cache in TMPDIR internal/cmd/wasm-tools: add Wazero-based wasm-tools executable This removes the requirement for wasmtime in the Makefile Makefile: use Wazero-based internal/cmd/wasm-tools executable instead of Wasmtime .github/workflows/test: ignore all Markdown files internal/wasmtools: rebuild wasm-tools.wasm.gz internal/{cmd/wasm-tools,wasmtools}, wit: default mapping of ./ and / directories .github/workflows: install wasm wrapped in Go version of wasm-tools all: revert 26c1e72; Wazero directory handling tricky to manage Wazero flattens ./ into '', which is treated as /, overriding the / mapping. Makefile: just use wasm-tools It’s faster, and works with arbitrary paths.
1 parent 6e9a74c commit a411917

22 files changed

+275
-214
lines changed

.github/dependabot.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ updates:
2323
schedule:
2424
interval: weekly
2525
open-pull-requests-limit: 10
26+
27+
- package-ecosystem: cargo
28+
directory: "/internal/wasmtools"
29+
schedule:
30+
interval: weekly
31+
open-pull-requests-limit: 10

.github/workflows/release.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ jobs:
3232
with:
3333
go-version-file: go.mod
3434

35-
- name: Set up wams-tools.wasm
36-
run: make internal/wasmtools/wasm-tools.wasm
37-
3835
- name: Run Go tests
3936
run: go test ${{ env.go-modules }}
4037

.github/workflows/test.yaml

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
paths-ignore:
99
- '.prettier*'
1010
- '.vscode/**'
11-
- '*.md'
11+
- '**/*.md'
1212
- 'docs/**'
1313
- 'LICENSE'
1414

@@ -34,13 +34,8 @@ jobs:
3434
with:
3535
go-version-file: go.mod
3636

37-
- name: Set up wams-tools.wasm
38-
run: make internal/wasmtools/wasm-tools.wasm
39-
4037
- name: Vet Go code
41-
run: |
42-
make build
43-
go vet ${{ env.go-modules }} ./tests/...
38+
run: go vet ${{ env.go-modules }} ./tests/...
4439

4540
# Test with Go
4641
test-go:
@@ -61,14 +56,6 @@ jobs:
6156
with:
6257
go-version: ${{ matrix.go-version }}
6358

64-
- name: Set up wasm-tools
65-
uses: bytecodealliance/actions/wasm-tools/setup@v1
66-
with:
67-
version: ${{ env.wasm-tools-version }}
68-
69-
- name: Set up wams-tools.wasm
70-
run: make internal/wasmtools/wasm-tools.wasm
71-
7259
- name: Run Go tests
7360
run: go test -v ${{ env.go-modules }}
7461

@@ -108,11 +95,6 @@ jobs:
10895
with:
10996
tinygo-version: ${{ matrix.tinygo-version }}
11097

111-
- name: Set up wasm-tools
112-
uses: bytecodealliance/actions/wasm-tools/setup@v1
113-
with:
114-
version: ${{ env.wasm-tools-version }}
115-
11698
- name: Test with TinyGo
11799
run: tinygo test -v ${{ env.go-modules }}
118100

@@ -144,14 +126,12 @@ jobs:
144126
with:
145127
tinygo-version: ${{ matrix.tinygo-version }}
146128

129+
# TinyGo needs wasm-tools for -target=wasip2
147130
- name: Set up wasm-tools
148131
uses: bytecodealliance/actions/wasm-tools/setup@v1
149132
with:
150133
version: ${{ env.wasm-tools-version }}
151134

152-
- name: Set up wams-tools.wasm
153-
run: make internal/wasmtools/wasm-tools.wasm
154-
155135
- name: Set up Wasmtime
156136
uses: bytecodealliance/actions/wasmtime/setup@v1
157137
with:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.DS_Store
22
/generated
3+
/internal/wasmtools/target
4+
/internal/wasmtools/wasm-tools.wasm
35
go.work.sum

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66

77
### Added
88

9-
- `internal/wasmtools` now loads compiled `wasm-tools` Wasm module from the `wasm-tools` crate and executes it using wazero. This allows `wit-bindgen-go` to run on any platform without needing to install `wasm-tools` natively.
9+
- [`wasm-tools`](https://crates.io/crates/wasm-tools) is now vendored as a WebAssembly module, executed using [Wazero](https://wazero.io/). This allows package `wit` and `wit-bindgen-go` to run on any supported platform without needing to separately install `wasm-tools`.
1010

1111
### Changed
1212

Makefile

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ json: $(wit_files)
66

77
.PHONY: $(wit_files)
88
$(wit_files): internal/wasmtools/wasm-tools.wasm
9-
wasmtime --dir . internal/wasmtools/wasm-tools.wasm component wit -j --all-features $@ > $@.json
9+
wasm-tools component wit -j --all-features $@ > $@.json
1010

1111
# golden recompiles the .golden.wit test files.
1212
.PHONY: golden
@@ -21,25 +21,26 @@ generated: clean json
2121
.PHONY: clean
2222
clean:
2323
rm -rf ./generated/*
24+
rm -f internal/wasmtools/wasm-tools.wasm
2425
rm -f internal/wasmtools/wasm-tools.wasm.gz
2526

2627
# tests/generated writes generated Go code to the tests directory
2728
.PHONY: tests/generated
2829
tests/generated: json
2930
go generate ./tests
3031

31-
# build builds the cmd/wit-bindgen-go binary
32-
.PHONY: build
33-
build: internal/wasmtools/wasm-tools.wasm
34-
go build -o wit-bindgen-go ./cmd/wit-bindgen-go
32+
# wasm-tools builds the internal/wasmtools/wasm-tools.wasm.gz artifact
33+
.PHONY: wasm-tools
34+
wasm-tools: internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm
35+
gzip -c $< > internal/wasmtools/wasm-tools.wasm.gz
3536

36-
internal/wasmtools/wasm-tools.wasm: internal/wasmtools/wasm-tools.wasm.gz
37-
gzip -dc $< > $@
38-
39-
internal/wasmtools/wasm-tools.wasm.gz:
37+
internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm: internal/wasmtools/Cargo.*
4038
cd internal/wasmtools && \
4139
cargo build --target wasm32-wasip1 --release -p wasm-tools
42-
gzip -c internal/wasmtools/target/wasm32-wasip1/release/wasm-tools.wasm > $@
40+
41+
# internal/wasmtools/wasm-tools.wasm decompresses wasm-tools.wasm.gz for other make targets
42+
internal/wasmtools/wasm-tools.wasm: internal/wasmtools/wasm-tools.wasm.gz
43+
gzip -dc internal/wasmtools/wasm-tools.wasm.gz > $@
4344

4445
# test runs Go and TinyGo tests
4546
GOTESTARGS :=

cmd/wit-bindgen-go/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"go.bytecodealliance.org/cmd/wit-bindgen-go/cmd/generate"
1111
"go.bytecodealliance.org/cmd/wit-bindgen-go/cmd/wit"
12-
"go.bytecodealliance.org/internal/witcli"
12+
"go.bytecodealliance.org/internal/module"
1313
)
1414

1515
func main() {
@@ -64,7 +64,7 @@ var version = &cli.Command{
6464
Name: "version",
6565
Usage: "print the version",
6666
Action: func(ctx context.Context, cmd *cli.Command) error {
67-
fmt.Fprintf(cmd.Writer, "%s version %s\n", cmd.Root().Name, witcli.Version())
67+
fmt.Fprintf(cmd.Writer, "%s version %s\n", cmd.Root().Name, module.Version())
6868
return nil
6969
},
7070
}

internal/cmd/wasm-tools/main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/fs"
7+
"os"
8+
9+
"go.bytecodealliance.org/internal/wasmtools"
10+
)
11+
12+
func main() {
13+
ctx := context.Background()
14+
wasmTools, err := wasmtools.New(ctx)
15+
if err != nil {
16+
exit(err)
17+
}
18+
19+
fsMap := map[string]fs.FS{
20+
"./": os.DirFS("./"),
21+
}
22+
23+
var args []string
24+
if len(os.Args) != 0 {
25+
args = os.Args[1:]
26+
}
27+
28+
err = wasmTools.Run(ctx, os.Stdin, os.Stdout, os.Stderr, fsMap, args...)
29+
if err != nil {
30+
exit(err)
31+
}
32+
}
33+
34+
func exit(err error) {
35+
fmt.Fprintf(os.Stderr, "wasm-tools: %v\n", err)
36+
os.Exit(1)
37+
}

internal/witcli/version.go renamed to internal/module/module.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
package witcli
1+
package module
22

33
import (
44
"runtime/debug"
55
"sync"
66
)
77

8-
// Version returns the version string of this module.
8+
// Path returns the path of the main module.
9+
func Path() string {
10+
build := buildInfo()
11+
if build == nil {
12+
return "(none)"
13+
}
14+
return build.Main.Path
15+
}
16+
17+
// Version returns the version string of the main module.
918
func Version() string {
1019
return versionString()
1120
}
1221

22+
var buildInfo = sync.OnceValue(func() *debug.BuildInfo {
23+
build, _ := debug.ReadBuildInfo()
24+
return build
25+
})
26+
1327
var versionString = sync.OnceValue(func() string {
14-
build, ok := debug.ReadBuildInfo()
15-
if !ok {
28+
build := buildInfo()
29+
if build == nil {
1630
return "(none)"
1731
}
1832
version := build.Main.Version

internal/wasmtools/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)