Defer bailing out workspace root probing
authorAlex Crichton <alex@alexcrichton.com>
Wed, 13 Dec 2017 21:19:12 +0000 (13:19 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 13 Dec 2017 21:19:12 +0000 (13:19 -0800)
This commit alters the logic to bail out on probing for `CARGO_HOME` to
do it a little later rather than early on in the loop iteration, notably
allowing members to reside in `CARGO_HOME` itself.

Closes #4815

src/cargo/core/workspace.rs
tests/workspaces.rs

index dee5b248d6a1ca47ec18b9ea979c0cc6f90fc3de..c83dc63426bdfa6bd61b4bd28e51b9c4b1a1bb64 100644 (file)
@@ -333,9 +333,6 @@ impl<'cfg> Workspace<'cfg> {
         }
 
         for path in paths::ancestors(manifest_path).skip(2) {
-            if self.config.home() == path {
-                return Ok(None);
-            }
             let ances_manifest_path = path.join("Cargo.toml");
             debug!("find_root - trying {}", ances_manifest_path.display());
             if ances_manifest_path.exists() {
@@ -354,6 +351,15 @@ impl<'cfg> Workspace<'cfg> {
                     WorkspaceConfig::Member { .. } => {}
                 }
             }
+
+            // Don't walk across `CARGO_HOME` when we're looking for the
+            // workspace root. Sometimes a project will be organized with
+            // `CARGO_HOME` pointing inside of the workspace root or in the
+            // current project, but we don't want to mistakenly try to put
+            // crates.io crates into the workspace by accident.
+            if self.config.home() == path {
+                break
+            }
         }
 
         Ok(None)
index 19214dbd0798a0124739a28ea7798c3d6993a716..5eb01a0801072027aeb05e329f2a75bd36f1c8db 100644 (file)
@@ -1782,3 +1782,28 @@ fn include_and_exclude() {
     assert_that(&p.root().join("foo/bar/target"), existing_dir());
 }
 */
+
+#[test]
+fn cargo_home_at_root_works() {
+    let p = project("lib")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "lib"
+            version = "0.1.0"
+
+            [workspace]
+            members = ["a"]
+        "#)
+        .file("src/lib.rs", "")
+        .file("a/Cargo.toml", r#"
+            [package]
+            name = "a"
+            version = "0.1.0"
+        "#)
+        .file("a/src/lib.rs", "");
+    let p = p.build();
+
+    assert_that(p.cargo("build"), execs().with_status(0));
+    assert_that(p.cargo("build").arg("--frozen").env("CARGO_HOME", p.root()),
+                execs().with_status(0));
+}