Avoid unnecessary rebuild on local installation
authorBoris Egorov <egorov@linux.com>
Sat, 20 Feb 2016 09:18:29 +0000 (15:18 +0600)
committerBoris Egorov <egorov@linux.com>
Mon, 22 Feb 2016 08:04:32 +0000 (14:04 +0600)
Fixes #2143

src/cargo/ops/cargo_install.rs
tests/test_cargo_install.rs

index 36fdabaf49615f9731841bc60d532d13c9c12719..cf4532cb94ad3a35676085cff41dc4c2816a5c9f 100644 (file)
@@ -71,7 +71,11 @@ pub fn install(root: Option<&str>,
     let dst = root.join("bin");
     try!(check_overwrites(&dst, &pkg, &opts.filter, &list));
 
-    let target_dir = config.cwd().join("target-install");
+    let target_dir = if source_id.is_path() {
+        config.target_dir(&pkg)
+    } else {
+        config.cwd().join("target-install")
+    };
     config.set_target_dir(&target_dir);
     let compile = try!(ops::compile_pkg(&pkg, Some(source), opts).chain_error(|| {
         human(format!("failed to compile `{}`, intermediate artifacts can be \
@@ -89,7 +93,10 @@ pub fn install(root: Option<&str>,
         }));
         t.bins.push(dst);
     }
-    try!(fs::remove_dir_all(&target_dir));
+
+    if !source_id.is_path() {
+        try!(fs::remove_dir_all(&target_dir));
+    }
 
     list.v1.entry(pkg.package_id().clone()).or_insert_with(|| {
         BTreeSet::new()
index f5e42c99408f18dc12fbcb3e8cc03d4deb035dd5..db00ef614fb8600dc826465238ac2fd66bc6c9cc 100644 (file)
@@ -2,6 +2,7 @@ use std::fmt;
 use std::fs::{self, File};
 use std::io::prelude::*;
 use std::path::{Path, PathBuf};
+use support::paths::CargoPathExt;
 
 use cargo::util::ProcessBuilder;
 use hamcrest::{assert_that, existing_file, is_not, Matcher, MatchResult};
@@ -403,7 +404,7 @@ test!(compile_failure {
 error: main function not found
 error: aborting due to previous error
 failed to compile `foo v0.1.0 (file://[..])`, intermediate artifacts can be \
-    found at `[..]target-install`
+    found at `[..]target`
 
 Caused by:
   Could not compile `foo`.
@@ -543,3 +544,27 @@ test!(installs_from_cwd_by_default {
     assert_that(cargo_process("install"), execs().with_status(0));
     assert_that(cargo_home(), has_installed_exe("foo"));
 });
+
+test!(do_not_rebuilds_on_local_install {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.1.0"
+            authors = []
+        "#)
+        .file("src/main.rs", "fn main() {}");
+
+    assert_that(p.cargo_process("build").arg("--release"),
+                execs().with_status(0));
+    assert_that(cargo_process("install").arg("--path").arg(p.root()),
+                execs().with_status(0).with_stdout("\
+  Installing [..]
+").with_stderr("\
+be sure to add `[..]` to your PATH to be able to run the installed binaries
+"));
+
+    assert!(p.build_dir().c_exists());
+    assert!(p.release_bin("foo").c_exists());
+    assert_that(cargo_home(), has_installed_exe("foo"));
+});