Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check duplicate dependencies #717

Merged
merged 13 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/project/manifest/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{Activation, PyPiRequirement, SystemRequirements, Target, TargetSelec
use crate::consts;
use crate::project::manifest::channel::{PrioritizedChannel, TomlPrioritizedChannelStrOrMap};
use crate::project::manifest::target::Targets;
use crate::project::manifest::UniquePackageName;
use crate::project::SpecType;
use crate::task::Task;
use crate::utils::spanned::PixiSpanned;
Expand Down Expand Up @@ -229,15 +230,15 @@ impl<'de> Deserialize<'de> for Feature {

#[serde(default)]
#[serde_as(as = "IndexMap<_, PickFirst<(DisplayFromStr, _)>>")]
dependencies: IndexMap<PackageName, NamelessMatchSpec>,
dependencies: IndexMap<UniquePackageName, NamelessMatchSpec>,

#[serde(default)]
#[serde_as(as = "Option<IndexMap<_, PickFirst<(DisplayFromStr, _)>>>")]
host_dependencies: Option<IndexMap<PackageName, NamelessMatchSpec>>,
host_dependencies: Option<IndexMap<UniquePackageName, NamelessMatchSpec>>,

#[serde(default)]
#[serde_as(as = "Option<IndexMap<_, PickFirst<(DisplayFromStr, _)>>>")]
build_dependencies: Option<IndexMap<PackageName, NamelessMatchSpec>>,
build_dependencies: Option<IndexMap<UniquePackageName, NamelessMatchSpec>>,

#[serde(default)]
pypi_dependencies: Option<IndexMap<rip::types::PackageName, PyPiRequirement>>,
Expand All @@ -252,13 +253,31 @@ impl<'de> Deserialize<'de> for Feature {
}

let inner = FeatureInner::deserialize(deserializer)?;

let mut dependencies = HashMap::from_iter([(SpecType::Run, inner.dependencies)]);
let mut dependencies = HashMap::from_iter([(
SpecType::Run,
inner
.dependencies
.into_iter()
.map(|(p, s)| (p.as_inner(), s))
.collect(),
)]);
if let Some(host_deps) = inner.host_dependencies {
dependencies.insert(SpecType::Host, host_deps);
dependencies.insert(
SpecType::Host,
host_deps
.into_iter()
.map(|(p, s)| (p.as_inner(), s))
.collect(),
);
}
if let Some(build_deps) = inner.build_dependencies {
dependencies.insert(SpecType::Build, build_deps);
dependencies.insert(
SpecType::Build,
build_deps
.into_iter()
.map(|(p, s)| (p.as_inner(), s))
.collect(),
);
}

let default_target = Target {
Expand Down
75 changes: 67 additions & 8 deletions src/project/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use miette::{miette, Diagnostic, IntoDiagnostic, LabeledSpan, NamedSource};
pub use python::PyPiRequirement;
use rattler_conda_types::{MatchSpec, NamelessMatchSpec, PackageName, Platform, Version};
use serde_with::{serde_as, DisplayFromStr, Map, PickFirst};
use std::hash::Hash;
use std::hash::{Hash, Hasher};
use std::{
collections::HashMap,
path::{Path, PathBuf},
Expand Down Expand Up @@ -742,6 +742,47 @@ impl ProjectManifest {
}
}

#[derive(Debug, Clone, Eq)]
pub(crate) struct UniquePackageName {
inner: PackageName,
}

impl PartialEq for UniquePackageName {
fn eq(&self, other: &Self) -> bool {
self.as_inner().eq(&other.as_inner())
}
}

impl Hash for UniquePackageName {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_inner().as_normalized().hash(state);
}
}

impl UniquePackageName {
pub fn as_inner(&self) -> PackageName {
self.inner.clone()
}
}

impl<'de> Deserialize<'de> for UniquePackageName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let package_name = PackageName::deserialize(deserializer)?;
if package_name.as_source() != package_name.as_normalized() {
Err(serde::de::Error::custom(
"invalid dependency: please avoid using capitalized names for the dependencies",
ruben-arts marked this conversation as resolved.
Show resolved Hide resolved
))
} else {
Ok(Self {
inner: package_name,
})
}
}
}

impl<'de> Deserialize<'de> for ProjectManifest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -767,15 +808,15 @@ impl<'de> Deserialize<'de> for ProjectManifest {
// default_target: Target,
#[serde(default)]
#[serde_as(as = "IndexMap<_, PickFirst<(DisplayFromStr, _)>>")]
dependencies: IndexMap<PackageName, NamelessMatchSpec>,
dependencies: IndexMap<UniquePackageName, NamelessMatchSpec>,

#[serde(default)]
#[serde_as(as = "Option<IndexMap<_, PickFirst<(DisplayFromStr, _)>>>")]
host_dependencies: Option<IndexMap<PackageName, NamelessMatchSpec>>,
host_dependencies: Option<IndexMap<UniquePackageName, NamelessMatchSpec>>,

#[serde(default)]
#[serde_as(as = "Option<IndexMap<_, PickFirst<(DisplayFromStr, _)>>>")]
build_dependencies: Option<IndexMap<PackageName, NamelessMatchSpec>>,
build_dependencies: Option<IndexMap<UniquePackageName, NamelessMatchSpec>>,

#[serde(default)]
pypi_dependencies: Option<IndexMap<rip::types::PackageName, PyPiRequirement>>,
Expand All @@ -799,13 +840,31 @@ impl<'de> Deserialize<'de> for ProjectManifest {
}

let toml_manifest = TomlProjectManifest::deserialize(deserializer)?;

let mut dependencies = HashMap::from_iter([(SpecType::Run, toml_manifest.dependencies)]);
let mut dependencies = HashMap::from_iter([(
SpecType::Run,
toml_manifest
.dependencies
.into_iter()
.map(|(p, s)| (p.as_inner(), s))
.collect(),
)]);
if let Some(host_deps) = toml_manifest.host_dependencies {
dependencies.insert(SpecType::Host, host_deps);
dependencies.insert(
SpecType::Host,
host_deps
.into_iter()
.map(|(p, s)| (p.as_inner(), s))
.collect(),
);
}
if let Some(build_deps) = toml_manifest.build_dependencies {
dependencies.insert(SpecType::Build, build_deps);
dependencies.insert(
SpecType::Build,
build_deps
.into_iter()
.map(|(p, s)| (p.as_inner(), s))
.collect(),
);
}

let default_target = Target {
Expand Down
Loading