Skip to content

Fix to allow -o flag to point to a directory - #5569

Open
asadkhanbn600i wants to merge 1 commit into
tinygo-org:devfrom
asadkhanbn600i:fix-allow-o-flag-directory
Open

Fix to allow -o flag to point to a directory#5569
asadkhanbn600i wants to merge 1 commit into
tinygo-org:devfrom
asadkhanbn600i:fix-allow-o-flag-directory

Conversation

@asadkhanbn600i

Copy link
Copy Markdown

What this does

Fixes tinygo build -o <dir>/ ... failing with open <dir>/: is a directory
when the target directory already exists.

Why

Build() in main.go only derived a default binary name when outpath == "".
If outpath was explicitly set but pointed to an existing directory, the
code fell straight to os.Rename(result.Binary, outpath), which fails
since a directory can't be renamed-over/opened as a file.

Fix

Added a check: if outpath is a directory, derive the same default binary
name used in the empty-outpath case, and place the binary inside that
directory — mirroring how go build -o dir/ behaves for a package.

Scope

Only handles the case where the directory already exists. If -o somedir/
is given and somedir doesn't exist yet, behavior is unchanged. Happy to
extend this in a follow-up if maintainers want that too.

Testing

  • make test passes locally
  • Manually verified: existing directory (new behavior), explicit file path
    (unchanged), and no -o flag (unchanged)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant