diff --git a/src/cargo/core/source_id.rs b/src/cargo/core/source_id.rs index eec12567f13..8c916b7b445 100644 --- a/src/cargo/core/source_id.rs +++ b/src/cargo/core/source_id.rs @@ -204,6 +204,17 @@ impl SourceId { SourceId::new(SourceKind::Path, url, None) } + /// Creates a `SourceId` from a filesystem path. + /// + /// `path`: an absolute path. + pub fn for_manifest_path(manifest_path: &Path) -> CargoResult { + if crate::util::toml::is_embedded(manifest_path) { + Self::for_path(manifest_path) + } else { + Self::for_path(manifest_path.parent().unwrap()) + } + } + /// Creates a `SourceId` from a Git reference. pub fn for_git(url: &Url, reference: GitReference) -> CargoResult { SourceId::new(SourceKind::Git(reference), url.clone(), None) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index b988ca6686a..277af3587d7 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -447,7 +447,7 @@ impl<'gctx> Workspace<'gctx> { BTreeMap>>, > = self.gctx.get("patch")?; - let source = SourceId::for_path(self.root())?; + let source = SourceId::for_manifest_path(self.root_manifest())?; let mut warnings = Vec::new(); @@ -1144,7 +1144,7 @@ impl<'gctx> Workspace<'gctx> { if let Some(p) = loaded.get(manifest_path).cloned() { return Ok(p); } - let source_id = SourceId::for_path(manifest_path.parent().unwrap())?; + let source_id = SourceId::for_manifest_path(manifest_path)?; let package = ops::read_package(manifest_path, source_id, self.gctx)?; loaded.insert(manifest_path.to_path_buf(), package.clone()); Ok(package) @@ -1817,11 +1817,7 @@ impl<'gctx> Packages<'gctx> { match self.packages.entry(manifest_path.to_path_buf()) { Entry::Occupied(e) => Ok(e.into_mut()), Entry::Vacant(v) => { - let source_id = if crate::util::toml::is_embedded(manifest_path) { - SourceId::for_path(manifest_path)? - } else { - SourceId::for_path(manifest_path.parent().unwrap())? - }; + let source_id = SourceId::for_manifest_path(manifest_path)?; let manifest = read_manifest(manifest_path, source_id, self.gctx)?; Ok(v.insert(match manifest { EitherManifest::Real(manifest) => { @@ -1979,8 +1975,7 @@ pub fn find_workspace_root( gctx: &GlobalContext, ) -> CargoResult> { find_workspace_root_with_loader(manifest_path, gctx, |self_path| { - let key = self_path.parent().unwrap(); - let source_id = SourceId::for_path(key)?; + let source_id = SourceId::for_manifest_path(self_path)?; let manifest = read_manifest(self_path, source_id, gctx)?; Ok(manifest .workspace_config() diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index e66c5a00aa4..85aeb8697a7 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -946,7 +946,7 @@ fn inheritable_from_path( return Ok(ws_root.inheritable().clone()); }; - let source_id = SourceId::for_path(workspace_path_root)?; + let source_id = SourceId::for_manifest_path(&workspace_path)?; let man = read_manifest(&workspace_path, source_id, gctx)?; match man.workspace_config() { WorkspaceConfig::Root(root) => {