also looking at this
fix(native): preserve workspace sources and native overlay lifecycle
#384
20 files
+500
-227
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -1,20 +1,38 @@ | ||
| 1 | import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' | |
| 2 | import { join, resolve } from 'node:path' | |
| 1 | import { existsSync, mkdtempSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs' | |
| 2 | import { dirname, join, resolve } from 'node:path' | |
| 3 | 3 | import { resolveZig } from './zig' |
| 4 | import { createHash } from 'node:crypto' | |
| 5 | import { cpus } from 'node:os' | |
| 4 | 6 | |
| 5 | 7 | const source = resolve(import.meta.dir, '..') |
| 6 | 8 | const zig = resolveZig(source) |
| 7 | console.log('Tracked core project:', JSON.stringify({ file: join(source, 'packages/zig/build.zig'), exists: existsSync(join(source, 'packages/zig/build.zig')) })) | |
| 9 | const trackedBuild = join(source, 'packages/zig/build.zig') | |
| 10 | console.log('Tracked core project:', JSON.stringify({ file: trackedBuild, exists: existsSync(trackedBuild), sha256: createHash('sha256').update(readFileSync(trackedBuild)).digest('hex'), bun: Bun.version, executable: process.execPath, cpu: cpus()[0]?.model, platform: process.platform })) | |
| 11 | const zigLibrary = join(dirname(realpathSync(zig.path)), 'lib/std/zig.zig') | |
| 12 | console.log('Pinned compiler bytes:', JSON.stringify({ executable: zig.path, sha256: createHash('sha256').update(readFileSync(zig.path)).digest('hex'), library: zigLibrary, librarySha256: existsSync(zigLibrary) ? createHash('sha256').update(readFileSync(zigLibrary)).digest('hex') : null })) | |
| 8 | 13 | // Keep the fixture on the checkout's volume. Hosted Windows keeps the |
| 9 | 14 | // checkout on D: and the system temporary directory/compiler cache on C:. |
| 10 | 15 | const root = mkdtempSync(join(source, '.zig-core-project-')) |
| 11 | 16 | const core = join(root, 'packages', 'zig') |
| 12 | 17 | try { |
| 13 | 18 | mkdirSync(core, { recursive: true }) |
| 19 | if (process.platform === 'win32') { | |
| 20 | const volumes = Bun.spawnSync(['pwsh', '-NoProfile', '-NonInteractive', '-Command', 'Get-Volume | Where-Object { $_.DriveLetter -in @("C", "D") } | Select-Object DriveLetter,FileSystemType,HealthStatus | ConvertTo-Json -Compress'], { stdout: 'pipe', stderr: 'pipe' }) | |
| 21 | console.log('Native source/cache volumes:', volumes.stdout.toString(), volumes.stderr.toString()) | |
| 22 | } | |
| 23 | const native = Bun.spawnSync([zig.path, 'run', join(source, 'scripts/zig-source-cache-probe.zig'), '--cache-dir', join(root, 'io-build-cache'), '--', join(source, 'packages/zig'), join(root, 'io-manifest-cache')], { cwd: join(source, 'packages/zig'), stdout: 'pipe', stderr: 'pipe' }) | |
| 24 | console.log('Native source/cache probe:', native.exitCode, native.stdout.toString(), native.stderr.toString()) | |
| 25 | if (native.exitCode !== 0) throw new Error('The pinned native source/cache I/O probe failed.') | |
| 14 | 26 | writeFileSync(join(root, 'pantry.lock'), readFileSync(join(source, 'pantry.lock'))) |
| 15 | 27 | writeFileSync(join(root, 'package.json'), JSON.stringify({ version: '1.2.3' })) |
| 16 | 28 | writeFileSync(join(root, 'build.zig'), 'const std=; pub fn build(b:*std.Build) void {_=b.standardOptimizeOption(.{});}\n') |
| 17 | 29 | 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') |
| 30 | const manifest = { name: 'craft', fingerprint: '0xf45c4a848bf58c1c' } | |
| 31 | writeFileSync(join(core, 'build.zig.zon'), `.{ .name=.${manifest.name}, .fingerprint=${manifest.fingerprint}, .version="1.2.3", .dependencies=.{ .fixture=.{ .path="../../dependency" } }, .paths=.{"build.zig","build.zig.zon"} }\n`) | |
| 32 | const dependency = join(root, 'dependency') | |
| 33 | mkdirSync(dependency) | |
| 34 | writeFileSync(join(dependency, 'build.zig'), 'const std=; pub fn build(b:*std.Build) void {_=b;}\n') | |
| 35 | writeFileSync(join(dependency, 'build.zig.zon'), `.{ .name=.${manifest.name}, .fingerprint=${manifest.fingerprint}, .version="0.0.1", .paths=.{"build.zig","build.zig.zon"} }\n`) | |
| 18 | 36 | const env = { ...process.env, CRAFT_ZIG: zig.path } |
| 19 | 37 | const runner = join(root, 'runner.ts') |
| 20 | 38 | writeFileSync(runner, `import {runZig} from ${JSON.stringify(join(source, 'scripts/zig.ts'))};process.exitCode=runZig(${JSON.stringify(root)},['--core','--versioned','build']);\n`) |
| @@ -0,0 +1,49 @@ | ||
| 1 | const std = ; | |
| 2 | const Io = std.Io; | |
| 3 | const Cache = std.Build.Cache; | |
| 4 | ||
| 5 | pub fn main(init: std.process.Init) !void { | |
| 6 | const arena = init.arena.allocator(); | |
| 7 | const io = init.io; | |
| 8 | const args = try init.minimal.args.toSlice(arena); | |
| 9 | const cwd = try std.process.currentPathAlloc(io, arena); | |
| 10 | std.debug.print("NATIVE_CWD={s}\n", .{cwd}); | |
| 11 | const source = try Io.Dir.cwd().openDir(io, args[1], .{}); | |
| 12 | defer source.close(io); | |
| 13 | const file = source.openFile(io, "build.zig", .{}) catch |err| { | |
| 14 | std.debug.print("SOURCE_OPEN={t}\n", .{err}); | |
| 15 | return err; | |
| 16 | }; | |
| 17 | defer file.close(io); | |
| 18 | const stat = file.stat(io) catch |err| { | |
| 19 | std.debug.print("SOURCE_STAT={t}\n", .{err}); | |
| 20 | return err; | |
| 21 | }; | |
| 22 | const contents = source.readFileAlloc(io, "build.zig", arena, .limited(1024 * 1024)) catch |err| { | |
| 23 | std.debug.print("SOURCE_READ={t}\n", .{err}); | |
| 24 | return err; | |
| 25 | }; | |
| 26 | std.debug.print("SOURCE_SIZE={d} READ_BYTES={d}\n", .{ stat.size, contents.len }); | |
| 27 | var cache: Cache = .{ | |
| 28 | .io = io, | |
| 29 | .gpa = arena, | |
| 30 | .manifest_dir = try Io.Dir.cwd().createDirPathOpen(io, args[2], .{}), | |
| 31 | .cwd = cwd, | |
| 32 | }; | |
| 33 | defer cache.manifest_dir.close(io); | |
| 34 | cache.addPrefix(.{ .path = null, .handle = Io.Dir.cwd() }); | |
| 35 | for (0..4) |_| cache.addPrefix(.{ .path = args[1], .handle = source }); | |
| 36 | var manifest = cache.obtain(); | |
| 37 | defer manifest.deinit(); | |
| 38 | var check_diagnostic: Cache.Manifest.CheckDiagnostic = undefined; | |
| 39 | _ = try manifest.check(&check_diagnostic, .none); | |
| 40 | var diagnostic: Cache.Manifest.AddDiscoveredPathDiagnostic = .none; | |
| 41 | manifest.addDiscoveredPath(.{ | |
| 42 | .discovered_path = .{ .prefixed = .{ .prefix = 4, .sub_path = "build.zig" } }, | |
| 43 | .diagnostic = &diagnostic, | |
| 44 | }) catch |err| { | |
| 45 | std.debug.print("SOURCE_CACHE={t} DETAILS={f}\n", .{ err, diagnostic }); | |
| 46 | return err; | |
| 47 | }; | |
| 48 | std.debug.print("SOURCE_CACHE=passed\n", .{}); | |
| 49 | } | |