Skip to content

Commit c52d4da

Browse files
authored
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
2 parents 81d8406 + 080f747 commit c52d4da

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/cargo/core/source_id.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ impl SourceId {
204204
SourceId::new(SourceKind::Path, url, None)
205205
}
206206

207+
/// Creates a `SourceId` from a filesystem path.
208+
///
209+
/// `path`: an absolute path.
210+
pub fn for_manifest_path(manifest_path: &Path) -> CargoResult<SourceId> {
211+
if crate::util::toml::is_embedded(manifest_path) {
212+
Self::for_path(manifest_path)
213+
} else {
214+
Self::for_path(manifest_path.parent().unwrap())
215+
}
216+
}
217+
207218
/// Creates a `SourceId` from a Git reference.
208219
pub fn for_git(url: &Url, reference: GitReference) -> CargoResult<SourceId> {
209220
SourceId::new(SourceKind::Git(reference), url.clone(), None)

src/cargo/core/workspace.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl<'gctx> Workspace<'gctx> {
447447
BTreeMap<String, BTreeMap<String, TomlDependency<ConfigRelativePath>>>,
448448
> = self.gctx.get("patch")?;
449449

450-
let source = SourceId::for_path(self.root())?;
450+
let source = SourceId::for_manifest_path(self.root_manifest())?;
451451

452452
let mut warnings = Vec::new();
453453

@@ -1144,7 +1144,7 @@ impl<'gctx> Workspace<'gctx> {
11441144
if let Some(p) = loaded.get(manifest_path).cloned() {
11451145
return Ok(p);
11461146
}
1147-
let source_id = SourceId::for_path(manifest_path.parent().unwrap())?;
1147+
let source_id = SourceId::for_manifest_path(manifest_path)?;
11481148
let package = ops::read_package(manifest_path, source_id, self.gctx)?;
11491149
loaded.insert(manifest_path.to_path_buf(), package.clone());
11501150
Ok(package)
@@ -1817,11 +1817,7 @@ impl<'gctx> Packages<'gctx> {
18171817
match self.packages.entry(manifest_path.to_path_buf()) {
18181818
Entry::Occupied(e) => Ok(e.into_mut()),
18191819
Entry::Vacant(v) => {
1820-
let source_id = if crate::util::toml::is_embedded(manifest_path) {
1821-
SourceId::for_path(manifest_path)?
1822-
} else {
1823-
SourceId::for_path(manifest_path.parent().unwrap())?
1824-
};
1820+
let source_id = SourceId::for_manifest_path(manifest_path)?;
18251821
let manifest = read_manifest(manifest_path, source_id, self.gctx)?;
18261822
Ok(v.insert(match manifest {
18271823
EitherManifest::Real(manifest) => {
@@ -1979,8 +1975,7 @@ pub fn find_workspace_root(
19791975
gctx: &GlobalContext,
19801976
) -> CargoResult<Option<PathBuf>> {
19811977
find_workspace_root_with_loader(manifest_path, gctx, |self_path| {
1982-
let key = self_path.parent().unwrap();
1983-
let source_id = SourceId::for_path(key)?;
1978+
let source_id = SourceId::for_manifest_path(self_path)?;
19841979
let manifest = read_manifest(self_path, source_id, gctx)?;
19851980
Ok(manifest
19861981
.workspace_config()

src/cargo/util/toml/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ fn inheritable_from_path(
946946
return Ok(ws_root.inheritable().clone());
947947
};
948948

949-
let source_id = SourceId::for_path(workspace_path_root)?;
949+
let source_id = SourceId::for_manifest_path(&workspace_path)?;
950950
let man = read_manifest(&workspace_path, source_id, gctx)?;
951951
match man.workspace_config() {
952952
WorkspaceConfig::Root(root) => {

0 commit comments

Comments
 (0)