Skip to content

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

Merged
dgryski merged 1 commit into
tinygo-org:devfrom
asadkhanbn600i:fix-allow-o-flag-directory
Aug 5, 2026
Merged

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

Conversation

@asadkhanbn600i

Copy link
Copy Markdown
Contributor

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.
@asadkhanbn600i asadkhanbn600i changed the title Fix to allow -o flag to point to a directory Fixes #5548 Aug 3, 2026
@asadkhanbn600i asadkhanbn600i changed the title Fixes #5548 Fix to allow -o flag to point to a directory :: #5548 Aug 3, 2026

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Tested this manually.

@asadkhanbn600i

Copy link
Copy Markdown
Contributor Author

Hi @dgryski I don’t have merge access, Can you please merge it on my behalf. Thank you.

@dgryski
dgryski merged commit 854f91e into tinygo-org:dev Aug 5, 2026
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.

2 participants