Skip to content

Commit

Permalink
Fix compiler config collection with profiles (#2041)
Browse files Browse the repository at this point in the history
- **Remove redundant paths**
- **Add failing tests**
- **Fix collection of compiler config from profiles**
- **Add tests for compiler config precedence**
  • Loading branch information
maciektr authored Mar 4, 2025
1 parent d955bf3 commit a157fa5
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 44 deletions.
50 changes: 29 additions & 21 deletions scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,6 @@ impl TomlManifest {
self.workspace.as_ref().cloned()
}

pub fn fetch_workspace(&self) -> Result<TomlWorkspace> {
self.get_workspace()
.ok_or_else(|| anyhow!("manifest is not a workspace"))
}

pub fn to_manifest(
&self,
manifest_path: &Utf8Path,
Expand Down Expand Up @@ -855,23 +850,20 @@ impl TomlManifest {

fn collect_profile_definition(&self, profile: Profile) -> Result<TomlProfile> {
let toml_cairo = self.cairo.clone().unwrap_or_default();
let toml_profiles = self.profile.clone();
let all_toml_profiles = self.profile.as_ref();

let profile_definition = toml_profiles
.clone()
.unwrap_or_default()
.get(profile.as_str())
.cloned();
let profile_definition =
all_toml_profiles.and_then(|profiles| profiles.get(profile.as_str()).cloned());

let parent_profile = profile_definition
.clone()
.unwrap_or_default()
.inherits
.as_ref()
.and_then(|p| p.inherits.clone())
.map(Profile::new)
.unwrap_or_else(|| {
if profile.is_custom() {
Ok(Profile::default())
} else {
// Default profiles do not inherit from any other profile.
Ok(profile.clone())
}
})?;
Expand All @@ -884,19 +876,16 @@ impl TomlManifest {
}

let parent_default = TomlProfile::default_for_profile(&parent_profile);
let parent_definition = toml_profiles
.unwrap_or_default()
.get(parent_profile.as_str())
.cloned()
let parent_definition = all_toml_profiles
.and_then(|profiles| profiles.get(parent_profile.as_str()).cloned())
.unwrap_or(parent_default.clone());

let mut parent_definition = toml_merge(&parent_default, &parent_definition)?;

let mut parent_definition = merge_profile(&parent_default, &parent_definition)?;
let parent_cairo = toml_merge(&parent_definition.cairo, &toml_cairo)?;
parent_definition.cairo = parent_cairo;

let profile = if let Some(profile_definition) = profile_definition {
toml_merge(&parent_definition, &profile_definition)?
merge_profile(&parent_definition, &profile_definition)?
} else {
parent_definition
};
Expand Down Expand Up @@ -995,6 +984,25 @@ impl TomlManifest {
}
}

fn merge_profile(target: &TomlProfile, source: &TomlProfile) -> Result<TomlProfile> {
let inherits = source.inherits.clone().or(target.inherits.clone());
let cairo = if let (Some(target), Some(source)) = (&target.cairo, &source.cairo) {
Some(toml_merge(target, source)?.clone())
} else {
source.cairo.clone().or(target.cairo.clone())
};
let tool = if let (Some(target), Some(source)) = (&target.tool, &source.tool) {
Some(toml_merge(target, source)?.clone())
} else {
source.tool.clone().or(target.tool.clone())
};
Ok(TomlProfile {
inherits,
cairo,
tool,
})
}

/// Returns the absolute canonical path of the README file for a [`TomlPackage`].
pub fn readme_for_package(
package_root: &Utf8Path,
Expand Down
Loading

0 comments on commit a157fa5

Please sign in to comment.