From 3ff6fcd59b6689cdc21bca4a82dcf4310e1db87e Mon Sep 17 00:00:00 2001 From: diamondburned Date: Tue, 28 May 2024 13:05:48 -0700 Subject: [PATCH 1/2] allow building on Tinygo This commit adds package internal/tinygostub that contains stubs for some of the standard library functions that are not available in Tinygo. This allows most of the module to be built with Tinygo. --- internal/tinygostub/samefile.go | 10 ++++++++++ internal/tinygostub/samefile_tinygo_wasm.go | 10 ++++++++++ interp/test.go | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 internal/tinygostub/samefile.go create mode 100644 internal/tinygostub/samefile_tinygo_wasm.go diff --git a/internal/tinygostub/samefile.go b/internal/tinygostub/samefile.go new file mode 100644 index 00000000..3bbe27ff --- /dev/null +++ b/internal/tinygostub/samefile.go @@ -0,0 +1,10 @@ +//go:build !(tinygo && wasm) + +package tinygostub + +import "os" + +// SameFile calls [os.SameFile]. +func SameFile(fi1, fi2 os.FileInfo) bool { + return os.SameFile(fi1, fi2) +} diff --git a/internal/tinygostub/samefile_tinygo_wasm.go b/internal/tinygostub/samefile_tinygo_wasm.go new file mode 100644 index 00000000..13a374be --- /dev/null +++ b/internal/tinygostub/samefile_tinygo_wasm.go @@ -0,0 +1,10 @@ +//go:build tinygo && wasm + +package tinygostub + +import "os" + +// SameFile returns false. +func SameFile(fi1, fi2 os.FileInfo) bool { + return false +} diff --git a/interp/test.go b/interp/test.go index 3f8ffca7..a6f441d6 100644 --- a/interp/test.go +++ b/interp/test.go @@ -13,6 +13,7 @@ import ( "golang.org/x/term" "mvdan.cc/sh/v3/expand" + "mvdan.cc/sh/v3/internal/tinygostub" "mvdan.cc/sh/v3/syntax" ) @@ -88,7 +89,7 @@ func (r *Runner) binTest(ctx context.Context, op syntax.BinTestOperator, x, y st if err1 != nil || err2 != nil { return false } - return os.SameFile(info1, info2) + return tinygostub.SameFile(info1, info2) case syntax.TsEql: return atoi(x) == atoi(y) case syntax.TsNeq: From 8693c4502ed315a77843150bc864b681a625f8c6 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Tue, 28 May 2024 13:14:15 -0700 Subject: [PATCH 2/2] add workflow that guarantees Tinygo build This workflow guarantees that at least these packages build under Tinygo x86_64 and WASM: - mvdan.cc/sh/v3/expand - mvdan.cc/sh/v3/syntax - mvdan.cc/sh/v3/interp --- .github/workflows/test.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3e2a57dd..b966c65d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -40,6 +40,34 @@ jobs: - name: Test as root, without cgo, and with busybox run: docker run -v="$PWD:/pwd" -w=/pwd -e=CGO_ENABLED=0 golang:1.22.0-alpine go test ./... + test-tinygo: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install Tinygo + uses: acifani/setup-tinygo@v2 + with: + tinygo-version: 0.32.0 + + - name: Create temporary main package + run: | + mkdir _tinygo && cat< _tinygo/main.go + package main + + import ( + _ "mvdan.cc/sh/v3/expand" + _ "mvdan.cc/sh/v3/interp" + _ "mvdan.cc/sh/v3/syntax" + ) + + func main() {} + EOF + + - name: Build with Tinygo natively + run: tinygo build -o /dev/null ./_tinygo + - name: Build with Tinygo on WASM + run: tinygo build -o /dev/null -target wasm ./_tinygo + docker: name: Build and test Docker images # Only deploy if previous stages pass.