Repository navigation
feat(mkdir): add --fail-if-exists flag to error on existing directory - #19021
Conversation
The default mkdir stays -p-like/idempotent (making it error by default was reverted in nushell#19008 as a breaking change). Per the maintainer's suggestion on already exists ("<path>: File exists", matching coreutils) while still creating non-existing targets in the same call. Default behavior unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
4337c24 to
f0d2e02
Compare
|
I'm not sure I understand why this is necessary with mkdir. Why does one want to show an error if you mkdir a path that already exists? |
|
Mostly two things. The first is just parity with regular The other one is catching mistakes with generated dirs. If a script makes a fresh output folder named by timestamp or id, you usually want it to blow up when that folder already exists instead of quietly reusing it and mixing new output into old results. Right now you'd have to do a separate The default doesn't change at all, so anyone using |
|
I buy your second paragraph about programatic creation, meh on the first. |
There was a problem hiding this comment.
🟡 Changes recommended
The preflight existence check introduces a race that can violate the flag’s promised behavior.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Adds opt-in mkdir --fail-if-exists behavior while preserving idempotent defaults.
Changes:
- Adds the new switch and error handling.
- Adds command examples and tests.
File summaries
| File | Description |
|---|---|
crates/nu-command/src/filesystem/umkdir.rs |
Implements the flag and verbose error records. |
crates/nu-command/tests/commands/umkdir.rs |
Tests existing and new targets. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| // --verbose does not report an existing directory as created. | ||
| // --verbose does not report an existing directory as created and so | ||
| // `--fail-if-exists` can report the existing path as an error. | ||
| let already_existed = dir.exists(); |
| let actual: String = test() | ||
| .cwd(dirs.test()) | ||
| .run("mkdir -v --fail-if-exists already_there | get 0.created | to text")?; | ||
|
|
||
| assert_eq!("false", actual.trim()); |
|
Yeah, fair — the parity thing isn't really the point, I'll drop that framing. The case I actually care about: a script does something like Happy to reword the docs to lead with that instead of the |
Determine AlreadyExists from an atomic create_dir instead of a racy exists() pre-check (TOCTOU), and assert the error field in the verbose test. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Thanks |
Description
Follow-up to #19003.
The default
mkdirbehavior stays exactly as-is: idempotent and-p-like. #19006 tried makingmkdirerror by default and was reverted in #19008, since people rely onmkdir footo ensure a directory exists, and^mkdir -pdoes not error either.Per @fdncred's suggestion in the thread, this PR adds an opt-in flag instead.
With
--fail-if-exists,mkdirerrors on a target that already exists with<path>: File exists, matching coreutils, while still creating any non-existing targets passed in the same call. Without the flag, behavior is unchanged.The
--verbosecreated: truepart of the issue was already fixed in #19017, so this covers the remaining piece.User-facing changes (Release notes)
Added
mkdir --fail-if-exists <dir>, which errors if the directory already exists instead of silently succeeding. Defaultmkdirbehavior is unchanged.Additional notes
Verification:
commands::umkdirtests for erroring on an existing directory with the flag.mkdir --fail-if-existsstill creates new directories.mkdir -v --fail-if-existsreportscreated: false.mkdirand intermediary directories still pass.cargo fmt --all --check: cleancargo clippy -p nu-command: cleannu-commandtest suite: cleanDisclosure: I use AI assistance for code review and for drafting text; I review and verify every change myself before sending it.