Skip to content

create_dir_all("") should complain about invalid filename #105108

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
rosik opened this issue Nov 30, 2022 · 5 comments · Fixed by #125112
Closed

create_dir_all("") should complain about invalid filename #105108

rosik opened this issue Nov 30, 2022 · 5 comments · Fixed by #125112
Labels
A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@rosik
Copy link
Contributor

rosik commented Nov 30, 2022

I tried this code:

fn main() {
    std::fs::create_dir_all("").unwrap();
}

The filename is invalid, so I've expected to see some error similar to posix utils:

$ mkdir -p ""
mkdir: cannot create directory ‘’: No such file or directory
$ mkdir ""
mkdir: cannot create directory ‘’: No such file or directory

Instead, nothing interesting happened, create_dir_all succeeded.

Meta

$ rustc --version --verbose
rustc 1.67.0-nightly (e0098a5cc 2022-11-29)
binary: rustc
commit-hash: e0098a5cc3a87d857e597af824d0ce1ed1ad85e0
commit-date: 2022-11-29
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy
@rosik rosik added the C-bug Category: This is a bug. label Nov 30, 2022
@rosik
Copy link
Contributor Author

rosik commented Nov 30, 2022

P.S. std::fs::create_dir("") is fine:

$ cargo run
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:2:29

I would expect to see the same message from create_dir_all

@edneedham
Copy link

edneedham commented Dec 1, 2022

Here is what I have found:

fn create_dir_all(&self, path: &Path) -> io::Result<()> { if path == Path::new("") { return Ok(()); } on line 2393 in the fs module

I think you're right in that the proper behaviour would be just to produce an error message in the
same way as posix utils. I don't know why this check exists, it doesn't seem to produce anything.

@ChrisDenton
Copy link
Member

I don't know why this check exists

For some history see #23383. Though a lot has changed in 7 years.

@edneedham
Copy link

Thanks for the info, Chris.

@ChrisDenton ChrisDenton added T-libs Relevant to the library team, which will review and decide on the PR/issue. A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` labels Mar 22, 2023
@tbu-
Copy link
Contributor

tbu- commented May 14, 2024

Since Path::new("abc").parent() == Some(Path::new("")), it makes sense for create_dir_all to succeed in this case. I'd say this needs documentation at most.

tbu- added a commit to tbu-/rust that referenced this issue May 14, 2024
The behavior makes sense because `Path::new("one_component").parent() ==
Some(Path::new(""))`, so if one naively wants to create the parent
directory for a file to be written, it simply works.

Closes rust-lang#105108 by documenting the current behavior.
jhpratt added a commit to jhpratt/rust that referenced this issue Jun 16, 2024
…olnay

Document behavior of `create_dir_all` wrt. empty path

The behavior makes sense because `Path::new("one_component").parent() == Some(Path::new(""))`, so if one naively wants to create the parent directory for a file to be written, it simply works.

Closes rust-lang#105108 by documenting the current behavior.
@bors bors closed this as completed in 6add5c9 Jun 16, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 16, 2024
Rollup merge of rust-lang#125112 - tbu-:pr_create_dir_all_empty, r=dtolnay

Document behavior of `create_dir_all` wrt. empty path

The behavior makes sense because `Path::new("one_component").parent() == Some(Path::new(""))`, so if one naively wants to create the parent directory for a file to be written, it simply works.

Closes rust-lang#105108 by documenting the current behavior.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-io Area: `std::io`, `std::fs`, `std::net` and `std::path` C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants