Skip to content

Document behavior of create_dir_all wrt. empty path#125112

Merged
bors merged 1 commit into
rust-lang:masterfrom
tbu-:pr_create_dir_all_empty
Jun 16, 2024
Merged

Document behavior of create_dir_all wrt. empty path#125112
bors merged 1 commit into
rust-lang:masterfrom
tbu-:pr_create_dir_all_empty

Conversation

@tbu-
Copy link
Copy Markdown
Contributor

@tbu- tbu- commented 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 #105108 by documenting the current behavior.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 14, 2024

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 14, 2024
@Mark-Simulacrum
Copy link
Copy Markdown
Member

r? dtolnay - technically a new stable guarantee, I think? So probably needs libs-api FCP.

@rustbot rustbot assigned dtolnay and unassigned Mark-Simulacrum May 19, 2024
@Mark-Simulacrum Mark-Simulacrum added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 19, 2024
@dtolnay
Copy link
Copy Markdown
Member

dtolnay commented May 20, 2024

@rust-lang/libs-api:
@rfcbot fcp merge

fs::create_dir_all("") does not try to create the current directory, even if the current directory does not exist. Is this the intended behavior? Can it be documented?

fn main() {
    let tempdir = tempfile::tempdir().unwrap();
    let repro = tempdir.path().join("repro");
    std::fs::create_dir(&repro).unwrap();
    std::env::set_current_dir(&repro).unwrap();
    std::fs::remove_dir(&repro).unwrap();
    std::fs::write(&repro, "").unwrap();
    std::fs::create_dir_all(&repro).unwrap_err(); // FAIL
    std::fs::create_dir_all("").unwrap(); // ok
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=39a021f969f8b24a38bd729535041664

@rfcbot
Copy link
Copy Markdown

rfcbot commented May 20, 2024

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels May 20, 2024
@dtolnay
Copy link
Copy Markdown
Member

dtolnay commented May 20, 2024

Our previous empty paths topic: #114149. In that case we decided not to regard the empty path as current dir for read_dir.

@dtolnay
Copy link
Copy Markdown
Member

dtolnay commented May 20, 2024

Interestingly fs::create_dir_all(".") does not try to create the current dir either, and fs::create_dir_all("./x") fails with "No such file or directory" if the current dir does not exist.

@tbu-
Copy link
Copy Markdown
Contributor Author

tbu- commented May 20, 2024

Our previous empty paths topic: #114149. In that case we decided not to regard the empty path as current dir for read_dir.

I'd add that this case is a bit different, because create_dir_all creates all the directories present in the given parameter. The empty string contains no directories to be created, so it can immediately return. This is different from e.g. create_dir which creates exactly one directory.

@tbu-
Copy link
Copy Markdown
Contributor Author

tbu- commented May 20, 2024

Interestingly fs::create_dir_all(".") does not try to create the current dir either, and fs::create_dir_all("./x") fails with "No such file or directory" if the current dir does not exist.

On Linux, mkdir . also seems to fail even when the current directory was unlinked:

$ mkdir .
mkdir: cannot create directory ‘.’: File exists

@BurntSushi
Copy link
Copy Markdown
Member

I think the wording here is a little vague. It says that it always "succeeds," but doesn't say what "succeeds" means. Can we elaborate on what success means for this case?

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- force-pushed the pr_create_dir_all_empty branch from 04d2e35 to 6add5c9 Compare May 21, 2024 05:57
@tbu-
Copy link
Copy Markdown
Contributor Author

tbu- commented May 21, 2024

I think the wording here is a little vague. It says that it always "succeeds," but doesn't say what "succeeds" means. Can we elaborate on what success means for this case?

Done.

@dtolnay dtolnay added S-waiting-on-team and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 1, 2024
@joshtriplett
Copy link
Copy Markdown
Member

@tbu- Yeah, that's a good point. Even if we wanted to create the current directory, we can't.

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jun 6, 2024
@rfcbot
Copy link
Copy Markdown

rfcbot commented Jun 6, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jun 6, 2024
@dtolnay dtolnay added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-team labels Jun 11, 2024
@rfcbot rfcbot added the finished-final-comment-period The final comment period is finished for this PR / Issue. label Jun 16, 2024
@rfcbot rfcbot removed the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jun 16, 2024
@rfcbot
Copy link
Copy Markdown

rfcbot commented Jun 16, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jun 16, 2024
Copy link
Copy Markdown
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dtolnay
Copy link
Copy Markdown
Member

dtolnay commented Jun 16, 2024

@bors r+ rollup

@bors
Copy link
Copy Markdown
Collaborator

bors commented Jun 16, 2024

📌 Commit 6add5c9 has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jun 16, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 16, 2024
Rollup of 4 pull requests

Successful merges:

 - rust-lang#125112 (Document behavior of `create_dir_all` wrt. empty path)
 - rust-lang#126127 (Spell out other trait diagnostic)
 - rust-lang#126309 (unify git command preperation)
 - rust-lang#126539 (Update `Arc::try_unwrap()` docs)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e4bc7de into rust-lang:master Jun 16, 2024
@rustbot rustbot added this to the 1.81.0 milestone Jun 16, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request 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.
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Jul 4, 2024
celinval added a commit to celinval/rust-dev that referenced this pull request Jul 17, 2024