ReviewOS

also looking at this

craft-native/craft

fix(build): select the core Zig project explicitly

#383
Merged chris wants to merge codex/zig-core-build-root into main
4 files +44 -2
scripts/verify-zig-core-project.tsadded+30-0
Changes to scripts/verify-zig-core-project.ts
@@ -0,0 +1,30 @@
1import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
2import { join, resolve } from 'node:path'
3import { resolveZig } from './zig'
4
5const source = resolve(import.meta.dir, '..')
6const zig = resolveZig(source)
7console.log('Tracked core project:', JSON.stringify({ file: join(source, 'packages/zig/build.zig'), exists: existsSync(join(source, 'packages/zig/build.zig')) }))
8// Keep the fixture on the checkout's volume. Hosted Windows keeps the
9// checkout on D: and the system temporary directory/compiler cache on C:.
10const root = mkdtempSync(join(source, '.zig-core-project-'))
11const core = join(root, 'packages', 'zig')
12try {
13 mkdirSync(core, { recursive: true })
14 writeFileSync(join(root, 'pantry.lock'), readFileSync(join(source, 'pantry.lock')))
15 writeFileSync(join(root, 'package.json'), JSON.stringify({ version: '1.2.3' }))
16 writeFileSync(join(root, 'build.zig'), 'const std=; pub fn build(b:*std.Build) void {_=b.standardOptimizeOption(.{});}\n')
17 writeFileSync(join(core, 'build.zig'), 'const std=; pub fn build(b:*std.Build) void {_=b.standardOptimizeOption(.{}); const version=b.option([]const u8,"version","version") orelse "missing"; std.debug.print("PROJECT_VERSION={s}\\n",.{version});}\n')
18 const env = { ...process.env, CRAFT_ZIG: zig.path }
19 const runner = join(root, 'runner.ts')
20 writeFileSync(runner, `import {runZig} from ${JSON.stringify(join(source, 'scripts/zig.ts'))};process.exitCode=runZig(${JSON.stringify(root)},['--core','--versioned','build']);\n`)
21 const result = Bun.spawnSync([process.execPath, runner], { cwd: root, env, stdout: 'pipe', stderr: 'pipe' })
22 console.log(result.stdout.toString(), result.stderr.toString())
23 // The root wrapper rejects -Dversion. A successful configure therefore
24 // proves core selection even when Zig reuses its cached configuration.
25 if (result.exitCode !== 0)
26 throw new Error('The actual pinned compiler did not configure the selected core project with its package version.')
27}
28finally {
29 rmSync(root, { recursive: true, force: true })
30}