Format authored monorepo code with pinned language formatters
This commit is contained in:
@@ -64,11 +64,9 @@ export type CompiledCapabilityResourceRepository = {
|
||||
const sourceKey = (kind: LockedResource["kind"], source: GitSource) =>
|
||||
`${kind}\0${source.repository}\0${source.commit.toLowerCase()}`;
|
||||
|
||||
const bindingKey = (kind: LockedResource["kind"], binding: string) =>
|
||||
`${kind}\0${binding}`;
|
||||
const bindingKey = (kind: LockedResource["kind"], binding: string) => `${kind}\0${binding}`;
|
||||
|
||||
const importsKey = (entry: CapabilityResourceImport | LockedResource) =>
|
||||
bindingKey(entry.kind, entry.binding);
|
||||
const importsKey = (entry: CapabilityResourceImport | LockedResource) => bindingKey(entry.kind, entry.binding);
|
||||
|
||||
const assertImportsMatchLock = (
|
||||
label: string,
|
||||
@@ -77,22 +75,23 @@ const assertImportsMatchLock = (
|
||||
) => {
|
||||
const authored = [...imports].map(importsKey).sort();
|
||||
const locked = [...resources].map(importsKey).sort();
|
||||
if (
|
||||
authored.length !== locked.length ||
|
||||
authored.some((entry, index) => entry !== locked[index])
|
||||
) {
|
||||
if (authored.length !== locked.length || authored.some((entry, index) => entry !== locked[index])) {
|
||||
throw new Error(
|
||||
`${label} imports do not match quixos.lock:\n` +
|
||||
`authored: ${authored.join(", ") || "none"}\n` +
|
||||
`locked: ${locked.join(", ") || "none"}`,
|
||||
`authored: ${authored.join(", ") || "none"}\n` +
|
||||
`locked: ${locked.join(", ") || "none"}`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const exactRevisions = <Revision extends {
|
||||
revisionId: string;
|
||||
source: SourceRevision;
|
||||
}>(revisions: readonly Revision[]) => {
|
||||
const exactRevisions = <
|
||||
Revision extends {
|
||||
revisionId: string;
|
||||
source: SourceRevision;
|
||||
},
|
||||
>(
|
||||
revisions: readonly Revision[],
|
||||
) => {
|
||||
const seen = new Set<string>();
|
||||
return revisions.filter((revision) => {
|
||||
const key = `${revision.revisionId}\0${revision.source.repository}\0${revision.source.commit}`;
|
||||
@@ -122,19 +121,20 @@ const diagnosticsMessage = (
|
||||
message: string;
|
||||
path?: string;
|
||||
}[],
|
||||
) => `${label} did not compile:\n${diagnostics.map((entry) => {
|
||||
const location = entry.line > 0
|
||||
? `${entry.fileName}:${entry.line}:${entry.column + 1}`
|
||||
: `${entry.fileName}${entry.path ? `:${entry.path}` : ""}`;
|
||||
return `${location}: ${entry.phase} ${entry.code}: ${entry.message}`;
|
||||
}).join("\n")}`;
|
||||
) =>
|
||||
`${label} did not compile:\n${diagnostics
|
||||
.map((entry) => {
|
||||
const location =
|
||||
entry.line > 0
|
||||
? `${entry.fileName}:${entry.line}:${entry.column + 1}`
|
||||
: `${entry.fileName}${entry.path ? `:${entry.path}` : ""}`;
|
||||
return `${location}: ${entry.phase} ${entry.code}: ${entry.message}`;
|
||||
})
|
||||
.join("\n")}`;
|
||||
|
||||
const revisionFor = (node: ResolvedCapabilityResource) =>
|
||||
node.resource.revision;
|
||||
const revisionFor = (node: ResolvedCapabilityResource) => node.resource.revision;
|
||||
|
||||
const resourceClosure = (
|
||||
roots: readonly ResolvedCapabilityResource[],
|
||||
): ResolvedCapabilityResource[] => {
|
||||
const resourceClosure = (roots: readonly ResolvedCapabilityResource[]): ResolvedCapabilityResource[] => {
|
||||
const result: ResolvedCapabilityResource[] = [];
|
||||
const seen = new Set<string>();
|
||||
const visit = (node: ResolvedCapabilityResource) => {
|
||||
@@ -151,25 +151,26 @@ const environmentFor = (
|
||||
direct: readonly [LockedResource, ResolvedCapabilityResource][],
|
||||
closure: readonly ResolvedCapabilityResource[],
|
||||
): CapabilityImportEnvironment => ({
|
||||
interfaces: new Map(direct.flatMap(([locked, node]) =>
|
||||
node.resource.kind === "interface"
|
||||
? [[locked.binding, node.resource.revision] as const]
|
||||
: [])),
|
||||
packages: new Map(direct.flatMap(([locked, node]) =>
|
||||
node.resource.kind === "package"
|
||||
? [[locked.binding, node.resource.revision] as const]
|
||||
: [])),
|
||||
interfaceClosure: exactRevisions(closure.flatMap((node) =>
|
||||
node.resource.kind === "interface" ? [node.resource.revision] : [])),
|
||||
packageClosure: exactRevisions(closure.flatMap((node) =>
|
||||
node.resource.kind === "package" ? [node.resource.revision] : [])),
|
||||
interfaces: new Map(
|
||||
direct.flatMap(([locked, node]) =>
|
||||
node.resource.kind === "interface" ? [[locked.binding, node.resource.revision] as const] : [],
|
||||
),
|
||||
),
|
||||
packages: new Map(
|
||||
direct.flatMap(([locked, node]) =>
|
||||
node.resource.kind === "package" ? [[locked.binding, node.resource.revision] as const] : [],
|
||||
),
|
||||
),
|
||||
interfaceClosure: exactRevisions(
|
||||
closure.flatMap((node) => (node.resource.kind === "interface" ? [node.resource.revision] : [])),
|
||||
),
|
||||
packageClosure: exactRevisions(
|
||||
closure.flatMap((node) => (node.resource.kind === "package" ? [node.resource.revision] : [])),
|
||||
),
|
||||
externalAtoms: exactAtoms(closure.flatMap((node) => node.resource.externalAtoms)),
|
||||
});
|
||||
|
||||
const createResourceGraphResolver = (
|
||||
quixosCommit: string,
|
||||
resolveResource: CapabilityRepositoryResolver,
|
||||
) => {
|
||||
const createResourceGraphResolver = (quixosCommit: string, resolveResource: CapabilityRepositoryResolver) => {
|
||||
const resolved = new Map<string, Promise<ResolvedCapabilityResource>>();
|
||||
const active: string[] = [];
|
||||
|
||||
@@ -188,7 +189,7 @@ const createResourceGraphResolver = (
|
||||
const pending = (async () => {
|
||||
active.push(key);
|
||||
try {
|
||||
const snapshot = suppliedSnapshot ?? await resolveResource(locked.source, locked.kind);
|
||||
const snapshot = suppliedSnapshot ?? (await resolveResource(locked.source, locked.kind));
|
||||
const lockResult = await loadQuixosLock(path.join(snapshot.directory, "quixos.lock"));
|
||||
if (!lockResult.ok) {
|
||||
throw new Error(diagnosticsMessage(`${locked.kind} ${locked.binding} lock`, lockResult.diagnostics));
|
||||
@@ -196,22 +197,20 @@ const createResourceGraphResolver = (
|
||||
if (lockResult.lock.quixos.policy) {
|
||||
throw new Error(
|
||||
`${locked.kind} ${locked.binding} lock declares workspace Quixos policy ` +
|
||||
`${lockResult.lock.quixos.policy}; resource locks may only declare their exact authored-against commit`,
|
||||
`${lockResult.lock.quixos.policy}; resource locks may only declare their exact authored-against commit`,
|
||||
);
|
||||
}
|
||||
if (lockResult.lock.quixos.commit.toLowerCase() !== quixosCommit.toLowerCase()) {
|
||||
throw new Error(
|
||||
`${locked.kind} ${locked.binding} selects Quixos ${lockResult.lock.quixos.commit}, ` +
|
||||
`but the repository graph selects ${quixosCommit}`,
|
||||
`but the repository graph selects ${quixosCommit}`,
|
||||
);
|
||||
}
|
||||
const directPairs: Array<[LockedResource, ResolvedCapabilityResource]> = [];
|
||||
for (const dependency of lockResult.lock.resources) {
|
||||
directPairs.push([dependency, await visit(dependency)]);
|
||||
}
|
||||
const dependencyClosure = resourceClosure(
|
||||
directPairs.map(([, node]) => node),
|
||||
);
|
||||
const dependencyClosure = resourceClosure(directPairs.map(([, node]) => node));
|
||||
const environment = environmentFor(directPairs, dependencyClosure);
|
||||
const manifestName = locked.kind === "interface" ? "interface.qx" : "package.qx";
|
||||
const manifestPath = path.join(snapshot.directory, manifestName);
|
||||
@@ -225,22 +224,28 @@ const createResourceGraphResolver = (
|
||||
throw new Error(diagnosticsMessage(`${locked.kind} ${locked.binding}`, compiled.diagnostics));
|
||||
}
|
||||
if (compiled.resource.kind !== locked.kind) {
|
||||
throw new Error(
|
||||
`${manifestPath} declares ${compiled.resource.kind}, not ${locked.kind}`,
|
||||
);
|
||||
throw new Error(`${manifestPath} declares ${compiled.resource.kind}, not ${locked.kind}`);
|
||||
}
|
||||
assertImportsMatchLock(manifestPath, compiled.resource.imports, lockResult.lock.resources);
|
||||
if (compiled.resource.kind === "package") {
|
||||
let catalogText: string | undefined;
|
||||
try { catalogText = await readFile(path.join(snapshot.directory, "quixos.migrations.json"), "utf8"); }
|
||||
catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
|
||||
try {
|
||||
catalogText = await readFile(path.join(snapshot.directory, "quixos.migrations.json"), "utf8");
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
||||
}
|
||||
if (catalogText !== undefined) {
|
||||
const catalog = validateMigrationCatalog(JSON.parse(catalogText), new Set(compiled.resource.revision.exports.map((entry) => entry.id)));
|
||||
const catalog = validateMigrationCatalog(
|
||||
JSON.parse(catalogText),
|
||||
new Set(compiled.resource.revision.exports.map((entry) => entry.id)),
|
||||
);
|
||||
const root = await realpath(snapshot.directory);
|
||||
for (const migration of catalog.migrations) {
|
||||
const implementation = await realpath(path.join(root, migration.implementation.file));
|
||||
if (!implementation.startsWith(`${root}${path.sep}`)) throw new Error("Migration implementation escapes its package");
|
||||
if (contentDigest(await readFile(implementation, "utf8")) !== migration.implementation.digest) throw new Error(`Migration implementation digest mismatch: ${migration.id}`);
|
||||
if (!implementation.startsWith(`${root}${path.sep}`))
|
||||
throw new Error("Migration implementation escapes its package");
|
||||
if (contentDigest(await readFile(implementation, "utf8")) !== migration.implementation.digest)
|
||||
throw new Error(`Migration implementation digest mismatch: ${migration.id}`);
|
||||
}
|
||||
compiled.resource.revision.migrationCatalog = catalog;
|
||||
}
|
||||
@@ -252,10 +257,9 @@ const createResourceGraphResolver = (
|
||||
directory: snapshot.directory,
|
||||
lock: lockResult.lock,
|
||||
resource: compiled.resource,
|
||||
dependencies: new Map(directPairs.map(([dependency, node]) => [
|
||||
bindingKey(dependency.kind, dependency.binding),
|
||||
node,
|
||||
])),
|
||||
dependencies: new Map(
|
||||
directPairs.map(([dependency, node]) => [bindingKey(dependency.kind, dependency.binding), node]),
|
||||
),
|
||||
};
|
||||
} finally {
|
||||
active.pop();
|
||||
@@ -281,15 +285,15 @@ export const compileCapabilityResourceRepository = async (options: {
|
||||
if (!lockResult.ok) {
|
||||
throw new Error(diagnosticsMessage("Resource lock", lockResult.diagnostics));
|
||||
}
|
||||
const resolver = createResourceGraphResolver(
|
||||
lockResult.lock.quixos.commit,
|
||||
options.resolveResource,
|
||||
const resolver = createResourceGraphResolver(lockResult.lock.quixos.commit, options.resolveResource);
|
||||
const root = await resolver.visit(
|
||||
{
|
||||
kind: options.kind,
|
||||
binding: "<root>",
|
||||
source: options.source,
|
||||
},
|
||||
{ directory: options.rootDirectory },
|
||||
);
|
||||
const root = await resolver.visit({
|
||||
kind: options.kind,
|
||||
binding: "<root>",
|
||||
source: options.source,
|
||||
}, { directory: options.rootDirectory });
|
||||
return {
|
||||
resource: root.resource,
|
||||
lock: root.lock,
|
||||
@@ -307,9 +311,7 @@ export const compileWorkspaceRepository = async (options: {
|
||||
/** An editor's proposed source snapshot; locks and dependency revisions remain exact. */
|
||||
readSource?: (name: string) => Promise<string>;
|
||||
}): Promise<CompiledWorkspaceRepository> => {
|
||||
const rootLockResult = await loadQuixosLock(
|
||||
path.join(options.rootDirectory, "quixos.lock"),
|
||||
);
|
||||
const rootLockResult = await loadQuixosLock(path.join(options.rootDirectory, "quixos.lock"));
|
||||
if (!rootLockResult.ok) {
|
||||
throw new Error(diagnosticsMessage("Workspace lock", rootLockResult.diagnostics));
|
||||
}
|
||||
@@ -323,17 +325,25 @@ export const compileWorkspaceRepository = async (options: {
|
||||
const nodes = await resolver.nodes();
|
||||
const environment = environmentFor(directPairs, nodes);
|
||||
const workspacePath = path.join(options.rootDirectory, "workspace.qx");
|
||||
const sources = options.readSource ? await resolveQxSources(options.readSource) : await loadQxSources(options.rootDirectory);
|
||||
const sources = options.readSource
|
||||
? await resolveQxSources(options.readSource)
|
||||
: await loadQxSources(options.rootDirectory);
|
||||
const compiled = compileCapabilitySource(sources.source, workspacePath, environment);
|
||||
if (!compiled.ok) {
|
||||
throw new Error(diagnosticsMessage("Workspace", compiled.diagnostics.map((diagnostic) =>
|
||||
diagnostic.line > 0 ? { ...diagnostic, ...sources.originalPosition(diagnostic.line, diagnostic.column) } : diagnostic)));
|
||||
throw new Error(
|
||||
diagnosticsMessage(
|
||||
"Workspace",
|
||||
compiled.diagnostics.map((diagnostic) =>
|
||||
diagnostic.line > 0
|
||||
? { ...diagnostic, ...sources.originalPosition(diagnostic.line, diagnostic.column) }
|
||||
: diagnostic,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
assertImportsMatchLock(workspacePath, compiled.imports, rootLock.resources);
|
||||
const availableInterfaceIds = new Set(
|
||||
nodes.flatMap((node) => node.resource.kind === "interface"
|
||||
? [node.resource.revision.revisionId]
|
||||
: []),
|
||||
nodes.flatMap((node) => (node.resource.kind === "interface" ? [node.resource.revision.revisionId] : [])),
|
||||
);
|
||||
const availableAtomIds = new Set(compiled.workspace.atoms.map((atom) => atom.id));
|
||||
for (const node of nodes) {
|
||||
@@ -341,7 +351,7 @@ export const compileWorkspaceRepository = async (options: {
|
||||
if (!availableInterfaceIds.has(requirement.revisionId)) {
|
||||
throw new Error(
|
||||
`${node.kind} ${node.resource.revision.displayName} requires external interface ` +
|
||||
`${requirement.binding} (${requirement.revisionId}), but the workspace resource graph does not provide it`,
|
||||
`${requirement.binding} (${requirement.revisionId}), but the workspace resource graph does not provide it`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -349,28 +359,31 @@ export const compileWorkspaceRepository = async (options: {
|
||||
if (!availableAtomIds.has(atom.id)) {
|
||||
throw new Error(
|
||||
`${node.kind} ${node.resource.revision.displayName} requires external atom ` +
|
||||
`${atom.displayName} (${atom.id}), but the workspace does not define it`,
|
||||
`${atom.displayName} (${atom.id}), but the workspace does not define it`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
const workspace: WorkspaceRevision = {
|
||||
...compiled.workspace,
|
||||
...(options.workspaceId
|
||||
? { workspaceId: capabilityId.workspace(options.workspaceId) }
|
||||
: {}),
|
||||
...(options.workspaceId ? { workspaceId: capabilityId.workspace(options.workspaceId) } : {}),
|
||||
...(options.workspaceRevisionId
|
||||
? { id: capabilityId.workspaceRevision(options.workspaceRevisionId) }
|
||||
: options.sourceRootCommit ? { id: capabilityId.workspaceRevision(`workspace-revision:${options.workspaceId ?? compiled.workspace.workspaceId}:${options.sourceRootCommit}`) } : {}),
|
||||
...(options.sourceRootCommit
|
||||
? { sourceRootCommit: options.sourceRootCommit }
|
||||
: {}),
|
||||
: options.sourceRootCommit
|
||||
? {
|
||||
id: capabilityId.workspaceRevision(
|
||||
`workspace-revision:${options.workspaceId ?? compiled.workspace.workspaceId}:${options.sourceRootCommit}`,
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
...(options.sourceRootCommit ? { sourceRootCommit: options.sourceRootCommit } : {}),
|
||||
};
|
||||
const checked = compileWorkspaceRevision(workspace);
|
||||
if (!checked.ok) {
|
||||
throw new Error(
|
||||
`Instantiated workspace did not compile:\n${checked.issues.map((entry) =>
|
||||
`${entry.path}: ${entry.message}`).join("\n")}`,
|
||||
`Instantiated workspace did not compile:\n${checked.issues
|
||||
.map((entry) => `${entry.path}: ${entry.message}`)
|
||||
.join("\n")}`,
|
||||
);
|
||||
}
|
||||
return {
|
||||
@@ -378,9 +391,6 @@ export const compileWorkspaceRepository = async (options: {
|
||||
plan: checked.plan,
|
||||
lock: rootLock,
|
||||
resources: nodes,
|
||||
directResources: new Map(directPairs.map(([locked, node]) => [
|
||||
bindingKey(locked.kind, locked.binding),
|
||||
node,
|
||||
])),
|
||||
directResources: new Map(directPairs.map(([locked, node]) => [bindingKey(locked.kind, locked.binding), node])),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user