@ttsc/unplugin 0.28.3 → 0.28.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -8
- package/lib/api.js +3 -0
- package/lib/api.js.map +1 -1
- package/lib/api.mjs +1 -0
- package/lib/api.mjs.map +1 -1
- package/lib/core/index.d.cts +17 -6
- package/lib/core/index.d.mts +17 -6
- package/lib/core/index.d.ts +17 -6
- package/lib/core/index.js +117 -21
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +115 -21
- package/lib/core/index.mjs.map +1 -1
- package/lib/core/transform.d.cts +93 -25
- package/lib/core/transform.d.mts +93 -25
- package/lib/core/transform.d.ts +93 -25
- package/lib/core/transform.js +803 -121
- package/lib/core/transform.js.map +1 -1
- package/lib/core/transform.mjs +804 -122
- package/lib/core/transform.mjs.map +1 -1
- package/lib/core/tsconfigPaths.d.cts +61 -0
- package/lib/core/tsconfigPaths.d.mts +61 -0
- package/lib/core/tsconfigPaths.d.ts +61 -0
- package/lib/core/tsconfigPaths.js +190 -7
- package/lib/core/tsconfigPaths.js.map +1 -1
- package/lib/core/tsconfigPaths.mjs +188 -8
- package/lib/core/tsconfigPaths.mjs.map +1 -1
- package/lib/next.d.cts +27 -10
- package/lib/next.d.mts +27 -10
- package/lib/next.d.ts +27 -10
- package/lib/next.js +229 -8
- package/lib/next.js.map +1 -1
- package/lib/next.mjs +229 -8
- package/lib/next.mjs.map +1 -1
- package/lib/turbopack.d.cts +5 -4
- package/lib/turbopack.d.mts +5 -4
- package/lib/turbopack.d.ts +5 -4
- package/lib/turbopack.js +13 -7
- package/lib/turbopack.js.map +1 -1
- package/lib/turbopack.mjs +14 -8
- package/lib/turbopack.mjs.map +1 -1
- package/package.json +3 -3
- package/src/core/index.ts +122 -21
- package/src/core/transform.ts +1073 -132
- package/src/core/tsconfigPaths.ts +254 -8
- package/src/next.ts +262 -10
- package/src/turbopack.ts +13 -9
package/lib/core/transform.js
CHANGED
|
@@ -9,8 +9,49 @@ var ttsc = require('ttsc');
|
|
|
9
9
|
var pathIdentity = require('ttsc/path-identity');
|
|
10
10
|
var tsconfigPaths = require('./tsconfigPaths.js');
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
|
|
12
|
+
/**
|
|
13
|
+
* A verdict about one generation that later deliveries replay instead of
|
|
14
|
+
* repeating the whole compile behind it.
|
|
15
|
+
*
|
|
16
|
+
* The two kinds are replayed on different evidence, and each carries its own: a
|
|
17
|
+
* pass verdict knows the pass it belongs to, and an unstable generation knows
|
|
18
|
+
* the recorded environment it was proven against.
|
|
19
|
+
*/
|
|
20
|
+
class TtscTerminalGenerationError extends Error {
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The compile succeeded and produced no output for one requested module,
|
|
24
|
+
* because the program does not contain it.
|
|
25
|
+
*
|
|
26
|
+
* Not a terminal generation error, and deliberately not a build failure. It is
|
|
27
|
+
* a fact about one file, and the answer to it is to leave that file to the host
|
|
28
|
+
* (samchon/ttsc#1308). It is a distinct type rather than a message match so the
|
|
29
|
+
* decision travels as a type: `@ttsc/metro` used to recognise this case by
|
|
30
|
+
* searching the message text for "did not return output", which is how one
|
|
31
|
+
* product came to hold two different answers to one condition.
|
|
32
|
+
*/
|
|
33
|
+
class TtscMissingProgramOutputError extends Error {
|
|
34
|
+
/** The module the bundler asked for. */
|
|
35
|
+
file;
|
|
36
|
+
/** The project config whose program does not contain it. */
|
|
37
|
+
tsconfig;
|
|
38
|
+
constructor(file, tsconfig) {
|
|
39
|
+
super(`ttsc: ${file} is not part of the program described by ${tsconfig}, so it was left untransformed. Add it to that project's "include" if ttsc plugins should apply to it.`);
|
|
40
|
+
this.name = "TtscMissingProgramOutputError";
|
|
41
|
+
this.file = file;
|
|
42
|
+
this.tsconfig = tsconfig;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A bounded proof failure that stays authoritative until its inputs change.
|
|
47
|
+
*
|
|
48
|
+
* This is the adapter failing to _obtain_ a coherent snapshot — a race it lost
|
|
49
|
+
* — so a later attempt may well succeed with the same inputs. It is retried
|
|
50
|
+
* when its recorded environment moves, and a new delivery epoch grants it the
|
|
51
|
+
* one fresh attempt the per-pass cache clear used to give it
|
|
52
|
+
* (samchon/ttsc#1300).
|
|
53
|
+
*/
|
|
54
|
+
class TtscUnstableGenerationError extends TtscTerminalGenerationError {
|
|
14
55
|
validation;
|
|
15
56
|
constructor(message, validation) {
|
|
16
57
|
super(message);
|
|
@@ -18,11 +59,58 @@ class TtscUnstableGenerationError extends Error {
|
|
|
18
59
|
this.validation = validation;
|
|
19
60
|
}
|
|
20
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* A compile this pass already attempted, whose envelope failed outright.
|
|
64
|
+
*
|
|
65
|
+
* The envelope cannot say whether the host reported diagnostics about the
|
|
66
|
+
* project or failed to run at all: an ordinary type error arrives as an
|
|
67
|
+
* `"exception"` carrying the compiler's own diagnostic text, exactly as a
|
|
68
|
+
* crashed host would. Sniffing that message to tell the two apart would be a
|
|
69
|
+
* guess, so the adapter uses the one boundary it genuinely owns. Inside a pass
|
|
70
|
+
* the answer is already settled, so every later module replays it instead of
|
|
71
|
+
* repeating a whole-project transform to reach the same verdict, which is what
|
|
72
|
+
* made a single broken save cost one compile per delivered module
|
|
73
|
+
* (samchon/ttsc#1303).
|
|
74
|
+
*
|
|
75
|
+
* The scope is exactly the pass. A host whose `buildStart` repeats drops the
|
|
76
|
+
* verdict at its next rebuild, so a transient host failure costs that one
|
|
77
|
+
* rebuild. A host with no pass boundary never retains one at all and keeps
|
|
78
|
+
* retrying on its very next delivery. Between them sits a host that opens
|
|
79
|
+
* exactly one pass for its whole process — Bun's runtime plugin, and a Vite dev
|
|
80
|
+
* server configured with `server.watch: null` — where the verdict lasts the
|
|
81
|
+
* session. That follows from what those hosts already publish about themselves,
|
|
82
|
+
* that their session is one immutable load session and the remedy for changed
|
|
83
|
+
* inputs is to restart, and it is the deliberate trade: without it, one type
|
|
84
|
+
* error costs such a session a whole-project compile per delivered module,
|
|
85
|
+
* which is the workload samchon/ttsc#970 is about.
|
|
86
|
+
*
|
|
87
|
+
* It carries the original error's message, stack and `cause` rather than
|
|
88
|
+
* replacing them, so what a bundler reports is what it reported before the
|
|
89
|
+
* verdict existed.
|
|
90
|
+
*/
|
|
91
|
+
class TtscPassVerdictError extends TtscTerminalGenerationError {
|
|
92
|
+
/** The delivery pass this verdict belongs to, and its whole scope. */
|
|
93
|
+
epoch;
|
|
94
|
+
constructor(original, epoch) {
|
|
95
|
+
super(original instanceof Error
|
|
96
|
+
? original.message
|
|
97
|
+
: formatUnknownError(original), { cause: original });
|
|
98
|
+
if (original instanceof Error) {
|
|
99
|
+
this.name = original.name;
|
|
100
|
+
if (original.stack !== undefined)
|
|
101
|
+
this.stack = original.stack;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
this.name = "TtscPassVerdictError";
|
|
105
|
+
}
|
|
106
|
+
this.epoch = epoch;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
21
109
|
/** Proof witnesses retained beside a compiler result without extending its API. */
|
|
22
110
|
const TRANSFORM_GENERATION_FAILURES = new WeakMap();
|
|
23
111
|
/** Retry baselines retained only for attempts that could not be published. */
|
|
24
112
|
const TRANSFORM_FAILED_GENERATION_VALIDATIONS = new WeakMap();
|
|
25
|
-
/**
|
|
113
|
+
/** Cache promises whose unchanged terminal verdict may be replayed. */
|
|
26
114
|
const TERMINAL_TRANSFORM_GENERATIONS = new WeakMap();
|
|
27
115
|
/** Maximum witnesses printed and retained for each failed transform attempt. */
|
|
28
116
|
const MAX_GENERATION_PROOF_FAILURES = 8;
|
|
@@ -42,10 +130,27 @@ const DEFAULT_FILESYSTEM_OPERATIONS = Object.freeze({
|
|
|
42
130
|
const TRANSFORM_CACHE_FILESYSTEM = new WeakMap();
|
|
43
131
|
const TRANSFORM_RESULT_FILESYSTEM = new WeakMap();
|
|
44
132
|
/**
|
|
45
|
-
*
|
|
46
|
-
* {@link beginTtscTransformBuild}
|
|
133
|
+
* The current delivery epoch of each cache whose owner has declared a real
|
|
134
|
+
* per-pass lifecycle by calling {@link beginTtscTransformBuild}.
|
|
135
|
+
*
|
|
136
|
+
* A _delivery epoch_ is one bundler pass: the window inside which each module
|
|
137
|
+
* is requested at most once, so its first delivery may be settled against the
|
|
138
|
+
* state the pass started from. It is deliberately not the same fact as whether
|
|
139
|
+
* the generation is still valid, which the recorded snapshot answers.
|
|
140
|
+
* Conflating the two is what made every host with a repeating `buildStart` —
|
|
141
|
+
* webpack and Rspack watch, Rollup and Rolldown watch, `vite build --watch`,
|
|
142
|
+
* esbuild rebuild — discard a perfectly good whole-project compile on every
|
|
143
|
+
* edit (samchon/ttsc#1300).
|
|
144
|
+
*
|
|
145
|
+
* Absent from the map means persistent validation: a host with no pass boundary
|
|
146
|
+
* at all (a watching Vite dev server, Metro, the Turbopack loader), where every
|
|
147
|
+
* delivery proves the generation for itself.
|
|
47
148
|
*/
|
|
48
|
-
const
|
|
149
|
+
const TRANSFORM_CACHE_EPOCHS = new WeakMap();
|
|
150
|
+
/** The pass a delivery belongs to, or `undefined` under persistent validation. */
|
|
151
|
+
function transformCacheEpoch(cache) {
|
|
152
|
+
return cache === undefined ? undefined : TRANSFORM_CACHE_EPOCHS.get(cache);
|
|
153
|
+
}
|
|
49
154
|
function createHostPathIdentityContext(filesystem = DEFAULT_FILESYSTEM_OPERATIONS) {
|
|
50
155
|
return pathIdentity.createFilesystemPathIdentityContext({
|
|
51
156
|
caseSensitive: filesystem.caseSensitive,
|
|
@@ -85,27 +190,37 @@ function resultFilesystem(result) {
|
|
|
85
190
|
return (TRANSFORM_RESULT_FILESYSTEM.get(result) ?? DEFAULT_FILESYSTEM_OPERATIONS);
|
|
86
191
|
}
|
|
87
192
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
193
|
+
* Open a new delivery pass, enabling constant-time first delivery for every
|
|
194
|
+
* module this pass asks for.
|
|
195
|
+
*
|
|
196
|
+
* This deliberately retains the cached generation. The pass boundary is a
|
|
197
|
+
* statement about _deliveries_ — each module is requested at most once inside
|
|
198
|
+
* it — not about whether the compiled program is still correct, which the
|
|
199
|
+
* generation's own recorded snapshot answers and which
|
|
200
|
+
* {@link matchesCachedSource} proves once at the pass's first delivery. Clearing
|
|
201
|
+
* here instead made a host whose `buildStart` repeats recompile the whole
|
|
202
|
+
* project on every rebuild even when no compiler input had changed
|
|
203
|
+
* (samchon/ttsc#1300). Use {@link resetTtscTransformCache} to actually discard a
|
|
204
|
+
* generation and its watchers.
|
|
90
205
|
*
|
|
91
|
-
* Hosts without a guaranteed
|
|
92
|
-
*
|
|
206
|
+
* Hosts without a guaranteed pass boundary use persistent validation unless
|
|
207
|
+
* they have another immutable lifecycle. Bun runtime setup, for example,
|
|
93
208
|
* defines one process-scoped module-loading session.
|
|
94
209
|
*/
|
|
95
210
|
function beginTtscTransformBuild(cache) {
|
|
96
|
-
|
|
97
|
-
BUILD_SCOPED_TRANSFORM_CACHES.add(cache);
|
|
211
|
+
TRANSFORM_CACHE_EPOCHS.set(cache, (TRANSFORM_CACHE_EPOCHS.get(cache) ?? 0) + 1);
|
|
98
212
|
}
|
|
99
213
|
/**
|
|
100
|
-
*
|
|
214
|
+
* Discard every generation, dispose its watchers, and return the cache to
|
|
215
|
+
* persistent validation mode.
|
|
101
216
|
*
|
|
102
|
-
* This is
|
|
103
|
-
*
|
|
104
|
-
*
|
|
217
|
+
* This is the unconditional lifecycle boundary, and it is distinct from
|
|
218
|
+
* {@link beginTtscTransformBuild}: a pass ending is not a reason to throw a
|
|
219
|
+
* proven compile away, while a session ending is.
|
|
105
220
|
*/
|
|
106
221
|
function resetTtscTransformCache(cache) {
|
|
107
222
|
clearTtscTransformCache(cache);
|
|
108
|
-
|
|
223
|
+
TRANSFORM_CACHE_EPOCHS.delete(cache);
|
|
109
224
|
}
|
|
110
225
|
/** Dispose generation-owned filesystem resources before clearing a cache. */
|
|
111
226
|
function clearTtscTransformCache(cache) {
|
|
@@ -161,15 +276,19 @@ async function transformTtsc(id, source, options, aliases, cache, hooks) {
|
|
|
161
276
|
tsconfig,
|
|
162
277
|
});
|
|
163
278
|
for (;;) {
|
|
279
|
+
// Read once per iteration, before the cache is consulted, so a delivery
|
|
280
|
+
// belongs to the pass that was current when it started examining the
|
|
281
|
+
// generation. A pass opened while this one awaits an in-flight compile is
|
|
282
|
+
// picked up by the next iteration, which is the one that runs when the
|
|
283
|
+
// entry it awaited turns out to have been superseded.
|
|
284
|
+
const epoch = transformCacheEpoch(cache);
|
|
164
285
|
let transformed = cache?.get(key);
|
|
165
286
|
if (transformed !== undefined) {
|
|
166
287
|
const terminal = TERMINAL_TRANSFORM_GENERATIONS.get(transformed);
|
|
167
288
|
if (terminal !== undefined) {
|
|
168
|
-
// A
|
|
169
|
-
// invitation for every later module to repeat the whole compile.
|
|
170
|
-
|
|
171
|
-
// establishes that a new generation could differ.
|
|
172
|
-
if (!failedGenerationEnvironmentChanged(terminal.validation, {
|
|
289
|
+
// A terminal verdict is an answer about one observed environment, not an
|
|
290
|
+
// invitation for every later module to repeat the whole compile.
|
|
291
|
+
if (replaysTerminalGeneration(terminal, epoch, {
|
|
173
292
|
currentFile: file,
|
|
174
293
|
currentSource: source,
|
|
175
294
|
filesystem,
|
|
@@ -191,8 +310,7 @@ async function transformTtsc(id, source, options, aliases, cache, hooks) {
|
|
|
191
310
|
if (cache?.get(key) !== transformed) {
|
|
192
311
|
continue;
|
|
193
312
|
}
|
|
194
|
-
|
|
195
|
-
if (!buildScoped) {
|
|
313
|
+
if (epoch === undefined) {
|
|
196
314
|
await settleProjectMutationEvents(cached);
|
|
197
315
|
if (cache?.get(key) !== transformed) {
|
|
198
316
|
continue;
|
|
@@ -207,15 +325,33 @@ async function transformTtsc(id, source, options, aliases, cache, hooks) {
|
|
|
207
325
|
projectRoot: cached.projectRoot,
|
|
208
326
|
result: cached.result,
|
|
209
327
|
}) &&
|
|
210
|
-
matchesCachedSource(cached, file, source,
|
|
211
|
-
reportSuccessDiagnostics(cached
|
|
328
|
+
matchesCachedSource(cached, file, source, epoch)) {
|
|
329
|
+
reportSuccessDiagnostics(cached, epoch);
|
|
212
330
|
// A resolved `"exception"` / `"failure"` envelope makes this throw;
|
|
213
|
-
// that is a failed generation too, so
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
331
|
+
// that is a failed generation too, so it is retained for this pass or
|
|
332
|
+
// evicted outside one before being surfaced.
|
|
333
|
+
let code;
|
|
334
|
+
try {
|
|
335
|
+
code = selectOrEvict(cache, key, transformed, epoch, {
|
|
336
|
+
file,
|
|
337
|
+
projectRoot: cached.projectRoot,
|
|
338
|
+
result: cached.result,
|
|
339
|
+
tsconfig: cached.tsconfig,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
if (!(error instanceof TtscMissingProgramOutputError)) {
|
|
344
|
+
notifyFailedGenerationInputs(hooks, cached);
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
// The compile is fine and simply has nothing for this module, so the
|
|
348
|
+
// module goes back to the host untransformed rather than failing the
|
|
349
|
+
// build (samchon/ttsc#1308). It still counts as delivered in this
|
|
350
|
+
// pass, and there is nothing to watch for a file with no output.
|
|
351
|
+
reportMissingProgramOutput(cached, error, epoch);
|
|
352
|
+
markCachedSourceServed(cached, file);
|
|
353
|
+
return undefined;
|
|
354
|
+
}
|
|
219
355
|
notifyWatchInputs(hooks, cached, file);
|
|
220
356
|
markCachedSourceServed(cached, file);
|
|
221
357
|
return createTransformResult(source, code);
|
|
@@ -235,6 +371,10 @@ async function transformTtsc(id, source, options, aliases, cache, hooks) {
|
|
|
235
371
|
compilerOptions: options.compilerOptions,
|
|
236
372
|
currentFile: file,
|
|
237
373
|
currentSource: source,
|
|
374
|
+
// Stamp the pass this compile was started for, not the one it happens
|
|
375
|
+
// to finish in: a boundary crossed mid-compile leaves the generation
|
|
376
|
+
// belonging to the earlier pass, so the next pass re-proves it.
|
|
377
|
+
deliveryEpoch: epoch,
|
|
238
378
|
filesystem,
|
|
239
379
|
plugins: options.plugins,
|
|
240
380
|
trackProjectMembership: cache !== undefined,
|
|
@@ -248,12 +388,25 @@ async function transformTtsc(id, source, options, aliases, cache, hooks) {
|
|
|
248
388
|
continue;
|
|
249
389
|
}
|
|
250
390
|
const { projectRoot, result } = cached;
|
|
251
|
-
reportSuccessDiagnostics(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
391
|
+
reportSuccessDiagnostics(cached, epoch);
|
|
392
|
+
let code;
|
|
393
|
+
try {
|
|
394
|
+
code = selectOrEvict(cache, key, generation, epoch, {
|
|
395
|
+
file,
|
|
396
|
+
projectRoot,
|
|
397
|
+
result,
|
|
398
|
+
tsconfig: cached.tsconfig,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
catch (error) {
|
|
402
|
+
if (!(error instanceof TtscMissingProgramOutputError)) {
|
|
403
|
+
notifyFailedGenerationInputs(hooks, cached);
|
|
404
|
+
throw error;
|
|
405
|
+
}
|
|
406
|
+
reportMissingProgramOutput(cached, error, epoch);
|
|
407
|
+
markCachedSourceServed(cached, file);
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
257
410
|
notifyWatchInputs(hooks, cached, file);
|
|
258
411
|
markCachedSourceServed(cached, file);
|
|
259
412
|
if (isVolatileFile(envelopeDerivation(cached), { file, projectRoot, result })) {
|
|
@@ -289,20 +442,107 @@ async function awaitOrEvict(cache, key, generation) {
|
|
|
289
442
|
}
|
|
290
443
|
}
|
|
291
444
|
/**
|
|
292
|
-
* Extract the transformed source,
|
|
293
|
-
*
|
|
294
|
-
* {@link selectTransformedSource}
|
|
295
|
-
*
|
|
445
|
+
* Extract the transformed source, and decide what a throwing generation is.
|
|
446
|
+
*
|
|
447
|
+
* {@link selectTransformedSource} throws for two different reasons, and only one
|
|
448
|
+
* of them is about the generation. A host `"exception"` or a compiler
|
|
449
|
+
* `"failure"` means the compile produced nothing for anyone: inside a pass that
|
|
450
|
+
* verdict is retained and replayed, because evicting it made every remaining
|
|
451
|
+
* module repeat the whole-project transform only to reach the identical answer
|
|
452
|
+
* (samchon/ttsc#1303), and outside a pass it keeps being evicted so a
|
|
453
|
+
* long-lived worker retries on its very next delivery exactly as before.
|
|
454
|
+
*
|
|
455
|
+
* A `"success"` envelope that has no output for the module asking is the other
|
|
456
|
+
* reason, and it is a fact about that one file: an ordinary condition for a
|
|
457
|
+
* module the bundle reaches and the tsconfig program does not contain. It is
|
|
458
|
+
* neither retained nor evicted. The error reaches the caller, and the
|
|
459
|
+
* generation, which compiled perfectly well for every other module, stays.
|
|
296
460
|
*/
|
|
297
|
-
function selectOrEvict(cache, key, generation, props) {
|
|
461
|
+
function selectOrEvict(cache, key, generation, epoch, props) {
|
|
298
462
|
try {
|
|
299
463
|
return selectTransformedSource(props);
|
|
300
464
|
}
|
|
301
465
|
catch (error) {
|
|
302
|
-
|
|
466
|
+
const verdict = retainPassVerdict(cache, key, generation, epoch, props.result, error);
|
|
467
|
+
if (verdict !== undefined) {
|
|
468
|
+
throw verdict;
|
|
469
|
+
}
|
|
470
|
+
// A generation that compiled fine and simply has no output for the module
|
|
471
|
+
// asking is not a failed generation. Discarding it made every later module
|
|
472
|
+
// recompile the whole project to reach the same answer, which is the cost
|
|
473
|
+
// samchon/ttsc#1303 is about, for a bundle that merely reaches a file the
|
|
474
|
+
// tsconfig program does not contain.
|
|
475
|
+
if (props.result.type !== "success") {
|
|
476
|
+
evictGeneration(cache, key, generation);
|
|
477
|
+
}
|
|
303
478
|
throw error;
|
|
304
479
|
}
|
|
305
480
|
}
|
|
481
|
+
/**
|
|
482
|
+
* Retain the verdict of a compile this pass already attempted, or return
|
|
483
|
+
* `undefined` when nothing may be retained.
|
|
484
|
+
*
|
|
485
|
+
* Only inside a delivery pass. A pass is the window in which every delivery is
|
|
486
|
+
* settled against the state the pass started from, so an attempt it already
|
|
487
|
+
* made is part of that state and the remaining modules replay it rather than
|
|
488
|
+
* each repeating a whole-project transform to reach the same answer. Outside a
|
|
489
|
+
* pass there is no such window, and a long-lived worker must keep retrying on
|
|
490
|
+
* its very next delivery so a transient host failure never becomes permanent.
|
|
491
|
+
*/
|
|
492
|
+
function retainPassVerdict(cache, key, generation, epoch, result, error) {
|
|
493
|
+
// Only an envelope that failed outright is a statement about the generation.
|
|
494
|
+
// `selectTransformedSource` also throws for a file the compile simply has no
|
|
495
|
+
// output for, which is an ordinary condition for a module the bundle reaches
|
|
496
|
+
// but the tsconfig program does not contain, and which says nothing about the
|
|
497
|
+
// other modules. Retaining that would fail the whole pass, naming a file none
|
|
498
|
+
// of them asked about.
|
|
499
|
+
if (result.type === "success" ||
|
|
500
|
+
epoch === undefined ||
|
|
501
|
+
cache?.get(key) !== generation) {
|
|
502
|
+
return undefined;
|
|
503
|
+
}
|
|
504
|
+
const existing = TERMINAL_TRANSFORM_GENERATIONS.get(generation);
|
|
505
|
+
if (existing !== undefined) {
|
|
506
|
+
return existing;
|
|
507
|
+
}
|
|
508
|
+
const verdict = new TtscPassVerdictError(error, epoch);
|
|
509
|
+
TERMINAL_TRANSFORM_GENERATIONS.set(generation, verdict);
|
|
510
|
+
return verdict;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Whether a terminal verdict still answers for this delivery.
|
|
514
|
+
*
|
|
515
|
+
* Inside the pass that produced or confirmed it, it is replayed without
|
|
516
|
+
* re-probing anything: the pass settles every delivery against the state it
|
|
517
|
+
* started from, so re-walking the project once per module would spend exactly
|
|
518
|
+
* the cost this gate exists to remove.
|
|
519
|
+
*
|
|
520
|
+
* Across passes the two kinds part company. A pass verdict is dropped, because
|
|
521
|
+
* a new pass is the first boundary at which the host itself claims something
|
|
522
|
+
* may have changed, and the compile it stood for was never proven against a
|
|
523
|
+
* recorded environment. An unstable generation was, so it keeps its own rule:
|
|
524
|
+
* one fresh attempt per pass, and otherwise replayed until that recorded
|
|
525
|
+
* environment provably moves.
|
|
526
|
+
*/
|
|
527
|
+
function replaysTerminalGeneration(terminal, epoch, props) {
|
|
528
|
+
if (terminal instanceof TtscPassVerdictError) {
|
|
529
|
+
// A pass verdict has no recorded environment to re-confirm against, so the
|
|
530
|
+
// pass that produced it is its whole scope.
|
|
531
|
+
return epoch !== undefined && terminal.epoch === epoch;
|
|
532
|
+
}
|
|
533
|
+
if (!(terminal instanceof TtscUnstableGenerationError)) {
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
// An unstable generation does have one, and confirming it per delivery is the
|
|
537
|
+
// behaviour its own contract describes, so the pass does not cache that
|
|
538
|
+
// answer. A new pass still grants the fresh attempt the per-pass cache clear
|
|
539
|
+
// used to give it.
|
|
540
|
+
if (epoch !== undefined &&
|
|
541
|
+
terminal.validation.cached.deliveryEpoch !== epoch) {
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
return !failedGenerationEnvironmentChanged(terminal.validation, props);
|
|
545
|
+
}
|
|
306
546
|
/**
|
|
307
547
|
* Delete a failed generation from the cache only when it is still the entry
|
|
308
548
|
* stored under `key`. The identity check prevents an older failed generation's
|
|
@@ -546,6 +786,47 @@ function collectDeclaredIdentities(state, projectRoot, listed) {
|
|
|
546
786
|
* transform scratch tree (see
|
|
547
787
|
* {@link TtscCachedProjectTransform.scratchDirectory}).
|
|
548
788
|
*/
|
|
789
|
+
/**
|
|
790
|
+
* Register the failed generation's own project inputs so the host can observe
|
|
791
|
+
* the fix.
|
|
792
|
+
*
|
|
793
|
+
* A successful delivery registers the derived watch inputs, which is how a
|
|
794
|
+
* type-only file that no bundler graph contains still invalidates the modules
|
|
795
|
+
* depending on it. A failed one used to register nothing: `selectWatchInputs`
|
|
796
|
+
* returns an empty list for an `"exception"` envelope, and the throw happens
|
|
797
|
+
* before `notifyWatchInputs` is reached at all. When the failing compile is the
|
|
798
|
+
* first of a watching session, that leaves no channel through which the fix can
|
|
799
|
+
* arrive: the user repairs a file the bundler does not track, nothing is
|
|
800
|
+
* invalidated, and the error stays on screen (samchon/ttsc#1312).
|
|
801
|
+
*
|
|
802
|
+
* The generation records the project walk even when the compile failed, so the
|
|
803
|
+
* files a fix would touch are exactly what it already holds. The cost is paid
|
|
804
|
+
* only on a failure, and only until the next compile succeeds and narrows the
|
|
805
|
+
* set back to the derived inputs.
|
|
806
|
+
*/
|
|
807
|
+
function notifyFailedGenerationInputs(hooks, cached) {
|
|
808
|
+
const addWatchFile = hooks?.addWatchFile;
|
|
809
|
+
if (addWatchFile === undefined) {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
for (const key of Object.keys(cached.inputHashes)) {
|
|
813
|
+
const input = path.resolve(cached.projectRoot, key);
|
|
814
|
+
if (isTransformScratchInput(input, cached.scratchDirectory)) {
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
// No evidence argument, deliberately. `missing: false` would be a claim
|
|
818
|
+
// this path cannot back: a failed generation is replayed for the rest of
|
|
819
|
+
// its pass without re-proving its inputs, so the walk that recorded them
|
|
820
|
+
// may be older than the delivery, and one of them having been deleted is a
|
|
821
|
+
// live reason for that compile to have failed. Letting the adapter probe
|
|
822
|
+
// also routes an absent input to the missing-input poll, which is the only
|
|
823
|
+
// channel through which restoring it can invalidate anything: a bundler
|
|
824
|
+
// watch on a path that does not exist registers nothing, and no module
|
|
825
|
+
// graph carries a type-only input. It costs one `existsSync` per input,
|
|
826
|
+
// and only where the adapter reads evidence at all, which is Vite serve.
|
|
827
|
+
addWatchFile(input);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
549
830
|
function notifyWatchInputs(hooks, cached, file) {
|
|
550
831
|
const addWatchFile = hooks?.addWatchFile;
|
|
551
832
|
if (addWatchFile === undefined) {
|
|
@@ -924,16 +1205,17 @@ function createTransformResult(source, code) {
|
|
|
924
1205
|
* state.
|
|
925
1206
|
*
|
|
926
1207
|
* Always compares the current module's in-memory source with the generation
|
|
927
|
-
* snapshot. A cache
|
|
928
|
-
*
|
|
929
|
-
*
|
|
930
|
-
*
|
|
931
|
-
*
|
|
932
|
-
*
|
|
933
|
-
*
|
|
1208
|
+
* snapshot. A cache with a delivery epoch can use that comparison alone for a
|
|
1209
|
+
* stable generation's first delivery of each module in the current pass, once
|
|
1210
|
+
* the pass's own first delivery has proven the whole generation still matches
|
|
1211
|
+
* the filesystem. An incomplete generation may not take this shortcut:
|
|
1212
|
+
* otherwise a sibling output captured during a filesystem race could still be
|
|
1213
|
+
* served once. Later graph-bearing requests validate the file's derived input
|
|
1214
|
+
* set and project membership; graph-free envelopes conservatively re-hash the
|
|
1215
|
+
* complete project and out-of-walk snapshots. Any mismatch forces a complete
|
|
934
1216
|
* re-transform.
|
|
935
1217
|
*/
|
|
936
|
-
function matchesCachedSource(cached, file, source,
|
|
1218
|
+
function matchesCachedSource(cached, file, source, epoch) {
|
|
937
1219
|
const identities = envelopeDerivation(cached).identityContext;
|
|
938
1220
|
const currentKey = toProjectKey(cached.projectRoot, file, identities);
|
|
939
1221
|
const identity = pathIdentityKey(file, identities);
|
|
@@ -943,10 +1225,24 @@ function matchesCachedSource(cached, file, source, buildScoped) {
|
|
|
943
1225
|
if (expected !== hashText(source)) {
|
|
944
1226
|
return false;
|
|
945
1227
|
}
|
|
946
|
-
if (
|
|
947
|
-
cached.
|
|
948
|
-
|
|
949
|
-
|
|
1228
|
+
if (epoch !== undefined && cached.projectSnapshotComplete === true) {
|
|
1229
|
+
if (cached.deliveryEpoch !== epoch) {
|
|
1230
|
+
// The pass's first delivery. The generation was settled against an
|
|
1231
|
+
// earlier pass, so prove the whole of it once — every input the envelope
|
|
1232
|
+
// declares, the directory membership, the universal host inputs, and the
|
|
1233
|
+
// out-of-walk snapshot — before any of this pass's deliveries may be
|
|
1234
|
+
// settled against it. That proof is what a per-pass recompile used to buy
|
|
1235
|
+
// (samchon/ttsc#1300), at a walk instead of a compile.
|
|
1236
|
+
if (!matchesCompleteInputSnapshot(cached, currentKey, source)) {
|
|
1237
|
+
return false;
|
|
1238
|
+
}
|
|
1239
|
+
cached.deliveryEpoch = epoch;
|
|
1240
|
+
cached.servedFiles?.clear();
|
|
1241
|
+
return true;
|
|
1242
|
+
}
|
|
1243
|
+
if (!cached.servedFiles?.has(identity)) {
|
|
1244
|
+
return true;
|
|
1245
|
+
}
|
|
950
1246
|
}
|
|
951
1247
|
if (cached.result.type !== "exception" &&
|
|
952
1248
|
cached.result.graph !== undefined &&
|
|
@@ -1654,7 +1950,12 @@ function matchesCompleteInputSnapshot(cached, currentKey, source) {
|
|
|
1654
1950
|
const declaredInputs = declaredProjectInputKeys(state, cached);
|
|
1655
1951
|
const current = collectProjectInputSnapshot(cached.projectRoot, state.identityContext, resultFilesystem(cached.result), cached.inputSignatures === undefined
|
|
1656
1952
|
? undefined
|
|
1657
|
-
: { hashes: cached.inputHashes, signatures: cached.inputSignatures }
|
|
1953
|
+
: { hashes: cached.inputHashes, signatures: cached.inputSignatures }, {
|
|
1954
|
+
// Judge membership by the rule the compile ran under, and read only the
|
|
1955
|
+
// inputs this comparison actually consults.
|
|
1956
|
+
declaredKeys: declaredInputs,
|
|
1957
|
+
policy: cached.membershipPolicy,
|
|
1958
|
+
});
|
|
1658
1959
|
if (!walkSnapshotComplete(current, declaredInputs)) {
|
|
1659
1960
|
return false;
|
|
1660
1961
|
}
|
|
@@ -1945,24 +2246,36 @@ function markCachedSourceServed(cached, file) {
|
|
|
1945
2246
|
* slash path. Exported so hosts without a per-build boundary (`@ttsc/metro`)
|
|
1946
2247
|
* can fold the identical input universe into their own cache fingerprints.
|
|
1947
2248
|
*/
|
|
1948
|
-
function collectProjectInputHashes(projectRoot, identities = createHostPathIdentityContext(), filesystem = DEFAULT_FILESYSTEM_OPERATIONS) {
|
|
1949
|
-
return collectProjectInputSnapshot(projectRoot, identities, filesystem
|
|
1950
|
-
|
|
2249
|
+
function collectProjectInputHashes(projectRoot, identities = createHostPathIdentityContext(), filesystem = DEFAULT_FILESYSTEM_OPERATIONS, policy) {
|
|
2250
|
+
return collectProjectInputSnapshot(projectRoot, identities, filesystem, undefined, {
|
|
2251
|
+
policy,
|
|
2252
|
+
}).hashes;
|
|
1951
2253
|
}
|
|
1952
2254
|
/** Hash project files and snapshot the directory topology in one walk. */
|
|
1953
|
-
function collectProjectInputSnapshot(projectRoot, identities, filesystem = DEFAULT_FILESYSTEM_OPERATIONS, proven) {
|
|
2255
|
+
function collectProjectInputSnapshot(projectRoot, identities, filesystem = DEFAULT_FILESYSTEM_OPERATIONS, proven, options) {
|
|
1954
2256
|
const hashes = {};
|
|
1955
2257
|
const fileSignatures = {};
|
|
1956
2258
|
const provenSignatures = {};
|
|
1957
2259
|
const unstableFiles = new Set();
|
|
1958
2260
|
let attributed = true;
|
|
1959
|
-
const walked = walkProjectInputs(projectRoot, filesystem);
|
|
2261
|
+
const walked = walkProjectInputs(projectRoot, filesystem, options?.policy);
|
|
1960
2262
|
const walkFailures = [...walked.failures];
|
|
1961
2263
|
let complete = walked.complete;
|
|
1962
2264
|
for (const file of walked.files) {
|
|
1963
2265
|
try {
|
|
1964
|
-
const before = inputMetadataEvidence(file, filesystem);
|
|
1965
2266
|
const key = toProjectKey(projectRoot, file, identities);
|
|
2267
|
+
// A caller validating a generation compares hashes over that
|
|
2268
|
+
// generation's declared inputs alone (`sameHashes` takes the declared key
|
|
2269
|
+
// set), so reading anything else is work whose result is never consulted.
|
|
2270
|
+
// Skipping it is what keeps a directory full of emitted files from
|
|
2271
|
+
// costing a read per file on the pass that first sees them
|
|
2272
|
+
// (samchon/ttsc#1307). Capture passes supply no restriction and still
|
|
2273
|
+
// record the whole walk.
|
|
2274
|
+
if (options?.declaredKeys !== undefined &&
|
|
2275
|
+
!options.declaredKeys.has(key)) {
|
|
2276
|
+
continue;
|
|
2277
|
+
}
|
|
2278
|
+
const before = inputMetadataEvidence(file, filesystem);
|
|
1966
2279
|
// A file whose signature still equals the one captured around the read
|
|
1967
2280
|
// that produced the recorded hash carries that content, so the whole
|
|
1968
2281
|
// project does not have to be re-read to prove one delivery. A signature
|
|
@@ -2025,18 +2338,22 @@ function collectProjectInputSnapshot(projectRoot, identities, filesystem = DEFAU
|
|
|
2025
2338
|
};
|
|
2026
2339
|
}
|
|
2027
2340
|
/**
|
|
2028
|
-
* Enumerate every regular file under `root`, skipping
|
|
2029
|
-
*
|
|
2341
|
+
* Enumerate every regular file under `root`, skipping the directories no
|
|
2342
|
+
* configuration can name ({@link isIgnoredProjectDirectory}) and the ones the
|
|
2343
|
+
* resolved configuration excludes ({@link isExcludedProjectDirectory}).
|
|
2030
2344
|
*
|
|
2031
2345
|
* Uses an iterative DFS instead of `fs.readdirSync` recursion to avoid
|
|
2032
2346
|
* unbounded call-stack depth on deep project trees. The result is sorted so
|
|
2033
2347
|
* that hash comparisons are deterministic across OS-level directory orderings.
|
|
2034
2348
|
*/
|
|
2035
|
-
function walkProjectInputs(root, filesystem = DEFAULT_FILESYSTEM_OPERATIONS) {
|
|
2349
|
+
function walkProjectInputs(root, filesystem = DEFAULT_FILESYSTEM_OPERATIONS, policy = tsconfigPaths.PERMISSIVE_PROJECT_MEMBERSHIP_POLICY) {
|
|
2036
2350
|
let complete = true;
|
|
2037
|
-
const directories = [];
|
|
2038
2351
|
const failures = [];
|
|
2039
2352
|
const files = [];
|
|
2353
|
+
// Collected in one pass, then digested in a second. A directory's digest has
|
|
2354
|
+
// to know whether each child directory can hold program inputs, and the walk
|
|
2355
|
+
// learns that only after descending, so the two cannot be one pass.
|
|
2356
|
+
const visited = [];
|
|
2040
2357
|
const stack = [root];
|
|
2041
2358
|
while (stack.length !== 0) {
|
|
2042
2359
|
const current = stack.pop();
|
|
@@ -2068,33 +2385,99 @@ function walkProjectInputs(root, filesystem = DEFAULT_FILESYSTEM_OPERATIONS) {
|
|
|
2068
2385
|
path: current,
|
|
2069
2386
|
});
|
|
2070
2387
|
}
|
|
2071
|
-
|
|
2388
|
+
const visit = {
|
|
2389
|
+
childDirectories: [],
|
|
2390
|
+
entries: [],
|
|
2391
|
+
ownInput: false,
|
|
2072
2392
|
path: current,
|
|
2073
2393
|
// If membership moved during enumeration, force the next delivery to
|
|
2074
2394
|
// replace this generation instead of blessing a torn directory/file
|
|
2075
2395
|
// snapshot as stable.
|
|
2076
|
-
|
|
2077
|
-
?
|
|
2396
|
+
stable: after !== undefined && before === after
|
|
2397
|
+
? undefined
|
|
2078
2398
|
: `unstable:${before}:${after ?? "missing"}`,
|
|
2079
|
-
}
|
|
2399
|
+
};
|
|
2080
2400
|
for (const entry of entries) {
|
|
2081
2401
|
if (isIgnoredProjectDirectory(entry.name)) {
|
|
2082
2402
|
continue;
|
|
2083
2403
|
}
|
|
2084
2404
|
const file = path.join(current, entry.name);
|
|
2405
|
+
if (entry.isDirectory() && isExcludedProjectDirectory(file, policy)) {
|
|
2406
|
+
continue;
|
|
2407
|
+
}
|
|
2408
|
+
const possible = isPossibleProgramEntry(entry, policy);
|
|
2409
|
+
visit.entries.push({
|
|
2410
|
+
kind: [
|
|
2411
|
+
entry.isDirectory(),
|
|
2412
|
+
entry.isFile(),
|
|
2413
|
+
entry.isSymbolicLink(),
|
|
2414
|
+
].join(":"),
|
|
2415
|
+
name: entry.name,
|
|
2416
|
+
possible,
|
|
2417
|
+
});
|
|
2085
2418
|
if (entry.isDirectory()) {
|
|
2419
|
+
visit.childDirectories.push(file);
|
|
2086
2420
|
stack.push(file);
|
|
2087
2421
|
}
|
|
2088
|
-
else if (entry.isFile()) {
|
|
2422
|
+
else if (entry.isFile() && possible) {
|
|
2423
|
+
// Only a file that could enter the program is hashed. A file that
|
|
2424
|
+
// could not is either irrelevant to every generation, or it is one the
|
|
2425
|
+
// compiler actually read, in which case the graph reports it and
|
|
2426
|
+
// `isProjectWalkPath` now agrees it is out of the walk, so it is
|
|
2427
|
+
// recorded and proven by the out-of-walk snapshot instead. Hashing an
|
|
2428
|
+
// emitted tree here bought nothing and cost a read per file, including
|
|
2429
|
+
// in `@ttsc/metro`, whose fingerprint re-keys every transformed file
|
|
2430
|
+
// (samchon/ttsc#1307).
|
|
2089
2431
|
files.push(file);
|
|
2432
|
+
visit.ownInput = true;
|
|
2090
2433
|
}
|
|
2091
2434
|
}
|
|
2435
|
+
visited.push(visit);
|
|
2436
|
+
}
|
|
2437
|
+
// A directory matters to program membership only if its subtree can hold a
|
|
2438
|
+
// program input. Propagate that up from the directories that hold one, so a
|
|
2439
|
+
// bundler creating `out/` and filling it with JavaScript a project admitting
|
|
2440
|
+
// none can never compile is not a membership change at any level: not in the
|
|
2441
|
+
// directory itself, and not in the parent that now lists it
|
|
2442
|
+
// (samchon/ttsc#1307).
|
|
2443
|
+
const byPath = new Map(visited.map((visit) => [visit.path, visit]));
|
|
2444
|
+
const relevant = new Set();
|
|
2445
|
+
for (const visit of visited) {
|
|
2446
|
+
if (!visit.ownInput) {
|
|
2447
|
+
continue;
|
|
2448
|
+
}
|
|
2449
|
+
let current = visit.path;
|
|
2450
|
+
while (current !== undefined && !relevant.has(current)) {
|
|
2451
|
+
relevant.add(current);
|
|
2452
|
+
const parent = path.dirname(current);
|
|
2453
|
+
current = parent === current || !byPath.has(parent) ? undefined : parent;
|
|
2454
|
+
}
|
|
2092
2455
|
}
|
|
2456
|
+
const directories = visited.map((visit) => {
|
|
2457
|
+
const membership = visit.entries
|
|
2458
|
+
.filter((entry) => entry.possible &&
|
|
2459
|
+
(!visit.childDirectories.includes(path.join(visit.path, entry.name)) ||
|
|
2460
|
+
relevant.has(path.join(visit.path, entry.name))))
|
|
2461
|
+
.map((entry) => `${entry.name}:${entry.kind}`);
|
|
2462
|
+
return {
|
|
2463
|
+
path: visit.path,
|
|
2464
|
+
relevant: relevant.has(visit.path),
|
|
2465
|
+
signature: visit.stable ??
|
|
2466
|
+
hashText(membership.sort().join(String.fromCharCode(0))),
|
|
2467
|
+
};
|
|
2468
|
+
});
|
|
2093
2469
|
directories.sort((left, right) => left.path.localeCompare(right.path));
|
|
2094
2470
|
files.sort();
|
|
2095
2471
|
return { complete, directories, failures, files };
|
|
2096
2472
|
}
|
|
2097
|
-
/**
|
|
2473
|
+
/**
|
|
2474
|
+
* Return a directory's metadata stamp, used to detect that its membership moved
|
|
2475
|
+
* _while_ the walk was enumerating it, and to feed the observed-clock floor.
|
|
2476
|
+
*
|
|
2477
|
+
* This is the right instrument for that job and the wrong one for comparing two
|
|
2478
|
+
* generations: it moves for ignored entries too. {@link walkProjectInputs}
|
|
2479
|
+
* records the filtered membership digest for the comparison instead.
|
|
2480
|
+
*/
|
|
2098
2481
|
function projectDirectorySignature(directory, filesystem = DEFAULT_FILESYSTEM_OPERATIONS) {
|
|
2099
2482
|
try {
|
|
2100
2483
|
const stats = filesystem.statBigInt(directory);
|
|
@@ -2119,9 +2502,24 @@ function projectDirectorySignature(directory, filesystem = DEFAULT_FILESYSTEM_OP
|
|
|
2119
2502
|
}
|
|
2120
2503
|
/** Compare two deterministic project-directory membership snapshots. */
|
|
2121
2504
|
function sameProjectDirectories(left, right) {
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2505
|
+
// Compare only the directories that can hold program inputs, on either side.
|
|
2506
|
+
// A directory irrelevant on both is not part of the program's membership at
|
|
2507
|
+
// all, so its appearance, disappearance or churn says nothing: that is a
|
|
2508
|
+
// bundler's output tree. One that gained or lost relevance is present in the
|
|
2509
|
+
// comparison from the side where it counts, and so is caught.
|
|
2510
|
+
const select = (snapshots) => new Map(snapshots
|
|
2511
|
+
.filter((directory) => directory.relevant)
|
|
2512
|
+
.map((directory) => [directory.path, directory]));
|
|
2513
|
+
const leftRelevant = select(left);
|
|
2514
|
+
const rightRelevant = select(right);
|
|
2515
|
+
const paths = new Set([...leftRelevant.keys(), ...rightRelevant.keys()]);
|
|
2516
|
+
for (const location of paths) {
|
|
2517
|
+
if (leftRelevant.get(location)?.signature !==
|
|
2518
|
+
rightRelevant.get(location)?.signature) {
|
|
2519
|
+
return false;
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
return true;
|
|
2125
2523
|
}
|
|
2126
2524
|
/**
|
|
2127
2525
|
* Open one directory's change notification through the cache-owned watch seam,
|
|
@@ -2138,7 +2536,7 @@ function openDirectoryWatch(filesystem, directory, listener, onError) {
|
|
|
2138
2536
|
return { close: () => watcher.close() };
|
|
2139
2537
|
}
|
|
2140
2538
|
/** Watch every walked directory for membership changes after generation. */
|
|
2141
|
-
async function createProjectMutationTracker(directories, filesystem = DEFAULT_FILESYSTEM_OPERATIONS) {
|
|
2539
|
+
async function createProjectMutationTracker(directories, filesystem = DEFAULT_FILESYSTEM_OPERATIONS, policy = tsconfigPaths.PERMISSIVE_PROJECT_MEMBERSHIP_POLICY) {
|
|
2142
2540
|
const tracker = {
|
|
2143
2541
|
changes: new Set(),
|
|
2144
2542
|
changesOmitted: false,
|
|
@@ -2147,7 +2545,7 @@ async function createProjectMutationTracker(directories, filesystem = DEFAULT_FI
|
|
|
2147
2545
|
membershipChanged: false,
|
|
2148
2546
|
};
|
|
2149
2547
|
if (process.platform === "win32" && filesystem.watch === undefined) {
|
|
2150
|
-
await registerWindowsProjectMutationTracker(tracker, directories.map((directory) => ({ directory: directory.path })), false, filesystem);
|
|
2548
|
+
await registerWindowsProjectMutationTracker(tracker, directories.map((directory) => ({ directory: directory.path })), false, filesystem, (location, filename) => reportsProgramMembership(path.join(location, filename), filename, policy, filesystem));
|
|
2151
2549
|
return tracker;
|
|
2152
2550
|
}
|
|
2153
2551
|
const watchers = [];
|
|
@@ -2159,11 +2557,16 @@ async function createProjectMutationTracker(directories, filesystem = DEFAULT_FI
|
|
|
2159
2557
|
for (const directory of directories) {
|
|
2160
2558
|
try {
|
|
2161
2559
|
watchers.push(openDirectoryWatch(filesystem, directory.path, (eventType, filename) => {
|
|
2162
|
-
if (eventType
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2560
|
+
if (eventType !== "rename") {
|
|
2561
|
+
return;
|
|
2562
|
+
}
|
|
2563
|
+
if (filename !== null &&
|
|
2564
|
+
!reportsProgramMembership(path.join(directory.path, filename), filename, policy, filesystem)) {
|
|
2565
|
+
return;
|
|
2166
2566
|
}
|
|
2567
|
+
recordProjectMutation(tracker, filename === null
|
|
2568
|
+
? directory.path
|
|
2569
|
+
: path.join(directory.path, filename));
|
|
2167
2570
|
}, () => {
|
|
2168
2571
|
tracker.failed = true;
|
|
2169
2572
|
}));
|
|
@@ -2248,6 +2651,80 @@ async function createHostInputMutationTracker(inputs, filesystem, covered, event
|
|
|
2248
2651
|
}
|
|
2249
2652
|
return tracker;
|
|
2250
2653
|
}
|
|
2654
|
+
/**
|
|
2655
|
+
* Whether a path lies inside a directory the configuration excludes.
|
|
2656
|
+
*
|
|
2657
|
+
* Lexical, exactly like the walk and like {@link isProjectWalkPath}, and for the
|
|
2658
|
+
* reason that predicate states: walk membership is lexical, so resolving a path
|
|
2659
|
+
* to physical identity first would collapse two spellings the walk keeps apart
|
|
2660
|
+
* and claim it covered a subtree it never followed. A junction whose target the
|
|
2661
|
+
* walk hashes under its own name is exactly that, and canonicalizing here would
|
|
2662
|
+
* suppress every event in it.
|
|
2663
|
+
*
|
|
2664
|
+
* `strictly` excludes an exact match, for the case where the excluded entry
|
|
2665
|
+
* names a file rather than a directory: `exclude` accepts one, the walk applies
|
|
2666
|
+
* exclusion to directories alone, so that file is still hashed and its events
|
|
2667
|
+
* must keep counting.
|
|
2668
|
+
*/
|
|
2669
|
+
function insideExcludedProjectDirectory(location, policy, strictly) {
|
|
2670
|
+
if (policy.excludedDirectories.length === 0) {
|
|
2671
|
+
return false;
|
|
2672
|
+
}
|
|
2673
|
+
const resolved = path.resolve(location);
|
|
2674
|
+
return policy.excludedDirectories.some((excluded) => {
|
|
2675
|
+
const target = path.resolve(excluded);
|
|
2676
|
+
if (strictly && target === resolved) {
|
|
2677
|
+
return false;
|
|
2678
|
+
}
|
|
2679
|
+
return pathIsWithin(resolved, target);
|
|
2680
|
+
});
|
|
2681
|
+
}
|
|
2682
|
+
/**
|
|
2683
|
+
* Whether one directory event can be a change to the program's membership.
|
|
2684
|
+
*
|
|
2685
|
+
* The live tracker has to answer the same question the membership digest does,
|
|
2686
|
+
* or the two disagree about the same project: a bundler writing content-hashed
|
|
2687
|
+
* output fires a rename per rebuild, and treating that as membership kept the
|
|
2688
|
+
* cost samchon/ttsc#1307 removes on every host that has no build boundary,
|
|
2689
|
+
* which is every host the narrow path exists for.
|
|
2690
|
+
*
|
|
2691
|
+
* A name that could be a program input counts, unless it sits under a directory
|
|
2692
|
+
* the walk never descends into. A name that could not still counts when the
|
|
2693
|
+
* path is now a directory, because the walk's watches were opened for the
|
|
2694
|
+
* directories that existed when the generation was captured, so a directory
|
|
2695
|
+
* created since is not watched and the sources that may appear in it would
|
|
2696
|
+
* otherwise be invisible. A directory the configuration excludes is the
|
|
2697
|
+
* exception: the walk cannot see inside it, so the tracker must not either, or
|
|
2698
|
+
* emptying and recreating an `outDir` costs a compile per build. An event whose
|
|
2699
|
+
* name the host did not report is unattributable and always counts.
|
|
2700
|
+
*/
|
|
2701
|
+
function reportsProgramMembership(location, filename, policy, filesystem) {
|
|
2702
|
+
if (isPossibleProgramFileName(filename, policy)) {
|
|
2703
|
+
// A name the program could admit. It still says nothing if it lies inside a
|
|
2704
|
+
// directory the walk never descends into, because the digest cannot see
|
|
2705
|
+
// there either and the tracker must not be the one side that reacts.
|
|
2706
|
+
return !insideExcludedProjectDirectory(location, policy, true);
|
|
2707
|
+
}
|
|
2708
|
+
let directory;
|
|
2709
|
+
try {
|
|
2710
|
+
directory = filesystem.lstat(location).isDirectory();
|
|
2711
|
+
}
|
|
2712
|
+
catch {
|
|
2713
|
+
// Gone again, or unreadable. Its name could not have been a program input,
|
|
2714
|
+
// and a directory removed under this one reports its own contents leaving
|
|
2715
|
+
// through the watch that was opened on it.
|
|
2716
|
+
return false;
|
|
2717
|
+
}
|
|
2718
|
+
if (!directory) {
|
|
2719
|
+
return false;
|
|
2720
|
+
}
|
|
2721
|
+
// A directory counts, because it can hold sources and the tracker is not
|
|
2722
|
+
// watching it yet, unless the configuration says the program does not contain
|
|
2723
|
+
// it. Emptying and recreating an `outDir`, which is what `emptyOutDir` and
|
|
2724
|
+
// `output.clean` do on every build, would otherwise void the generation once
|
|
2725
|
+
// per build on every host that has no build boundary.
|
|
2726
|
+
return !insideExcludedProjectDirectory(location, policy, false);
|
|
2727
|
+
}
|
|
2251
2728
|
/** Record enough exact mutation evidence without retaining an event stream. */
|
|
2252
2729
|
function recordProjectMutation(tracker, changed) {
|
|
2253
2730
|
tracker.membershipChanged = true;
|
|
@@ -2268,8 +2745,21 @@ let windowsProjectMutationBroker;
|
|
|
2268
2745
|
* temporary tree is deleted. Isolation turns that unrecoverable process abort
|
|
2269
2746
|
* into an ordinary broker exit and a conservative cache miss in the host.
|
|
2270
2747
|
*/
|
|
2271
|
-
async function registerWindowsProjectMutationTracker(tracker, locations, allEvents, filesystem
|
|
2748
|
+
async function registerWindowsProjectMutationTracker(tracker, locations, allEvents, filesystem,
|
|
2749
|
+
/**
|
|
2750
|
+
* Optional filter for the project-directory tracker, whose events have to be
|
|
2751
|
+
* narrowed to program membership exactly as the in-process watcher's are. The
|
|
2752
|
+
* name-watching trackers pass none, since they already watch exact names.
|
|
2753
|
+
*/
|
|
2754
|
+
membership) {
|
|
2272
2755
|
const broker = getWindowsProjectMutationBroker();
|
|
2756
|
+
// The child watches canonical directories, and reports its events under that
|
|
2757
|
+
// spelling. Everything else in the adapter speaks the walk's own spelling,
|
|
2758
|
+
// which on Windows can be an 8.3 short form of the same directory, so keep
|
|
2759
|
+
// the way back: a filter that compared the child's spelling against the
|
|
2760
|
+
// configuration's would be comparing two names for one directory that share
|
|
2761
|
+
// no common prefix (samchon/ttsc#1307).
|
|
2762
|
+
const spellings = new Map();
|
|
2273
2763
|
const normalized = locations.map((location) => {
|
|
2274
2764
|
let directory;
|
|
2275
2765
|
try {
|
|
@@ -2278,6 +2768,7 @@ async function registerWindowsProjectMutationTracker(tracker, locations, allEven
|
|
|
2278
2768
|
catch {
|
|
2279
2769
|
directory = path.resolve(location.directory);
|
|
2280
2770
|
}
|
|
2771
|
+
spellings.set(directory, location.directory);
|
|
2281
2772
|
return {
|
|
2282
2773
|
directory,
|
|
2283
2774
|
...(location.names === undefined ? {} : { names: location.names }),
|
|
@@ -2291,7 +2782,12 @@ async function registerWindowsProjectMutationTracker(tracker, locations, allEven
|
|
|
2291
2782
|
const ready = new Promise((resolve) => {
|
|
2292
2783
|
resolveReady = resolve;
|
|
2293
2784
|
});
|
|
2294
|
-
broker.trackers.set(id, {
|
|
2785
|
+
broker.trackers.set(id, {
|
|
2786
|
+
membership,
|
|
2787
|
+
ready: resolveReady,
|
|
2788
|
+
spellings,
|
|
2789
|
+
tracker,
|
|
2790
|
+
});
|
|
2295
2791
|
tracker.drain = () => drainWindowsProjectMutationBroker(broker);
|
|
2296
2792
|
tracker.close = () => {
|
|
2297
2793
|
const active = broker.trackers.get(id);
|
|
@@ -2386,9 +2882,17 @@ function getWindowsProjectMutationBroker() {
|
|
|
2386
2882
|
registration.ready();
|
|
2387
2883
|
if (record.ready !== true && record.failed !== true) {
|
|
2388
2884
|
if (typeof record.directory === "string") {
|
|
2885
|
+
// The walk's spelling for this directory, which is what every
|
|
2886
|
+
// comparison and every recorded witness downstream expects.
|
|
2887
|
+
const reported = registration.spellings.get(record.directory) ?? record.directory;
|
|
2888
|
+
if (typeof record.filename === "string" &&
|
|
2889
|
+
registration.membership !== undefined &&
|
|
2890
|
+
!registration.membership(reported, record.filename)) {
|
|
2891
|
+
return;
|
|
2892
|
+
}
|
|
2389
2893
|
recordProjectMutation(registration.tracker, typeof record.filename === "string"
|
|
2390
|
-
? path.join(
|
|
2391
|
-
:
|
|
2894
|
+
? path.join(reported, record.filename)
|
|
2895
|
+
: reported);
|
|
2392
2896
|
}
|
|
2393
2897
|
else {
|
|
2394
2898
|
registration.tracker.membershipChanged = true;
|
|
@@ -2567,7 +3071,7 @@ async function settleProjectMutationEvents(cached) {
|
|
|
2567
3071
|
* Missing paths and files reached through symlinks or Windows junctions are
|
|
2568
3072
|
* out-of-walk inputs that only the reference graph can prove relevant.
|
|
2569
3073
|
*/
|
|
2570
|
-
function isProjectWalkPath(root, file, _identities = createHostPathIdentityContext(), filesystem = DEFAULT_FILESYSTEM_OPERATIONS) {
|
|
3074
|
+
function isProjectWalkPath(root, file, _identities = createHostPathIdentityContext(), filesystem = DEFAULT_FILESYSTEM_OPERATIONS, policy = tsconfigPaths.PERMISSIVE_PROJECT_MEMBERSHIP_POLICY) {
|
|
2571
3075
|
// Walk membership is lexical. Resolving `file` to physical identity first
|
|
2572
3076
|
// would turn `root/alias/value.ts` into `root/target/value.ts`, hide the
|
|
2573
3077
|
// symlink segment from the lstat loop below, and falsely claim the project
|
|
@@ -2581,7 +3085,20 @@ function isProjectWalkPath(root, file, _identities = createHostPathIdentityConte
|
|
|
2581
3085
|
return false;
|
|
2582
3086
|
}
|
|
2583
3087
|
const segments = relative.split(path.sep);
|
|
2584
|
-
|
|
3088
|
+
// The last segment is the file itself, which the walk names rather than
|
|
3089
|
+
// descends into, so only the directory components decide walk membership.
|
|
3090
|
+
if (segments.slice(0, -1).some(isIgnoredProjectDirectory)) {
|
|
3091
|
+
return false;
|
|
3092
|
+
}
|
|
3093
|
+
if (isExcludedProjectDirectory(path.dirname(path.resolve(file)), policy)) {
|
|
3094
|
+
return false;
|
|
3095
|
+
}
|
|
3096
|
+
// The walk hashes only files that could enter the program, so a path it does
|
|
3097
|
+
// not hash is out of the walk by definition. Answering otherwise would leave
|
|
3098
|
+
// a graph input the compiler really read in neither snapshot: absent from
|
|
3099
|
+
// `inputHashes` because the walk skipped it, and absent from the out-of-walk
|
|
3100
|
+
// snapshot because this predicate claimed the walk covered it.
|
|
3101
|
+
if (!isPossibleProgramFileName(path.basename(file), policy)) {
|
|
2585
3102
|
return false;
|
|
2586
3103
|
}
|
|
2587
3104
|
let current = resolvedRoot;
|
|
@@ -2769,7 +3286,7 @@ function selectExternalInputPaths(props) {
|
|
|
2769
3286
|
isTransformScratchInput(absolute, props.scratchDirectory) ||
|
|
2770
3287
|
seen.has(spelling) ||
|
|
2771
3288
|
(!missingCandidate &&
|
|
2772
|
-
isProjectWalkPath(props.projectRoot, absolute, identities, filesystem))) {
|
|
3289
|
+
isProjectWalkPath(props.projectRoot, absolute, identities, filesystem, props.membershipPolicy))) {
|
|
2773
3290
|
continue;
|
|
2774
3291
|
}
|
|
2775
3292
|
// Preserve distinct lexical aliases even when they currently select the
|
|
@@ -2932,21 +3449,44 @@ function insideProject(directory, projectRoot) {
|
|
|
2932
3449
|
*/
|
|
2933
3450
|
const NOTIFIABLE_ABSENCE_DIRECTORY_LIMIT = 512;
|
|
2934
3451
|
function isIgnoredProjectDirectory(name) {
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
3452
|
+
// The residue of what used to be a fifteen-name list, kept to the three
|
|
3453
|
+
// directories no tsconfig can name and no program can contain: the VCS
|
|
3454
|
+
// store, the package manager's tree (TypeScript's own default `exclude`
|
|
3455
|
+
// carries it too), and ttsc's own plugin cache. Everything else the old list
|
|
3456
|
+
// guessed at, and guessing was wrong in both directions: a bundler writing
|
|
3457
|
+
// to an unnamed directory changed project membership with its own output,
|
|
3458
|
+
// while a real source directory named `build` or `temp` was dropped from the
|
|
3459
|
+
// walk and its new files were never seen (samchon/ttsc#1307). Those are now
|
|
3460
|
+
// decided by `ITtscProjectMembershipPolicy`, which reads the configuration
|
|
3461
|
+
// that actually knows.
|
|
3462
|
+
return name === ".git" || name === ".ttsc" || name === "node_modules";
|
|
3463
|
+
}
|
|
3464
|
+
/**
|
|
3465
|
+
* Whether the resolved configuration keeps this directory out of the program.
|
|
3466
|
+
*
|
|
3467
|
+
* Compared by physical containment rather than by name, so `outDir: "./dist"`
|
|
3468
|
+
* excludes that one directory instead of every directory called `dist` at every
|
|
3469
|
+
* depth, which is the distinction the name list could not draw.
|
|
3470
|
+
*/
|
|
3471
|
+
function isExcludedProjectDirectory(directory, policy) {
|
|
3472
|
+
return insideExcludedProjectDirectory(directory, policy, false);
|
|
3473
|
+
}
|
|
3474
|
+
/**
|
|
3475
|
+
* Whether this entry could enter the program, and so whether its appearance or
|
|
3476
|
+
* removal is a membership change.
|
|
3477
|
+
*
|
|
3478
|
+
* A directory always could, since it can hold sources. A file could only if it
|
|
3479
|
+
* carries an extension the resolved configuration admits, which is what makes a
|
|
3480
|
+
* bundle emitted beside the sources invisible to a project that compiles no
|
|
3481
|
+
* JavaScript.
|
|
3482
|
+
*/
|
|
3483
|
+
function isPossibleProgramEntry(entry, policy) {
|
|
3484
|
+
return entry.isFile() ? isPossibleProgramFileName(entry.name, policy) : true;
|
|
3485
|
+
}
|
|
3486
|
+
/** The same question for a bare file name, for callers holding no `Dirent`. */
|
|
3487
|
+
function isPossibleProgramFileName(name, policy) {
|
|
3488
|
+
const lowered = name.toLowerCase();
|
|
3489
|
+
return policy.inputExtensions.some((extension) => lowered.endsWith(extension));
|
|
2950
3490
|
}
|
|
2951
3491
|
/**
|
|
2952
3492
|
* Compare two project-walk snapshots.
|
|
@@ -3300,7 +3840,7 @@ function failedGenerationEnvironmentChanged(validation, props) {
|
|
|
3300
3840
|
expectedSourceHash !== currentSourceHash) {
|
|
3301
3841
|
return true;
|
|
3302
3842
|
}
|
|
3303
|
-
const current = collectProjectInputSnapshot(validation.cached.projectRoot, identities, props.filesystem);
|
|
3843
|
+
const current = collectProjectInputSnapshot(validation.cached.projectRoot, identities, props.filesystem, undefined, { policy: validation.cached.membershipPolicy });
|
|
3304
3844
|
if (validation.projectWalkComplete !==
|
|
3305
3845
|
walkSnapshotComplete(current, validation.declaredInputs) ||
|
|
3306
3846
|
validation.projectWalkFailures !==
|
|
@@ -3400,9 +3940,14 @@ async function captureTransformGeneration(props) {
|
|
|
3400
3940
|
const configured = createTransformTsconfig(props, scratchDirectory);
|
|
3401
3941
|
const temporaryTsconfig = configured.path === props.tsconfig ? undefined : configured.path;
|
|
3402
3942
|
const identities = createHostPathIdentityContext(props.filesystem);
|
|
3403
|
-
|
|
3943
|
+
// Read from the project's own tsconfig rather than the generated one: a
|
|
3944
|
+
// relative `outDir` is anchored at the config that declares it, and the
|
|
3945
|
+
// generated config lives in a system temp directory. The caller's
|
|
3946
|
+
// compiler-options overlay still wins, since it wins for the compile too.
|
|
3947
|
+
const membershipPolicy = tsconfigPaths.mergeMembershipPolicyOverlay(tsconfigPaths.readProjectMembershipPolicy(props.tsconfig), props.compilerOptions, projectRoot);
|
|
3948
|
+
const before = collectProjectInputSnapshot(projectRoot, identities, props.filesystem, undefined, { policy: membershipPolicy });
|
|
3404
3949
|
tracker = props.trackProjectMembership
|
|
3405
|
-
? await createProjectMutationTracker(before.projectDirectories, props.filesystem)
|
|
3950
|
+
? await createProjectMutationTracker(before.projectDirectories, props.filesystem, membershipPolicy)
|
|
3406
3951
|
: undefined;
|
|
3407
3952
|
const result = withTransformScratchEnvironment(scratchDirectory, () => new ttsc.TtscCompiler({
|
|
3408
3953
|
cwd: projectRoot,
|
|
@@ -3467,12 +4012,13 @@ async function captureTransformGeneration(props) {
|
|
|
3467
4012
|
: undefined;
|
|
3468
4013
|
const externalInputPaths = selectExternalInputPaths({
|
|
3469
4014
|
filesystem: props.filesystem,
|
|
4015
|
+
membershipPolicy,
|
|
3470
4016
|
projectRoot,
|
|
3471
4017
|
result,
|
|
3472
4018
|
scratchDirectory,
|
|
3473
4019
|
temporaryTsconfig,
|
|
3474
4020
|
});
|
|
3475
|
-
const inputSnapshot = collectProjectInputSnapshot(projectRoot, identities, props.filesystem);
|
|
4021
|
+
const inputSnapshot = collectProjectInputSnapshot(projectRoot, identities, props.filesystem, undefined, { policy: membershipPolicy });
|
|
3476
4022
|
// Whether the recorded snapshot describes one coherent state of the
|
|
3477
4023
|
// project. A membership event during the compile taints it exactly like an
|
|
3478
4024
|
// unstable walk pair; whether notifications can be *opened* is a separate
|
|
@@ -3507,6 +4053,12 @@ async function captureTransformGeneration(props) {
|
|
|
3507
4053
|
delete inputSnapshot.provenSignatures[currentFileKey];
|
|
3508
4054
|
}
|
|
3509
4055
|
const cached = {
|
|
4056
|
+
// The pass this compile was started for. Its snapshot describes the
|
|
4057
|
+
// project as of this compile, so it is settled for this pass and any
|
|
4058
|
+
// later pass must re-prove it.
|
|
4059
|
+
...(props.deliveryEpoch === undefined
|
|
4060
|
+
? {}
|
|
4061
|
+
: { deliveryEpoch: props.deliveryEpoch }),
|
|
3510
4062
|
// Capture the out-of-walk input hashes while the generation is fresh so
|
|
3511
4063
|
// cache validation can re-check them; computed before dispose so the
|
|
3512
4064
|
// scratch-tree exclusion is the only reason its disposed artifacts never
|
|
@@ -3516,7 +4068,9 @@ async function captureTransformGeneration(props) {
|
|
|
3516
4068
|
externalInputPaths,
|
|
3517
4069
|
inputHashes: inputSnapshot.hashes,
|
|
3518
4070
|
inputSignatures: inputSnapshot.provenSignatures,
|
|
4071
|
+
membershipPolicy,
|
|
3519
4072
|
projectDirectories: inputSnapshot.projectDirectories,
|
|
4073
|
+
tsconfig: props.tsconfig,
|
|
3520
4074
|
projectSnapshotComplete: false,
|
|
3521
4075
|
projectRoot,
|
|
3522
4076
|
result,
|
|
@@ -3876,10 +4430,37 @@ function readPaths(value) {
|
|
|
3876
4430
|
function createAliasPaths(aliases) {
|
|
3877
4431
|
const paths = {};
|
|
3878
4432
|
for (const alias of normalizeAliases(aliases)) {
|
|
3879
|
-
if (typeof alias.find !== "string"
|
|
4433
|
+
if (typeof alias.find !== "string") {
|
|
4434
|
+
// Vite's array form accepts a `RegExp` find, and `{ find: /^~/ }` is a
|
|
4435
|
+
// common way to spell a prefix alias. A tsconfig `paths` map has no
|
|
4436
|
+
// regular-expression form, so there is nothing to translate it into
|
|
4437
|
+
// (samchon/ttsc#1315). Reducing the simple prefix cases to a string is
|
|
4438
|
+
// possible in principle and deliberately not done: telling `/^~/` from
|
|
4439
|
+
// `/^~(?=\/)/` or `/^@app/` — which matches `@apple` too — means
|
|
4440
|
+
// implementing enough of a regular-expression engine that a wrong
|
|
4441
|
+
// reduction becomes likely, and a mistranslated alias resolves imports to
|
|
4442
|
+
// the wrong file silently, which is worse than not forwarding it.
|
|
4443
|
+
//
|
|
4444
|
+
// Not reported, unlike the wildcard below, and that asymmetry is the
|
|
4445
|
+
// whole point: Vite merges two `RegExp` aliases of its own into every
|
|
4446
|
+
// resolved config, `/^\/?@vite\/env/` and `/^\/?@vite\/client/`. Measured
|
|
4447
|
+
// on a bare project with no user aliases at all, `resolve.alias` has
|
|
4448
|
+
// exactly those two entries under both `serve` and `build`, so a report
|
|
4449
|
+
// on this form would fire twice for every Vite user in every build, name
|
|
4450
|
+
// aliases they never wrote, and say nothing about their configuration.
|
|
4451
|
+
// A diagnostic that cannot distinguish the user's input from the host's
|
|
4452
|
+
// is noise, and noise is what teaches people to stop reading the channel
|
|
4453
|
+
// the out-of-program report depends on. The documentation carries this
|
|
4454
|
+
// form instead, in both README and guide.
|
|
4455
|
+
continue;
|
|
4456
|
+
}
|
|
4457
|
+
if (alias.find.length === 0) {
|
|
3880
4458
|
continue;
|
|
3881
4459
|
}
|
|
3882
4460
|
if (alias.find.includes("*")) {
|
|
4461
|
+
// A `paths` key reads `*` as its own wildcard, so forwarding a `find`
|
|
4462
|
+
// that already contains one cannot preserve the caller's meaning.
|
|
4463
|
+
reportUntranslatableAlias(JSON.stringify(alias.find), 'a "paths" key already reads "*" as its own wildcard');
|
|
3883
4464
|
continue;
|
|
3884
4465
|
}
|
|
3885
4466
|
const key = alias.find.replace(/\/+$/, "");
|
|
@@ -3894,9 +4475,50 @@ function createAliasPaths(aliases) {
|
|
|
3894
4475
|
}
|
|
3895
4476
|
return paths;
|
|
3896
4477
|
}
|
|
4478
|
+
/**
|
|
4479
|
+
* Alias descriptions already reported in this process.
|
|
4480
|
+
*
|
|
4481
|
+
* The message is about configuration rather than about a module:
|
|
4482
|
+
* `resolve.alias` is resolved once and then consulted on every delivery, so
|
|
4483
|
+
* reporting per delivery would repeat one statement about the config for every
|
|
4484
|
+
* file in the bundle. Keyed by the description, so a Vite dev server that
|
|
4485
|
+
* reloads its config reports again only when the alias itself changed.
|
|
4486
|
+
*/
|
|
4487
|
+
const REPORTED_UNTRANSLATABLE_ALIASES = new Set();
|
|
4488
|
+
/**
|
|
4489
|
+
* Tell the user once that an alias they declared is not reaching the compile.
|
|
4490
|
+
*
|
|
4491
|
+
* A dropped alias is not silent in its consequence — the compile resolves
|
|
4492
|
+
* through the tsconfig's own `paths`, and a module that resolves for the
|
|
4493
|
+
* bundler but not for the compiler surfaces as the out-of-program report
|
|
4494
|
+
* (samchon/ttsc#1308) — but that report names the module, not the alias, so the
|
|
4495
|
+
* user cannot learn from it that a configuration they wrote was ignored.
|
|
4496
|
+
*
|
|
4497
|
+
* Only the wildcard form reaches here. Every entry it names was written by the
|
|
4498
|
+
* user, because nothing injects one; the `RegExp` form is left to the
|
|
4499
|
+
* documentation precisely because Vite does inject those, and
|
|
4500
|
+
* {@link createAliasPaths} carries that measurement.
|
|
4501
|
+
*/
|
|
4502
|
+
function reportUntranslatableAlias(description, reason) {
|
|
4503
|
+
if (REPORTED_UNTRANSLATABLE_ALIASES.has(description)) {
|
|
4504
|
+
return;
|
|
4505
|
+
}
|
|
4506
|
+
REPORTED_UNTRANSLATABLE_ALIASES.add(description);
|
|
4507
|
+
process.stderr.write(`ttsc: the Vite alias ${description} was not forwarded to the compile, because ${reason}. Declare it in your tsconfig's "paths" if ttsc must resolve through it.\n`);
|
|
4508
|
+
}
|
|
4509
|
+
/**
|
|
4510
|
+
* Collect the host's declared aliases without deciding which of them can be
|
|
4511
|
+
* expressed as `paths`.
|
|
4512
|
+
*
|
|
4513
|
+
* That decision belongs to {@link createAliasPaths} alone. It used to be split:
|
|
4514
|
+
* this function's type guard required a string `find` and dropped Vite's
|
|
4515
|
+
* `RegExp` form before `createAliasPaths` ever saw it, which left
|
|
4516
|
+
* `createAliasPaths`'s own non-string branch unreachable and put the drop
|
|
4517
|
+
* somewhere nothing could report it (samchon/ttsc#1315).
|
|
4518
|
+
*/
|
|
3897
4519
|
function normalizeAliases(aliases) {
|
|
3898
4520
|
if (Array.isArray(aliases)) {
|
|
3899
|
-
return aliases.filter(
|
|
4521
|
+
return aliases.filter(isDeclaredAlias);
|
|
3900
4522
|
}
|
|
3901
4523
|
if (typeof aliases === "object" && aliases !== null) {
|
|
3902
4524
|
return Object.entries(aliases)
|
|
@@ -3941,12 +4563,11 @@ function isRelativeSpecifier(value) {
|
|
|
3941
4563
|
value.startsWith(".\\") ||
|
|
3942
4564
|
value.startsWith("..\\"));
|
|
3943
4565
|
}
|
|
3944
|
-
function
|
|
4566
|
+
function isDeclaredAlias(value) {
|
|
3945
4567
|
return (typeof value === "object" &&
|
|
3946
4568
|
value !== null &&
|
|
3947
4569
|
"find" in value &&
|
|
3948
4570
|
"replacement" in value &&
|
|
3949
|
-
typeof value.find === "string" &&
|
|
3950
4571
|
typeof value.replacement === "string");
|
|
3951
4572
|
}
|
|
3952
4573
|
/**
|
|
@@ -3979,19 +4600,57 @@ function selectTransformedSource(props) {
|
|
|
3979
4600
|
if (source !== undefined) {
|
|
3980
4601
|
return source;
|
|
3981
4602
|
}
|
|
3982
|
-
throw new
|
|
4603
|
+
throw new TtscMissingProgramOutputError(props.file, props.tsconfig);
|
|
3983
4604
|
}
|
|
3984
4605
|
/**
|
|
3985
|
-
*
|
|
4606
|
+
* Tell the user once that a module was left untransformed, and why.
|
|
4607
|
+
*
|
|
4608
|
+
* The condition is ordinary and the build continues, but it must never be
|
|
4609
|
+
* silent: a file the program does not contain keeps whatever plugin syntax it
|
|
4610
|
+
* carries, so a typia `assert<T>()` in it becomes a runtime failure rather than
|
|
4611
|
+
* a build failure. One line per file per generation per pass, on the channel
|
|
4612
|
+
* the generation's other non-fatal diagnostics already use, so a bundle that
|
|
4613
|
+
* reaches many such files does not repeat itself per delivery.
|
|
4614
|
+
*/
|
|
4615
|
+
function reportMissingProgramOutput(cached, error, epoch) {
|
|
4616
|
+
const reported = (cached.missingOutputReported ??= new Set());
|
|
4617
|
+
if (cached.missingOutputEpoch !== epoch) {
|
|
4618
|
+
cached.missingOutputEpoch = epoch;
|
|
4619
|
+
reported.clear();
|
|
4620
|
+
}
|
|
4621
|
+
if (reported.has(error.file)) {
|
|
4622
|
+
return;
|
|
4623
|
+
}
|
|
4624
|
+
reported.add(error.file);
|
|
4625
|
+
process.stderr.write(`${error.message}
|
|
4626
|
+
`);
|
|
4627
|
+
}
|
|
4628
|
+
/**
|
|
4629
|
+
* Forward non-fatal plugin diagnostics to stderr, once per generation per pass.
|
|
3986
4630
|
*
|
|
3987
4631
|
* A `success` result may still carry warnings or informational messages from
|
|
3988
|
-
* plugins
|
|
3989
|
-
*
|
|
4632
|
+
* plugins — `@ttsc/lint` reports every rule below error severity this way.
|
|
4633
|
+
* These are surfaced via stderr rather than throwing so the build continues.
|
|
4634
|
+
* Failures and exceptions are handled by the caller.
|
|
4635
|
+
*
|
|
4636
|
+
* They describe one compile of one program, so writing them per delivery
|
|
4637
|
+
* printed the same warning once per module and scaled the noise with exactly
|
|
4638
|
+
* the reuse the cache exists to provide (samchon/ttsc#1304). A pass that reuses
|
|
4639
|
+
* a retained generation still surfaces them once, because a build's warnings
|
|
4640
|
+
* are part of what that build reports; a host with no pass boundary surfaces
|
|
4641
|
+
* them once per generation, which is the same rule with one pass.
|
|
3990
4642
|
*/
|
|
3991
|
-
function reportSuccessDiagnostics(
|
|
4643
|
+
function reportSuccessDiagnostics(cached, epoch) {
|
|
4644
|
+
const result = cached.result;
|
|
3992
4645
|
if (result.type !== "success" || result.diagnostics === undefined) {
|
|
3993
4646
|
return;
|
|
3994
4647
|
}
|
|
4648
|
+
if (cached.diagnosticsReported === true &&
|
|
4649
|
+
cached.diagnosticsEpoch === epoch) {
|
|
4650
|
+
return;
|
|
4651
|
+
}
|
|
4652
|
+
cached.diagnosticsReported = true;
|
|
4653
|
+
cached.diagnosticsEpoch = epoch;
|
|
3995
4654
|
const text = formatDiagnostics(result.diagnostics);
|
|
3996
4655
|
if (text.length !== 0) {
|
|
3997
4656
|
process.stderr.write(`${text}\n`);
|
|
@@ -4015,7 +4674,7 @@ function formatDiagnostics(diagnostics) {
|
|
|
4015
4674
|
diag.line === undefined
|
|
4016
4675
|
? undefined
|
|
4017
4676
|
: `${diag.line}:${diag.character ?? 1}`,
|
|
4018
|
-
diag.messageText,
|
|
4677
|
+
stripTerminalEscapes(diag.messageText),
|
|
4019
4678
|
]
|
|
4020
4679
|
.filter((part) => part !== undefined && part !== "")
|
|
4021
4680
|
.join(": "))
|
|
@@ -4023,15 +4682,38 @@ function formatDiagnostics(diagnostics) {
|
|
|
4023
4682
|
}
|
|
4024
4683
|
function formatUnknownError(error) {
|
|
4025
4684
|
if (error instanceof Error) {
|
|
4026
|
-
return error.message;
|
|
4685
|
+
return stripTerminalEscapes(error.message);
|
|
4027
4686
|
}
|
|
4028
4687
|
if (typeof error === "object" &&
|
|
4029
4688
|
error !== null &&
|
|
4030
4689
|
"message" in error &&
|
|
4031
4690
|
typeof error.message === "string") {
|
|
4032
|
-
return error.message;
|
|
4691
|
+
return stripTerminalEscapes(error.message);
|
|
4033
4692
|
}
|
|
4034
|
-
return String(error);
|
|
4693
|
+
return stripTerminalEscapes(String(error));
|
|
4694
|
+
}
|
|
4695
|
+
/**
|
|
4696
|
+
* Remove terminal colour and cursor sequences from text the adapter surfaces.
|
|
4697
|
+
*
|
|
4698
|
+
* An ordinary type error reaches the adapter as an `"exception"` envelope whose
|
|
4699
|
+
* `error` is the host's own rendered output, colour and all, and the envelope
|
|
4700
|
+
* carries no structured diagnostics to format instead. What the adapter hands
|
|
4701
|
+
* back is not going to a terminal: it becomes the `Error` a bundler reports, so
|
|
4702
|
+
* it lands in a Vite overlay, a webpack error report or a CI annotation, where
|
|
4703
|
+
* the escapes render as literal noise around the file and line the reader needs
|
|
4704
|
+
* (samchon/ttsc#1312).
|
|
4705
|
+
*
|
|
4706
|
+
* The colour originates in the host's rendering rather than in anything this
|
|
4707
|
+
* adapter configures, so this is the adapter-side repair, applied to every
|
|
4708
|
+
* message it surfaces rather than to one call site.
|
|
4709
|
+
*/
|
|
4710
|
+
function stripTerminalEscapes(text) {
|
|
4711
|
+
// Built from a char code so no control byte lives in this source file, and
|
|
4712
|
+
// written with `[[]` (a class holding one literal bracket) so the pattern
|
|
4713
|
+
// needs no backslash escapes to survive the string it is assembled from.
|
|
4714
|
+
const escape = String.fromCharCode(27);
|
|
4715
|
+
const controlSequence = new RegExp(escape + "[[][0-9;?]*[ -/]*[@-~]", "g");
|
|
4716
|
+
return text.replace(controlSequence, "");
|
|
4035
4717
|
}
|
|
4036
4718
|
/**
|
|
4037
4719
|
* Locate the tsconfig that should govern the transform for `file`.
|