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

@@ -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 });