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.
This commit is contained in:
shumeiko.it
2026-11-01 00:13:38 +01:00
parent d2ad282498
commit 39d9f63043
2 changed files with 55 additions and 23 deletions

View File

@@ -677,6 +677,22 @@ describe('Tauri Production Configuration', () => {
fs.readdirSync(path.dirname(marketplaceResourcePath)) fs.readdirSync(path.dirname(marketplaceResourcePath))
.filter((name) => name.startsWith('marketplace.db.stage-')), .filter((name) => name.startsWith('marketplace.db.stage-')),
).toEqual([]); ).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 { } finally {
fs.rmSync(fixtureRoot, { recursive: true, force: true }); fs.rmSync(fixtureRoot, { recursive: true, force: true });
} }

View File

@@ -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) { function repositoryRelative(absolutePath) {
const relative = path.relative(root, absolutePath); const relative = path.relative(root, absolutePath);
if (!relative || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) { 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}`); throw new Error(`Required marketplace database is missing or unsafe: ${marketplaceDb}`);
} }
const sourceRevision = execFileSync( // A git checkout binds the bundle to its exact revision. A source archive
'git', // has no history to check against, so its provenance records no revision.
['-C', root, 'rev-parse', 'HEAD'], const sourceRevision = gitRevision();
{ encoding: 'utf8', windowsHide: true }, const marketplaceBytes = fs.readFileSync(marketplaceDb);
).trim().toLowerCase(); if (sourceRevision) {
if (!/^[0-9a-f]{40}$/.test(sourceRevision)) { const marketplaceGitBlob = readGitBlob(sourceRevision, marketplaceDbRelative);
throw new Error(`Could not resolve an exact source revision: ${sourceRevision}`); if (!marketplaceBytes.equals(marketplaceGitBlob)) {
} throw new Error('canonical marketplace database does not match exact source revision');
const marketplaceGitBlob = readGitBlob(sourceRevision, marketplaceDbRelative); }
if (!fs.readFileSync(marketplaceDb).equals(marketplaceGitBlob)) { } else {
throw new Error('canonical marketplace database does not match exact source revision'); console.log('[build-sidecar] Not a git checkout: provenance records no source revision');
} }
// Dynamic import esbuild (available via vite dependency) // Dynamic import esbuild (available via vite dependency)
@@ -206,13 +220,15 @@ try {
fs.writeFileSync(metaFile, JSON.stringify(result.metafile)); fs.writeFileSync(metaFile, JSON.stringify(result.metafile));
console.log('[build-sidecar] Wrote esbuild metafile', metaFile); console.log('[build-sidecar] Wrote esbuild metafile', metaFile);
const trackedFiles = new Set( const trackedFiles = sourceRevision
execFileSync( ? new Set(
'git', execFileSync(
['-C', root, 'ls-files', '-z'], 'git',
{ encoding: 'utf8', windowsHide: true }, ['-C', root, 'ls-files', '-z'],
).split('\0').filter(Boolean), { encoding: 'utf8', windowsHide: true },
); ).split('\0').filter(Boolean),
)
: null;
const localInputs = new Set([buildScriptRelative, 'package.json', 'package-lock.json']); const localInputs = new Set([buildScriptRelative, 'package.json', 'package-lock.json']);
for (const input of Object.keys(result.metafile.inputs)) { for (const input of Object.keys(result.metafile.inputs)) {
const relative = repositoryRelative(path.resolve(root, input)); const relative = repositoryRelative(path.resolve(root, input));
@@ -227,7 +243,7 @@ try {
const sourceInputs = [...localInputs] const sourceInputs = [...localInputs]
.sort((left, right) => left < right ? -1 : left > right ? 1 : 0) .sort((left, right) => left < right ? -1 : left > right ? 1 : 0)
.map((relative) => { .map((relative) => {
if (!trackedFiles.has(relative)) { if (trackedFiles && !trackedFiles.has(relative)) {
throw new Error(`Sidecar repository input is not tracked: ${relative}`); throw new Error(`Sidecar repository input is not tracked: ${relative}`);
} }
const absolute = path.join(root, ...relative.split('/')); const absolute = path.join(root, ...relative.split('/'));
@@ -264,7 +280,7 @@ try {
fs.rmSync(provenanceTemp, { force: true }); fs.rmSync(provenanceTemp, { force: true });
} }
console.log( 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); const stat = fs.statSync(outFile);
@@ -272,11 +288,11 @@ try {
console.log(`[build-sidecar] Done. Output: ${sizeMB} MB`); console.log(`[build-sidecar] Done. Output: ${sizeMB} MB`);
// Production startup seeds the user's writable DB from this immutable // Production startup seeds the user's writable DB from this immutable
// packaged resource. Publish the exact Git blob through a temporary file so // packaged resource. Publish it through a temporary file so a failed write
// a failed write can never leave a partial database in the bundle. // can never leave a partial database in the bundle.
const marketplaceTemp = `${marketplaceResource}.stage-${process.pid}.tmp`; const marketplaceTemp = `${marketplaceResource}.stage-${process.pid}.tmp`;
try { try {
fs.writeFileSync(marketplaceTemp, marketplaceGitBlob, { flag: 'wx' }); fs.writeFileSync(marketplaceTemp, marketplaceBytes, { flag: 'wx' });
fs.renameSync(marketplaceTemp, marketplaceResource); fs.renameSync(marketplaceTemp, marketplaceResource);
} finally { } finally {
fs.rmSync(marketplaceTemp, { force: true }); fs.rmSync(marketplaceTemp, { force: true });