From 48f145c4df989db06a302a00aa04fc39559c5292 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 20 Mar 2025 14:55:45 +0100 Subject: [PATCH] compileopts: enable support for GOARCH=wasm in `tinygo test` Support for GOARCH=wasm was only available for `tinygo build`, not for any of the other subcommands (`test`, `run`, etc). With this PR, these subcommands should work again for supported values of GOOS. This fixes the following error for example: $ make tinygo-test-wasip1 GOOS=wasip1 GOARCH=wasm /home/ayke/bin/tinygo test cmp compress/lzw compress/zlib [...etc] cannot resolve packages: GOARCH=wasm but GOOS is unset. Please set GOOS to wasm, wasip1, or wasip2. make: *** [GNUmakefile:489: tinygo-test-wasip1] Error 1 --- compileopts/target.go | 15 +++++++++++++++ main.go | 21 +++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/compileopts/target.go b/compileopts/target.go index 6917a944b..62d02963f 100644 --- a/compileopts/target.go +++ b/compileopts/target.go @@ -178,6 +178,21 @@ func (spec *TargetSpec) resolveInherits() error { // Load a target specification. func LoadTarget(options *Options) (*TargetSpec, error) { + if options.Target == "" && options.GOARCH == "wasm" { + // Set a specific target if we're building from a known GOOS/GOARCH + // combination that is defined in a target JSON file. + switch options.GOOS { + case "js": + options.Target = "wasm" + case "wasip1": + options.Target = "wasip1" + case "wasip2": + options.Target = "wasip2" + default: + return nil, errors.New("GOARCH=wasm but GOOS is not set correctly. Please set GOOS to wasm, wasip1, or wasip2.") + } + } + if options.Target == "" { return defaultTarget(options) } diff --git a/main.go b/main.go index 7197deea7..5d5b7b6c8 100644 --- a/main.go +++ b/main.go @@ -1684,24 +1684,9 @@ func main() { usage(command) os.Exit(1) } - if options.Target == "" { - switch { - case options.GOARCH == "wasm": - switch options.GOOS { - case "js": - options.Target = "wasm" - case "wasip1": - options.Target = "wasip1" - case "wasip2": - options.Target = "wasip2" - default: - fmt.Fprintln(os.Stderr, "GOARCH=wasm but GOOS is not set correctly. Please set GOOS to wasm, wasip1, or wasip2.") - os.Exit(1) - } - case filepath.Ext(outpath) == ".wasm": - fmt.Fprintln(os.Stderr, "you appear to want to build a wasm file, but have not specified either a target flag, or the GOARCH/GOOS to use.") - os.Exit(1) - } + if filepath.Ext(outpath) == ".wasm" && options.GOARCH != "wasm" && options.Target == "" { + fmt.Fprintln(os.Stderr, "you appear to want to build a wasm file, but have not specified either a target flag, or the GOARCH/GOOS to use.") + os.Exit(1) } err := Build(pkgName, outpath, options)