Skip to content

Commit ec095a2

Browse files
1aronclaude
andcommitted
Fix(CLI): Keep watch quiet for dependencies a full rebuild already read
A full rebuild started by a source edit or a retry read every dependency but left no checkpoint for them. After a failed dependency rebuild, a late or duplicate notification for an unchanged dependency, which Windows often delivers, therefore rebuilt everything again and reported a second watch restart; compat-windows caught this in the BH-0004 publication retry test. Each full rebuild now samples the watched dependencies before it reads them and records those versions once it succeeds. Unchanged notifications are skipped, while a dependency edited during the rebuild no longer matches its sample and still rebuilds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fdeea6d commit ec095a2

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎packages/cli/src/dependency-rebuild.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function fileRevision(file: string) {
2121
/** Called inside the watch queue, so each comparison follows the previous rebuild. */
2222
export function createDependencyRebuild(rebuild: () => Promise<void>, ready: () => boolean) {
2323
const completed = new Map<string, string>()
24-
return async (file: string) => {
24+
const rebuildDependency = async (file: string) => {
2525
const absolute = resolve(file)
2626
const revision = fileRevision(absolute)
2727
if (ready() && revision !== undefined && completed.get(absolute) === revision) return
@@ -32,4 +32,27 @@ export function createDependencyRebuild(rebuild: () => Promise<void>, ready: ()
3232
await rebuild()
3333
if (revision !== undefined) completed.set(absolute, revision)
3434
}
35+
return Object.assign(rebuildDependency, {
36+
/**
37+
* Samples dependency versions before a full rebuild that reads them all, which
38+
* may start from a source edit or a retry rather than a dependency event.
39+
*/
40+
sample(files: Iterable<string>) {
41+
const revisions = new Map<string, string>()
42+
for (const file of files) {
43+
const absolute = resolve(file)
44+
const revision = fileRevision(absolute)
45+
if (revision !== undefined) revisions.set(absolute, revision)
46+
}
47+
return revisions
48+
},
49+
/**
50+
* Records the sampled versions once that rebuild succeeds, so late or duplicate
51+
* notifications for unchanged dependencies do not rebuild again. Files edited
52+
* during the rebuild no longer match their sample and still rebuild.
53+
*/
54+
complete(revisions: ReadonlyMap<string, string>) {
55+
for (const [file, revision] of revisions) completed.set(file, revision)
56+
}
57+
})
3558
}

‎packages/cli/src/generate.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export default async function runGenerate(specifiedSourcePaths: string[] = [], o
318318
pipelineReady = false
319319
attemptedDependencies.clear()
320320
missingDependencies.clear()
321+
const dependencyRevisions = rebuildDependency.sample(watchedDependencies)
321322
const sources = scanPaths()
322323
const stamps = sourceStamps(scanner.cwd, sources)
323324
for (const [source, stamp] of stamps) readStamps.set(source, stamp)
@@ -328,6 +329,7 @@ export default async function runGenerate(specifiedSourcePaths: string[] = [], o
328329
await syncDependencies([...scanner.resetDependencies, ...scanner.sourcePolicyDependencies, ...attemptedDependencies])
329330
pipelineReady = true
330331
missingDependencies.clear()
332+
rebuildDependency.complete(dependencyRevisions)
331333
process.stderr.write(initial ? '\nStart watching source changes\n' : '\nRestart watching source changes\n')
332334
if (!initial) scanner.emit('resetDependencyChange')
333335
} finally {

‎packages/cli/tests/issues/481.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,36 @@ test('an invalid pipeline rebuilds even when this dependency was previously comp
165165
await rebuild(file)
166166
expect(attempts).toBe(2)
167167
})
168+
169+
test('a successful full rebuild checkpoints dependencies so late unchanged notifications stay quiet', async () => {
170+
const file = resource()
171+
let attempts = 0
172+
const rebuild = createDependencyRebuild(async () => {
173+
if (++attempts === 1) throw new Error('publication failed')
174+
}, () => true)
175+
await expect(rebuild(file)).rejects.toThrow('publication failed')
176+
// A source edit then rebuilds everything, reading this unchanged dependency.
177+
const sample = rebuild.sample([file])
178+
attempts++
179+
rebuild.complete(sample)
180+
await rebuild(file)
181+
expect(attempts).toBe(2)
182+
})
183+
184+
test('a dependency edited during a full rebuild still rebuilds after its checkpoint', async () => {
185+
const file = resource()
186+
let attempts = 0
187+
const rebuild = createDependencyRebuild(async () => { attempts++ }, () => true)
188+
const sample = rebuild.sample([file])
189+
writeFileSync(file, 'blue')
190+
rebuild.complete(sample)
191+
await rebuild(file)
192+
expect(attempts).toBe(1)
193+
})
194+
195+
test('missing dependencies are not sampled', () => {
196+
const file = resource()
197+
const rebuild = createDependencyRebuild(async () => {}, () => true)
198+
unlinkSync(file)
199+
expect(rebuild.sample([file]).size).toBe(0)
200+
})

0 commit comments

Comments
 (0)