Skip to content

Commit

Permalink
fix: refine codes
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Nov 6, 2024
1 parent 8e03fa7 commit b346ef9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 56 deletions.
4 changes: 2 additions & 2 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ pub async fn execute_cmd(
let desired_pkg_type_: PkgType = package_type_str.parse()?;
let (
desired_pkg_src_name_,
desired_pkg_requested_version,
desired_pkg_src_version_str_,
desired_pkg_src_version_,
) = parse_pkg_name_version(&command_data.package_name.unwrap())?;

Expand Down Expand Up @@ -559,7 +559,7 @@ pub async fn execute_cmd(
name: desired_pkg_src_name_.clone(),
},
version_req: desired_pkg_src_version_.clone(),
original_version_req: desired_pkg_requested_version,
version_req_str: desired_pkg_src_version_str_,
},
};
extra_dependency_relationships.push(extra_dependency_relationship);
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/manifest_lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl<'a> From<&'a ManifestLockItem> for PkgInfo {
name: dep.name,
},
version_req: VersionReq::STAR,
original_version_req: None,
version_req_str: None,
})
.collect()
})
Expand Down
12 changes: 6 additions & 6 deletions core/src/ten_manager/src/solver/introducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn extract_introducer_relations_from_raw_solver_results(
// The `version` declared in the `dependencies` section in
// manifest.json is always present.
requested_version_str = requested_dep_in_introducer
.original_version_req
.version_req_str
.as_ref()
.unwrap()
.clone();
Expand All @@ -88,7 +88,7 @@ pub fn extract_introducer_relations_from_raw_solver_results(

pub fn get_dependency_chain(
introducer: &PkgInfo,
conflicted_pkg: &PkgInfo,
conflict_pkg: &PkgInfo,
introducer_relations: &HashMap<PkgInfo, (String, Option<PkgInfo>)>,
) -> Vec<(String, PkgInfo)> {
let mut chain = Vec::new();
Expand All @@ -99,17 +99,17 @@ pub fn get_dependency_chain(
// dependency chain first.
let requested_dep_in_introducer = introducer
.get_dependency_by_type_and_name(
&conflicted_pkg.pkg_identity.pkg_type.to_string(),
&conflicted_pkg.pkg_identity.name,
&conflict_pkg.pkg_identity.pkg_type.to_string(),
&conflict_pkg.pkg_identity.name,
)
.unwrap();
chain.push((
requested_dep_in_introducer
.original_version_req
.version_req_str
.as_ref()
.unwrap()
.clone(),
conflicted_pkg.clone(),
conflict_pkg.clone(),
));

loop {
Expand Down
91 changes: 51 additions & 40 deletions core/src/ten_manager/src/solver/solver_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ use crate::{
};

#[derive(Debug)]
pub struct ConflictPkg {
pub struct ConflictPkgVersion {
// The version of the pkg that causes the conflict, introduced by the
// following introducer.
pub pkg_version: String,

pub introducer_type: String,
pub introducer_name: String,
pub introducer_version: String,

// The version of the pkg that causes the conflict, introduced by the
// introducer.
pub pkg_version: String,
}

#[derive(Debug)]
pub struct ConflictInfo {
pub conflict_pkg_type: String,
pub conflict_pkg_name: String,

// The introducers are needed to get the dependency chain. The conflicted
// pkg (i.e., pkg_type and pkg_name) can not be used to get the dependency
// chain, considering the following scenario:
Expand All @@ -51,10 +54,9 @@ pub struct ConflictInfo {
// └─ [extension]D@0.2.1
//
// But what we want is the chain leading to D@0.2.0.
pub left: ConflictPkg,
pub right: ConflictPkg,
pub pkg_type: String,
pub pkg_name: String,
pub conflict_pkg_version_1: ConflictPkgVersion,
pub conflict_pkg_version_2: ConflictPkgVersion,

pub error_message: String,
}

Expand Down Expand Up @@ -93,20 +95,22 @@ pub fn parse_error_statement(clingo_output: &[String]) -> Result<ConflictInfo> {
.replace("{9}", &introducer2_version);

return Ok(ConflictInfo {
left: ConflictPkg {
conflict_pkg_type: pkg_type,
conflict_pkg_name: pkg_name,

conflict_pkg_version_1: ConflictPkgVersion {
introducer_type: introducer1_type,
introducer_name: introducer1_name,
introducer_version: introducer1_version,
pkg_version: version1,
},
right: ConflictPkg {
conflict_pkg_version_2: ConflictPkgVersion {
introducer_type: introducer2_type,
introducer_name: introducer2_name,
introducer_version: introducer2_version,
pkg_version: version2,
},
pkg_type,
pkg_name,

error_message,
});
}
Expand Down Expand Up @@ -150,50 +154,57 @@ pub fn print_conflict_info(
);
println!();

// Get PkgInfo for both versions
let pkg_info1 = get_pkg_info_from_candidates(
&conflict_info.left.introducer_type,
&conflict_info.left.introducer_name,
&conflict_info.left.introducer_version,
// Get PkgInfo for both introducer packages.
let introducer_pkg_info_1 = get_pkg_info_from_candidates(
&conflict_info.conflict_pkg_version_1.introducer_type,
&conflict_info.conflict_pkg_version_1.introducer_name,
&conflict_info.conflict_pkg_version_1.introducer_version,
all_candidates,
)?;
let pkg_info2 = get_pkg_info_from_candidates(
&conflict_info.right.introducer_type,
&conflict_info.right.introducer_name,
&conflict_info.right.introducer_version,
let introducer_pkg_info_2 = get_pkg_info_from_candidates(
&conflict_info.conflict_pkg_version_2.introducer_type,
&conflict_info.conflict_pkg_version_2.introducer_name,
&conflict_info.conflict_pkg_version_2.introducer_version,
all_candidates,
)?;

// The pkg_info1/pkg_info2 (i.e., the introducer) depends on the pkg
// identified by `pkg_name` (i.e., the leaf node). The pkg_version used
// to get the pkg_info does not matter, as it will be replaced by the
// original_version_req in the introducer, refer to
// `get_dependency_chain()`.
// The introducer_pkg_info_1/introducer_pkg_info_2 (i.e., the introducer)
// depends on the pkg identified by `conflict_pkg_type` &
// `conflict_pkg_name` (i.e., the leaf node). The conflict_pkg_version_x
// used to get the pkg_info does not matter, as it will be replaced by
// the `version_req_str` of the introducer, refer
// to `get_dependency_chain()`.
let leaf_node_pkg = get_pkg_info_from_candidates(
&conflict_info.pkg_type,
&conflict_info.pkg_name,
&conflict_info.left.pkg_version,
&conflict_info.conflict_pkg_type,
&conflict_info.conflict_pkg_name,
&conflict_info.conflict_pkg_version_1.pkg_version,
all_candidates,
)?;

// Get dependency chains
let chain1 =
get_dependency_chain(&pkg_info1, &leaf_node_pkg, introducer_relations);
let chain2 =
get_dependency_chain(&pkg_info2, &leaf_node_pkg, introducer_relations);
let chain1 = get_dependency_chain(
&introducer_pkg_info_1,
&leaf_node_pkg,
introducer_relations,
);
let chain2 = get_dependency_chain(
&introducer_pkg_info_2,
&leaf_node_pkg,
introducer_relations,
);

print_dependency_chain(
&chain1,
&conflict_info.pkg_type,
&conflict_info.pkg_name,
&conflict_info.left.pkg_version,
&conflict_info.conflict_pkg_type,
&conflict_info.conflict_pkg_name,
&conflict_info.conflict_pkg_version_1.pkg_version,
);

print_dependency_chain(
&chain2,
&conflict_info.pkg_type,
&conflict_info.pkg_name,
&conflict_info.right.pkg_version,
&conflict_info.conflict_pkg_type,
&conflict_info.conflict_pkg_name,
&conflict_info.conflict_pkg_version_2.pkg_version,
);

Ok(())
Expand Down
13 changes: 8 additions & 5 deletions core/src/ten_rust/src/pkg_info/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ use crate::pkg_info::manifest::{dependency::ManifestDependency, Manifest};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PkgDependency {
pub pkg_identity: PkgIdentity,
pub version_req: VersionReq,

// The original requested version of this dependency, ex: the `version`
// The version requirement of this dependency, ex: the `version`
// field declared in the `dependencies` section in the manifest.json, or
// the `pkg@version` parameter in the command line.
pub original_version_req: Option<String>,
//
// The `version_req_str` is the original value in string form, while
// `version_req` is the result after being converted to `VersionReq`.
pub version_req: VersionReq,
pub version_req_str: Option<String>,
}

pub fn get_pkg_dependencies_from_manifest(
Expand Down Expand Up @@ -53,7 +56,7 @@ pub fn get_pkg_dependencies_from_manifest_dependencies(
dependencies.push(PkgDependency {
pkg_identity: PkgIdentity { pkg_type, name },
version_req,
original_version_req: Some(manifest_dependency.version.clone()),
version_req_str: Some(manifest_dependency.version.clone()),
});
}

Expand Down Expand Up @@ -83,7 +86,7 @@ impl PkgDependency {
PkgDependency {
pkg_identity: PkgIdentity { pkg_type, name },
version_req,
original_version_req: None,
version_req_str: None,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_rust/src/pkg_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ impl PkgInfo {
pub fn get_dependency_by_type_and_name(
&self,
pkg_type: &str,
name: &str,
pkg_name: &str,
) -> Option<&PkgDependency> {
self.dependencies.iter().find(|dep| {
dep.pkg_identity.pkg_type.to_string() == pkg_type
&& dep.pkg_identity.name == name
&& dep.pkg_identity.name == pkg_name
})
}
}
Expand Down

0 comments on commit b346ef9

Please sign in to comment.