Skip to content
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

Don't canonicalize executable path #9991

Merged
merged 3 commits into from
Oct 23, 2021

Conversation

Byron
Copy link
Member

@Byron Byron commented Oct 21, 2021

Otherwise symbolic links may also accidentally be resolved which may lead to unexpected results in the case of 'coreutils', a binary that depends on the executable name being a symbolic link.

This means a path like /bin/echo being canonicalized to /bin/coreutils will loose all information about the desired functionality.

For example, test failures will occur if 'echo' is resolved that way and it's not trivial to find the cause of it in the provided error messages. For exampledoc_workspace_open_different_library_and_package_names did fail for me on MacOS, Nix packages in PATH, but works with this patch.

With this patch, there is still the possibility that a path gets canonicalized for its relative path components, but still results in changing the name of the binary. I could imagine to check for binary name changes and panic if coreutils or busybox is encountered, which are known to fail without a symlink telling them which program to emulate.

Otherwise symbolic links may also accidentally be resolved which may
lead to unexpected results in the case of 'coreutils', a binary
that depends on the executable name being a symbolic link.

This means a path like /bin/echo being canonicalized to /bin/coreutils
will loose all information about the desired functionality.

Test failures will occur if 'echo' is resolved that way and it's
not trivial to find the cause of it in the provided error messages.
@rust-highfive
Copy link

r? @ehuss

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 21, 2021
@joshtriplett
Copy link
Member

Might it make sense to get the directory name of the path, canonicalize that, then re-append the basename? That would allow unconditionally canonicalizing without affecting argv[0].

@Byron
Copy link
Member Author

Byron commented Oct 21, 2021

@joshtriplett I very much like the idea, and think it would be great to do that transformation right before trying to canonicalize the path. This means it would still avoid any canonicalization if there is seemingly no need due to the lack of relative path components.

If that sounds good, I can adjust the PR accordingly.

@alexcrichton
Copy link
Member

Typically in Cargo we're very wary of the canonicalize function and try to use it as little as possible, so I was curious why this was used in this case.

It turns out this is somewhat of a storied history. From what I can tell the canonicalize() call was introduced in #3789 and AFAIK there was no discussion of it at the time, it was simply there. In #4634 when removing the reliance on current_exe that kept the calls to canonicalize() and propagated it to a few more places. Finally #5359 refactored to basically what it is today.

From looking at the history I don't know of any reason why we need to use canonicalize() here, and ideally we'd remove it entirely. I think everything should work here if we do current_dir().join(the_exe) instead of canonicalize since if the exe is a root path then that takes over, otherwise if it's relative then it's relative to the current directory.

@Byron
Copy link
Member Author

Byron commented Oct 23, 2021

@alexcrichton Great detective work! I have put it to the test, removing canonicalization entirely.

@joshtriplett joshtriplett changed the title Only canonicalize executable path if it has relative directories Don't canonicalize executable path Oct 23, 2021
@joshtriplett
Copy link
Member

@Byron There's another call to canonicalize a few lines later.

@Byron Byron force-pushed the fix-test-failure-due-to-echo-resolution branch from f4e8ec8 to cf8e464 Compare October 23, 2021 07:29
@joshtriplett
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Oct 23, 2021

📌 Commit cf8e464 has been approved by joshtriplett

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 23, 2021
@bors
Copy link
Contributor

bors commented Oct 23, 2021

⌛ Testing commit cf8e464 with merge e165bc8...

@bors
Copy link
Contributor

bors commented Oct 23, 2021

☀️ Test successful - checks-actions
Approved by: joshtriplett
Pushing e165bc8 to master...

@bors bors merged commit e165bc8 into rust-lang:master Oct 23, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 26, 2021
Update cargo

4 commits in 7fbbf4e8f23e3c24b8afff541dcb17e53eb5ff88..6c1bc24b8b49d4bc965f67d7037906dc199c72b7
2021-10-19 02:16:48 +0000 to 2021-10-24 17:51:41 +0000
- Fix a clippy warning (rust-lang/cargo#10002)
- Upgrade Cargo to the 2021 edition (rust-lang/cargo#10000)
- Don't canonicalize executable path (rust-lang/cargo#9991)
- Bump to 0.59.0, update changelog (rust-lang/cargo#9998)
@ehuss ehuss added this to the 1.58.0 milestone Feb 6, 2022
@Byron Byron deleted the fix-test-failure-due-to-echo-resolution branch February 24, 2022 02:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants