Файли
openidea/crates/ide-run/src/detect/xcode.rs
T
LU XIANGRUIтаwildcatDownstairs 7a27f285e6 fix(xcode): set up code insight for Xcode projects without asking (#7219)
Opening a trusted Xcode project with xcode-build-server on PATH and no buildServer.json/.bsp now writes the config on open (added to .git/info/exclude unless tracked or ignored); an existing config is never touched. Runs build into Xcode's DerivedData, where the config reads build logs, so new files resolve after ▶ without restarting sourcekit-lsp; a workspace next to the project is built even without schemes or a Podfile (Tuist, SwiftPM).

Fixes #7213

Co-authored-by: wildcatDownstairs <[email protected]>
2026-12-01 19:45:16 +08:00

211 рядки
10 KiB
Rust

//! Xcode schemes: one configuration per scheme that builds its app and runs it on the iOS
//! Simulator or the Mac (see [`crate::xcode::prepare`]). A workspace next to the projects builds them all
//! and links what they depend on (CocoaPods, Tuist, a SwiftPM workspace), so its schemes and theirs
//! are built through it, even when it has no schemes of its own.
use super::{ConfigExt, config};
use crate::RunConfig;
use crate::xcode::{XcodeScheme, schemes};
use std::path::Path;
/// A folder holding an Xcode project or workspace, other than the `ios/` / `macos/` host app
/// of a Flutter or React Native project, which that project's own tooling runs, and a CocoaPods
/// `Pods/` folder.
pub(super) fn is_project(dir: &Path) -> bool {
let hosted = dir.parent().is_some_and(|p| p.join("pubspec.yaml").is_file() || p.join("package.json").is_file());
!hosted
&& !crate::xcode::is_pods_dir(dir)
&& std::fs::read_dir(dir).is_ok_and(|entries| {
entries.flatten().any(|e| e.path().extension().is_some_and(|x| x == "xcodeproj" || x == "xcworkspace"))
})
}
pub(super) fn detect(dir: &Path, out: &mut Vec<RunConfig>) {
let found = schemes(dir);
let workspace = found
.iter()
.map(|s| s.container.clone())
.find(|c| c.extension().is_some_and(|x| x == "xcworkspace"))
.or_else(|| crate::xcode::root_workspace(dir));
for scheme in &found {
let container = workspace.as_ref().unwrap_or(&scheme.container);
let container = container.strip_prefix(dir).unwrap_or(container).to_string_lossy().into_owned();
out.push(scheme_config(scheme, &container));
}
}
fn scheme_config(scheme: &XcodeScheme, container: &str) -> RunConfig {
let flag = if container.ends_with(".xcworkspace") { "-workspace" } else { "-project" };
let mut args = vec![flag, container, "-scheme", &scheme.name];
if let Some(configuration) = &scheme.launch_configuration {
args.extend(["-configuration", configuration]);
}
let mut c =
config(format!("xcode:run:{container}:{}", scheme.name), scheme.name.clone(), "xcode", "xcodebuild", &args)
.debug("lldb-dap");
let params = &mut c.extras.params;
params.insert("container".into(), container.to_owned());
params.insert("scheme".into(), scheme.name.clone());
params.extend(scheme.launch_configuration.clone().map(|v| ("configuration".to_owned(), v)));
if !scheme.arguments.is_empty() {
params.insert("arguments".into(), scheme.arguments.join(" "));
}
c.env = scheme.environment.iter().cloned().collect();
c
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_configuration_per_scheme_built_through_the_workspace() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/xcode");
assert!(is_project(&root));
let mut out = Vec::new();
detect(&root, &mut out);
let ids: Vec<_> = out.iter().map(|c| c.id.as_str()).collect();
assert_eq!(
ids,
[
"xcode:run:App.xcworkspace:App",
"xcode:run:App.xcworkspace:Scratch",
"xcode:run:App.xcworkspace:Everything"
]
);
let app = &out[0];
assert_eq!((app.kind.as_str(), app.command.as_str()), ("xcode", "xcodebuild"));
assert_eq!(app.debug_adapter.as_deref(), Some("lldb-dap"));
assert_eq!(app.args, ["-workspace", "App.xcworkspace", "-scheme", "App", "-configuration", "Staging"]);
assert_eq!(app.extras.params["container"], "App.xcworkspace");
assert_eq!(app.extras.params["configuration"], "Staging");
assert_eq!(app.extras.params["arguments"], "-com.apple.CoreData.SQLDebug 1 --name=\"A & B\"");
assert_eq!(app.env["API_HOST"], "localhost:8080");
assert!(!app.extras.params.contains_key(crate::xcode::SIMULATOR_PARAM));
}
#[test]
fn a_cocoapods_project_builds_and_tests_through_its_workspace() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let scheme = std::fs::read_to_string(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/xcode/App.xcodeproj/xcshareddata/xcschemes/App.xcscheme"),
)
.unwrap();
for dir in ["App.xcodeproj", "Pods/Pods.xcodeproj"] {
let schemes = root.join(dir).join("xcshareddata/xcschemes");
std::fs::create_dir_all(&schemes).unwrap();
std::fs::write(schemes.join("App.xcscheme"), &scheme).unwrap();
}
let args = |out: &[RunConfig]| out.iter().map(|c| c.args.join(" ")).collect::<Vec<_>>();
// Project only.
std::fs::rename(root.join("Pods"), root.join("Pods.off")).unwrap();
let mut out = Vec::new();
detect(root, &mut out);
assert_eq!(args(&out), ["-project App.xcodeproj -scheme App -configuration Staging"]);
std::fs::rename(root.join("Pods.off"), root.join("Pods")).unwrap();
// `pod install`'s (or Tuist's) workspace has no schemes of its own.
std::fs::create_dir(root.join("App.xcworkspace")).unwrap();
// Without a Podfile, a `Pods/` folder is just another project.
assert!(is_project(&root.join("Pods")));
std::fs::write(root.join("Podfile"), "").unwrap();
assert!(is_project(root) && !is_project(&root.join("Pods")));
let mut out = Vec::new();
detect(root, &mut out);
assert_eq!(args(&out), ["-workspace App.xcworkspace -scheme App -configuration Staging"]);
let mut tests = Vec::new();
super::super::xcode_test::detect(root, &mut tests);
assert_eq!(args(&tests), ["test -workspace App.xcworkspace -scheme App"]);
// A leftover workspace that sorts first: the one named after the project still wins.
std::fs::create_dir(root.join("Aardvark.xcworkspace")).unwrap();
let mut out = Vec::new();
detect(root, &mut out);
assert_eq!(args(&out), ["-workspace App.xcworkspace -scheme App -configuration Staging"]);
assert_eq!(crate::xcode::root_workspace(root), Some(root.join("App.xcworkspace")));
// Without a workspace named after a project, the first by name.
std::fs::remove_dir(root.join("App.xcworkspace")).unwrap();
std::fs::create_dir(root.join("Old.xcworkspace")).unwrap();
assert_eq!(crate::xcode::root_workspace(root), Some(root.join("Aardvark.xcworkspace")));
}
fn project_with_scheme(root: &Path) {
let scheme = std::fs::read_to_string(
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/xcode/App.xcodeproj/xcshareddata/xcschemes/App.xcscheme"),
)
.unwrap();
let schemes = root.join("App.xcodeproj/xcshareddata/xcschemes");
std::fs::create_dir_all(&schemes).unwrap();
std::fs::write(schemes.join("App.xcscheme"), &scheme).unwrap();
}
fn workspace(root: &Path, name: &str, location: &str) {
std::fs::create_dir(root.join(name)).unwrap();
let data = format!(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Workspace\n version = \"1.0\">\n <FileRef\n location = \"{location}\">\n </FileRef>\n</Workspace>\n"
);
std::fs::write(root.join(name).join("contents.xcworkspacedata"), data).unwrap();
}
fn run_and_test_args(root: &Path) -> (Vec<String>, Vec<String>) {
let args = |out: &[RunConfig]| out.iter().map(|c| c.args.join(" ")).collect::<Vec<_>>();
let (mut out, mut tests) = (Vec::new(), Vec::new());
detect(root, &mut out);
super::super::xcode_test::detect(root, &mut tests);
(args(&out), args(&tests))
}
#[test]
fn an_unrelated_workspace_beside_the_project_is_not_built() {
// No Podfile: a leftover or playground workspace that doesn't name the project.
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
project_with_scheme(root);
workspace(root, "Playground.xcworkspace", "group:Playground.playground");
std::fs::create_dir(root.join("Old.xcworkspace")).unwrap();
assert_eq!(crate::xcode::root_workspace(root), None);
let (run, test) = run_and_test_args(root);
assert_eq!(run, ["-project App.xcodeproj -scheme App -configuration Staging"]);
assert_eq!(test, ["test -project App.xcodeproj -scheme App"]);
// A project in a subfolder with the same name is not this one.
workspace(root, "Sub.xcworkspace", "group:Sub/App.xcodeproj");
assert_eq!(crate::xcode::root_workspace(root), None);
}
#[test]
fn a_tuist_workspace_that_references_the_project_is_built() {
// Tuist names the workspace after the manifest, not after the project.
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
project_with_scheme(root);
workspace(root, "Wasai.xcworkspace", "container:App.xcodeproj");
let (run, test) = run_and_test_args(root);
assert_eq!(run, ["-workspace Wasai.xcworkspace -scheme App -configuration Staging"]);
assert_eq!(test, ["test -workspace Wasai.xcworkspace -scheme App"]);
std::fs::remove_dir_all(root.join("Wasai.xcworkspace")).unwrap();
workspace(root, "Other.xcworkspace", "group:./App.xcodeproj");
assert_eq!(crate::xcode::root_workspace(root), Some(root.join("Other.xcworkspace")));
}
#[test]
fn a_workspace_without_schemes_builds_the_projects_schemes() {
// A hand-made workspace named after the project, with no Podfile and no schemes.
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
project_with_scheme(root);
std::fs::create_dir(root.join("App.xcworkspace")).unwrap();
let (run, test) = run_and_test_args(root);
assert_eq!(run, ["-workspace App.xcworkspace -scheme App -configuration Staging"]);
assert_eq!(test, ["test -workspace App.xcworkspace -scheme App"]);
// Workspace only: nothing to build without a scheme.
std::fs::remove_dir_all(root.join("App.xcodeproj")).unwrap();
let mut out = Vec::new();
detect(root, &mut out);
assert!(out.is_empty());
}
}