Skip to content

Commit 954e34d

Browse files
authored
mkdir errors on already-created dir (#19006)
## Description closes #19003. tl;dr: since we always run `mkdir` with 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 `translate` in the `error` field when `--verbose` is passed. this can pretty easily run into #19004 (for example if `mktemp` was used beforehand). this also keeps the dir name in the error, so you can have something like `path: /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) `nushell` will now error when a dir already exists. nushell's `--verbose` will 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 typical `mkdir` implementations. ## Disclosure AI was consulted while creating this, but did not generate code.
1 parent 9cb59b9 commit 954e34d

2 files changed

Lines changed: 69 additions & 9 deletions

File tree

‎crates/nu-command/src/filesystem/umkdir.rs‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use uucore::{localized_help_template, translate};
66
#[derive(Clone)]
77
pub struct UMkdir;
88

9-
const IS_RECURSIVE: bool = true;
109
const DEFAULT_MODE: u32 = 0o777;
1110

1211
#[cfg(target_family = "unix")]
@@ -102,18 +101,40 @@ impl Command for UMkdir {
102101
});
103102
}
104103

105-
let config = uu_mkdir::Config {
106-
recursive: IS_RECURSIVE,
107-
mode: Some(get_mode()),
108-
verbose: false,
109-
set_security_context: false,
110-
context: None,
104+
let mode = Some(get_mode());
105+
let verbose = false;
106+
let set_security_context = false;
107+
let context = None;
108+
109+
let config_recursive = uu_mkdir::Config {
110+
recursive: true,
111+
mode,
112+
verbose,
113+
set_security_context,
114+
context,
115+
};
116+
117+
let config_nonrecursive = uu_mkdir::Config {
118+
recursive: false,
119+
mode,
120+
verbose,
121+
set_security_context,
122+
context,
111123
};
112124

113125
let mut verbose_out = Vec::new();
114126
let mut err = None;
115127
for (dir, dir_span) in directories {
116-
if let Err(error) = mkdir(&dir, &config) {
128+
// we create dirs in two steps to detect if the dir already exists.
129+
// this is because we run with `recursive`,
130+
// which would otherwise swallow this error
131+
let result = if let Some(parent) = dir.parent() {
132+
mkdir(parent, &config_recursive).and_then(|_| mkdir(&dir, &config_nonrecursive))
133+
} else {
134+
mkdir(&dir, &config_nonrecursive)
135+
};
136+
137+
if let Err(error) = result {
117138
let shell_error = ShellError::Generic(GenericError::new(
118139
format!("{error}"),
119140
translate!(&error.to_string()),
@@ -125,7 +146,7 @@ impl Command for UMkdir {
125146
record! {
126147
"path" => Value::string(dir.display().to_string(), call.head),
127148
"created" => Value::bool(false, call.head),
128-
"error" => Value::string(format!("{error}"), call.head),
149+
"error" => Value::string(translate!(&error.to_string()), call.head),
129150
}
130151
.into_value(call.head),
131152
)

‎crates/nu-command/tests/commands/umkdir.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use nu_test_support::playground::Playground;
22
use nu_test_support::prelude::*;
3+
use pretty_assertions::assert_matches;
34

45
#[test]
56
fn creates_directory() -> Result {
@@ -123,6 +124,44 @@ fn respects_cwd() -> Result {
123124
})
124125
}
125126

127+
#[test]
128+
fn handles_existing_directory_two_calls() -> Result {
129+
Playground::setup("mkdir_test_handles_existing_1", |dirs, _| {
130+
let _ = test()
131+
.cwd(dirs.test())
132+
.run("mkdir foo; mkdir foo")
133+
.expect_error()?;
134+
135+
assert!(dirs.test().join("foo").is_dir());
136+
137+
Ok(())
138+
})
139+
}
140+
141+
#[test]
142+
fn handles_existing_directory_verbose() -> Result {
143+
Playground::setup("mkdir_test_handles_existing_2", |dirs, _| {
144+
let actual: Value = test().cwd(dirs.test()).run("mkdir foo foo --verbose")?;
145+
146+
assert!(dirs.test().join("foo").is_dir());
147+
148+
let actual = actual.into_list()?;
149+
let [dir1, dir2] = actual.as_slice() else {
150+
panic!();
151+
};
152+
153+
let dir1 = dir1.as_record()?;
154+
let created = dir1.get("created").map(Value::as_bool).transpose()?;
155+
assert_matches!(created, Some(true));
156+
157+
let dir2 = dir2.as_record()?;
158+
let created = dir2.get("created").map(Value::as_bool).transpose()?;
159+
assert_matches!(created, Some(false));
160+
161+
Ok(())
162+
})
163+
}
164+
126165
#[cfg(not(windows))]
127166
#[test]
128167
#[serial]

0 commit comments

Comments
 (0)