diff --git a/packages/vx/docs/cli.md b/packages/vx/docs/cli.md index a872db0c94..415146dedf 100644 --- a/packages/vx/docs/cli.md +++ b/packages/vx/docs/cli.md @@ -3703,6 +3703,8 @@ So a config that reads the environment keeps its lock-time values. selects it or depends on it. - So is one whose directory the checkout kept for the ignored files in it (an install's `node_modules`), a config-less project's too. +- Under `--affected`, only when a task the diff reaches is its, or depends + on one of its: a change elsewhere runs as usual. - Only a repository where a sparse checkout was set asks git about a config-less project's directory: one spawn per run. - The refusal names every such project, and the one diff --git a/packages/vx/docs/modules/orchestrator.md b/packages/vx/docs/modules/orchestrator.md index a340530898..352d176751 100644 --- a/packages/vx/docs/modules/orchestrator.md +++ b/packages/vx/docs/modules/orchestrator.md @@ -3261,14 +3261,25 @@ interface LoadProjectsBase { warn: (m: string) => void lock?: boolean // `vx lock`'s audit: the one load that runs the `project` hooks staged?: ReadonlyMap // entries a load in this process already produced + sparseLater?: boolean // a project a sparse checkout left out loads from the lock into `sparseLeft` instead of refusing } export interface LoadedProjects { projects: Map configured: readonly ProjectMeta[] // every project that can carry tasks stage?: Map // with `lock`: each config the hooks left, by dir, before the default build reads?: Map> // with `stage`: the files each project's plugins made it from, by dir + sparseLeft?: Map // with `sparseLater`: the projects a sparse checkout left out, by name } export function loadProjects(args: LoadProjectsArgs): Promise +// A project a sparse checkout left out: its name, dir, and the path `git sparse-checkout add` takes. +export interface SparseLeft { + name: string + dir: string + add: string +} +// "X is outside this sparse checkout, but this run needs it": a run under +// `--affected` says it only when a kept task is the project's. +export function sparseRefusal(workspaceRoot: string, left: readonly SparseLeft[]): UserError /** A reader's view: from the lock (no lock is NO_LOCK), every stage applied, no closure. */ export function loadResolvedProjects( diff --git a/packages/vx/src/cli/select.ts b/packages/vx/src/cli/select.ts index 5c63b5f10e..2946717b61 100644 --- a/packages/vx/src/cli/select.ts +++ b/packages/vx/src/cli/select.ts @@ -58,6 +58,7 @@ import { type VerbWorkspace, workspaceOfDiscovery, } from './workspace-config.js' +import type { SparseLeft } from '../orchestrator/index.js' /** * The projects whose tasks declare an `inputs.workspaceFiles` glob matching a @@ -457,9 +458,16 @@ export async function resolveFilters( ) // Every reader of staged configs here shares one load, which the run reuses // (`RunHandoff.staged`): the `project` stage runs once per project per run. + // A project a sparse checkout left out loads from the lock: every + // `--affected` and graph-walking selection refused it, though only a run + // that selects it or depends on it needs it. Such a load is never handed + // to the run, whose own load refuses what it needs. + const sparseLeft = new Map() let stagedPromise: Promise> | undefined const stagedOnce = (): Promise> => - (stagedPromise ??= discovering.then((projects) => loadCliProjects(root, projects, 'all', load))) + (stagedPromise ??= discovering.then((projects) => + loadCliProjects(root, projects, 'all', { ...load, sparseLeft }), + )) // A scoped enumeration lists only its projects: null asks git. const enumeration = async () => (git === undefined ? undefined : await git.start()) // Each `[]` diff starts now: merge-base and diff need only root and @@ -665,6 +673,7 @@ export async function resolveFilters( // The owners walk fell through to its per-file sweep; the run loads for itself. staged = undefined } + if (sparseLeft.size > 0) staged = undefined } return { names: [...selected].sort(), diff --git a/packages/vx/src/cli/workspace-config.ts b/packages/vx/src/cli/workspace-config.ts index b00736c09c..02a78f80bb 100644 --- a/packages/vx/src/cli/workspace-config.ts +++ b/packages/vx/src/cli/workspace-config.ts @@ -16,7 +16,7 @@ import { loadWorkspacePlugins, lockReader, } from '../orchestrator/index.js' -import type { BranchFacts, VxPlugin } from '../orchestrator/index.js' +import type { BranchFacts, SparseLeft, VxPlugin } from '../orchestrator/index.js' import { buildPackageGraph, expandHome, @@ -228,6 +228,11 @@ export interface CliLoadOptions { * itself: printed per read, one cycle said it seven times. */ warn?: (message: string) => void + /** + * Given, a project a sparse checkout left out loads from the lock instead + * of refusing, and is added here (`LoadProjectsArgs.sparseLater`). + */ + sparseLeft?: Map } /** @@ -259,6 +264,8 @@ export async function loadCliProjects( : { closure: false as const }), configs: discoveryOf(metas).lock ?? lockReader(workspaceRoot), warn, + ...(opts.sparseLeft !== undefined ? { sparseLater: true } : {}), }) + for (const [name, left] of loaded.sparseLeft ?? []) opts.sparseLeft?.set(name, left) return loaded.projects } diff --git a/packages/vx/src/orchestrator/index.ts b/packages/vx/src/orchestrator/index.ts index f91f559557..06df215675 100644 --- a/packages/vx/src/orchestrator/index.ts +++ b/packages/vx/src/orchestrator/index.ts @@ -40,6 +40,7 @@ export { lockReader, type LoadedProjects, type LoadProjectsArgs, + type SparseLeft, } from './projects.js' export { computeTaskHash, createHashCache, type HashCache } from './task-hash.js' export type { diff --git a/packages/vx/src/orchestrator/prepare.ts b/packages/vx/src/orchestrator/prepare.ts index 36d380748e..ef02bd09d4 100644 --- a/packages/vx/src/orchestrator/prepare.ts +++ b/packages/vx/src/orchestrator/prepare.ts @@ -88,6 +88,8 @@ import { loadWorkspacePlugins, lockReader, type LoadedProjects, + type SparseLeft, + sparseRefusal, } from './projects.js' import { keptByAffected } from './affected-tasks.js' import { applyGraphStage } from './graph-stage.js' @@ -594,6 +596,7 @@ async function loadScope( packageGraph: scope.packageGraph, configs: readLock, warn: warnOf(log), + ...(options.affected !== undefined ? { sparseLater: true } : {}), } let loaded: LoadedProjects try { @@ -609,11 +612,11 @@ async function loadScope( localCache.close() throw err } - const { projects, configured: projectsWithConfigs } = loaded + const { projects, configured: projectsWithConfigs, sparseLeft } = loaded mark('load configs') const everyProject = (): Promise => loadProjects({ ...loadArgs, seeds: 'all', closure: false, staged: projects }) - return { allStaged, projects, projectsWithConfigs, everyProject } + return { allStaged, projects, projectsWithConfigs, everyProject, sparseLeft } } type Load = Awaited> @@ -873,6 +876,19 @@ async function buildGraph( nodes = graphOf(kept) } } + // What `--affected` kept is what runs: a project a sparse checkout left out + // is refused only now, when a kept task or one it depends on is its. + if (load.sparseLeft !== undefined) { + const needed = new Map() + for (const n of nodes.values()) { + const left = load.sparseLeft.get(n.projectName) + if (left !== undefined) needed.set(left.name, left) + } + if (needed.size > 0) { + cache.close() + throw sparseRefusal(workspaceRoot, [...needed.values()]) + } + } if (unproven.length > 0) await refuseUndeclaredDeps(unproven, everyProject) // An `--exclude-dependencies` name no project declares is refused like any // other typed name (`=biuld` ran the whole chain). After the graph, so a diff --git a/packages/vx/src/orchestrator/projects.ts b/packages/vx/src/orchestrator/projects.ts index 2e557a8f3f..fc2f3feec1 100644 --- a/packages/vx/src/orchestrator/projects.ts +++ b/packages/vx/src/orchestrator/projects.ts @@ -394,6 +394,14 @@ interface LoadProjectsBase { * so the stage's cost and warnings come once per run. Never across runs. */ staged?: ReadonlyMap + /** + * An `--affected` run: a project a sparse checkout left out loads from the + * lock instead of refusing, named in `LoadedProjects.sparseLeft`, and the + * run refuses only when its final graph holds one of its tasks + * (`sparseRefusal`). Refused at load, every such run in a sparse checkout + * failed, though the diff reached none of those projects. + */ + sparseLater?: boolean } export interface LoadedProjects { @@ -411,6 +419,29 @@ export interface LoadedProjects { stage?: Map /** With `stage`: the files each project's plugins made it from, absolute, by directory. */ reads?: Map> + /** With `sparseLater`: each loaded project a sparse checkout left out, and what `git sparse-checkout add` takes for it. */ + sparseLeft?: Map +} + +/** A project a sparse checkout left out, and the path that brings it back. */ +export interface SparseLeft { + name: string + dir: string + add: string +} + +/** The refusal of a run that needs projects a sparse checkout left out. */ +export function sparseRefusal(workspaceRoot: string, left: readonly SparseLeft[]): UserError { + const add = `git sparse-checkout add ${[...new Set(left.map((s) => s.add))].join(' ')}` + const one = left.length === 1 ? left[0]! : null + return new UserError( + one !== null + ? `${lockedProject(workspaceRoot, one.name, one.dir)} is outside this sparse checkout, but this run needs it — run \`${add}\`` + : `${listed( + left.map((s) => lockedProject(workspaceRoot, s.name, s.dir)), + 5, + )} are outside this sparse checkout, but this run needs them — run \`${add}\``, + ) } /** @@ -508,6 +539,7 @@ export async function loadProjects(args: LoadProjectsArgs): Promise path.resolve(m.dir) === root)?.name const projects = new Map() + const sparseLeft = new Map() // What each project's `project` plugins said they made it from, by // directory (`ProjectHookContext.read`): `vx lock` scans them for secrets. const reads = hooks ? new Map>() : undefined @@ -596,31 +628,26 @@ export async function loadProjects(args: LoadProjectsArgs): Promise !there[i]) - const gone = goneAll[0] - if (gone !== undefined) { + let goneAll = [...withFile, ...bare].filter((_, i) => !there[i]) + if (goneAll.length > 0) { // A sparse checkout left it out, not deleted: `vx lock` there // refuses too. Asked only on this failure path, of every gone project // of the round, so one command brings them all back. - const sparse = goneAll.flatMap((m) => { + const sparse = goneAll.flatMap((m): SparseLeft[] => { const dir = relPosix(workspaceRoot, m.dir) if (m.configPath === null) - return skipWorktreeSync(workspaceRoot, dir) === null ? [] : [{ meta: m, add: dir }] + return skipWorktreeSync(workspaceRoot, dir) === null + ? [] + : [{ name: m.name, dir: m.dir, add: dir }] const at = skipWorktreeSync(workspaceRoot, relPosix(workspaceRoot, m.configPath)) - return at === null ? [] : [{ meta: m, add: path.posix.dirname(at) }] + return at === null ? [] : [{ name: m.name, dir: m.dir, add: path.posix.dirname(at) }] }) - if (sparse.length > 0) { - const add = `git sparse-checkout add ${[...new Set(sparse.map((s) => s.add))].join(' ')}` - const one = sparse.length === 1 ? sparse[0]!.meta : null - throw new UserError( - one !== null - ? `${lockedProject(workspaceRoot, one.name, one.dir)} is outside this sparse checkout, but this run needs it — run \`${add}\`` - : `${listed( - sparse.map((s) => lockedProject(workspaceRoot, s.meta.name, s.meta.dir)), - 5, - )} are outside this sparse checkout, but this run needs them — run \`${add}\``, - ) - } + if (sparse.length > 0 && args.sparseLater !== true) throw sparseRefusal(workspaceRoot, sparse) + for (const s of sparse) sparseLeft.set(s.name, s) + goneAll = goneAll.filter((m) => !sparseLeft.has(m.name)) + } + const gone = goneAll[0] + if (gone !== undefined) { // A submodule a clone skipped, not deleted either: `vx lock` would // drop it, and the committed lock would flip between machines. const unchecked = goneAll.flatMap((m) => { @@ -724,9 +751,10 @@ export async function loadProjects(args: LoadProjectsArgs): Promise 0 ? { sparseLeft } : {} return stage === undefined - ? { projects, configured } - : { projects, configured, stage, reads: reads! } + ? { projects, configured, ...left } + : { projects, configured, stage, reads: reads!, ...left } } function withDefaultBuild(config: ProjectConfig): ProjectConfig { diff --git a/packages/vx/tests/sparse-checkout-config.test.ts b/packages/vx/tests/sparse-checkout-config.test.ts index 4724f956c3..f4cdd53163 100644 --- a/packages/vx/tests/sparse-checkout-config.test.ts +++ b/packages/vx/tests/sparse-checkout-config.test.ts @@ -108,6 +108,26 @@ it('a run that does not need the excluded project proceeds', async () => { expect(r.out).toContain('built') }, 30_000) +it('--affected refuses the excluded project only when what it reaches needs it', async () => { + // Every --affected run, and every graph-walking filter, loaded each + // project and refused the left-out one, though the diff reached none of it. + const nothing = await vx(root, ['run', 'build', '--affected=HEAD']) + expect([nothing.code, nothing.err]).toEqual([0, expect.stringContaining('nothing affected')]) + const walk = await vx(root, ['run', 'build', '--filter', '...other', '--output-logs=full']) + expect([walk.code, walk.out.includes('built')]).toEqual([0, true]) + await Bun.write(path.join(root, 'packages/other/src.txt'), 'edit') + const other = await vx(root, ['run', 'build', '--affected=HEAD', '--output-logs=full']) + expect([other.code, other.out.includes('other#build'), other.err]).toEqual([ + 0, + true, + expect.not.stringContaining('sparse'), + ]) + // app's build depends on lib's: the run needs lib. + await Bun.write(path.join(root, 'packages/app/src.txt'), 'edit') + const app = await vx(root, ['run', 'build', '--affected=HEAD']) + expect([app.code, app.out.includes('built'), app.err.trim()]).toEqual([1, false, REFUSAL]) +}, 30_000) + it('vx lock and its --check refuse what the sparse checkout left out, the lock untouched', async () => { const lockFile = path.join(root, '.vx/projects.lock') const before = await Bun.file(lockFile).text()