diff --git a/crates/ide-run/src/xcode.rs b/crates/ide-run/src/xcode.rs index 60c015f8c6..8cd56e161f 100644 --- a/crates/ide-run/src/xcode.rs +++ b/crates/ide-run/src/xcode.rs @@ -230,25 +230,13 @@ pub fn default_simulator(dir: &Path) -> Option { pick_simulator(&String::from_utf8_lossy(&out.stdout)) } +/// The same rule as the toolbar's default (`currentIosSimulator`), over the same ordering. fn pick_simulator(json: &str) -> Option { - let devices: serde_json::Value = serde_json::from_str(json).ok()?; - let mut runtimes: Vec<(Vec, &Vec)> = devices - .get("devices")? - .as_object()? - .iter() - .filter_map(|(runtime, list)| { - let version = runtime.strip_prefix("com.apple.CoreSimulator.SimRuntime.iOS-")?; - Some((version.split('-').filter_map(|n| n.parse().ok()).collect(), list.as_array()?)) - }) - .collect(); - runtimes.sort_by(|a, b| b.0.cmp(&a.0)); - let all = || runtimes.iter().flat_map(|(_, list)| list.iter()); - let field = |d: &serde_json::Value, key: &str| d.get(key).and_then(|v| v.as_str()).unwrap_or_default().to_owned(); - all() - .find(|d| field(d, "state") == "Booted") - .or_else(|| all().find(|d| field(d, "name").starts_with("iPhone"))) - .map(|d| field(d, "udid")) - .filter(|udid| !udid.is_empty()) + let list = crate::simulator::parse_list(json).ok()?; + list.iter() + .find(|d| d.state == "Booted") + .or_else(|| list.iter().find(|d| d.name.starts_with("iPhone"))) + .map(|d| d.udid.clone()) } /// The run configuration parameter that holds the simulator's UDID. Empty means diff --git a/src/features/run/configs.test.ts b/src/features/run/configs.test.ts index bb706e2bae..7560a88f27 100644 --- a/src/features/run/configs.test.ts +++ b/src/features/run/configs.test.ts @@ -108,6 +108,19 @@ describe("config merge / override", () => { ]; expect(defaultSelection(spring)?.id).toBe("application:demo.App"); }); + + it("prefers the root Xcode project's first scheme over scripts in subfolders", () => { + const list = [ + cfg({ id: "npm:Scripts/slack_ios/worker:dev", kind: "npm", cwd: "Scripts/slack_ios/worker" }), + cfg({ id: "xcode:run:Tools/Tools.xcodeproj:Tool", kind: "xcode", cwd: "Tools" }), + cfg({ id: "xcode:run:wasai.xcworkspace:wasai", name: "wasai", kind: "xcode" }), + cfg({ id: "xcode:run:wasai.xcworkspace:WasaiKit", name: "WasaiKit", kind: "xcode" }), + cfg({ id: "xcode:test:wasai.xcworkspace:wasai", kind: "xcode-test", isTest: true }), + ]; + expect(defaultSelection(list)?.id).toBe("xcode:run:wasai.xcworkspace:wasai"); + // An Xcode project in a subfolder leaves the root's own run script first. + expect(defaultSelection([cfg({ id: "npm:dev", kind: "npm" }), list[1]!])?.id).toBe("npm:dev"); + }); }); describe("validateConfig", () => { diff --git a/src/features/run/store.ts b/src/features/run/store.ts index 5f0ad9bc41..3328c9f1a7 100644 --- a/src/features/run/store.ts +++ b/src/features/run/store.ts @@ -369,11 +369,15 @@ async function reattachRemoteRuns(root: string, knownConfigs: readonly RunConfig } } -/** IDEA picks an application config first; prefer a run/dev/start target over builds and tests. */ +/** + * IDEA picks an application config first; prefer a run/dev/start target over builds and tests. + * In an Xcode project the root's first scheme wins, as it does for the backend's builds. + */ export function defaultSelection(configs: readonly RunConfig[]): RunConfig | undefined { const score = (c: RunConfig) => { if (c.isTest) return 1; - if (c.kind === "spring-boot") return 4; + // A root Xcode scheme has no cwd; one in a subfolder runs there. + if (c.kind === "spring-boot" || (c.kind === "xcode" && !c.cwd)) return 4; const s = `${c.id} ${c.name}`.toLowerCase(); if (/bootrun|spring-boot:run|:run\b|run:|\bdev\b|\bstart\b|\bserve\b|runserver/.test(s)) return 3; if (/build|package|lint|check|format|clean/.test(s)) return 0; diff --git a/src/workbench/iosSimulators.test.ts b/src/workbench/iosSimulators.test.ts index a2a1791bfb..09bbd9127f 100644 --- a/src/workbench/iosSimulators.test.ts +++ b/src/workbench/iosSimulators.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from "vitest"; import type { IosSimulator, RunConfig } from "@/platform/ipc"; -import { iosSimulatorFor, refreshIosSimulators, useIosSimulators } from "./iosSimulators"; +import { currentIosSimulator, iosSimulatorFor, refreshIosSimulators, useIosSimulators } from "./iosSimulators"; vi.mock("./contributions", () => ({ notifications: () => ({ notify: vi.fn() }) })); @@ -34,6 +34,34 @@ describe("iosSimulatorFor", () => { }); }); +describe("currentIosSimulator", () => { + // Ordered as the backend lists them: booted, then newest runtime, then by name, then devices. + const named = (udid: string, name: string, runtime = "iOS 27.0", state = "Shutdown") => ({ + ...sim(udid, state), + name, + runtime, + }); + const pad = named("PAD", "iPad (A16)"); + const phone = named("PHONE", "iPhone 18"); + const oldPhone = named("OLD", "iPhone 16", "iOS 18.2"); + const device = { ...named("DEVICE", "iPhone", "iOS 27.0", "Available"), physical: true }; + + it("defaults to a booted simulator, then the newest iPhone, as the backend does", () => { + useIosSimulators.setState({ list: [pad, phone, oldPhone, device], preferred: null }); + expect(currentIosSimulator()?.udid).toBe("PHONE"); + const booted = { ...oldPhone, state: "Booted" }; + useIosSimulators.setState({ list: [booted, pad, phone, device] }); + expect(currentIosSimulator()?.udid).toBe("OLD"); + useIosSimulators.setState({ list: [pad, device] }); + expect(currentIosSimulator()?.udid).toBe("PAD"); + }); + + it("keeps the simulator the user picked", () => { + useIosSimulators.setState({ list: [pad, phone], preferred: "PAD" }); + expect(currentIosSimulator()?.udid).toBe("PAD"); + }); +}); + describe("refreshIosSimulators", () => { it("keeps the chooser hidden when the first listing fails, and reports later failures", async () => { useIosSimulators.setState({ root: "/p", list: null, error: null }); diff --git a/src/workbench/iosSimulators.ts b/src/workbench/iosSimulators.ts index 6017e1f58e..8d3a422142 100644 --- a/src/workbench/iosSimulators.ts +++ b/src/workbench/iosSimulators.ts @@ -36,10 +36,20 @@ function storedSimulator(root: string): string | null { } } -/** The toolbar's simulator: the chosen one while listed, else the first (booted ones come first). */ +/** + * The toolbar's simulator: the chosen one while listed, else the backend's default + * (`pick_simulator`): a booted simulator, then the newest iPhone. The list comes booted first, + * then newest runtime first. + */ export function currentIosSimulator(state = useIosSimulators.getState()): IosSimulator | null { const list = state.list ?? []; - return list.find((s) => s.udid === state.preferred) ?? list[0] ?? null; + return ( + list.find((s) => s.udid === state.preferred) ?? + list.find((s) => !s.physical && s.state === "Booted") ?? + list.find((s) => !s.physical && s.name.startsWith("iPhone")) ?? + list[0] ?? + null + ); } /**