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)