From a4394d3eced88274bd2f2e6c08de71efc94ec092 Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Sun, 6 Dec 2026 03:46:52 +0800 Subject: [PATCH] fix(gradle): list tasks and build when a build enables Isolated Projects (#8279) With org.gradle.isolated-projects=true the IDE's own Gradle runs (task listing, single-file compile classpath, IDE build step) failed: they force the configuration cache off, which Gradle refuses under Isolated Projects, and their init scripts read every project from the root. When the build's or the Gradle user home's gradle.properties enables it, those runs now pass -Dorg.gradle.isolated-projects=false and the Gradle 8 name too; other builds keep their command line. Run/debug/test init scripts are left for a follow-up that reads each project on its own. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- crates/ide-build/src/compile/single.rs | 11 ++-- crates/ide-build/src/gradle.rs | 88 ++++++++++++++++++++++++++ crates/ide-build/src/jbuild/model.rs | 1 + crates/ide-build/src/jbuild/tests.rs | 16 +++++ crates/ide-build/src/sync.rs | 44 +++++++++++-- 5 files changed, 150 insertions(+), 10 deletions(-) diff --git a/crates/ide-build/src/compile/single.rs b/crates/ide-build/src/compile/single.rs index db0aeb9002..c4623b757f 100644 --- a/crates/ide-build/src/compile/single.rs +++ b/crates/ide-build/src/compile/single.rs @@ -603,17 +603,16 @@ pub fn gradle_compile_file_plan( fs::create_dir_all(cache)?; let init_script = cache.join("gradle-compile-cp.init.gradle"); fs::write(&init_script, GRADLE_COMPILE_CP_INIT_SCRIPT)?; - let args = vec![ - "--console=plain".into(), - "-q".into(), - "-Dorg.gradle.configuration-cache=false".into(), + let gradle_dir = subdir(root, &gradle.dir); + let mut args = vec!["--console=plain".into(), "-q".into(), "-Dorg.gradle.configuration-cache=false".into()]; + args.extend(crate::gradle::no_isolated_projects(&gradle_dir, &gradle.env)); + args.extend([ "--init-script".into(), init_script.to_string_lossy().into_owned(), format!("-Popenidea.target={}", project.path), format!("-Popenidea.scope={scope}"), "help".into(), - ]; - let gradle_dir = subdir(root, &gradle.dir); + ]); let mut reported: Option = None; let out = process::run_bounded(&gradle.command, &args, &gradle_dir, &gradle.env, cancel, |_, line| { if let Some(json) = line.strip_prefix("@openidea-compile-cp\t") { diff --git a/crates/ide-build/src/gradle.rs b/crates/ide-build/src/gradle.rs index 5eb1753785..76a16c9e44 100644 --- a/crates/ide-build/src/gradle.rs +++ b/crates/ide-build/src/gradle.rs @@ -12,6 +12,47 @@ use std::time::{Duration, Instant}; pub const SETTINGS_FILES: &[&str] = &["settings.gradle.kts", "settings.gradle"]; pub const BUILD_FILES: &[&str] = &["build.gradle.kts", "build.gradle"]; +const ISOLATED_PROJECTS: [&str; 2] = ["org.gradle.isolated-projects", "org.gradle.unsafe.isolated-projects"]; + +/// Arguments turning Isolated Projects off for the IDE's own model reads of the build at `dir`, when +/// the build enables it: Gradle refuses them without the configuration cache, and their init scripts +/// read projects across the build. Other builds keep their command line. Gradle 9 still reads the +/// Gradle 8 name. +pub(crate) fn no_isolated_projects(dir: &Path, env: &BTreeMap) -> Vec { + // The variable as the Gradle process gets it: the project's and the login shell's environment too. + let inherited = ide_core::process::host_vars_in(dir) + .chain(ide_core::process::shell_env_in(dir)) + .find(|(key, _)| key == "GRADLE_USER_HOME") + .map(|(_, value)| value); + let home = user_home(dir, env, inherited, dirs::home_dir()); + if !isolated_projects(dir, home.as_deref()) { + return Vec::new(); + } + ISOLATED_PROJECTS.iter().map(|key| format!("-D{key}=false")).collect() +} + +fn user_home( + dir: &Path, + env: &BTreeMap, + inherited: Option, + home: Option, +) -> Option { + let set = env.get("GRADLE_USER_HOME").map(PathBuf::from).or_else(|| inherited.map(PathBuf::from)); + set.map(|path| dir.join(path)).or_else(|| Some(home?.join(".gradle"))) +} + +/// The user home's `gradle.properties` overrides the build's; when the two names disagree, Gradle +/// keeps Isolated Projects off. +fn isolated_projects(dir: &Path, user_home: Option<&Path>) -> bool { + let user = user_home.map(file_properties).unwrap_or_default(); + let build = file_properties(dir); + let values: Vec = ISOLATED_PROJECTS + .iter() + .filter_map(|key| user.get(*key).or_else(|| build.get(*key))) + .map(|value| value.eq_ignore_ascii_case("true")) + .collect(); + values.contains(&true) && !values.contains(&false) +} #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)] #[serde(rename_all = "camelCase")] @@ -1578,6 +1619,53 @@ dependencies { use super::*; + #[test] + fn isolated_projects_are_read_from_the_build_and_the_user_home() { + let temp = tempfile::tempdir().unwrap(); + let (build, home) = (temp.path().join("build"), temp.path().join("home")); + std::fs::create_dir_all(&build).unwrap(); + std::fs::create_dir_all(&home).unwrap(); + let enabled = |user_home: Option<&Path>| isolated_projects(&build, user_home); + assert!(!enabled(Some(&home)), "no gradle.properties"); + std::fs::write(build.join("gradle.properties"), "org.gradle.jvmargs=-Xmx2g\n").unwrap(); + assert!(!enabled(Some(&home)), "not enabled"); + std::fs::write(build.join("gradle.properties"), "org.gradle.isolated-projects=true\n").unwrap(); + assert!(enabled(Some(&home)), "the build's gradle.properties"); + assert!(enabled(None)); + std::fs::write(home.join("gradle.properties"), "org.gradle.isolated-projects=false\n").unwrap(); + assert!(!enabled(Some(&home)), "the user home overrides the build"); + std::fs::write(build.join("gradle.properties"), "").unwrap(); + std::fs::write(home.join("gradle.properties"), "org.gradle.unsafe.isolated-projects = true\n").unwrap(); + assert!(enabled(Some(&home)), "the user home's gradle.properties, Gradle 8 name"); + std::fs::write(build.join("gradle.properties"), "org.gradle.isolated-projects=false\n").unwrap(); + assert!(!enabled(Some(&home)), "disabled under either name wins"); + } + + #[test] + fn the_gradle_user_home_is_the_builds_setting_then_the_environment_then_dot_gradle() { + let dir = Path::new("/build"); + let env = BTreeMap::from([("GRADLE_USER_HOME".to_owned(), "/setting".to_owned())]); + let inherited = || Some(std::ffi::OsString::from("/inherited")); + let home = || Some(PathBuf::from("/home/u")); + assert_eq!(user_home(dir, &env, inherited(), home()), Some(PathBuf::from("/setting"))); + assert_eq!(user_home(dir, &BTreeMap::new(), inherited(), home()), Some(PathBuf::from("/inherited"))); + assert_eq!(user_home(dir, &BTreeMap::new(), None, home()), Some(PathBuf::from("/home/u/.gradle"))); + let relative = Some(std::ffi::OsString::from("gradle-home")); + assert_eq!(user_home(dir, &BTreeMap::new(), relative, home()), Some(PathBuf::from("/build/gradle-home"))); + } + + #[test] + fn builds_without_isolated_projects_keep_their_command_line() { + let temp = tempfile::tempdir().unwrap(); + let env = BTreeMap::from([("GRADLE_USER_HOME".to_owned(), temp.path().join("home").display().to_string())]); + assert!(no_isolated_projects(temp.path(), &env).is_empty()); + std::fs::write(temp.path().join("gradle.properties"), "org.gradle.isolated-projects=true\n").unwrap(); + assert_eq!( + no_isolated_projects(temp.path(), &env), + ["-Dorg.gradle.isolated-projects=false", "-Dorg.gradle.unsafe.isolated-projects=false"] + ); + } + #[test] fn settings_scripts() { let kts = r#" diff --git a/crates/ide-build/src/jbuild/model.rs b/crates/ide-build/src/jbuild/model.rs index 3a8d904d1e..008fb2d3ec 100644 --- a/crates/ide-build/src/jbuild/model.rs +++ b/crates/ide-build/src/jbuild/model.rs @@ -300,6 +300,7 @@ pub(super) fn gradle_step( &["--console=plain", "--no-configuration-cache"], &subdir(root, &g.dir), ); + step.args.extend(crate::gradle::no_isolated_projects(&step.cwd, &g.env)); step.args.extend(["--init-script".to_owned(), init.to_string_lossy().into_owned()]); step.args.push(format!("-Popenidea.build.output={}", output_root.display())); if let Some(file) = model_file { diff --git a/crates/ide-build/src/jbuild/tests.rs b/crates/ide-build/src/jbuild/tests.rs index e04013fdef..080c6124e0 100644 --- a/crates/ide-build/src/jbuild/tests.rs +++ b/crates/ide-build/src/jbuild/tests.rs @@ -792,6 +792,22 @@ fn builds_gradle_projects_with_javac_and_only_what_changed() { assert_eq!(error.path.as_deref(), Some(f.root.join("lib/src/main/java/l/L.java").as_path())); } +#[test] +fn builds_gradle_projects_that_enable_isolated_projects() { + let Some(f) = Fixture::gradle(&[ + ( + "gradle.properties", + "org.gradle.daemon=false\norg.gradle.configuration-cache=true\norg.gradle.isolated-projects=true\norg.gradle.unsafe.isolated-projects=true\n", + ), + ("app/src/main/java/a/A.java", "package a;\npublic class A {}\n"), + ]) else { + return; + }; + let built = f.launch(false); + assert_eq!(built.failure, None, "{:?}\n{}", built.messages, build_output_tail(&f.events.lock().unwrap())); + assert!(f.out("t.app/classes/a/A.class").is_file()); +} + #[test] fn leaves_gradle_projects_with_other_compilers_to_gradle() { let Some(f) = Fixture::gradle(&[ diff --git a/crates/ide-build/src/sync.rs b/crates/ide-build/src/sync.rs index f450a96f72..9a2aef9cc4 100644 --- a/crates/ide-build/src/sync.rs +++ b/crates/ide-build/src/sync.rs @@ -499,11 +499,9 @@ pub fn sync_gradle_model( "-q".into(), // The init script reports the evaluated projects, even when a build enables the configuration cache. "-Dorg.gradle.configuration-cache=false".into(), - "--init-script".into(), - init_script.to_string_lossy().into_owned(), - "tasks".into(), - "--all".into(), ]; + args.extend(gradle::no_isolated_projects(&root.join(&model.dir), &options.env)); + args.extend(["--init-script".into(), init_script.to_string_lossy().into_owned(), "tasks".into(), "--all".into()]); if options.offline { args.push("--offline".into()); } @@ -1208,4 +1206,42 @@ mod tests { assert!(args.contains("-T1"), "args: {args}"); assert!(args.contains("maven-help-plugin:"), "args: {args}"); } + + /// Needs `JAVA_HOME` and Gradle (`OPENIDEA_TEST_GRADLE`, else `gradle` on the search path). + #[test] + fn gradle_sync_lists_tasks_of_builds_that_enable_isolated_projects() { + let has_jdk = std::env::var_os("JAVA_HOME").is_some(); + let gradle = std::env::var_os("OPENIDEA_TEST_GRADLE") + .map(std::path::PathBuf::from) + .or_else(|| ide_core::process::find_executable("gradle", &ide_core::tool_path::search_path())); + let Some(gradle) = gradle.filter(|_| has_jdk) else { + eprintln!("skipped: needs JAVA_HOME and gradle (OPENIDEA_TEST_GRADLE)"); + return; + }; + let temp = tempfile::tempdir().unwrap(); + let root = dunce::canonicalize(temp.path()).unwrap(); + let files = [ + ( + "gradle.properties", + "org.gradle.daemon=false\norg.gradle.configuration-cache=true\norg.gradle.isolated-projects=true\norg.gradle.unsafe.isolated-projects=true\n", + ), + ("settings.gradle", "rootProject.name = 'root'\ninclude 'lib'\n"), + ("build.gradle", ""), + ("lib/build.gradle", "plugins { id 'java' }\n"), + ]; + for (rel, text) in files { + let path = root.join(rel); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, text).unwrap(); + } + let mut model = gradle::load(&root).unwrap(); + model.command = gradle.to_string_lossy().into_owned(); + let cache = root.join(".cache"); + sync_gradle_model(&root, &cache, &model, &SyncOptions::default(), &CancellationToken::new(), &|_| {}).unwrap(); + let loaded = crate::load(&root, &cache, None); + let build = &loaded.gradle[0]; + assert_eq!(build.sync_error, None); + let lib = build.projects.iter().find(|p| p.path == ":lib").expect("the reported :lib project"); + assert!(lib.tasks.iter().any(|t| t.name == "compileJava"), "{:?}", lib.tasks); + } }