diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index b5d1da8a253..ee74c31a4de 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -1702,10 +1702,17 @@ fn find_workspace_root_with_loader( mut loader: impl FnMut(&Path) -> CargoResult>, ) -> CargoResult> { // Check if there are any workspace roots that have already been found that would work - for (ws_root, ws_root_config) in config.ws_roots.borrow().iter() { - if manifest_path.starts_with(ws_root) && !ws_root_config.is_excluded(manifest_path) { - // Add `Cargo.toml` since ws_root is the root and not the file - return Ok(Some(ws_root.join("Cargo.toml").clone())); + { + let roots = config.ws_roots.borrow(); + // Iterate through the manifests parent directories until we find a workspace + // root. Note we skip the first item since that is just the path itself + for current in manifest_path.ancestors().skip(1) { + if let Some(ws_config) = roots.get(current) { + if !ws_config.is_excluded(manifest_path) { + // Add `Cargo.toml` since ws_root is the root and not the file + return Ok(Some(current.join("Cargo.toml"))); + } + } } }