diff --git a/crates/ide-build/src/maven.rs b/crates/ide-build/src/maven.rs index eb437ed2d3..7830f9467e 100644 --- a/crates/ide-build/src/maven.rs +++ b/crates/ide-build/src/maven.rs @@ -316,6 +316,8 @@ struct RawProfile { active_by_default: bool, /// `` (the common `1.8`, `21` and `[17,)` forms). activation_jdk: Option, + /// `` (``, ``, ``; Maven matches all present fields). + activation_os: Option, modules: Vec, properties: Vec<(String, String)>, deps: Vec, @@ -324,6 +326,55 @@ struct RawProfile { managed_plugins: Vec, } +/// ``: the fields the POM names, all of which Maven requires to match. +#[derive(Clone, Debug, Default, PartialEq)] +struct RawOsActivation { + name: Option, + family: Option, + arch: Option, +} + +impl RawOsActivation { + /// Whether this machine satisfies the activation: every named field matches, as Maven's + /// `OsActivation` does. Families map as Maven's `OsUtils` (`windows`, `unix`/`linux`, + /// `mac`/`darwin`); a name matches the running OS name or its family alias. + fn matches(&self) -> bool { + let os = std::env::consts::OS; // "macos" | "windows" | "linux" + let arch = std::env::consts::ARCH; // "x86_64" | "aarch64" + fn family_of(name: &str) -> &str { + match name { + "macos" | "darwin" => "mac", + "windows" => "windows", + "linux" => "unix", + other => other, + } + } + let running_family = family_of(os); + // Absent fields impose no constraint (Maven matches only what the POM names); a leading + // `!` negates the field, as in Maven's `!windows`. + let ok = |value: Option<&String>, f: &dyn Fn(&str) -> bool| { + value.is_none_or(|v| match v.trim().strip_prefix('!') { + Some(negated) => !f(negated.trim()), + None => f(v.trim()), + }) + }; + let name_ok = ok(self.name.as_ref(), &|v| { + v.eq_ignore_ascii_case(os) || v.eq_ignore_ascii_case(running_family) || family_of(v) == running_family + }); + let family_ok = ok(self.family.as_ref(), &|v| { + v.eq_ignore_ascii_case(running_family) + || (v.eq_ignore_ascii_case("unix") && matches!(running_family, "mac" | "unix")) + }); + let arch_ok = ok(self.arch.as_ref(), &|v| { + v.eq_ignore_ascii_case(arch) + || (v.eq_ignore_ascii_case("amd64") && arch == "x86_64") + || (v.eq_ignore_ascii_case("aarch64") && arch == "aarch64") + || (v.eq_ignore_ascii_case("arm64") && arch == "aarch64") + }); + name_ok && family_ok && arch_ok + } +} + #[derive(Clone, Debug, Default)] struct RawPom { group_id: Option, @@ -443,6 +494,14 @@ fn read_raw(project: &Element) -> RawPom { .at("activation/activeByDefault") .is_some_and(|a| a.text.trim().eq_ignore_ascii_case("true")), activation_jdk: p.at("activation/jdk").map(|jdk| jdk.text.trim().to_owned()).filter(|jdk| !jdk.is_empty()), + activation_os: p + .at("activation/os") + .map(|os| RawOsActivation { + name: os.text_of("name").map(|n| n.trim().to_owned()).filter(|n| !n.is_empty()), + family: os.text_of("family").map(|f| f.trim().to_owned()).filter(|f| !f.is_empty()), + arch: os.text_of("arch").map(|a| a.trim().to_owned()).filter(|a| !a.is_empty()), + }) + .filter(|os| os.name.is_some() || os.family.is_some() || os.arch.is_some()), modules: module_names(p), properties: read_properties(p.child("properties")), deps: p.list("dependencies", "dependency").into_iter().filter_map(read_dep).collect(), @@ -522,9 +581,9 @@ struct Inherited { /// The nearest `maven-compiler-plugin` `` / ``: a module usually /// inherits its compiler level from the parent's (managed) plugin configuration. compiler_release: Option, - /// ``, `activeByDefault`: profiles of the POM and its ancestors — - /// Maven lets a child enable a parent's profiles with `-P`. - profiles: Vec<(String, bool)>, + /// ``, `activeByDefault`, OS-activation matched on this machine: profiles of the POM and + /// its ancestors — Maven lets a child enable a parent's profiles with `-P`. + profiles: Vec<(String, bool, bool)>, } fn merge_by(base: &mut Vec, own: &[T], key: impl Fn(&T) -> String) { @@ -778,22 +837,22 @@ impl Resolver { Self { local_repo, profiles: profiles.to_vec(), jdk_version, poms: HashMap::new(), contexts: HashMap::new() } } - /// The active profiles of a POM: chosen ones, JDK-activated ones, and the + /// The active profiles of a POM: chosen ones, JDK- and OS-activated ones, and the /// `activeByDefault` ones not turned off while another profile is active. fn active<'a>(&self, profiles: &'a [RawProfile]) -> impl Iterator { let chosen = |id: &str| profile_choice(&self.profiles, id); let jdk_version = self.jdk_version; + let auto_active = move |p: &RawProfile| { + p.activation_jdk.as_deref().is_some_and(|spec| jdk_matches(spec, jdk_version)) + || p.activation_os.as_ref().is_some_and(RawOsActivation::matches) + }; let any_active = profiles.iter().any(|p| { let choice = chosen(&p.id); - choice == Some(true) - || (choice.is_none() && p.activation_jdk.as_deref().is_some_and(|spec| jdk_matches(spec, jdk_version))) + choice == Some(true) || (choice.is_none() && auto_active(p)) }); profiles.iter().filter(move |p| match chosen(&p.id) { Some(value) => value, - None => { - p.activation_jdk.as_deref().is_some_and(|spec| jdk_matches(spec, jdk_version)) - || (p.active_by_default && !any_active) - } + None => auto_active(p) || (p.active_by_default && !any_active), }) } @@ -868,8 +927,13 @@ impl Resolver { } merge_by( &mut ctx.profiles, - &pom.profiles.iter().map(|p| (p.id.clone(), p.active_by_default)).collect::>(), - |(id, _)| id.clone(), + &pom.profiles + .iter() + .map(|p| { + (p.id.clone(), p.active_by_default, p.activation_os.as_ref().is_some_and(RawOsActivation::matches)) + }) + .collect::>(), + |(id, ..)| id.clone(), ); // Managed dependencies: own entries win; imported BOMs fill the gaps. let props = self.properties_for(&pom, &ctx, &dir); @@ -1807,11 +1871,15 @@ pub fn load_with_linked_jdk( .filter(|v| !v.is_empty() && !v.contains("${")); // The project can activate the profiles of its own POM and its ancestors'. - for (id, active) in &ctx.profiles { + // OS-activated ones count as active by default for the tool window: a click + // then disables them explicitly (-P !id), which is the only way to keep + // Maven's own activation from putting them in the build (#8171). + for (id, by_default, os_active) in &ctx.profiles { + let auto = *by_default || *os_active; if let Some(existing) = model.profiles.iter_mut().find(|x| x.id == *id) { - existing.active_by_default |= *active; + existing.active_by_default |= auto; } else { - model.profiles.push(MavenProfile { id: id.clone(), active_by_default: *active, ..Default::default() }); + model.profiles.push(MavenProfile { id: id.clone(), active_by_default: auto, ..Default::default() }); } } let artifact_id = i(&pom.artifact_id); @@ -1837,7 +1905,7 @@ pub fn load_with_linked_jdk( plugins, dependencies, system_paths, - profiles: ctx.profiles.iter().map(|(id, _)| id.clone()).collect(), + profiles: ctx.profiles.iter().map(|(id, ..)| id.clone()).collect(), java_version: jv, source_encoding, command: wrapper(&canonical_root, &dir_canon), @@ -3575,6 +3643,72 @@ mod tests { assert_eq!(profiles, [("jdk8", false), ("jdk21", true), ("own", false)]); } + /// A profile ``-activated on this machine shows as active by default in the tool + /// window, so unchecking it sends an explicit `-P !id` — the only way to keep Maven's + /// own `` activation from putting the profile in the build (#8171). An `` + /// block naming another platform stays unchecked. + #[test] + fn os_activated_profiles_count_as_active_by_default_here() { + let tmp = tempfile::tempdir().unwrap(); + let root = tmp.path(); + let (windows, this_machine) = match std::env::consts::OS { + "macos" => ("windows", "mac"), + "linux" => ("windows", "unix"), + _ => ("linux", "windows"), + }; + write( + root, + "pom.xml", + format!( + r#"demodemo1.0 + + other-platform{} + this-platform{} + + "#, + windows, this_machine + ) + .as_str(), + ); + let model = load_with_repo(root, None); + let profile = |id: &str| model.profiles.iter().find(|p| p.id == id).unwrap_or_else(|| panic!("missing {id}")); + assert!( + profile("this-platform").active_by_default, + " activation on this machine counts as active by default" + ); + assert!(!profile("other-platform").active_by_default, "another platform's activation stays off"); + + // A name without a family works too, and an empty block (no fields) never matches. + write( + root, + "pom.xml", + r#"demodemo1.0 + + by-namenot-an-os + + "#, + ); + let model = load_with_repo(root, None); + assert!(!model.profiles.iter().any(|p| p.id == "by-name" && p.active_by_default)); + + // A negated family matches every other platform, as in Maven. + let other = if std::env::consts::OS == "windows" { "mac" } else { "windows" }; + write( + root, + "pom.xml", + format!( + r#"demodemo1.0 + + not-other!{other} + + "# + ) + .as_str(), + ); + let model = load_with_repo(root, None); + assert!(model.profiles.iter().any(|p| p.id == "not-other" && p.active_by_default)); + } + #[test] fn parent_and_bom_from_the_local_repository_and_wrapper() { let tmp = tempfile::tempdir().unwrap();