Skip to content

refactor: Consolidate creation of SourceId from manifest path #15172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/cargo/core/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceId> {
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> {
SourceId::new(SourceKind::Git(reference), url.clone(), None)
Expand Down
13 changes: 4 additions & 9 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl<'gctx> Workspace<'gctx> {
BTreeMap<String, BTreeMap<String, TomlDependency<ConfigRelativePath>>>,
> = 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();

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -1979,8 +1975,7 @@ pub fn find_workspace_root(
gctx: &GlobalContext,
) -> CargoResult<Option<PathBuf>> {
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()
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down