Skip to content

Commit 24904ce

Browse files
committed
refator: do not use two paths
1 parent 82d730a commit 24904ce

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,14 @@ impl<'gctx> InstallablePackage<'gctx> {
316316
fn install_one(mut self, dry_run: bool) -> CargoResult<bool> {
317317
self.gctx.shell().status("Installing", &self.pkg)?;
318318

319-
let dst = self.root.join("bin").into_path_unlocked();
320-
// `dst` is usually absolute; if not, make it absolute so messages are clearer.
319+
// Normalize to absolute path for consistency throughout.
321320
// See: https://github.com/rust-lang/cargo/issues/16023
321+
let dst_path = self.root.join("bin").into_path_unlocked();
322322
let cwd = self.gctx.cwd();
323-
let dst_abs_root = if dst.is_absolute() {
324-
paths::normalize_path(dst.as_path())
323+
let dst = if dst_path.is_absolute() {
324+
paths::normalize_path(dst_path.as_path())
325325
} else {
326-
paths::normalize_path(&cwd.join(&dst))
326+
paths::normalize_path(&cwd.join(&dst_path))
327327
};
328328

329329
let mut td_opt = None;
@@ -466,18 +466,13 @@ impl<'gctx> InstallablePackage<'gctx> {
466466
// Move the temporary copies into `dst` starting with new binaries.
467467
for bin in to_install.iter() {
468468
let src = staging_dir.path().join(bin);
469-
let dst_rel = dst.join(bin);
470-
let dst_abs = dst_abs_root.join(bin);
471-
self.gctx.shell().status("Installing", dst_abs.display())?;
469+
let dst = dst.join(bin);
470+
self.gctx.shell().status("Installing", dst.display())?;
472471
if !dry_run {
473-
fs::rename(&src, &dst_rel).with_context(|| {
474-
format!(
475-
"failed to move `{}` to `{}`",
476-
src.display(),
477-
dst_abs.display()
478-
)
472+
fs::rename(&src, &dst).with_context(|| {
473+
format!("failed to move `{}` to `{}`", src.display(), dst.display())
479474
})?;
480-
installed.bins.push(dst_rel);
475+
installed.bins.push(dst);
481476
successful_bins.insert(bin.to_string());
482477
}
483478
}
@@ -488,16 +483,11 @@ impl<'gctx> InstallablePackage<'gctx> {
488483
let mut try_install = || -> CargoResult<()> {
489484
for &bin in to_replace.iter() {
490485
let src = staging_dir.path().join(bin);
491-
let dst_rel = dst.join(bin);
492-
let dst_abs = dst_abs_root.join(bin);
493-
self.gctx.shell().status("Replacing", dst_abs.display())?;
486+
let dst = dst.join(bin);
487+
self.gctx.shell().status("Replacing", dst.display())?;
494488
if !dry_run {
495-
fs::rename(&src, &dst_rel).with_context(|| {
496-
format!(
497-
"failed to move `{}` to `{}`",
498-
src.display(),
499-
dst_abs.display()
500-
)
489+
fs::rename(&src, &dst).with_context(|| {
490+
format!("failed to move `{}` to `{}`", src.display(), dst.display())
501491
})?;
502492
successful_bins.insert(bin.to_string());
503493
}
@@ -673,7 +663,15 @@ pub fn install(
673663
lockfile_path: Option<&Path>,
674664
) -> CargoResult<()> {
675665
let root = resolve_root(root, gctx)?;
676-
let dst = root.join("bin").into_path_unlocked();
666+
// Normalize to absolute path for consistency throughout.
667+
// See: https://github.com/rust-lang/cargo/issues/16023
668+
let dst_path = root.join("bin").into_path_unlocked();
669+
let cwd = gctx.cwd();
670+
let dst = if dst_path.is_absolute() {
671+
paths::normalize_path(dst_path.as_path())
672+
} else {
673+
paths::normalize_path(&cwd.join(&dst_path))
674+
};
677675
let map = SourceConfigMap::new(gctx)?;
678676

679677
let current_rust_version = if opts.honor_rust_version.unwrap_or(true) {
@@ -795,22 +793,14 @@ pub fn install(
795793
if installed_anything {
796794
// Print a warning that if this directory isn't in PATH that they won't be
797795
// able to run these commands.
798-
// `dst` is usually absolute; if not, make it absolute so messages are clearer.
799-
// See: https://github.com/rust-lang/cargo/issues/16023
800-
let cwd = gctx.cwd();
801-
let dst_abs = if dst.is_absolute() {
802-
paths::normalize_path(dst.as_path())
803-
} else {
804-
paths::normalize_path(&cwd.join(&dst))
805-
};
806796
let path = gctx.get_env_os("PATH").unwrap_or_default();
807-
let dst_in_path = env::split_paths(&path).any(|path| path == dst_abs);
797+
let dst_in_path = env::split_paths(&path).any(|path| path == dst);
808798

809799
if !dst_in_path {
810800
gctx.shell().warn(&format!(
811801
"be sure to add `{}` to your PATH to be \
812802
able to run the installed binaries",
813-
dst_abs.display()
803+
dst.display()
814804
))?;
815805
}
816806
}

0 commit comments

Comments
 (0)