From 581672eebc025a442e55b8a6d862daf85a432951 Mon Sep 17 00:00:00 2001 From: Akbar Husain <43771058+akbar1214@users.noreply.github.com> Date: Thu, 3 Dec 2026 00:58:59 +0530 Subject: [PATCH] fix(run): pass the installed macOS SDK to clang for C/C++ files (#7595) A compiler that derives its default sysroot from the macOS version it targets (e.g. Homebrew LLVM) can look for an SDK that is not installed, so single-file C/C++ runs failed with 'stdio.h' file not found. Tool lookup now resolves the SDK with xcrun --show-sdk-path (only when the developer tools are installed, since the xcrun stub otherwise opens an installer dialog; cached after the first lookup), and the generated compile command passes it as -isysroot. Co-authored-by: akbar1214 <43771058+akbar1214@users.noreply.github.com> --- crates/ide-host/src/ops/sdk.rs | 3 +++ crates/ide-sdk/src/locate.rs | 19 +++++++++++++++++++ src/bindings.ts | 2 ++ src/features/run/producers.test.ts | 11 +++++++++++ src/features/run/producers.ts | 11 ++++++++++- src/features/run/runtimes.ts | 3 +++ 6 files changed, 48 insertions(+), 1 deletion(-) diff --git a/crates/ide-host/src/ops/sdk.rs b/crates/ide-host/src/ops/sdk.rs index 859784b568..20dfd98cb3 100644 --- a/crates/ide-host/src/ops/sdk.rs +++ b/crates/ide-host/src/ops/sdk.rs @@ -20,6 +20,8 @@ pub use container_python::{ContainerPython, sdk_container_python, sdk_container_ pub struct LocatedTools { pub tools: Vec, pub compile_dir: ide_core::IpcPath, + /// macOS SDK for `clang -isysroot`; absent off macOS or when no SDK is installed. + pub macos_sysroot: Option, } /// An interpreter another project uses: the project root and the SDK itself. @@ -361,6 +363,7 @@ pub async fn sdk_locate_tools( blocking_value(move || LocatedTools { tools: ide_sdk::locate::locate_tools(&names, near.as_deref()), compile_dir: ide_sdk::locate::compile_dir().into(), + macos_sysroot: ide_sdk::locate::macos_sysroot().map(Into::into), }) .await } diff --git a/crates/ide-sdk/src/locate.rs b/crates/ide-sdk/src/locate.rs index 1cf252c6f7..ca0b4a5ad1 100644 --- a/crates/ide-sdk/src/locate.rs +++ b/crates/ide-sdk/src/locate.rs @@ -121,6 +121,25 @@ pub fn compile_dir() -> PathBuf { std::env::temp_dir().join("openidea-run") } +/// The macOS SDK to pass to `clang -isysroot`. A newer compiler (e.g. Homebrew LLVM) +/// derives its default sysroot from the macOS version it targets, which can name an +/// SDK that is not installed (`MacOSX27.sdk`), so single-file C/C++ runs need the +/// SDK `xcrun` actually points at. `None` off macOS or when `xcrun` has no SDK. +pub fn macos_sysroot() -> Option { + // Without the Command Line Tools, `/usr/bin/xcrun` is a stub that opens an installer dialog. + let developer_tools = Path::new("/Library/Developer/CommandLineTools/usr/bin").is_dir() + || Path::new("/Applications/Xcode.app").is_dir(); + if cfg!(not(target_os = "macos")) || !developer_tools { + return None; + } + static SYSROOT: OnceLock = OnceLock::new(); + if let Some(path) = SYSROOT.get() { + return Some(path.clone()); + } + let path = PathBuf::from(query(Path::new("/usr/bin/xcrun"), &["--show-sdk-path"], VERSION_TIMEOUT)?); + path.is_dir().then(|| SYSROOT.get_or_init(|| path).clone()) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/bindings.ts b/src/bindings.ts index b5c60f8d85..380ea65d04 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -12448,6 +12448,8 @@ export type LocatedTool = { export type LocatedTools = { tools: LocatedTool[], compileDir: IpcPath, + /** macOS SDK for `clang -isysroot`; absent off macOS or when no SDK is installed. */ + macosSysroot: IpcPath | null, }; /** diff --git a/src/features/run/producers.test.ts b/src/features/run/producers.test.ts index 94806799ea..acbcd6c6c5 100644 --- a/src/features/run/producers.test.ts +++ b/src/features/run/producers.test.ts @@ -875,6 +875,17 @@ describe("single-file producers (scratches)", () => { expect(produceContext(scratch("lib.c", "int add(int a, int b);"), ALL)).toBeNull(); }); + it("C / C++: pass the detected macOS SDK to clang so it finds the system headers", () => { + const sdk = "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"; + const mac = env({ clang: null, "clang++": null, rustc: null }, { macosSysroot: sdk }); + const c = produceContext(scratch("a.c", "int main(void) { return 0; }"), mac)!; + expect(c.config!.command).toContain(`clang -g -isysroot ${sdk} -o `); + const cpp = produceContext(scratch("a.cpp", "int main() {}"), mac)!; + expect(cpp.config!.command).toContain(`clang++ -g -std=c++17 -isysroot ${sdk} -o `); + // The SDK is only a C/C++ compiler flag; other compiled languages keep their own flags. + expect(produceContext(scratch("a.rs", "fn main() {}"), mac)!.config!.command).not.toContain("-isysroot"); + }); + it("compiled binaries of same-named files in different directories don't collide", () => { const a = produceContext({ ...scratch("x.rs", "fn main() {}"), path: "/a/x.rs" }, ALL)!; const b = produceContext({ ...scratch("x.rs", "fn main() {}"), path: "/b/x.rs" }, ALL)!; diff --git a/src/features/run/producers.ts b/src/features/run/producers.ts index 480ed71532..70fb2e93b8 100644 --- a/src/features/run/producers.ts +++ b/src/features/run/producers.ts @@ -64,6 +64,8 @@ export interface ToolEnv { tool(name: string): LocatedTool | null | undefined; /** Where single source files are compiled to. */ compileDir: string; + /** macOS SDK for `clang -isysroot`, so newer compilers don't pick an uninstalled SDK by target version. */ + macosSysroot?: string | null; /** Windows: compile-and-run lines are for `cmd /c` (quoting, `.exe`, `mkdir`). */ windows?: boolean; /** The project interpreter from settings (Python). */ @@ -624,7 +626,14 @@ function compiled( : lang === "kotlin" ? // The jar's manifest names the file's main class (`HiKt`, `pkg.HiKt`). ["-include-runtime", "-d", out, s.arg] - : ["-g", ...(lang === "cpp" ? ["-std=c++17"] : []), "-o", out, s.arg]; + : [ + "-g", + ...(lang === "cpp" ? ["-std=c++17"] : []), + ...(env.macosSysroot ? ["-isysroot", env.macosSysroot] : []), + "-o", + out, + s.arg, + ]; const build = `${mkdirLine(dir, win)} && ${shellLine([compiler.command, ...flags], win)}`; const runArgs = test?.filter ?? []; const run = lang === "kotlin" ? [java!.command, "-jar", out] : [out, ...runArgs]; diff --git a/src/features/run/runtimes.ts b/src/features/run/runtimes.ts index 45985d3bc1..49851bfc31 100644 --- a/src/features/run/runtimes.ts +++ b/src/features/run/runtimes.ts @@ -20,6 +20,7 @@ interface Entry { const cache = new Map(); let compileDir: string | null = null; +let macosSysroot: string | null = null; let locateUnavailable = false; export function clearRuntimeCache() { @@ -64,6 +65,7 @@ export async function locateTools( try { const res = await call(commands.sdkLocateTools(todo, near || null)); compileDir = res.compileDir; + macosSysroot = res.macosSysroot ?? null; found = new Map(res.tools.map((t) => [t.name, t.path ? { path: t.path, version: t.version } : null])); } catch { // The backend predates `sdk_locate_tools` (needs a restart). @@ -158,6 +160,7 @@ export async function toolEnv( return { tool: (n) => tools.get(n), compileDir: compileDir ?? "/tmp/openidea-run", + macosSysroot, windows: isWindows(), python: py, php: phpInterpreter,