refactor: Consolidate creation of SourceId from manifest path (#15172)

### 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
This commit is contained in:
Weihang Lo 2025-02-12 00:41:17 +00:00 committed by GitHub
commit c52d4da4e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 10 deletions

View File

@ -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)

View File

@ -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();
@ -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<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()

View File

@ -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) => {