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 <[email protected]>
Цей коміт міститься в:
Akbar Husain authored and GitHub committed 2026-12-03 03:28:59 +08:00
1 джерело eace2fc741
коміт 581672eebc
6 files changed
+48 -1

No files matched your search

+3
Переглянути файл
@@ -20,6 +20,8 @@ pub use container_python::{ContainerPython, sdk_container_python, sdk_container_
pub struct LocatedTools {
pub tools: Vec<ide_sdk::locate::LocatedTool>,
pub compile_dir: ide_core::IpcPath,
/// macOS SDK for `clang -isysroot`; absent off macOS or when no SDK is installed.
pub macos_sysroot: Option<ide_core::IpcPath>,
}
/// 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
}
+19
Переглянути файл
@@ -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<PathBuf> {
// 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<PathBuf> = 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::*;
+2
Переглянути файл
@@ -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,
};
/**
+11
Переглянути файл
@@ -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)!;
+10 -1
Переглянути файл
@@ -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];
+3
Переглянути файл
@@ -20,6 +20,7 @@ interface Entry {
const cache = new Map<string, Entry>();
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,