Skip to content

Commit

Permalink
Distinguish between library and plugin IDs in component dependencies
Browse files Browse the repository at this point in the history
commit-id:8452ee94
  • Loading branch information
integraledelebesgue committed Jan 29, 2025
1 parent f304dce commit ab50890
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
25 changes: 24 additions & 1 deletion scarb/src/compiler/compilation_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,30 @@ pub struct CompilationUnitComponent {
/// Items for the Cairo's `#[cfg(...)]` attribute to be enabled in this component.
pub cfg_set: Option<CfgSet>,
/// Dependencies of this component.
pub dependencies: Vec<CompilationUnitComponentId>,
pub dependencies: Vec<CompilationUnitDependency>,
}

impl CompilationUnitComponent {
/// Checks whether the [`CompilationUnitComponent`] represents a plugin package.
pub fn is_cairo_plugin(&self) -> bool {
self.package.is_cairo_plugin()
}
}

/// The kind of the compilation unit dependency.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum CompilationUnitDependency {
Library(CompilationUnitComponentId),
Plugin(CompilationUnitComponentId),
}

impl CompilationUnitDependency {
pub fn component_id(&self) -> &CompilationUnitComponentId {
match self {
CompilationUnitDependency::Library(id) => id,
CompilationUnitDependency::Plugin(id) => id,
}
}
}

/// Information about a single package that is a compiler plugin to load for [`CompilationUnit`].
Expand Down
29 changes: 19 additions & 10 deletions scarb/src/compiler/db.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::compiler::plugin::proc_macro::{ProcMacroHost, ProcMacroHostPlugin};
use crate::compiler::{CairoCompilationUnit, CompilationUnitAttributes, CompilationUnitComponent};
use crate::compiler::{
CairoCompilationUnit, CompilationUnitAttributes, CompilationUnitComponent,
CompilationUnitDependency,
};
use crate::core::Workspace;
use crate::DEFAULT_MODULE_MAIN_FILE;
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -153,15 +156,21 @@ fn build_project_config(unit: &CairoCompilationUnit) -> Result<ProjectConfig> {
let dependencies = component
.dependencies
.iter()
.map(|compilation_unit_component_id| {
let compilation_unit_component = unit.components.iter().find(|component| component.id == *compilation_unit_component_id)
.expect("dependency of a component is guaranteed to exist in compilation unit components");
(
compilation_unit_component.cairo_package_name().to_string(),
DependencySettings {
discriminator: compilation_unit_component.id.to_discriminator()
},
)
.filter_map(|dependency| {
match dependency {
CompilationUnitDependency::Plugin(_) => None,
CompilationUnitDependency::Library(component_id) => {
let compilation_unit_component = unit.components.iter().find(|component| component.id == *component_id)
.expect("Library dependency of a component is guaranteed to exist in compilation unit components");

Some((
compilation_unit_component.cairo_package_name().to_string(),
DependencySettings {
discriminator: compilation_unit_component.id.to_discriminator()
},
))
}
}
})
.collect();

Expand Down
4 changes: 2 additions & 2 deletions scarb/src/ops/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ where
Some(
c.dependencies
.iter()
.map(|component_id|
.map(|dependency|
m::CompilationUnitComponentDependencyMetadataBuilder::default()
.id(component_id.to_metadata_component_id())
.id(dependency.component_id().clone().to_metadata_component_id())
.build().
unwrap()
).collect()
Expand Down
14 changes: 8 additions & 6 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::compiler::plugin::proc_macro::ProcMacroInstance;
use crate::compiler::plugin::{fetch_cairo_plugin, CairoPluginProps};
use crate::compiler::{
CairoCompilationUnit, CompilationUnit, CompilationUnitAttributes, CompilationUnitCairoPlugin,
CompilationUnitComponent, CompilationUnitComponentId, ProcMacroCompilationUnit, Profile,
CompilationUnitComponent, CompilationUnitDependency, ProcMacroCompilationUnit, Profile,
};
use crate::core::lockfile::Lockfile;
use crate::core::package::{Package, PackageClass, PackageId};
Expand Down Expand Up @@ -517,7 +517,9 @@ fn cairo_compilation_unit_for_target(
.find(|component| component.package.id == member.id)
.unwrap();
let mut test_package_deps = solution.component_dependencies(member_component, &components);
test_package_deps.push(member_component.id.clone());
test_package_deps.push(CompilationUnitDependency::Library(
member_component.id.clone(),
));

let dependencies_for_components: Vec<_> = components
.iter()
Expand Down Expand Up @@ -721,7 +723,7 @@ impl<'a> PackageSolutionCollector<'a> {
&self,
component: &CompilationUnitComponent,
components: &[CompilationUnitComponent],
) -> Vec<CompilationUnitComponentId> {
) -> Vec<CompilationUnitDependency> {
let package_id = component.id.package_id;

// Those are direct dependencies of the component.
Expand All @@ -730,14 +732,14 @@ impl<'a> PackageSolutionCollector<'a> {
.package_dependencies(package_id, self.target_kind.as_ref().unwrap());

// We iterate over all the compilation unit components to get dependency's version.
let mut dependencies: Vec<CompilationUnitComponentId> = components
let mut dependencies: Vec<_> = components
.iter()
.filter(|component_as_dependency| {
dependencies_packages.iter().any(|dependency_summary| {
dependency_summary.id == component_as_dependency.package.id
})
})
.map(|compilation_unit_component| compilation_unit_component.id.clone())
.map(|component| CompilationUnitDependency::Library(component.id.clone()))
.collect();

// Adds itself to dependencies
Expand All @@ -750,7 +752,7 @@ impl<'a> PackageSolutionCollector<'a> {
false
};
if !is_integration_test {
dependencies.push(component.id.clone());
dependencies.push(CompilationUnitDependency::Library(component.id.clone()));
}

dependencies
Expand Down

0 comments on commit ab50890

Please sign in to comment.