Skip to content

Commit 3bfe300

Browse files
authored
Rollup merge of rust-lang#50602 - Screwtapello:update-canonicalize-docs, r=cramertj
Update canonicalize docs I was recently working with file-paths in Rust, and I felt let down by the `std::fs::canonicalize` docs, so I figured I should submit a PR with some suggestions. I was looking for a method to turn a relative path into an absolute path. The `canonicalize` docs didn't mention the words "relative" or "absolute", but they did mention resolving symlinks (which is a kind of canonicalisation and does not imply converting to absolute), so I assumed that's all it did. To remedy this, I've added the word "absolute" to the description of both `std::fs::canonicalize` and `std::path::Path::canonicalize`. After calling `canonicalize` on Windows, I ran into a bunch of other problems I would not have expected from the function's behaviour on Linux. Specifically, if you call `canonicalize` on a path: - it's allowed to be much longer than it otherwise would - `.join("a/slash/delimited/path")` gives you a broken path that Windows can't use, where the same operation would have worked perfectly without `canonicalize` (if the path were short enough) - the resulting path may confuse other Windows programs if you pass it to them on the command-line, or write it to a config file that they read, etc. ...so I tried to summarize those behaviours too. If I understand correctly, those behaviours are a side-effect of calling `GetFinalPathNameByHandle`, and the documentation says `canonicalize` might not call that function in future, so maybe those side-effects shouldn't be part of the function's documentation. However, I bet there's a lot of applications deliberately calling `canonicalize` just for the path-length-extension alone, so that particular side-effect is de-facto part of the `canonicalize` interface.
2 parents 97da685 + 8720314 commit 3bfe300

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/libstd/fs.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1699,16 +1699,23 @@ pub fn read_link<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
16991699
fs_imp::readlink(path.as_ref())
17001700
}
17011701

1702-
/// Returns the canonical form of a path with all intermediate components
1703-
/// normalized and symbolic links resolved.
1702+
/// Returns the canonical, absolute form of a path with all intermediate
1703+
/// components normalized and symbolic links resolved.
17041704
///
17051705
/// # Platform-specific behavior
17061706
///
17071707
/// This function currently corresponds to the `realpath` function on Unix
17081708
/// and the `CreateFile` and `GetFinalPathNameByHandle` functions on Windows.
17091709
/// Note that, this [may change in the future][changes].
17101710
///
1711+
/// On Windows, this converts the path to use [extended length path][path]
1712+
/// syntax, which allows your program to use longer path names, but means you
1713+
/// can only join backslash-delimited paths to it, and it may be incompatible
1714+
/// with other applications (if passed to the application on the command-line,
1715+
/// or written to a file another application may read).
1716+
///
17111717
/// [changes]: ../io/index.html#platform-specific-behavior
1718+
/// [path]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
17121719
///
17131720
/// # Errors
17141721
///

src/libstd/path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2284,8 +2284,8 @@ impl Path {
22842284
fs::symlink_metadata(self)
22852285
}
22862286

2287-
/// Returns the canonical form of the path with all intermediate components
2288-
/// normalized and symbolic links resolved.
2287+
/// Returns the canonical, absolute form of the path with all intermediate
2288+
/// components normalized and symbolic links resolved.
22892289
///
22902290
/// This is an alias to [`fs::canonicalize`].
22912291
///

0 commit comments

Comments
 (0)