Skip to content

Commit fcba1f8

Browse files
committed
Improve clarity a bit more.
1 parent 42b48c0 commit fcba1f8

File tree

4 files changed

+144
-165
lines changed

4 files changed

+144
-165
lines changed

src/execution/standard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ export class StandardScriptExecution extends BaseExecutionWithCommand<StandardSc
410410
type: 'info',
411411
detail: 'running',
412412
notFreshReason,
413-
executionRequestedReason: executionRequestedReason,
413+
executionRequestedReason,
414414
});
415415

416416
const child = new ScriptChildProcess(

src/fingerprint.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,6 @@ export class Fingerprint {
295295
return this.string === other.string;
296296
}
297297

298-
requiresRebuild(previous: Fingerprint | undefined): boolean {
299-
// If we're not fully tracked, we always need to rebuild.
300-
if (!this.data.fullyTracked) {
301-
return true;
302-
}
303-
// If we don't have a previous fingerprint, we need to rebuild.
304-
if (previous === undefined) {
305-
return true;
306-
}
307-
// Otherwise, we need to rebuild if the fingerprint changed.
308-
return !this.equal(previous);
309-
}
310-
311298
difference(previous: Fingerprint): FingerprintDifference | undefined {
312299
// Do a string comparison first, because it's much faster than
313300
// checking field by field;

src/logging/default-logger.ts

Lines changed: 125 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
import * as pathlib from 'path';
88
import {unreachable} from '../util/unreachable.js';
99

10-
import type {Event} from '../event.js';
10+
import type {
11+
Event,
12+
FingerprintDifference,
13+
NeedsToRunReason,
14+
ServiceStoppedReason,
15+
} from '../event.js';
1116
import type {Logger} from './logger.js';
1217
import {
1318
stringToScriptReference,
@@ -315,138 +320,7 @@ export class DefaultLogger implements Logger {
315320
break;
316321
}
317322
case 'service-stopped': {
318-
let reason: string;
319-
switch (event.reason.name) {
320-
default: {
321-
const never: never = event.reason;
322-
throw new Error(
323-
`Unknown service stop reason: ${inspect(never)}`,
324-
);
325-
}
326-
case 'all consumers of the service are done':
327-
case 'the depgraph changed, service is no longer needed':
328-
case 'the run was aborted':
329-
case 'unknown':
330-
reason = event.reason.name;
331-
break;
332-
case 'restart': {
333-
const restartReason = event.reason;
334-
switch (restartReason.reason.name) {
335-
default: {
336-
const never: never = restartReason.reason;
337-
throw new Error(
338-
`Unknown restart reason: ${inspect(never)}`,
339-
);
340-
}
341-
case 'not-fully-tracked': {
342-
const notFullyTrackedReason = restartReason.reason.reason;
343-
switch (notFullyTrackedReason.name) {
344-
default: {
345-
const never: never = notFullyTrackedReason;
346-
throw new Error(
347-
`Unknown not-fully-tracked reason: ${inspect(never)}`,
348-
);
349-
}
350-
case 'dependency not fully tracked': {
351-
reason = `the service depends on ${labelForScript(
352-
this.rootPackageDir,
353-
stringToScriptReference(
354-
notFullyTrackedReason.dependency,
355-
),
356-
)}`;
357-
break;
358-
}
359-
case 'no files field': {
360-
throw new Error(
361-
'Internal error: a service is tracked even without a files field',
362-
);
363-
}
364-
case 'no output field': {
365-
throw new Error(
366-
'Internal error: a service never has output',
367-
);
368-
}
369-
}
370-
break;
371-
}
372-
case 'no-previous-fingerprint': {
373-
throw new Error(
374-
'Internal error: could not find a previous fingerprint, so we restarted the server?',
375-
);
376-
}
377-
case 'fingerprints-differed': {
378-
const difference = restartReason.reason.difference;
379-
switch (difference.name) {
380-
default: {
381-
const never: never = difference;
382-
throw new Error(
383-
`Unknown not-fully-tracked reason: ${inspect(never)}`,
384-
);
385-
}
386-
case 'config': {
387-
reason = `config field ${
388-
difference.field
389-
} changed from ${inspect(
390-
difference.previous,
391-
)} to ${inspect(difference.current)}`;
392-
break;
393-
}
394-
case 'environment': {
395-
reason = `the ${
396-
difference.field
397-
} of the environment changed from ${inspect(
398-
difference.previous,
399-
)} to ${inspect(difference.current)}`;
400-
break;
401-
}
402-
case 'dependency added': {
403-
reason = `a dependency was added: [${labelForScript(
404-
this.rootPackageDir,
405-
stringToScriptReference(difference.script),
406-
)}]`;
407-
break;
408-
}
409-
case 'dependency removed': {
410-
reason = `a dependency was removed: [${labelForScript(
411-
this.rootPackageDir,
412-
stringToScriptReference(difference.script),
413-
)}]`;
414-
break;
415-
}
416-
case 'dependency changed': {
417-
reason = `a dependency changed: [${labelForScript(
418-
this.rootPackageDir,
419-
stringToScriptReference(difference.script),
420-
)}]`;
421-
break;
422-
}
423-
case 'file added': {
424-
reason = `a file was added: ${pathlib.relative(
425-
this.rootPackageDir,
426-
difference.path,
427-
)}`;
428-
break;
429-
}
430-
case 'file removed': {
431-
reason = `a file was removed: ${pathlib.relative(
432-
this.rootPackageDir,
433-
difference.path,
434-
)}`;
435-
break;
436-
}
437-
case 'file changed': {
438-
reason = `a file was changed: ${pathlib.relative(
439-
this.rootPackageDir,
440-
difference.path,
441-
)}`;
442-
break;
443-
}
444-
}
445-
}
446-
}
447-
break;
448-
}
449-
}
323+
const reason = this.#explainServiceStopped(event.reason);
450324
console.log(`⬇️${prefix} Service stopped because ${reason}`);
451325
if (event.failure !== undefined) {
452326
// Use console.group to indent
@@ -466,6 +340,124 @@ export class DefaultLogger implements Logger {
466340
}
467341
}
468342

343+
#explainServiceStopped(reason: ServiceStoppedReason): string {
344+
switch (reason.name) {
345+
default: {
346+
const never: never = reason;
347+
throw new Error(`Unknown service stop reason: ${inspect(never)}`);
348+
}
349+
case 'all consumers of the service are done':
350+
case 'the depgraph changed, service is no longer needed':
351+
case 'the run was aborted':
352+
case 'unknown':
353+
return reason.name;
354+
case 'restart': {
355+
return this.#explainServiceRestart(reason.reason);
356+
}
357+
}
358+
}
359+
360+
#explainServiceRestart(reason: NeedsToRunReason): string {
361+
switch (reason.name) {
362+
default: {
363+
const never: never = reason;
364+
throw new Error(`Unknown restart reason: ${inspect(never)}`);
365+
}
366+
case 'not-fully-tracked': {
367+
const notFullyTrackedReason = reason.reason;
368+
switch (notFullyTrackedReason.name) {
369+
default: {
370+
const never: never = notFullyTrackedReason;
371+
throw new Error(
372+
`Unknown not-fully-tracked reason: ${inspect(never)}`,
373+
);
374+
}
375+
case 'dependency not fully tracked': {
376+
return `the service depends on [${labelForScript(
377+
this.rootPackageDir,
378+
stringToScriptReference(notFullyTrackedReason.dependency),
379+
)}] which must always be run`;
380+
}
381+
case 'no files field': {
382+
throw new Error(
383+
'Internal error: a service is tracked even without a files field',
384+
);
385+
}
386+
case 'no output field': {
387+
throw new Error('Internal error: a service never has output');
388+
}
389+
}
390+
}
391+
case 'no-previous-fingerprint': {
392+
throw new Error(
393+
'Internal error: could not find a previous fingerprint, so we restarted the server?',
394+
);
395+
}
396+
case 'fingerprints-differed': {
397+
return this.#explainServiceFingerprintDifference(reason.difference);
398+
}
399+
}
400+
}
401+
402+
#explainServiceFingerprintDifference(
403+
difference: FingerprintDifference,
404+
): string {
405+
switch (difference.name) {
406+
default: {
407+
const never: never = difference;
408+
throw new Error(`Unknown not-fully-tracked reason: ${inspect(never)}`);
409+
}
410+
case 'config': {
411+
return `config field ${difference.field} changed from ${inspect(
412+
difference.previous,
413+
)} to ${inspect(difference.current)}`;
414+
}
415+
case 'environment': {
416+
return `the ${
417+
difference.field
418+
} of the environment changed from ${inspect(
419+
difference.previous,
420+
)} to ${inspect(difference.current)}`;
421+
}
422+
case 'dependency added': {
423+
return `a dependency was added: [${labelForScript(
424+
this.rootPackageDir,
425+
stringToScriptReference(difference.script),
426+
)}]`;
427+
}
428+
case 'dependency removed': {
429+
return `a dependency was removed: [${labelForScript(
430+
this.rootPackageDir,
431+
stringToScriptReference(difference.script),
432+
)}]`;
433+
}
434+
case 'dependency changed': {
435+
return `a dependency changed: [${labelForScript(
436+
this.rootPackageDir,
437+
stringToScriptReference(difference.script),
438+
)}]`;
439+
}
440+
case 'file added': {
441+
return `a file was added: ${pathlib.relative(
442+
this.rootPackageDir,
443+
difference.path,
444+
)}`;
445+
}
446+
case 'file removed': {
447+
return `a file was removed: ${pathlib.relative(
448+
this.rootPackageDir,
449+
difference.path,
450+
)}`;
451+
}
452+
case 'file changed': {
453+
return `a file was changed: ${pathlib.relative(
454+
this.rootPackageDir,
455+
difference.path,
456+
)}`;
457+
}
458+
}
459+
}
460+
469461
printMetrics(): void {
470462
// printMetrics() not used in default-logger.
471463
}

0 commit comments

Comments
 (0)