-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
std::fs::canonicalize returns UNC paths on Windows, and a lot of software doesn't support UNC paths #42869
Comments
|
In reference to your commit which referenced this PR, normalization is not the same as merely joining the path onto the current directory due to drive relative paths being relative to the current directory on the given drive. For example given a drive relative path of |
thanks, @retep998 :) it's just a hacked-together build tool that probably will eventually be replaced with something else, and I didn't intend to notify this ticket about my commit. but I guess it goes to show that a good way to get an absolute path in std would be really helpful. |
Note, the i-wrong tag is only for the |
Quick testing on Windows 10.0.15063 indicates that both |
Technically, AFAIK it is safe to strip the prefix in common simple cases (absolute path with a drive letter, no reserved names, shorter than max_path), and leave it otherwise. So I think there's no need to compromise on correctness as far as stdlib goes. The trade-off is between failing early and exposing other software that doesn't support UNC paths vs maximizing interoperability with non-UNC software. In an ideal world, I would prefer the "fail early" approach, so that limitations are quickly found and removed. However, Windows/DOS path handling has exceptionally long and messy history and decades of Microsoft bending over backwards to let old software not upgrade its path handling. If Microsoft can't push developers towards UNC, and fails to enforce this even in their own products, I have no hope of Rust shifting the Windows ecosystem to UNC. It will rather just frustrate Rust users and make Rust seem less reliable on Windows. So in this case I suggest trying to maximize interoperability instead, and canonicalize to regular paths whenever possible (using UNC only for paths that can't be handled otherwise). Also, careful stripping of the prefix done in stdlib will be much safer than other crates stripping it unconditionally (because realistically whenever someone runs into this problem, they'll just strip it unconditionally) |
@kornelski I completely agree. The current behavior is unexpected in my opinion. |
I hope this is helpful… According to Microsoft:
Source: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx And the Ruby language uses forward slashes for File paths and that works on Windows. |
I've looked at this problem in detail. There are a few rules which need to be checked to safely strip the UNC prefix. It can be implemented as a simple state machine. I've implemented that using public APIs, but because So I'm still hoping canonicalize would do it automatically, because if it's done only for legacy-compatible paths there's no downside: all paths work for UNC-aware programs, and all paths that can work for legacy programs work too. |
Another example of this issue that I encountered in alexcrichton/cargo-vendor#71: url::URL.to_file_path() returns a non-UNC path (even if the URL was initialized with a UNC path). And std::path::Path.starts_with() doesn't normalize its arguments to UNC paths. So calling extern crate url;
use std::path::Path;
use url::Url;
fn main() {
// Path.canonicalize() returns a UNC path.
let unc_path_buf = Path::new(r"C:\Windows\System").canonicalize().expect("path");
let unc_path = unc_path_buf.as_path();
// Meanwhile, Url.to_file_path() returns a non-UNC path,
// even when initialized from a UNC path.
let file_url = Url::from_file_path(unc_path).expect("url");
let abs_path_buf = file_url.to_file_path().expect("path");
let abs_path = abs_path_buf.as_path();
// unc_path and abs_path refer to the same resource,
// and they both "start with" themselves.
assert!(unc_path.starts_with(unc_path));
assert!(abs_path.starts_with(abs_path));
// But they don't "start with" each other, so these fail.
assert!(unc_path.starts_with(abs_path));
assert!(abs_path.starts_with(unc_path));
} Arguably, Nevertheless, it does feel like something of a footgun, so it's worth at least documenting how it differs from that of some other APIs on Windows. |
Comparing canonical paths is a footgun in general because it is the wrong thing to do! Things like hard links and so on mean that such comparisons will never be entirely accurate. Please don't abuse canonicalization for this use case. If you want to tell whether two paths point to the same file, compare their file IDs! That's what |
but |
There are more ways than just |
bind mounts are equivalent to directory hardlinks.
…On Wed, May 9, 2018, 16:20 Kornel ***@***.***> wrote:
but starts_with is not for is-file-a-file comparison, but
is-file-in-a-directory check. There are no hardlinks involved (and AFAIK
apart from private implementation detail of macOS time machine, no OS
supports directory hardlinks).
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#42869 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AApc0j0s0uGzUojkJwFP7VFX8RpnGYzMks5twu0RgaJpZM4OEGyt>
.
|
Reported in rust-lang#838 Also see rust-lang/rust#42869
Reported in rust-lang#838 Also see rust-lang/rust#42869
Running `cargo test` on Windows gets flaky when calling `canonicalize()` on Path. This is because on Windows, the disk pathing for an absolute path starts with `//?/C:/foo/bar/`. See Rust GitHub issue and discussion here [1]. To resolve, add a helper function that remove the `//?/` from the canonical path. [1] - rust-lang/rust#42869
Running `cargo test` on Windows gets flaky when calling `canonicalize()` on Path. This is because on Windows, the disk pathing for an absolute path starts with `//?/C:/foo/bar/`. See Rust GitHub issue and discussion here [1]. To resolve, add a helper function that remove the `//?/` from the canonical path. [1] - rust-lang/rust#42869
It's more than 5 year now, do we have a reasonable solution? |
We now have Removing the |
Thanks for that but it's still extremely frustrating. No matter what I try I still get:
In my csv output file. Using: let file_path = path::absolute(path)?.to_string_lossy().to_string(); The expected value is I tried |
What's the input path? |
It's like |
Hm, using |
You're right, it worked, it just that I used the wrong binary. Many thanks! |
…3502) Call `dunce::canonicalize` rather than `Path::canonicalize`. On Linux/Macos, this just calls `std::fs::canonicalize()`, so no change there. PathBuf::canonicalize on Windows generally returns Windows NT UNC paths like `\\?\C:\Users\you\`. Lots of programs, including Microsoft's, don't work with these kinds of paths. For example, `npm` will print out a message something like `The current working directory is a UNC path. I'm going to use C:\Windows instead.`. dunce: > ... converts paths to legacy format whenever possible, but leaves UNC paths as-is when they can’t be unambiguously expressed in a simpler way. This allows legacy programs to access all paths they can possibly access, and UNC-aware programs to access all paths. rust-lang/rust#42869 (comment) Part of https://dfinity.atlassian.net/browse/SDK-1343
…nonicalize()` (#84) On Windows, `std::fs::canonicalize()` [produces a weird type of path that cannot be used with `Command::current_dir()`](rust-lang/rust#42869). This switches to using the [`normpath`](https://docs.rs/normpath/latest/normpath/trait.PathExt.html#tymethod.normalize) crate, which should avoid these issues.
Hi, I hope this is the right forum/format to register this problem, let me know if it's not.
Today I tried to use
std::fs::canonicalize
to make a path absolute so that I could execute it withstd::process::Command
.canonicalize
returns so-called "UNC paths", which look like this:\\?\C:\foo\bar\...
(sometimes the?
can be a hostname).It turns out you can't pass a UNC path as the current directory when starting a process (i.e.,
Command::new(...).current_dir(unc_path)
). In fact, a lot of other apps will blow up if you pass them a UNC path: for example, Microsoft's owncl.exe
compiler doesn't support it: rust-lang/cc-rs#169It feels to me that maybe returning UNC paths from canonicalize is the wrong choice, given that they don't work in so many places. It'd probably be better to return a simple "absolute path", which begins with the drive letter, instead of returning a UNC path, and instead provide a separate function specifically for generating UNC paths for people who need them.
Maybe if this is too much of an incompatible change, a new function for creating absolute paths should be added to std? I'd bet, however, that making the change to
canonicalize
itself would suddenly make more software suddenly start working rather than suddenly break.The text was updated successfully, but these errors were encountered: