Skip to content

Commit

Permalink
refactor: Consolidate creation of SourceId from manifest path (#15172)
Browse files Browse the repository at this point in the history
### What does this PR try to resolve?

This preps more features for cargo-script support and makes it clearer
where we don't yet support it.

### How should we test and review this PR?

### Additional information
  • Loading branch information
weihanglo authored Feb 12, 2025
2 parents 81d8406 + 080f747 commit c52d4da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
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

0 comments on commit c52d4da

Please sign in to comment.