Skip to content

Document canonicalization in std::env::current_dir #93598

Closed

Description

@AZMCode

As of now, the stable and nightly documentation on std::env::current_dir only states

Returns the current working directory as a PathBuf.

And proceeds to note a couple of errors the function may fail with.
This does not mention the fact that (as far as I am aware, testing on Linux) the path is also canonicalized, having resolved all symlinks in any segment of the path. I'd imagine not documenting this behavior could lead to confusion and/or bugs, if it already hasn't.

I tried this code within a symlink folder /example/symlink/here pointing to /symlink/target/here:

println!("{:#?}",std::env::current_dir().unwrap())

I expected to see get the output: "/example/symlink/here"
Instead, this was output: "/symlink/target/here"

Currently using the nightly compiler.

For obvious reasons, there's no backtrace.

Update: Read a bit through the code and realized this behavior might be libc-dependent
ldd --version on my system returns ldd (GNU libc) 2.33

Activity

  1. hkratz commented on Feb 3, 2022

    @hkratz
    Contributor

    For UNIX Rust just calls getcwd() and that behavior is documented in the POSIX spec, so it will be the same on all conforming implementations. We should document which platform-specific function we call here just like we do with other filesystem functions (e.g. here).

    @rustbot label T-libs A-docs

  2. added
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Feb 3, 2022
  3. GuillaumeGomez commented on Feb 3, 2022

    @GuillaumeGomez
    Member

    It was also missing for set_current_dir so I opened #93635 for both.

  4. AZMCode commented on Feb 4, 2022

    @AZMCode
    Author

    Thanks a lot

  5. added 3 commits that reference this issue on Feb 11, 2022
    7020697
    a023e7b
    15d71cf
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-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-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