Skip to content

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

Closed

Description

@rosik

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

Activity

  1. rosik commented on Nov 30, 2022

    @rosik
    ContributorAuthor

    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

  2. edneedham commented on Dec 1, 2022

    @edneedham

    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.

  3. ChrisDenton commented on Dec 1, 2022

    @ChrisDenton
    Member

    I don't know why this check exists

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

  4. edneedham commented on Dec 1, 2022

    @edneedham

    Thanks for the info, Chris.

  5. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    A-ioArea: `std::io` (note: `std::fs`, `std::net`, `std::path` are A-filesystem, A-net, A-path)
    on Mar 22, 2023
  6. tbu- commented on May 14, 2024

    @tbu-
    Contributor

    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.

  7. added 2 commits that reference this issue on May 14, 2024
    04d2e35
    6add5c9
  8. added a commit that references this issue on Jun 16, 2024
    e4bc7de
  9. added a commit that references this issue on Jun 16, 2024
    4481b6c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ioArea: `std::io` (note: `std::fs`, `std::net`, `std::path` are A-filesystem, A-net, A-path)C-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions