Repository navigation
mkdir errors on already-created dir - #19006
Merged
Merged
Conversation
pickx
force-pushed
the
mkdir-existing
branch
from
September 10, 2026 13:46
1d7b84d to
bfc232f
Compare
Contributor
|
Thanks |
Contributor
This sounds like a serious BC break to me. Previously you could do: # acts as assertion; the directory will be present after this line (bar any permission errors
mkdir path/to/dir
cp file.name path/to/dir/and now it would break if Also, if we say that nushell's ❯ dpkg -S /usr/bin/mkdir
coreutils: /usr/bin/mkdir
❯ ^mkdir -p path/to/dir
❯ $ans.exit_code
0
❯ ^mkdir -p path/to/dir
❯ $ans.exit_code
0
❯ "path/to/dir" | path exists
trueIMHO, a better way to solve the original issue is to add a new |
Contributor
|
Thanks for your input @weirdan. i hadn't considered that. let's revert. |
fdncred
added a commit
that referenced
this pull request
Sep 10, 2026
fdncred
pushed a commit
that referenced
this pull request
Sep 16, 2026
…#19021) # Description Follow-up to #19003. The default `mkdir` behavior (idempotent, `-p`-like) stays exactly as-is — #19006 tried making it error by default and was reverted in #19008, since people rely on `mkdir foo` to just ensure a directory exists (and `^mkdir -p` doesn't error either). Per @fdncred's suggestion in the thread, this instead adds an **opt-in** flag. With `--fail-if-exists`, `mkdir` errors on a target that already exists (message `<path>: File exists`, matching coreutils), while still creating any non-existing targets passed in the same call. Without the flag, nothing changes. The `--verbose` `created: true` half of the issue was already fixed in #19017, so this covers the remaining piece. # User-Facing Changes New opt-in switch: `mkdir --fail-if-exists <dir>` (short: none) errors if the directory already exists instead of silently succeeding. Default `mkdir` is unchanged. # Tests + Formatting Added `commands::umkdir` tests: errors on an existing dir with the flag, still creates new dirs with the flag, and `-v --fail-if-exists` reports `created: false`. Existing default-behavior tests (idempotent mkdir, intermediary dirs) still pass. `cargo fmt --all --check`, `cargo clippy -p nu-command`, and the `nu-command` test suite are clean. # After Submitting Nothing needed (no doc/`std` changes). --- _Disclosure: I use AI assistance for code review and for drafting text (English isn't my first language); I review and verify every change myself before sending it._ --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Member
|
marked as |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
closes #19003. tl;dr: since we always run
mkdirwith implied--parents, dirs are always created and errors are never thrown. however, we forgot to detect when dirs are not created because they already exist.note also that we now use
translatein theerrorfield when--verboseis passed. this can pretty easily run into #19004 (for example ifmktempwas used beforehand). this also keeps the dir name in the error, so you can have something likepath: /foo/bar, created: false, error: /foo/bar: File exists. in other words, we repeat the path. tell me if that's not what we want.User-facing changes (Release notes)
nushellwill now error when a dir already exists. nushell's--verbosewill no longer report a dir as created when it already exists. however, if multiple dirs are created in the same command (e.g.mkdir dir1 dir2/dir3 dir4) and some of them already exist, the non-existing ones will still be created. this matches the behavior of typicalmkdirimplementations.Disclosure
AI was consulted while creating this, but did not generate code.