From 890fade636630ac9722ea20aee4070bdf1bef003 Mon Sep 17 00:00:00 2001 From: aconeshana <330440659@qq.com> Date: Thu, 3 Dec 2026 14:31:49 +0800 Subject: [PATCH] fix(build): delete the output of resources the sources no longer have (#7687) The resources step copies and overwrites but never deletes, so a resource removed from the sources (for example META-INF/spring.factories after switching branches) kept its copy in the build output and stayed on the launch classpath, failing Spring Boot with "Unable to read meta-data". Classes already have this through the incremental compile record; resources had none. Each source set now records the output paths its resource roots map to (output folder, , path under the root, filtered by includes/excludes). After a successful resources step, and after a prepare run, files the previous record wrote that the sources no longer produce are deleted, along with folders they leave empty inside the output folder. Closes #7630 Co-authored-by: aconeshana <52204454+aconeshana@users.noreply.github.com> --- crates/ide-build/src/jbuild/mod.rs | 62 ++++++++++++++++++++++++++++ crates/ide-build/src/jbuild/tests.rs | 57 +++++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/crates/ide-build/src/jbuild/mod.rs b/crates/ide-build/src/jbuild/mod.rs index 2d824ef55..d56feee1e 100644 --- a/crates/ide-build/src/jbuild/mod.rs +++ b/crates/ide-build/src/jbuild/mod.rs @@ -960,6 +960,7 @@ impl JavaBuild<'_> { for project in &reactor_model.projects { for test in [false, true] { let stamp = resource_stamp(project.set(test), cancel)?; + self.prune_resources(dir, project, test, cancel)?; model::write_json(&state_path(dir, project, test, "resources"), &stamp)?; } } @@ -1027,11 +1028,42 @@ impl JavaBuild<'_> { })); } for (project, test, stamp) in changed { + self.prune_resources(dir, project, test, cancel)?; model::write_json(&state_path(dir, project, test, "resources"), &stamp)?; } Ok(None) } + /// Removes what the sources no longer have from a source set's output: the resources step + /// copies and overwrites, it never deletes, so a file deleted from the sources would stay on + /// the launch's classpath (a stale `META-INF/spring.factories` then fails the application). + fn prune_resources( + &self, + dir: &Path, + project: &ProjectModel, + test: bool, + cancel: &CancellationToken, + ) -> IdeResult<()> { + let set = project.set(test); + let expected = resource_outputs(set, cancel)?; + let recorded: BTreeSet = std::fs::read(state_path(dir, project, test, "resource-outputs")) + .ok() + .and_then(|b| serde_json::from_slice(&b).ok()) + .unwrap_or_default(); + for stale in recorded.difference(&expected) { + let _ = std::fs::remove_file(stale); + // The folders a removed resource leaves empty are not part of the output either. + let mut parent = stale.parent(); + while let Some(folder) = parent.filter(|d| d.starts_with(&set.output) && *d != set.output) { + if std::fs::remove_dir(folder).is_err() { + break; + } + parent = folder.parent(); + } + } + model::write_json(&state_path(dir, project, test, "resource-outputs"), &expected) + } + /// Removes the classes of a source set and its record: the next build compiles it from scratch. fn clean(&self, dir: &Path, project: &ProjectModel, test: bool) -> IdeResult<()> { let set = project.set(test); @@ -1228,5 +1260,35 @@ fn resource_stamp(set: &SourceSet, cancel: &CancellationToken) -> IdeResult IdeResult> { + let mut out = BTreeSet::new(); + for root in &set.resources { + let mut pending = vec![root.directory.clone()]; + while let Some(dir) = pending.pop() { + if cancel.is_cancelled() { + return Err(IdeError::cancelled()); + } + let Ok(entries) = std::fs::read_dir(&dir) else { continue }; + for entry in entries.flatten() { + let path = entry.path(); + if entry.file_type().is_ok_and(|t| t.is_dir()) { + pending.push(path); + continue; + } + let rel = path.strip_prefix(&root.directory).unwrap_or(&path).to_string_lossy().replace('\\', "/"); + let included = root.includes.is_empty() || root.includes.iter().any(|p| ant_match(p, &rel)); + if !included || root.excludes.iter().any(|p| ant_match(p, &rel)) { + continue; + } + let target = root.target_path.as_deref().unwrap_or("").replace('\\', "/"); + out.insert(set.output.join(target.trim_matches('/')).join(&rel)); + } + } + } + Ok(out) +} + #[cfg(test)] mod tests; diff --git a/crates/ide-build/src/jbuild/tests.rs b/crates/ide-build/src/jbuild/tests.rs index 6d3e8f803..9a4b3a638 100644 --- a/crates/ide-build/src/jbuild/tests.rs +++ b/crates/ide-build/src/jbuild/tests.rs @@ -416,6 +416,63 @@ fn builds_maven_modules_with_javac_and_only_what_changed() { assert!(f.out("t.app/test-classes/a/AggTest.class").is_file()); } +#[test] +fn maps_resources_to_the_output_paths_the_build_tool_writes() { + let tmp = tempfile::tempdir().unwrap(); + let root = dunce::canonicalize(tmp.path()).unwrap(); + write(&root, "src/main/resources/app.properties", "a=1"); + write(&root, "src/main/resources/notes.txt", "n"); + write(&root, "src/main/resources/deep/app.properties", "b=2"); + let resources = root.join("src/main/resources"); + let output = root.join("out/classes"); + let set = SourceSet { + output: output.clone(), + resources: vec![ + ResourceRoot { directory: resources.clone(), ..Default::default() }, + // A `` and an `` pattern, as a POM can configure them. + ResourceRoot { + directory: resources, + target_path: Some("META-INF".into()), + includes: vec!["*.properties".into()], + ..Default::default() + }, + ], + ..Default::default() + }; + assert_eq!( + resource_outputs(&set, &CancellationToken::new()).unwrap(), + BTreeSet::from([ + output.join("app.properties"), + output.join("notes.txt"), + output.join("deep/app.properties"), + output.join("META-INF/app.properties"), + ]) + ); +} + +#[test] +fn deletes_the_output_of_a_resource_removed_from_the_sources() { + let Some(f) = Fixture::new(&[ + ("app/src/main/java/a/A.java", "package a;\npublic class A {}\n"), + ("app/src/main/resources/app.properties", "version=${project.version}\n"), + ("app/src/main/resources/META-INF/spring.factories", "a.A\n"), + ]) else { + return; + }; + assert_eq!(f.launch(false).failure, None); + assert_eq!(fs::read_to_string(f.out("t.app/classes/META-INF/spring.factories")).unwrap(), "a.A\n"); + assert!(f.steps_of_launch().is_empty()); + + // A deleted resource leaves no copy behind: the launch would read a file the sources no longer have. + fs::remove_file(f.root.join("app/src/main/resources/META-INF/spring.factories")).unwrap(); + let steps = f.steps_of_launch(); + assert!(steps.iter().any(|s| s.contains("resources:resources")), "{steps:?}"); + assert!(!f.out("t.app/classes/META-INF/spring.factories").exists(), "the removed resource stays on the classpath"); + // The resource that is still there keeps its copy, and a launch with nothing changed runs nothing. + assert_eq!(fs::read_to_string(f.out("t.app/classes/app.properties")).unwrap(), "version=1\n"); + assert!(f.steps_of_launch().is_empty()); +} + #[test] fn recompiles_the_classes_an_api_change_reaches() { let Some(f) = Fixture::new(&[