From 854f91ecf23cc976b5ba8d83f000e8012695ef5e Mon Sep 17 00:00:00 2001 From: Mohammed-Asad-Khan Date: Sat, 1 Aug 2026 14:42:42 +0000 Subject: [PATCH] Fix to allow -o flag to point to a directory Previously, running 'tinygo build -o dist/ file.go' where dist/ already exists as a directory failed with 'open dist/: is a directory', because Build() only auto-derived a binary name when outpath was empty, not when it pointed to an existing directory. This adds that check, matching the behavior of 'go build -o dir/' with a package. Note: only handles the case where the directory already exists; if the directory doesn't exist yet, behavior is unchanged. --- main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 26a1f9938..986e8c176 100644 --- a/main.go +++ b/main.go @@ -177,8 +177,15 @@ func Build(pkgName, outpath string, config *compileopts.Config) error { // Pick a default output path based on the main directory. outpath = filepath.Base(result.MainDir) + config.DefaultBinaryExtension() } + } else if fi, statErr := os.Stat(outpath); statErr == nil && fi.IsDir() { + var name string + if strings.HasSuffix(pkgName, ".go") { + name = filepath.Base(pkgName[:len(pkgName)-3]) + config.DefaultBinaryExtension() + } else { + name = filepath.Base(result.MainDir) + config.DefaultBinaryExtension() + } + outpath = filepath.Join(outpath, name) } - if err := os.Rename(result.Binary, outpath); err != nil { // Moving failed. Do a file copy. inf, err := os.Open(result.Binary)