Skip to content

Commit

Permalink
Patch path dependencies for git sources
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Aug 1, 2023
1 parent 15380d2 commit b3b8647
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
30 changes: 28 additions & 2 deletions scarb/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use petgraph::graphmap::DiGraphMap;

use crate::core::registry::Registry;
use crate::core::resolver::Resolve;
use crate::core::{PackageId, Summary};
use crate::core::{ManifestDependency, PackageId, Summary};

/// Builds the list of all packages required to build the first argument.
///
Expand All @@ -24,6 +24,7 @@ use crate::core::{PackageId, Summary};
#[tracing::instrument(level = "trace", skip_all)]
pub async fn resolve(summaries: &[Summary], registry: &dyn Registry) -> Result<Resolve> {
// TODO(#2): This is very bad, use PubGrub here.

let mut graph = DiGraphMap::new();

let mut packages: HashMap<_, _> = HashMap::from_iter(
Expand All @@ -45,7 +46,9 @@ pub async fn resolve(summaries: &[Summary], registry: &dyn Registry) -> Result<R
graph.add_node(package_id);

for dep in summaries[&package_id].clone().full_dependencies() {
let results = registry.query(dep).await?;
let dep = rewrite_dependency_source_id(registry, &package_id, dep).await?;

let results = registry.query(&dep).await?;

let Some(dep_summary) = results.first() else {
bail!("cannot find package {}", dep.name)
Expand Down Expand Up @@ -115,6 +118,29 @@ pub async fn resolve(summaries: &[Summary], registry: &dyn Registry) -> Result<R
Ok(Resolve { graph })
}

async fn rewrite_dependency_source_id(
registry: &dyn Registry,
package_id: &PackageId,
dependency: &ManifestDependency,
) -> Result<ManifestDependency> {
// Rewrite path dependencies for git sources.
if package_id.source_id.is_git() && dependency.source_id.is_path() {
let rewritten_dep = ManifestDependency {
name: dependency.name.clone(),
version_req: dependency.version_req.clone(),
source_id: package_id.source_id,
};
// Check if this dependency can be queried from git source.
// E.g. packages below other package's manifest will not be accessible.
if !registry.query(&rewritten_dep).await?.is_empty() {
// If it is, return rewritten dependency.
return Ok(rewritten_dep);
}
};

Ok(dependency.clone())
}

#[cfg(test)]
mod tests {
//! These tests largely come from Elixir's `hex_solver` test suite.
Expand Down
46 changes: 45 additions & 1 deletion scarb/tests/git_source.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::collections::HashMap;
use std::fs;

use assert_fs::prelude::*;
use assert_fs::TempDir;
use gix::refs::transaction::PreviousValue;
use indoc::{formatdoc, indoc};
use scarb_metadata::Metadata;

use scarb_test_support::command::Scarb;
use scarb_test_support::command::{CommandExt, Scarb};
use scarb_test_support::fsx::ChildPathEx;
use scarb_test_support::gitx;
use scarb_test_support::project_builder::ProjectBuilder;
Expand Down Expand Up @@ -378,3 +380,45 @@ fn force_push() {
[..] Finished release target(s) in [..]
"#});
}

#[test]
fn transitive_path_dep() {
let git_dep = gitx::new("dep1", |t| {
ProjectBuilder::start()
.name("dep0")
.lib_cairo("fn hello() -> felt252 { 42 }")
.build(&t.child("zero"));
ProjectBuilder::start()
.name("dep1")
.lib_cairo("fn hello() -> felt252 { dep0::hello() }")
.dep("dep0", r#" path = "../zero" "#)
.build(&t.child("one"));
});

let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("hello")
.version("1.0.0")
.dep("dep0", &git_dep)
.dep("dep1", &git_dep)
.lib_cairo("fn world() -> felt252 { dep1::hello() }")
.build(&t);

let metadata = Scarb::quick_snapbox()
.args(["--json", "metadata", "--format-version", "1"])
.current_dir(&t)
.stdout_json::<Metadata>();

assert_eq!(metadata.packages.len(), 4);

let pkgs = metadata
.packages
.iter()
.map(|pkg| (pkg.name.clone(), pkg.source.to_string()))
.collect::<HashMap<String, _>>();

assert_eq!(pkgs["core"], "std");
assert!(pkgs["hello"].starts_with("path+"));
assert!(pkgs["dep0"].starts_with("git+"));
assert!(pkgs["dep1"].starts_with("git+"));
}

0 comments on commit b3b8647

Please sign in to comment.