Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vx/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions packages/vx/docs/modules/orchestrator.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ProjectEntry> // 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<string, ProjectEntry>
configured: readonly ProjectMeta[] // every project that can carry tasks
stage?: Map<string, ProjectConfig> // with `lock`: each config the hooks left, by dir, before the default build
reads?: Map<string, Set<string>> // with `stage`: the files each project's plugins made it from, by dir
sparseLeft?: Map<string, SparseLeft> // with `sparseLater`: the projects a sparse checkout left out, by name
}
export function loadProjects(args: LoadProjectsArgs): Promise<LoadedProjects>
// 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(
Expand Down
11 changes: 10 additions & 1 deletion packages/vx/src/cli/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string, SparseLeft>()
let stagedPromise: Promise<Map<string, ProjectEntry>> | undefined
const stagedOnce = (): Promise<Map<string, ProjectEntry>> =>
(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 `[<since>]` diff starts now: merge-base and diff need only root and
Expand Down Expand Up @@ -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(),
Expand Down
9 changes: 8 additions & 1 deletion packages/vx/src/cli/workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, SparseLeft>
}

/**
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions packages/vx/src/orchestrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions packages/vx/src/orchestrator/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -594,6 +596,7 @@ async function loadScope(
packageGraph: scope.packageGraph,
configs: readLock,
warn: warnOf(log),
...(options.affected !== undefined ? { sparseLater: true } : {}),
}
let loaded: LoadedProjects
try {
Expand All @@ -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<LoadedProjects> =>
loadProjects({ ...loadArgs, seeds: 'all', closure: false, staged: projects })
return { allStaged, projects, projectsWithConfigs, everyProject }
return { allStaged, projects, projectsWithConfigs, everyProject, sparseLeft }
}
type Load = Awaited<ReturnType<typeof loadScope>>

Expand Down Expand Up @@ -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<string, SparseLeft>()
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
Expand Down
68 changes: 48 additions & 20 deletions packages/vx/src/orchestrator/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@ interface LoadProjectsBase {
* so the stage's cost and warnings come once per run. Never across runs.
*/
staged?: ReadonlyMap<string, ProjectEntry>
/**
* 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 {
Expand All @@ -411,6 +419,29 @@ export interface LoadedProjects {
stage?: Map<string, ProjectConfig>
/** With `stage`: the files each project's plugins made it from, absolute, by directory. */
reads?: Map<string, Set<string>>
/** With `sparseLater`: each loaded project a sparse checkout left out, and what `git sparse-checkout add` takes for it. */
sparseLeft?: Map<string, SparseLeft>
}

/** 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}\``,
)
}

/**
Expand Down Expand Up @@ -508,6 +539,7 @@ export async function loadProjects(args: LoadProjectsArgs): Promise<LoadedProjec
const root = path.resolve(workspaceRoot)
const rootName = args.projectMetas.find((m) => path.resolve(m.dir) === root)?.name
const projects = new Map<string, ProjectEntry>()
const sparseLeft = new Map<string, SparseLeft>()
// 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<string, Set<string>>() : undefined
Expand Down Expand Up @@ -596,31 +628,26 @@ export async function loadProjects(args: LoadProjectsArgs): Promise<LoadedProjec
for (const [k, [, i]] of standing.entries())
if (left?.has(rels[k]!) === true) there[i] = false
}
const goneAll = [...withFile, ...bare].filter((_, i) => !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) => {
Expand Down Expand Up @@ -724,9 +751,10 @@ export async function loadProjects(args: LoadProjectsArgs): Promise<LoadedProjec
}
}
}
const left = sparseLeft.size > 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 {
Expand Down
20 changes: 20 additions & 0 deletions packages/vx/tests/sparse-checkout-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading