From 39d9f63043584bcf79bf899faad59cdd64916762 Mon Sep 17 00:00:00 2001 From: "shumeiko.it" Date: Sun, 1 Nov 2026 00:13:38 +0100 Subject: [PATCH] build: let the sidecar build run from a source archive (P1.3d) build-sidecar.mjs required a git checkout: it read HEAD, the tracked file list and the marketplace database blob from git. Outside a checkout it now records no source revision and ships the database from disk; a checkout keeps the exact-revision checks. --- packages/server/tests/tauri-config.test.ts | 16 ++++++ scripts/build-sidecar.mjs | 62 ++++++++++++++-------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/packages/server/tests/tauri-config.test.ts b/packages/server/tests/tauri-config.test.ts index 8b4109a..d7d9270 100644 --- a/packages/server/tests/tauri-config.test.ts +++ b/packages/server/tests/tauri-config.test.ts @@ -677,6 +677,22 @@ describe('Tauri Production Configuration', () => { fs.readdirSync(path.dirname(marketplaceResourcePath)) .filter((name) => name.startsWith('marketplace.db.stage-')), ).toEqual([]); + + // A source archive has no .git: the build still succeeds, records no + // revision and ships the database it finds on disk. + fs.rmSync(path.join(fixtureRoot, '.git'), { recursive: true, force: true }); + run(process.execPath, ['scripts/build-sidecar.mjs']); + const archiveService = fs.readFileSync( + path.join(fixtureRoot, 'app', 'src-tauri', 'resources', 'service.js'), + ); + const archiveLine = archiveService + .subarray(0, archiveService.indexOf(0x0a)) + .toString('utf8'); + const archiveManifest = JSON.parse( + Buffer.from(archiveLine.slice(prefix.length), 'base64').toString('utf8'), + ) as { sourceRevision: string | null }; + expect(archiveManifest.sourceRevision).toBeNull(); + expect(fs.readFileSync(marketplaceResourcePath, 'utf8')).toBe('dirty worktree database'); } finally { fs.rmSync(fixtureRoot, { recursive: true, force: true }); } diff --git a/scripts/build-sidecar.mjs b/scripts/build-sidecar.mjs index d555ad6..135dc2b 100644 --- a/scripts/build-sidecar.mjs +++ b/scripts/build-sidecar.mjs @@ -42,6 +42,20 @@ function readGitBlob(revision, relative) { ); } +/** The checkout's exact revision, or null when the tree is not a git checkout (a source archive). */ +function gitRevision() { + if (!fs.existsSync(path.join(root, '.git'))) return null; + const revision = execFileSync( + 'git', + ['-C', root, 'rev-parse', 'HEAD'], + { encoding: 'utf8', windowsHide: true }, + ).trim().toLowerCase(); + if (!/^[0-9a-f]{40}$/.test(revision)) { + throw new Error(`Could not resolve an exact source revision: ${revision}`); + } + return revision; +} + function repositoryRelative(absolutePath) { const relative = path.relative(root, absolutePath); if (!relative || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) { @@ -148,17 +162,17 @@ try { ) { throw new Error(`Required marketplace database is missing or unsafe: ${marketplaceDb}`); } - const sourceRevision = execFileSync( - 'git', - ['-C', root, 'rev-parse', 'HEAD'], - { encoding: 'utf8', windowsHide: true }, - ).trim().toLowerCase(); - if (!/^[0-9a-f]{40}$/.test(sourceRevision)) { - throw new Error(`Could not resolve an exact source revision: ${sourceRevision}`); - } - const marketplaceGitBlob = readGitBlob(sourceRevision, marketplaceDbRelative); - if (!fs.readFileSync(marketplaceDb).equals(marketplaceGitBlob)) { - throw new Error('canonical marketplace database does not match exact source revision'); + // A git checkout binds the bundle to its exact revision. A source archive + // has no history to check against, so its provenance records no revision. + const sourceRevision = gitRevision(); + const marketplaceBytes = fs.readFileSync(marketplaceDb); + if (sourceRevision) { + const marketplaceGitBlob = readGitBlob(sourceRevision, marketplaceDbRelative); + if (!marketplaceBytes.equals(marketplaceGitBlob)) { + throw new Error('canonical marketplace database does not match exact source revision'); + } + } else { + console.log('[build-sidecar] Not a git checkout: provenance records no source revision'); } // Dynamic import esbuild (available via vite dependency) @@ -206,13 +220,15 @@ try { fs.writeFileSync(metaFile, JSON.stringify(result.metafile)); console.log('[build-sidecar] Wrote esbuild metafile', metaFile); - const trackedFiles = new Set( - execFileSync( - 'git', - ['-C', root, 'ls-files', '-z'], - { encoding: 'utf8', windowsHide: true }, - ).split('\0').filter(Boolean), - ); + const trackedFiles = sourceRevision + ? new Set( + execFileSync( + 'git', + ['-C', root, 'ls-files', '-z'], + { encoding: 'utf8', windowsHide: true }, + ).split('\0').filter(Boolean), + ) + : null; const localInputs = new Set([buildScriptRelative, 'package.json', 'package-lock.json']); for (const input of Object.keys(result.metafile.inputs)) { const relative = repositoryRelative(path.resolve(root, input)); @@ -227,7 +243,7 @@ try { const sourceInputs = [...localInputs] .sort((left, right) => left < right ? -1 : left > right ? 1 : 0) .map((relative) => { - if (!trackedFiles.has(relative)) { + if (trackedFiles && !trackedFiles.has(relative)) { throw new Error(`Sidecar repository input is not tracked: ${relative}`); } const absolute = path.join(root, ...relative.split('/')); @@ -264,7 +280,7 @@ try { fs.rmSync(provenanceTemp, { force: true }); } console.log( - `[build-sidecar] Embedded ${sourceInputs.length} source inputs at ${sourceRevision.slice(0, 12)}`, + `[build-sidecar] Embedded ${sourceInputs.length} source inputs at ${sourceRevision ? sourceRevision.slice(0, 12) : 'no revision'}`, ); const stat = fs.statSync(outFile); @@ -272,11 +288,11 @@ try { console.log(`[build-sidecar] Done. Output: ${sizeMB} MB`); // Production startup seeds the user's writable DB from this immutable - // packaged resource. Publish the exact Git blob through a temporary file so - // a failed write can never leave a partial database in the bundle. + // packaged resource. Publish it through a temporary file so a failed write + // can never leave a partial database in the bundle. const marketplaceTemp = `${marketplaceResource}.stage-${process.pid}.tmp`; try { - fs.writeFileSync(marketplaceTemp, marketplaceGitBlob, { flag: 'wx' }); + fs.writeFileSync(marketplaceTemp, marketplaceBytes, { flag: 'wx' }); fs.renameSync(marketplaceTemp, marketplaceResource); } finally { fs.rmSync(marketplaceTemp, { force: true });