Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Open
rosik opened this issue Nov 30, 2022 · 5 comments · May be fixed by #125112
Open

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

rosik opened this issue Nov 30, 2022 · 5 comments · May be 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
Contributor

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.
@tbu- tbu- linked a pull request May 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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