The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
fix(maven): list every goal a plugin declares, not just the bound one (#841)
misswellf8105e547311
diff --git a/Cargo.lock b/Cargo.lockindex b7590a3f9..423882bf7 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4086,6 +4086,7 @@ dependencies = ["specta","tempfile","tracing",+ "zip",][[package]]diff --git a/crates/ide-build/Cargo.toml b/crates/ide-build/Cargo.tomlindex eff2f2485..946863725 100644--- a/crates/ide-build/Cargo.toml+++ b/crates/ide-build/Cargo.toml@@ -8,6 +8,7 @@ license.workspace = true[dependencies]dunce.workspace = trueide-core.workspace = true+zip.workspace = trueserde.workspace = trueserde_json.workspace = truespecta.workspace = truediff --git a/crates/ide-build/src/maven.rs b/crates/ide-build/src/maven.rsindex f9b926a9d..b467c95ac 100644--- a/crates/ide-build/src/maven.rs+++ b/crates/ide-build/src/maven.rs@@ -11,7 +11,9 @@ use crate::xml::{self, Element};use serde::{Deserialize, Serialize};use specta::Type;use std::collections::{BTreeMap, HashMap, HashSet};+use std::io::Read;use std::path::{Path, PathBuf};+use std::sync::{Mutex, OnceLock};/// Phases shown under "Lifecycle" (IDEA's default list).pub const LIFECYCLE: &[&str] =@@ -456,13 +458,20 @@ struct Resolver {/// `~/.m2/repository`, or `<localRepository>` from `~/.m2/settings.xml`.pub(crate) fn default_local_repo() -> Option<PathBuf> {let m2 = dirs::home_dir()?.join(".m2");- if let Some(settings) = std::fs::read_to_string(m2.join("settings.xml")).ok().and_then(|t| xml::parse(&t))- && let Some(repo) = settings.text_of("localRepository")- {- let home = dirs::home_dir().map(|h| h.to_string_lossy().into_owned()).unwrap_or_default();- return Some(PathBuf::from(repo.replace("${user.home}", &home)));+ local_repository_of(&m2.join("settings.xml")).or(Some(m2.join("repository")))+}++/// `<localRepository>` of a settings file, when it names one: the repository a build+/// with `-s <file>` resolves into, which is not necessarily the user's default.+pub fn local_repository_of(settings: &Path) -> Option<PathBuf> {+ let text = std::fs::read_to_string(settings).ok()?;+ let root = xml::parse(&text)?;+ let repo = root.text_of("localRepository")?.trim().to_owned();+ if repo.is_empty() {+ return None;}- Some(m2.join("repository"))+ let home = dirs::home_dir().map(|h| h.to_string_lossy().into_owned()).unwrap_or_default();+ Some(PathBuf::from(repo.replace("${user.home}", &home)))}impl Resolver {@@ -688,7 +697,76 @@ pub fn goal_prefix(artifact_id: &str) -> String {artifact_id.strip_suffix("-plugin").unwrap_or(artifact_id).to_owned()}-/// Goals of well-known plugins (their plugin descriptors are not read).+/// Goals of every plugin jar read so far, by the path of that jar.+static DESCRIPTOR: OnceLock<Mutex<HashMap<String, Vec<String>>>> = OnceLock::new();++/// `<goal>` names of the plugin descriptor inside its jar (`META-INF/maven/plugin.xml`),+/// which is where Maven itself takes the list `plugin:help` prints. Empty when the jar+/// is not in the local repository yet.+fn descriptor_goals(repo: &Path, group_id: &str, artifact_id: &str, version: &str) -> Vec<String> {+ // Keyed by the jar itself, not by its coordinates: a repository can be replaced+ // between runs, and a test's empty repository must not answer for another's.+ // One `push` per group segment: the layout is `<repo>/<group as directories>/…` on+ // every platform, Windows' separator included.+ let mut dir = repo.to_path_buf();+ dir.extend(group_id.split('.'));+ dir.push(artifact_id);+ dir.push(version);+ let Some(jar) = plugin_jar(&dir, artifact_id, version) else { return Vec::new() };+ let key = jar.to_string_lossy().into_owned();+ let cache = DESCRIPTOR.get_or_init(|| Mutex::new(HashMap::new()));+ if let Some(cached) = cache.lock().unwrap_or_else(|e| e.into_inner()).get(&key) {+ return cached.clone();+ }+ // A failed read is not remembered: the jar may be half written by Maven right now.+ let Some(goals) = read_descriptor(&jar) else { return Vec::new() };+ cache.lock().unwrap_or_else(|e| e.into_inner()).insert(key, goals.clone());+ goals+}++/// The plugin's jar in its version directory: `<artifactId>-<version>.jar`, or — for a+/// SNAPSHOT installed from a repository, whose file name carries a timestamp — the newest+/// `<artifactId>-*.jar` that is neither sources nor javadoc.+fn plugin_jar(dir: &Path, artifact_id: &str, version: &str) -> Option<PathBuf> {+ let exact = dir.join(format!("{artifact_id}-{version}.jar"));+ if exact.is_file() {+ return Some(exact);+ }+ let prefix = format!("{artifact_id}-");+ let mut best: Option<(std::time::SystemTime, PathBuf)> = None;+ for entry in std::fs::read_dir(dir).ok()?.flatten() {+ let path = entry.path();+ let Some(name) = path.file_name().and_then(|n| n.to_str()) else { continue };+ if !name.starts_with(&prefix) || !name.ends_with(".jar") || name.ends_with("-sources.jar") {+ continue;+ }+ if name.ends_with("-javadoc.jar") {+ continue;+ }+ let Ok(modified) = entry.metadata().and_then(|m| m.modified()) else { continue };+ if best.as_ref().is_none_or(|(at, _)| modified > *at) {+ best = Some((modified, path));+ }+ }+ best.map(|(_, p)| p)+}++fn read_descriptor(jar: &Path) -> Option<Vec<String>> {+ let file = std::fs::File::open(jar).ok()?;+ let mut archive = zip::ZipArchive::new(file).ok()?;+ let mut entry = archive.by_name("META-INF/maven/plugin.xml").ok()?;+ let mut text = String::new();+ entry.read_to_string(&mut text).ok()?;+ let root = xml::parse(&text)?;+ let mut goals: Vec<String> =+ root.list("mojos", "mojo").iter().filter_map(|m| m.text_of("goal")).map(|g| g.trim().to_owned()).collect();+ goals.retain(|g| !g.is_empty());+ goals.sort();+ goals.dedup();+ Some(goals)+}++/// Goals of well-known plugins, for a jar that is not in the local repository yet.fn known_goals(artifact_id: &str) -> &'static [&'static str] {match artifact_id {"maven-clean-plugin" => &["clean", "help"],@@ -840,9 +918,27 @@ fn lifecycle_plugins(packaging: &str) -> &'static [&'static str] {}}-/// A plugin as shown in the tree: execution goals plus the plugin's known goals, sorted.-pub(crate) fn plugin(group_id: &str, artifact_id: &str, version: Option<String>, goals: &[String]) -> MavenPlugin {+/// A plugin as shown in the tree: the goals its jar declares, the ones known by name for+/// the common plugins, and the ones the project binds in `<executions>`, sorted. IDEA lists+/// a plugin's goals the same way, which is why a plugin outside the table (checkstyle, say)+/// still shows every goal it offers instead of only the bound one.+pub(crate) fn plugin(+ group_id: &str,+ artifact_id: &str,+ version: Option<String>,+ goals: &[String],+ local_repo: Option<&Path>,+) -> MavenPlugin {let mut all: Vec<String> = known_goals(artifact_id).iter().map(|g| (*g).to_owned()).collect();+ let repo = local_repo.map(Path::to_path_buf).or_else(default_local_repo);+ // An unresolved `${…}` version names no jar: the table and the bound goals stand in.+ if let (Some(repo), Some(version)) = (repo.as_deref(), version.as_deref().filter(|v| !v.contains("${"))) {+ for g in descriptor_goals(repo, group_id, artifact_id, version) {+ if !all.contains(&g) {+ all.push(g);+ }+ }+ }for g in goals {if !all.contains(g) {all.push(g.clone());@@ -1007,7 +1103,7 @@ pub(crate) fn load_with_repo(root: &Path, local_repo: Option<PathBuf>) -> MavenM.or_else(|| ctx.managed_plugins.iter().find(|m| m.key() == p.key()).and_then(|m| m.version.clone())).map(|v| i(&v)).filter(|v| !v.contains("${"));- plugin(&i(&p.group_id), &i(&p.artifact_id), version, &p.goals)+ plugin(&i(&p.group_id), &i(&p.artifact_id), version, &p.goals, resolver.local_repo.as_deref())}).collect();@@ -1119,6 +1215,16 @@ pub fn build_files(root: &Path, model: &MavenModel) -> Vec<PathBuf> {/// Projects and their plugins / dependencies from `mvn help:effective-pom` output/// (`<project>` or `<projects>`), keyed `groupId:artifactId`.pub fn parse_effective_pom(text: &str) -> Vec<(String, Vec<MavenPlugin>, Vec<MavenDependency>)> {+ parse_effective_pom_in(text, None)+}++/// The same, with the local repository the project actually builds against (a+/// `-Dmaven.repo.local` of the build, say): the plugin goals come from jars there,+/// rather than from the user's default one.+pub fn parse_effective_pom_in(+ text: &str,+ local_repo: Option<&Path>,+) -> Vec<(String, Vec<MavenPlugin>, Vec<MavenDependency>)> {let Some(root) = xml::parse(text) else { return Vec::new() };let projects: Vec<&Element> =if root.name == "projects" { root.children_named("project").collect() } else { vec![&root] };@@ -1130,7 +1236,7 @@ pub fn parse_effective_pom(text: &str) -> Vec<(String, Vec<MavenPlugin>, Vec<Mavlet plugins = raw.plugins.iter()- .map(|pl| plugin(&pl.group_id, &pl.artifact_id, pl.version.clone(), &pl.goals))+ .map(|pl| plugin(&pl.group_id, &pl.artifact_id, pl.version.clone(), &pl.goals, local_repo)).collect();let deps = raw.deps@@ -1261,6 +1367,107 @@ pub fn parse_dependency_tree(output: &str) -> Vec<(String, Vec<MavenDependency>)mod tests {use super::*;+ /// A plugin jar in `repo` whose descriptor declares `goals`.+ fn write_plugin_jar(repo: &Path, group: &str, artifact: &str, version: &str, goals: &[&str]) {+ let dir = repo.join(group.replace('.', "/")).join(artifact).join(version);+ std::fs::create_dir_all(&dir).unwrap();+ let mojos: String = goals.iter().map(|g| format!("<mojo><goal>{g}</goal></mojo>")).collect();+ let descriptor = format!("<plugin><mojos>{mojos}</mojos></plugin>");+ let file = std::fs::File::create(dir.join(format!("{artifact}-{version}.jar"))).unwrap();+ let mut zip = zip::ZipWriter::new(file);+ zip.start_file("META-INF/maven/plugin.xml", zip::write::SimpleFileOptions::default()).unwrap();+ std::io::Write::write_all(&mut zip, descriptor.as_bytes()).unwrap();+ zip.finish().unwrap();+ }++ #[test]+ fn a_plugin_lists_the_goals_its_jar_declares() {+ let tmp = tempfile::tempdir().unwrap();+ let repo = tmp.path();+ write_plugin_jar(+ repo,+ "org.apache.maven.plugins",+ "maven-checkstyle-plugin",+ "3.3.1",+ &["checkstyle", "checkstyle-aggregate", "help", "check"],+ );+ let p = plugin(+ "org.apache.maven.plugins",+ "maven-checkstyle-plugin",+ Some("3.3.1".into()),+ &["check".to_owned()],+ Some(repo),+ );+ assert_eq!(p.goals, ["check", "checkstyle", "checkstyle-aggregate", "help"], "every goal the plugin offers");+ assert_eq!(p.prefix, "checkstyle");+ }++ #[test]+ fn a_settings_file_names_the_repository_to_read_goals_from() {+ let tmp = tempfile::tempdir().unwrap();+ let settings = tmp.path().join("settings.xml");+ std::fs::write(+ &settings,+ "<settings><localRepository>${user.home}/repo-from-settings</localRepository></settings>",+ )+ .unwrap();+ let repo = local_repository_of(&settings).unwrap();+ assert!(repo.ends_with("repo-from-settings"), "{}", repo.display());+ assert!(!repo.to_string_lossy().contains("${user.home}"));+ // A settings file that names none leaves Maven's default alone.+ let plain = tmp.path().join("plain.xml");+ std::fs::write(&plain, "<settings><offline>true</offline></settings>").unwrap();+ assert!(local_repository_of(&plain).is_none());+ assert!(local_repository_of(&tmp.path().join("missing.xml")).is_none());+ }++ #[test]+ fn effective_pom_goals_come_from_the_local_repository_it_is_given() {+ let tmp = tempfile::tempdir().unwrap();+ write_plugin_jar(tmp.path(), "org.apache.maven.plugins", "maven-demo-plugin", "1.0", &["run", "help"]);+ let text = "<project><groupId>g</groupId><artifactId>a</artifactId><build><plugins>\+ <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-demo-plugin</artifactId>\+ <version>1.0</version><executions><execution><goals><goal>run</goal></goals></execution></executions>\+ </plugin></plugins></build></project>";+ let with_repo = parse_effective_pom_in(text, Some(tmp.path()));+ assert_eq!(with_repo[0].1[0].goals, ["help", "run"], "the jar's own goals come first");+ // Without that repository the plugin is unknown: only what the project binds.+ assert_eq!(parse_effective_pom(text)[0].1[0].goals, ["run"]);+ }++ #[test]+ fn a_snapshot_plugin_is_found_by_its_timestamped_jar() {+ let tmp = tempfile::tempdir().unwrap();+ let dir = tmp.path().join("g/p/maven-demo-plugin/1.0-SNAPSHOT");+ std::fs::create_dir_all(&dir).unwrap();+ let descriptor = "<plugin><mojos><mojo><goal>run</goal></mojo></mojos></plugin>";+ let file = std::fs::File::create(dir.join("maven-demo-plugin-1.0-20240101.120000-3.jar")).unwrap();+ let mut zip = zip::ZipWriter::new(file);+ zip.start_file("META-INF/maven/plugin.xml", zip::write::SimpleFileOptions::default()).unwrap();+ std::io::Write::write_all(&mut zip, descriptor.as_bytes()).unwrap();+ zip.finish().unwrap();+ // A sibling sources jar must not win.+ std::fs::File::create(dir.join("maven-demo-plugin-1.0-20240101.120000-3-sources.jar")).unwrap();+ let p = plugin("g.p", "maven-demo-plugin", Some("1.0-SNAPSHOT".into()), &[], Some(tmp.path()));+ assert_eq!(p.goals, ["run"]);+ }++ #[test]+ fn a_plugin_without_its_jar_keeps_the_table_and_the_bound_goals() {+ let tmp = tempfile::tempdir().unwrap();+ let p = plugin(+ "org.apache.maven.plugins",+ "maven-checkstyle-plugin",+ Some("3.3.1".into()),+ &["check".to_owned()],+ Some(tmp.path()),+ );+ assert_eq!(p.goals, ["check"], "nothing beyond what the project binds");+ let known =+ plugin("org.apache.maven.plugins", "maven-compiler-plugin", Some("3.13.0".into()), &[], Some(tmp.path()));+ assert_eq!(known.goals, ["compile", "help", "testCompile"], "the common plugins keep their table");+ }+fn write(dir: &Path, rel: &str, text: &str) {let p = dir.join(rel);std::fs::create_dir_all(p.parent().unwrap()).unwrap();diff --git a/crates/ide-build/src/sync.rs b/crates/ide-build/src/sync.rsindex cbeed656f..815c46313 100644--- a/crates/ide-build/src/sync.rs+++ b/crates/ide-build/src/sync.rs@@ -48,6 +48,11 @@ struct CachedMavenProject {#[derive(Clone, Debug, Default, Serialize, Deserialize)]struct MavenCache {+ /// Bumped whenever what the cache holds stops matching what the model would be+ /// built as now (plugin goals read from a descriptor, say): an older cache is+ /// dropped instead of shadowing the model.+ #[serde(default)]+ version: u32,hash: String,at_ms: f64,error: Option<String>,@@ -66,6 +71,7 @@ struct GradleCache {}const MAVEN_CACHE: &str = "maven.json";+const MAVEN_CACHE_VERSION: u32 = 1;/// `dependency:tree` of a plugin version whose `-Dverbose` lists the occurrences Maven/// omitted (conflicts, duplicates): verbose output was dropped in 3.0 and is back since 3.2.@@ -121,6 +127,9 @@ fn now_ms() -> f64 {/// Merges the cached effective build into `model`.pub fn apply_maven_cache(root: &Path, cache_dir: &Path, model: &mut MavenModel) {let Some(cache) = read_cache::<MavenCache>(cache_dir, MAVEN_CACHE) else { return };+ if cache.version != MAVEN_CACHE_VERSION {+ return; // built by an older IDE: what it holds no longer matches the model+ }model.synced_at = Some(cache.at_ms);model.sync_error = cache.error;let changed = cache.hash != hash_files(&maven::build_files(root, model));@@ -211,8 +220,31 @@ pub fn sync_maven(let mut model = maven::load(root);maven::UnlinkedProjects::from_model(&model, &options.maven_settings).filter(&mut model);std::fs::create_dir_all(cache_dir)?;- let mut cache =- MavenCache { hash: hash_files(&maven::build_files(root, &model)), verbose: true, ..MavenCache::default() };+ let mut cache = MavenCache {+ version: MAVEN_CACHE_VERSION,+ hash: hash_files(&maven::build_files(root, &model)),+ verbose: true,+ ..MavenCache::default()+ };+ // Where the build resolves its jars, and therefore where the plugin goals are read+ // from: `-Dmaven.repo.local=` first, then `<localRepository>` of the `-s` settings,+ // then the user's default repository (which `plugin()` resolves itself).+ let local_repo: Option<PathBuf> =+ options.maven_options.iter().find_map(|a| a.strip_prefix("-Dmaven.repo.local=")).map(PathBuf::from).or_else(+ || {+ let mut args = options.maven_options.iter();+ while let Some(arg) = args.next() {+ let path = match arg.as_str() {+ "-s" | "--settings" => args.next().map(String::as_str),+ _ => arg.strip_prefix("-s=").or_else(|| arg.strip_prefix("--settings=")),+ };+ if let Some(path) = path {+ return maven::local_repository_of(Path::new(path));+ }+ }+ None+ },+ );let mut errors: Vec<String> = Vec::new();for (n, top) in model.projects.iter().filter(|p| !p.aggregated).enumerate() {let effective = cache_dir.join(format!("effective-pom-{n}.xml"));@@ -241,8 +273,9 @@ pub fn sync_maven(});}})?;- let effective_projects =- std::fs::read_to_string(&effective).map(|t| maven::parse_effective_pom(&t)).unwrap_or_default();+ let effective_projects = std::fs::read_to_string(&effective)+ .map(|t| maven::parse_effective_pom_in(&t, local_repo.as_deref()))+ .unwrap_or_default();let trees = maven::parse_dependency_tree(&out.text);for (key, plugins, deps) in effective_projects {let dependencies = trees.iter().find(|(k, _)| *k == key).map(|(_, t)| t.clone()).unwrap_or(deps);@@ -332,6 +365,7 @@ mod tests {let mut model = maven::load_with_repo(&root, None);assert!(model.synced_at.is_none());let cache = MavenCache {+ version: MAVEN_CACHE_VERSION,hash: hash_files(&maven::build_files(&root, &model)),at_ms: 5.0,error: None,@@ -343,6 +377,7 @@ mod tests {"maven-clean-plugin",Some("3.2.0".into()),&[],+ None,)],dependencies: vec![MavenDependency { artifact_id: "dep".into(), ..MavenDependency::default() }],}],@@ -368,6 +403,41 @@ mod tests {assert_eq!(model.projects[0].dependencies[0].artifact_id, "fresh");}+ #[test]+ fn a_cache_from_an_older_ide_is_ignored() {+ let tmp = tempfile::tempdir().unwrap();+ let root = tmp.path().join("p");+ let cache_dir = tmp.path().join("cache");+ std::fs::create_dir_all(&root).unwrap();+ std::fs::write(root.join("pom.xml"), "<project><groupId>g</groupId><artifactId>a</artifactId></project>")+ .unwrap();+ let mut model = maven::load_with_repo(&root, None);+ let cache = MavenCache {+ // What an older build wrote: no plugin descriptors were read back then, so+ // its goal lists would shadow the ones the model now knows.+ version: 0,+ hash: hash_files(&maven::build_files(&root, &model)),+ at_ms: 5.0,+ error: None,+ verbose: true,+ projects: vec![CachedMavenProject {+ key: "g:a".into(),+ plugins: vec![maven::plugin(+ "org.apache.maven.plugins",+ "maven-clean-plugin",+ Some("3.2.0".into()),+ &[],+ None,+ )],+ dependencies: vec![MavenDependency { artifact_id: "cached".into(), ..MavenDependency::default() }],+ }],+ };+ write_cache(&cache_dir, MAVEN_CACHE, &cache).unwrap();+ apply_maven_cache(&root, &cache_dir, &mut model);+ assert!(model.synced_at.is_none(), "the cache is dropped, the model stays unsynced");+ assert!(model.projects[0].dependencies.iter().all(|d| d.artifact_id != "cached"));+ }+#[test]fn gradle_cache_replaces_tasks_per_project() {let tmp = tempfile::tempdir().unwrap();