Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ impl<'gctx> InstallablePackage<'gctx> {
self.gctx.shell().status("Installing", &self.pkg)?;

let dst = self.root.join("bin").into_path_unlocked();
// `dst` is usually absolute; if not, make it absolute so messages are clearer.
// See: https://github.com/rust-lang/cargo/issues/16023
Copy link
Member

Choose a reason for hiding this comment

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

The two duplicate make-absolute code actually fix two different warnings, should we make one commit for each to make the intent more obvious?

let cwd = self.gctx.cwd();
let dst_abs_root = if dst.is_absolute() {
paths::normalize_path(dst.as_path())
} else {
paths::normalize_path(&cwd.join(&dst))
};

let mut td_opt = None;
let mut needs_cleanup = false;
Expand Down Expand Up @@ -458,13 +466,18 @@ impl<'gctx> InstallablePackage<'gctx> {
// Move the temporary copies into `dst` starting with new binaries.
for bin in to_install.iter() {
let src = staging_dir.path().join(bin);
let dst = dst.join(bin);
self.gctx.shell().status("Installing", dst.display())?;
let dst_rel = dst.join(bin);
let dst_abs = dst_abs_root.join(bin);
self.gctx.shell().status("Installing", dst_abs.display())?;
if !dry_run {
fs::rename(&src, &dst).with_context(|| {
format!("failed to move `{}` to `{}`", src.display(), dst.display())
fs::rename(&src, &dst_rel).with_context(|| {
format!(
"failed to move `{}` to `{}`",
src.display(),
dst_abs.display()
)
})?;
installed.bins.push(dst);
installed.bins.push(dst_rel);
successful_bins.insert(bin.to_string());
}
}
Expand All @@ -475,11 +488,16 @@ impl<'gctx> InstallablePackage<'gctx> {
let mut try_install = || -> CargoResult<()> {
for &bin in to_replace.iter() {
let src = staging_dir.path().join(bin);
let dst = dst.join(bin);
self.gctx.shell().status("Replacing", dst.display())?;
let dst_rel = dst.join(bin);
let dst_abs = dst_abs_root.join(bin);
self.gctx.shell().status("Replacing", dst_abs.display())?;
if !dry_run {
fs::rename(&src, &dst).with_context(|| {
format!("failed to move `{}` to `{}`", src.display(), dst.display())
fs::rename(&src, &dst_rel).with_context(|| {
format!(
"failed to move `{}` to `{}`",
src.display(),
dst_abs.display()
)
})?;
successful_bins.insert(bin.to_string());
}
Expand Down Expand Up @@ -777,14 +795,22 @@ pub fn install(
if installed_anything {
// Print a warning that if this directory isn't in PATH that they won't be
// able to run these commands.
// `dst` is usually absolute; if not, make it absolute so messages are clearer.
// See: https://github.com/rust-lang/cargo/issues/16023
let cwd = gctx.cwd();
let dst_abs = if dst.is_absolute() {
paths::normalize_path(dst.as_path())
} else {
paths::normalize_path(&cwd.join(&dst))
};
let path = gctx.get_env_os("PATH").unwrap_or_default();
let dst_in_path = env::split_paths(&path).any(|path| path == dst);

if !dst_in_path {
gctx.shell().warn(&format!(
"be sure to add `{}` to your PATH to be \
able to run the installed binaries",
dst.display()
dst_abs.display()
))?;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,9 @@ fn relative_install_location_without_trailing_slash() {
[INSTALLING] foo v0.0.1 ([ROOT]/foo)
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
[INSTALLING] t1/bin/foo[EXE]
[INSTALLING] [ROOT]/foo/t1/bin/foo[EXE]
[INSTALLED] package `foo v0.0.1 ([ROOT]/foo)` (executable `foo[EXE]`)
[WARNING] be sure to add `t1/bin` to your PATH to be able to run the installed binaries
[WARNING] be sure to add `[ROOT]/foo/t1/bin` to your PATH to be able to run the installed binaries

"#]])
.run();
Expand Down