Skip to content

The agent that grows with you

fix(desktop): never fail native builds on SDK resolution

brooklyn!4006ca2fa3a5
diff --git a/apps/desktop/scripts/macos-sysroot-native.test.mjs b/apps/desktop/scripts/macos-sysroot-native.test.mjs
index 6df8dbc6288..65149834650 100644
--- a/apps/desktop/scripts/macos-sysroot-native.test.mjs
+++ b/apps/desktop/scripts/macos-sysroot-native.test.mjs
@@ -1,39 +1,65 @@
-import { execFileSync } from 'node:child_process'
-import { mkdtempSync, rmSync } from 'node:fs'
+import { execFileSync, spawnSync } from 'node:child_process'
+import { mkdtempSync, readdirSync, realpathSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
-import { resolve } from 'node:path'
+import { dirname, resolve } from 'node:path'
import { expect, test } from 'vitest'
import { macosSysroot } from './macos-sysroot.mjs'
-test.skipIf(process.platform !== 'darwin')('builds both universal helpers with default, path and named SDK selection', () => {
+const env = { ...process.env, SDKROOT: '' }
+const xcrun = args => execFileSync('xcrun', args, { encoding: 'utf8', env }).trim()
+const sdkVersion = sdk => xcrun(['--sdk', sdk, '--show-sdk-version']).split('.').map(Number)
+const compareVersions = (a, b) => a.map((n, i) => n - (b[i] ?? 0)).find(d => d !== 0) ?? 0
+const helpers = [
+ ['build-command-screenshot-monitor.mjs', 'native/command-screenshot-monitor'],
+ ['build-hud-modifier-monitor.mjs', 'native/darwin-universal/hud-modifier-monitor']
+]
+
+// An installed SDK other than the default, older so the active linker accepts
+// it (a newer one is the very mismatch #113708 is about). Null when the host
+// only has the default installed.
+function olderInstalledSdk(defaultSdk) {
+ const seen = new Set([realpathSync(defaultSdk)])
+ const current = sdkVersion(defaultSdk)
+ for (const entry of readdirSync(dirname(defaultSdk)).sort().reverse()) {
+ if (!/^MacOSX.*\.sdk$/.test(entry)) continue
+ const sdk = resolve(dirname(defaultSdk), entry)
+ const real = realpathSync(sdk)
+ if (seen.has(real)) continue
+ seen.add(real)
+ if (compareVersions(sdkVersion(sdk), current) < 0) return sdk
+ }
+ return null
+}
+
+test.skipIf(process.platform !== 'darwin')('builds both universal helpers under every SDK selection rung', () => {
const dir = mkdtempSync(resolve(tmpdir(), 'hermes-sdk-builds-'))
- const env = { ...process.env, SDKROOT: '' }
- const helpers = [
- ['build-command-screenshot-monitor.mjs', 'native/command-screenshot-monitor'],
- ['build-hud-modifier-monitor.mjs', 'native/darwin-universal/hud-modifier-monitor']
- ]
try {
- const sdk = macosSysroot(env) ?? execFileSync('xcrun', ['--sdk', 'macosx', '--show-sdk-path'], {
- encoding: 'utf8', env
- }).trim()
- const version = execFileSync('xcrun', ['--sdk', sdk, '--show-sdk-version'], {
- encoding: 'utf8', env
- }).trim()
- for (const [name, SDKROOT] of [['default', ''], ['path', sdk], ['name', `macosx${version}`]]) {
+ const sdk = macosSysroot(env) ?? xcrun(['--sdk', 'macosx', '--show-sdk-path'])
+ const other = olderInstalledSdk(sdk)
+ const selections = [
+ ['default', ''],
+ ['path', sdk],
+ ['name', `macosx${sdkVersion(sdk).join('.')}`],
+ // A stale SDKROOT must be reported and ignored, never fail the build.
+ ['stale', resolve(dir, 'MacOSX99.sdk')],
+ ['not-an-sdk', dir],
+ ...(other ? [['other', other]] : [])
+ ]
+ for (const [name, SDKROOT] of selections) {
for (const [script, relativeBinary] of helpers) {
const dist = resolve(dir, name, script)
- execFileSync(process.execPath, [resolve(import.meta.dirname, script), '--out-dir', dist], {
- env: { ...env, SDKROOT }, timeout: 60_000
+ const build = spawnSync(process.execPath, [resolve(import.meta.dirname, script), '--out-dir', dist], {
+ encoding: 'utf8', env: { ...env, SDKROOT }, timeout: 60_000
})
- const architectures = execFileSync('xcrun', ['lipo', '-archs', resolve(dist, relativeBinary)], {
- encoding: 'utf8', env
- }).trim().split(/\s+/).sort()
+ expect(build.status, `${script} with ${name} SDK selection\n${build.stderr}`).toBe(0)
+ const architectures = xcrun(['lipo', '-archs', resolve(dist, relativeBinary)]).split(/\s+/).sort()
expect(architectures, `${script} with ${name} SDK selection`).toEqual(['arm64', 'x86_64'])
+ if (name === 'stale' || name === 'not-an-sdk') expect(build.stderr).toContain(`SDKROOT=${SDKROOT} is not an installed SDK`)
}
}
} finally {
rmSync(dir, { recursive: true, force: true })
}
-}, 120_000)
+}, 180_000)
diff --git a/apps/desktop/scripts/macos-sysroot.mjs b/apps/desktop/scripts/macos-sysroot.mjs
index dd706e0f63c..57b9d155902 100644
--- a/apps/desktop/scripts/macos-sysroot.mjs
+++ b/apps/desktop/scripts/macos-sysroot.mjs
@@ -3,23 +3,53 @@ import { execFileSync } from 'node:child_process'
import { existsSync } from 'node:fs'
import { resolve } from 'node:path'
+// Every rung below is a read: a failing tool or a missing directory falls to
+// the next rung instead of failing the build.
+const read = (file, args, env) => {
+ try {
+ return execFileSync(file, args, { encoding: 'utf8', env, stdio: 'pipe' }).trim()
+ } catch {
+ return ''
+ }
+}
+
+// Every macOS SDK carries SDKSettings.plist; xcrun accepts any existing
+// directory as an SDK path, so existence alone does not prove one.
+const isSdk = path => Boolean(path) && existsSync(resolve(path, 'SDKSettings.plist'))
+
+// The installed SDK an explicit SDKROOT names, or null. SDKROOT accepts SDK
+// names as well as paths; clang's -isysroot only accepts paths, so both forms
+// go through xcrun.
+function explicitSysroot(sdkroot, env) {
+ const path = read('xcrun', ['--sdk', sdkroot, '--show-sdk-path'], env)
+ return isSdk(path) ? path : null
+}
+
+// The MacOSX.sdk alias of the active developer directory (Command Line Tools
+// layout first, then Xcode's), or null when the toolchain has neither.
+function developerSysroot(env) {
+ const developerDir = read('xcode-select', ['-p'], env)
+ if (!developerDir) return null
+ return [
+ resolve(developerDir, 'SDKs/MacOSX.sdk'),
+ resolve(developerDir, 'Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'),
+ ].find(isSdk) ?? null
+}
+
// `--sdk macosx` can select a newer SDK than the linker understands (#113708).
// Prefer the active toolchain's MacOSX.sdk alias unless the caller pins an SDK.
+// A stale SDKROOT never fails the build: it is reported, then ignored, which
+// matches what `xcrun --sdk macosx` did with it before this resolver existed.
export function macosSysroot(env = process.env) {
if (env.SDKROOT) {
- // SDKROOT accepts SDK names as well as paths; clang's -isysroot only accepts paths.
- return execFileSync('xcrun', ['--sdk', env.SDKROOT, '--show-sdk-path'], {
- encoding: 'utf8', env
- }).trim()
+ const explicit = explicitSysroot(env.SDKROOT, env)
+ if (explicit) return explicit
+ console.warn(`[macos-sysroot] SDKROOT=${env.SDKROOT} is not an installed SDK; using the active toolchain's default`)
}
- const developerDir = execFileSync('xcode-select', ['-p'], { encoding: 'utf8', env }).trim()
- return [
- resolve(developerDir, 'SDKs/MacOSX.sdk'),
- resolve(developerDir, 'Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'),
- ].find(existsSync) ?? null
+ return developerSysroot(env)
}
-// Preserve xcrun's previous selection when neither developer layout has a default.
+// Preserve xcrun's previous selection when no rung produced a sysroot.
// `--sdk` must precede the tool name or xcrun passes it to clang.
export function xcrunClangArgv(sysroot) {
return sysroot ? ['clang', '-isysroot', sysroot] : ['--sdk', 'macosx', 'clang']
diff --git a/apps/desktop/scripts/macos-sysroot.test.mjs b/apps/desktop/scripts/macos-sysroot.test.mjs
index 6cede58474d..511512ce79d 100644
--- a/apps/desktop/scripts/macos-sysroot.test.mjs
+++ b/apps/desktop/scripts/macos-sysroot.test.mjs
@@ -1,7 +1,7 @@
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
-import { afterEach, expect, it, vi } from 'vitest'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
vi.mock('node:child_process', () => ({
execFileSync: vi.fn(),
@@ -10,41 +10,100 @@ vi.mock('node:child_process', () => ({
const { execFileSync } = await import('node:child_process')
const { macosSysroot, xcrunClangArgv } = await import('./macos-sysroot.mjs')
+const exec = vi.mocked(execFileSync)
+const opts = env => ({ encoding: 'utf8', env, stdio: 'pipe' })
+const XCODE_SDK = 'Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'
+const CLT_SDK = 'SDKs/MacOSX.sdk'
+
+let developerDir, env, warn
+const sdkDir = relative => {
+ const dir = path.join(developerDir, relative)
+ fs.mkdirSync(dir, { recursive: true })
+ fs.writeFileSync(path.join(dir, 'SDKSettings.plist'), '')
+ return dir
+}
+// Answers `xcode-select -p` with the fixture developer dir and `xcrun --sdk X
+// --show-sdk-path` with the given result (a string or an Error to throw).
+const toolchain = sdkPath => exec.mockImplementation(file => {
+ if (file === 'xcode-select') return `${developerDir}\n`
+ if (sdkPath instanceof Error) throw sdkPath
+ return `${sdkPath}\n`
+})
+
+beforeEach(() => {
+ developerDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sysroot-'))
+ env = { DEVELOPER_DIR: developerDir }
+ warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+})
+
afterEach(() => {
- vi.mocked(execFileSync).mockReset()
-})
-
-it('resolves explicit SDKs before developer defaults and preserves the no-SDK fallback', () => {
- const developerDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sysroot-'))
- const env = { DEVELOPER_DIR: developerDir }
- const sdk = path.join(developerDir, 'SDKs/MacOSX.sdk')
-
- try {
- for (const SDKROOT of [sdk, 'macosx']) {
- const override = { ...env, SDKROOT }
- vi.mocked(execFileSync).mockReturnValue(`${sdk}\n`)
- expect(macosSysroot(override)).toBe(sdk)
- expect(execFileSync).toHaveBeenLastCalledWith('xcrun', ['--sdk', SDKROOT, '--show-sdk-path'], {
- encoding: 'utf8', env: override
- })
- }
-
- vi.mocked(execFileSync).mockImplementationOnce(() => { throw new Error('SDK not found') })
- expect(() => macosSysroot({ ...env, SDKROOT: 'missing-sdk' })).toThrow('SDK not found')
-
- vi.mocked(execFileSync).mockReturnValue(`${developerDir}\n`)
- expect(macosSysroot(env)).toBeNull()
- expect(xcrunClangArgv(null)).toEqual(['--sdk', 'macosx', 'clang'])
-
- // When both layouts exist, the CLT default wins over the Xcode platform fallback.
- for (const relative of ['Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk', 'SDKs/MacOSX.sdk']) {
- const paired = path.join(developerDir, relative)
- fs.mkdirSync(paired, { recursive: true })
- expect(macosSysroot(env)).toBe(paired)
- expect(execFileSync).toHaveBeenLastCalledWith('xcode-select', ['-p'], { encoding: 'utf8', env })
- expect(xcrunClangArgv(paired)).toEqual(['clang', '-isysroot', paired])
- }
- } finally {
- fs.rmSync(developerDir, { recursive: true, force: true })
+ exec.mockReset()
+ warn.mockRestore()
+ fs.rmSync(developerDir, { recursive: true, force: true })
+})
+
+it('resolves an explicit SDKROOT path or name through xcrun before any developer default', () => {
+ const pinned = sdkDir('SDKs/MacOSX15.4.sdk')
+ sdkDir(CLT_SDK)
+ for (const SDKROOT of [pinned, 'macosx15.4']) {
+ const override = { ...env, SDKROOT }
+ toolchain(pinned)
+ expect(macosSysroot(override)).toBe(pinned)
+ expect(exec).toHaveBeenCalledWith('xcrun', ['--sdk', SDKROOT, '--show-sdk-path'], opts(override))
+ expect(exec).not.toHaveBeenCalledWith('xcode-select', expect.anything(), expect.anything())
+ expect(xcrunClangArgv(pinned)).toEqual(['clang', '-isysroot', pinned])
+ }
+ expect(warn).not.toHaveBeenCalled()
+})
+
+it('reports a stale SDKROOT and falls back to the toolchain default instead of failing the build', () => {
+ const paired = sdkDir(CLT_SDK)
+ const missing = path.join(developerDir, 'SDKs/MacOSX99.sdk')
+ // xcrun accepts any existing directory as an SDK path; a plain directory is not one.
+ const bare = fs.mkdtempSync(path.join(developerDir, 'not-an-sdk-'))
+ for (const [SDKROOT, answer] of [
+ ['macosx99', new Error('SDK "macosx99" cannot be located')],
+ [missing, missing],
+ [bare, bare]
+ ]) {
+ toolchain(answer)
+ expect(macosSysroot({ ...env, SDKROOT })).toBe(paired)
+ expect(warn).toHaveBeenLastCalledWith(expect.stringContaining(`SDKROOT=${SDKROOT}`))
}
+ expect(warn).toHaveBeenCalledTimes(3)
+})
+
+it('ignores an empty SDKROOT', () => {
+ const paired = sdkDir(CLT_SDK)
+ toolchain(new Error('unexpected xcrun call'))
+ expect(macosSysroot({ ...env, SDKROOT: '' })).toBe(paired)
+ expect(exec).toHaveBeenCalledTimes(1)
+ expect(exec).toHaveBeenLastCalledWith('xcode-select', ['-p'], opts({ ...env, SDKROOT: '' }))
+})
+
+it('prefers the Command Line Tools alias over the Xcode platform alias', () => {
+ toolchain()
+ for (const relative of [XCODE_SDK, CLT_SDK]) {
+ const paired = sdkDir(relative)
+ expect(macosSysroot(env)).toBe(paired)
+ expect(exec).toHaveBeenLastCalledWith('xcode-select', ['-p'], opts(env))
+ }
+})
+
+it('hands SDK selection back to xcrun when the toolchain has no default alias', () => {
+ toolchain()
+ expect(macosSysroot(env)).toBeNull()
+ // A bare MacOSX.sdk directory with no SDKSettings.plist is not an SDK either.
+ fs.mkdirSync(path.join(developerDir, CLT_SDK), { recursive: true })
+ expect(macosSysroot(env)).toBeNull()
+ expect(xcrunClangArgv(null)).toEqual(['--sdk', 'macosx', 'clang'])
+})
+
+it('hands SDK selection back to xcrun when xcode-select fails or answers nothing', () => {
+ sdkDir(CLT_SDK)
+ exec.mockImplementation(() => { throw new Error('xcode-select: error: unable to get active developer directory') })
+ expect(macosSysroot(env)).toBeNull()
+ exec.mockReturnValue('\n')
+ expect(macosSysroot(env)).toBeNull()
+ expect(warn).not.toHaveBeenCalled()
})