@lark-apaas/fullstack-cli 1.1.62-alpha.20260824155956 → 1.1.62-alpha.20260824202917
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/package.json
CHANGED
package/templates/scripts/dev.js
CHANGED
|
@@ -253,12 +253,15 @@ process.on('SIGINT', cleanup);
|
|
|
253
253
|
process.on('SIGHUP', cleanup);
|
|
254
254
|
|
|
255
255
|
// dist 缓存策略:
|
|
256
|
-
// 沙箱首次启动 → 保留 dist/, 让 nest --watch 复用上次编译产物
|
|
256
|
+
// 沙箱首次启动 + dist 对应当前 lockfile → 保留 dist/, 让 nest --watch 复用上次编译产物
|
|
257
|
+
// 沙箱首次启动 + dist 对应别的 lockfile → 清 dist/, 避免依赖换了却用旧产物 (stale binding)
|
|
257
258
|
// 后续启动 (agent 主动触发 npm run dev) → 全清 dist/, 让模型面对新鲜产物避免 stale
|
|
258
259
|
// 依赖升级 (lockfile hash 变) → 同后续启动 (清 dist), log 里点出 "deps changed"
|
|
259
260
|
// FORCE_CLEAN_DIST=true → escape hatch, 强清
|
|
260
261
|
// marker 放 os.tmpdir()/.miaoda-dev-boot (跟沙箱容器 lifecycle 同, 沙箱重启会清空,
|
|
261
|
-
// 语义对齐"沙箱首次启动");
|
|
262
|
+
// 语义对齐"沙箱首次启动"); "dist 对应哪份 lockfile" 另记在 dist/.miaoda-dist-lockfile,
|
|
263
|
+
// 跟 dist 一起备份/恢复 —— 不能用 mtime 判, TOS 恢复会把 mtime 全部重写(详见该模块头注释)。
|
|
264
|
+
// 具体逻辑抽到 lib/preserve-dev-cache.cjs 便于单测。
|
|
262
265
|
const { preserveDevCache } = require('./lib/preserve-dev-cache.cjs');
|
|
263
266
|
|
|
264
267
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import crypto from 'crypto';
|
|
2
3
|
import fs from 'fs';
|
|
3
4
|
import os from 'os';
|
|
4
5
|
import path from 'path';
|
|
@@ -114,7 +115,7 @@ describe('preserveDevCache', () => {
|
|
|
114
115
|
});
|
|
115
116
|
|
|
116
117
|
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
117
|
-
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', '
|
|
118
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'stale');
|
|
118
119
|
|
|
119
120
|
const logs: LogEntry[] = [];
|
|
120
121
|
const r = preserveDevCache(tmpDir, {
|
|
@@ -124,7 +125,10 @@ describe('preserveDevCache', () => {
|
|
|
124
125
|
});
|
|
125
126
|
|
|
126
127
|
expect(r.action).toBe('subsequent_boot_cleaned');
|
|
127
|
-
|
|
128
|
+
// 编译产物必须清空; dist/ 目录本身会留下 provenance(记录接下来针对哪份 lockfile 编译),
|
|
129
|
+
// 否则下次冷启动读不到 provenance,会退化成每次全清。
|
|
130
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server'))).toBe(false);
|
|
131
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', '.miaoda-dist-lockfile'))).toBe(true);
|
|
128
132
|
expect(fs.existsSync(markerPath)).toBe(true);
|
|
129
133
|
expect(logs[0].msg).toContain('subsequent boot');
|
|
130
134
|
});
|
|
@@ -148,7 +152,9 @@ describe('preserveDevCache', () => {
|
|
|
148
152
|
});
|
|
149
153
|
|
|
150
154
|
expect(r.action).toBe('deps_changed_cleaned');
|
|
151
|
-
expect(fs.existsSync(path.join(tmpDir, 'dist'))).toBe(false);
|
|
155
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server'))).toBe(false);
|
|
156
|
+
// provenance 换成新 lockfile 的 hash,下次冷启动才可能复用
|
|
157
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', '.miaoda-dist-lockfile'))).toBe(true);
|
|
152
158
|
expect(logs[0].msg).toContain('deps changed');
|
|
153
159
|
});
|
|
154
160
|
|
|
@@ -209,20 +215,49 @@ describe('preserveDevCache', () => {
|
|
|
209
215
|
});
|
|
210
216
|
});
|
|
211
217
|
|
|
212
|
-
describe('first boot
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
218
|
+
describe('first boot dist staleness (lockfile content hash)', () => {
|
|
219
|
+
const PROVENANCE = '.miaoda-dist-lockfile';
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 复刻生产侧 computeLockfileHash 的口径: 文件名 + 内容一起 hash。
|
|
223
|
+
* 这里的用例统一只用 yarn.lock,所以固定带该文件名。
|
|
224
|
+
*/
|
|
225
|
+
function lockHash(content: string, name = 'yarn.lock') {
|
|
226
|
+
return crypto.createHash('sha1').update(name).update(content).digest('hex');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** 造一个"已编译过"的 dist,可选带上 provenance 记录它针对哪份 lockfile 内容 */
|
|
230
|
+
function makeDist(builtAgainstLockContent: string | null) {
|
|
217
231
|
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
232
|
+
// main.js 是生产侧判定"有编译产物"的入口,不能换成别的文件名
|
|
218
233
|
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
219
|
-
|
|
234
|
+
if (builtAgainstLockContent !== null) {
|
|
235
|
+
fs.writeFileSync(
|
|
236
|
+
path.join(tmpDir, 'dist', PROVENANCE),
|
|
237
|
+
JSON.stringify({ lockfileHash: lockHash(builtAgainstLockContent) })
|
|
238
|
+
);
|
|
239
|
+
}
|
|
220
240
|
}
|
|
221
241
|
|
|
222
|
-
it('
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
242
|
+
it('preserves dist when provenance matches current lockfile', () => {
|
|
243
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v1');
|
|
244
|
+
makeDist('lock-v1');
|
|
245
|
+
|
|
246
|
+
const logs: LogEntry[] = [];
|
|
247
|
+
const r = preserveDevCache(tmpDir, {
|
|
248
|
+
log: makeLogger(logs),
|
|
249
|
+
forceClean: false,
|
|
250
|
+
markerPath,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
expect(r.action).toBe('first_boot_preserved');
|
|
254
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(true);
|
|
255
|
+
expect(logs[0].msg).toContain('preserved');
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it('cleans dist when provenance records a different lockfile', () => {
|
|
259
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v2');
|
|
260
|
+
makeDist('lock-v1');
|
|
226
261
|
|
|
227
262
|
const logs: LogEntry[] = [];
|
|
228
263
|
const r = preserveDevCache(tmpDir, {
|
|
@@ -232,14 +267,13 @@ describe('preserveDevCache', () => {
|
|
|
232
267
|
});
|
|
233
268
|
|
|
234
269
|
expect(r.action).toBe('first_boot_deps_changed_cleaned');
|
|
235
|
-
expect(fs.existsSync(path.join(tmpDir, 'dist'))).toBe(false);
|
|
270
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(false);
|
|
236
271
|
expect(logs[0].msg).toContain('deps changed');
|
|
237
272
|
});
|
|
238
273
|
|
|
239
|
-
it('
|
|
240
|
-
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), '
|
|
241
|
-
|
|
242
|
-
makeStaleDist();
|
|
274
|
+
it('cleans dist when provenance is missing (pre-existing dist, cannot verify)', () => {
|
|
275
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v1');
|
|
276
|
+
makeDist(null);
|
|
243
277
|
|
|
244
278
|
const logs: LogEntry[] = [];
|
|
245
279
|
const r = preserveDevCache(tmpDir, {
|
|
@@ -248,41 +282,162 @@ describe('preserveDevCache', () => {
|
|
|
248
282
|
markerPath,
|
|
249
283
|
});
|
|
250
284
|
|
|
285
|
+
expect(r.action).toBe('first_boot_unverifiable_cleaned');
|
|
286
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(false);
|
|
287
|
+
expect(logs[0].msg).toContain('provenance missing');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('preserves dist when no lockfile present (nothing to verify against)', () => {
|
|
291
|
+
makeDist(null);
|
|
292
|
+
|
|
293
|
+
const r = preserveDevCache(tmpDir, {
|
|
294
|
+
log: makeLogger([]),
|
|
295
|
+
forceClean: false,
|
|
296
|
+
markerPath,
|
|
297
|
+
});
|
|
298
|
+
|
|
251
299
|
expect(r.action).toBe('first_boot_preserved');
|
|
252
300
|
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(true);
|
|
253
301
|
});
|
|
254
302
|
|
|
255
|
-
|
|
256
|
-
|
|
303
|
+
// 回归:这是 mtime 方案在 BOE 上 100% 失效的场景(2026-08-25 实测 2/2 冷启动)。
|
|
304
|
+
// 沙箱冷启动从 TOS 备份恢复 workspace 时会把所有文件 mtime 重写成恢复时刻,
|
|
305
|
+
// lockfile 稳定落在 dist/ 之后 → 旧的 `lockfile mtime > buildinfo mtime` 恒成立 →
|
|
306
|
+
// 每次都清 dist。依赖内容没变时必须复用,无论 mtime 谁新谁旧。
|
|
307
|
+
it('preserves dist even when lockfile mtime is newer than everything in dist', () => {
|
|
308
|
+
makeDist('lock-v1');
|
|
309
|
+
// 显式把 lockfile 的 mtime 推到未来,模拟恢复后 lockfile 比 dist 内所有文件都"新"
|
|
310
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v1');
|
|
311
|
+
const future = new Date(Date.now() + 60_000);
|
|
312
|
+
fs.utimesSync(path.join(tmpDir, 'yarn.lock'), future, future);
|
|
313
|
+
|
|
314
|
+
const r = preserveDevCache(tmpDir, {
|
|
315
|
+
log: makeLogger([]),
|
|
316
|
+
forceClean: false,
|
|
317
|
+
markerPath,
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
expect(r.action).toBe('first_boot_preserved');
|
|
321
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(true);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('writes provenance after cleaning so the next cold boot can reuse', () => {
|
|
325
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v1');
|
|
326
|
+
makeDist(null);
|
|
327
|
+
|
|
328
|
+
// 第一次冷启动:无 provenance → 清一次(迁移成本)
|
|
329
|
+
const first = preserveDevCache(tmpDir, {
|
|
330
|
+
log: makeLogger([]),
|
|
331
|
+
forceClean: false,
|
|
332
|
+
markerPath,
|
|
333
|
+
});
|
|
334
|
+
expect(first.action).toBe('first_boot_unverifiable_cleaned');
|
|
335
|
+
|
|
336
|
+
// 模拟随后的编译产出,并模拟沙箱重启(marker 消失)
|
|
257
337
|
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
258
338
|
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
259
|
-
|
|
339
|
+
fs.unlinkSync(markerPath);
|
|
260
340
|
|
|
341
|
+
// 第二次冷启动:provenance 已在,依赖没变 → 复用
|
|
342
|
+
const second = preserveDevCache(tmpDir, {
|
|
343
|
+
log: makeLogger([]),
|
|
344
|
+
forceClean: false,
|
|
345
|
+
markerPath,
|
|
346
|
+
});
|
|
347
|
+
expect(second.action).toBe('first_boot_preserved');
|
|
348
|
+
expect(fs.existsSync(path.join(tmpDir, 'dist', 'server', 'main.js'))).toBe(true);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it('treats a dist holding only provenance as no dist', () => {
|
|
352
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v1');
|
|
353
|
+
fs.mkdirSync(path.join(tmpDir, 'dist'), { recursive: true });
|
|
354
|
+
fs.writeFileSync(
|
|
355
|
+
path.join(tmpDir, 'dist', PROVENANCE),
|
|
356
|
+
JSON.stringify({ lockfileHash: lockHash('lock-v1') })
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
const logs: LogEntry[] = [];
|
|
261
360
|
const r = preserveDevCache(tmpDir, {
|
|
361
|
+
log: makeLogger(logs),
|
|
362
|
+
forceClean: false,
|
|
363
|
+
markerPath,
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// provenance 匹配但没有真实产物,不能谎报 preserved
|
|
367
|
+
expect(r.action).toBe('first_boot_no_dist');
|
|
368
|
+
expect(logs[0].msg).toContain('no dist/');
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
// 新建应用的标准生命周期:首次进 dev 时 marker 和 dist 都没有。
|
|
372
|
+
// 这一分支若不写 provenance,每个应用的**首次冷启动**都必然复用失败。
|
|
373
|
+
it('records provenance on a brand-new project so its first cold boot can reuse', () => {
|
|
374
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v1');
|
|
375
|
+
|
|
376
|
+
const first = preserveDevCache(tmpDir, {
|
|
262
377
|
log: makeLogger([]),
|
|
263
378
|
forceClean: false,
|
|
264
379
|
markerPath,
|
|
265
380
|
});
|
|
381
|
+
expect(first.action).toBe('first_boot_no_dist');
|
|
266
382
|
|
|
267
|
-
|
|
268
|
-
|
|
383
|
+
// 编译产出 + 沙箱重启
|
|
384
|
+
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
385
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
386
|
+
fs.unlinkSync(markerPath);
|
|
387
|
+
|
|
388
|
+
const second = preserveDevCache(tmpDir, {
|
|
389
|
+
log: makeLogger([]),
|
|
390
|
+
forceClean: false,
|
|
391
|
+
markerPath,
|
|
392
|
+
});
|
|
393
|
+
expect(second.action).toBe('first_boot_preserved');
|
|
269
394
|
});
|
|
270
395
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
fs.writeFileSync(path.join(tmpDir, '
|
|
396
|
+
// 多种 lockfile 共存时,只 hash 第一个会让另一个的依赖变更完全隐形 —— 原 mtime 方案
|
|
397
|
+
// 取的是所有 lockfile 的 max(mtime),换成 hash 不能反而把守护面变窄。
|
|
398
|
+
it('detects dependency changes in any lockfile when several coexist', () => {
|
|
399
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'yarn-lock-stable');
|
|
400
|
+
fs.writeFileSync(path.join(tmpDir, 'package-lock.json'), '{"v":1}');
|
|
275
401
|
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
276
402
|
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
277
403
|
|
|
278
|
-
|
|
404
|
+
// 第一次:无 provenance → 清一次并写入当前(两个 lockfile 的)组合 hash
|
|
405
|
+
preserveDevCache(tmpDir, { log: makeLogger([]), forceClean: false, markerPath });
|
|
406
|
+
// 模拟随后的编译产出(上一步把 dist 清了)
|
|
407
|
+
fs.mkdirSync(path.join(tmpDir, 'dist', 'server'), { recursive: true });
|
|
408
|
+
fs.writeFileSync(path.join(tmpDir, 'dist', 'server', 'main.js'), 'compiled');
|
|
409
|
+
|
|
410
|
+
// 依赖没变 → 应当复用,确认基线正常
|
|
411
|
+
fs.unlinkSync(markerPath);
|
|
412
|
+
const baseline = preserveDevCache(tmpDir, {
|
|
279
413
|
log: makeLogger([]),
|
|
280
414
|
forceClean: false,
|
|
281
415
|
markerPath,
|
|
282
416
|
});
|
|
417
|
+
expect(baseline.action).toBe('first_boot_preserved');
|
|
283
418
|
|
|
284
|
-
|
|
285
|
-
|
|
419
|
+
// 只改 package-lock.json(yarn.lock 不动) → 必须被识别为依赖变更
|
|
420
|
+
fs.writeFileSync(path.join(tmpDir, 'package-lock.json'), '{"v":2}');
|
|
421
|
+
fs.unlinkSync(markerPath);
|
|
422
|
+
const afterNpmInstall = preserveDevCache(tmpDir, {
|
|
423
|
+
log: makeLogger([]),
|
|
424
|
+
forceClean: false,
|
|
425
|
+
markerPath,
|
|
426
|
+
});
|
|
427
|
+
expect(afterNpmInstall.action).toBe('first_boot_deps_changed_cleaned');
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it('stores the current lockfile hash into provenance when cleaning', () => {
|
|
431
|
+
fs.writeFileSync(path.join(tmpDir, 'yarn.lock'), 'lock-v2');
|
|
432
|
+
makeDist('lock-v1');
|
|
433
|
+
|
|
434
|
+
preserveDevCache(tmpDir, { log: makeLogger([]), forceClean: false, markerPath });
|
|
435
|
+
|
|
436
|
+
// 断言内容而非仅存在: 写成 prevLockfileHash 之类也能让"文件存在"通过
|
|
437
|
+
const written = JSON.parse(
|
|
438
|
+
fs.readFileSync(path.join(tmpDir, 'dist', PROVENANCE), 'utf-8')
|
|
439
|
+
);
|
|
440
|
+
expect(written.lockfileHash).toBe(lockHash('lock-v2'));
|
|
286
441
|
});
|
|
287
442
|
});
|
|
288
443
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// dev.js 启动时的 dist 缓存策略:
|
|
4
|
-
// - 沙箱首次启动 (marker 不存在) + dist
|
|
5
|
-
// - 沙箱首次启动 + dist
|
|
4
|
+
// - 沙箱首次启动 (marker 不存在) + dist 产物与当前 lockfile 一致: 保留 dist/, 让 nest --watch 复用上次编译产物
|
|
5
|
+
// - 沙箱首次启动 + dist 产物对不上当前 lockfile: 清 dist/, 避免 stale binding
|
|
6
6
|
// - 后续启动 (agent 主动触发 npm run dev): 全清 dist/, 让模型面对新鲜产物避免 stale
|
|
7
7
|
// - 依赖升级 (marker 里 lockfile hash 变): 后续启动分支自然覆盖 (清 dist), log 里点出原因
|
|
8
8
|
// - FORCE_CLEAN_DIST=true: escape hatch, 强清
|
|
@@ -11,8 +11,17 @@
|
|
|
11
11
|
// /tmp 会清空, marker 消失, 下次进 dev 视为"首次启动"复用 dist; 应用工作目录是
|
|
12
12
|
// persistent volume 会跨沙箱重启保留, 放这里的 marker 无法反映"沙箱首次启动"语义。
|
|
13
13
|
//
|
|
14
|
-
// dist 过期判定 (仅首次启动分支需要)
|
|
15
|
-
//
|
|
14
|
+
// dist 过期判定 (仅首次启动分支需要) 用 **dist 内的 lockfile 内容 hash**, 不用 mtime。
|
|
15
|
+
// 为什么不用 mtime (2026-08-25 BOE 实测): 沙箱冷启动是"销毁 → 从 TOS 备份恢复 workspace",
|
|
16
|
+
// 恢复过程把所有文件 mtime 重写成恢复时刻, 彼此只差几毫秒且顺序由解包顺序决定;
|
|
17
|
+
// 实测 package-lock.json 稳定落在 dist/ 之后(.647 vs .638), 于是
|
|
18
|
+
// `lockfile mtime > buildinfo mtime` 恒成立 → 每次冷启动都判 stale 清 dist →
|
|
19
|
+
// dist 复用在它唯一要优化的场景里 100% 失效(2/2 次冷启动均命中)。
|
|
20
|
+
// mtime 承载不了"dist 是针对哪份依赖编译的"这个语义, 跨备份恢复后信息已丢失;
|
|
21
|
+
// 内容 hash 不受恢复影响, 也顺带修掉 `git checkout` touch lockfile 的误判。
|
|
22
|
+
//
|
|
23
|
+
// provenance 文件放 dist/ 内部: 跟着 dist 一起备份/恢复(TOS 备份不排除 dist),
|
|
24
|
+
// 一起被清, 天然不会出现"dist 没了 hash 还在"的悬挂状态; 且模板 .gitignore 已忽略 dist/。
|
|
16
25
|
|
|
17
26
|
const fs = require('fs');
|
|
18
27
|
const os = require('os');
|
|
@@ -20,6 +29,8 @@ const path = require('path');
|
|
|
20
29
|
const crypto = require('crypto');
|
|
21
30
|
|
|
22
31
|
const MARKER_FILENAME = '.miaoda-dev-boot';
|
|
32
|
+
// 记录"当前 dist 是针对哪份 lockfile 编译的"
|
|
33
|
+
const DIST_PROVENANCE_FILENAME = '.miaoda-dist-lockfile';
|
|
23
34
|
|
|
24
35
|
/**
|
|
25
36
|
* @param {string} projectRoot 项目根目录
|
|
@@ -27,7 +38,7 @@ const MARKER_FILENAME = '.miaoda-dev-boot';
|
|
|
27
38
|
* @param {(level: string, msg: string) => void} [options.log]
|
|
28
39
|
* @param {boolean} [options.forceClean]
|
|
29
40
|
* @param {string} [options.markerPath] 覆盖 marker 文件路径 (单测用; 生产默认 /tmp/.miaoda-dev-boot)
|
|
30
|
-
* @returns {{ action: 'force_cleaned' | 'first_boot_preserved' | 'first_boot_deps_changed_cleaned' | 'first_boot_no_dist' | 'subsequent_boot_cleaned' | 'deps_changed_cleaned' | 'subsequent_boot_no_dist' }}
|
|
41
|
+
* @returns {{ action: 'force_cleaned' | 'first_boot_preserved' | 'first_boot_deps_changed_cleaned' | 'first_boot_unverifiable_cleaned' | 'first_boot_no_dist' | 'subsequent_boot_cleaned' | 'deps_changed_cleaned' | 'subsequent_boot_no_dist' }}
|
|
31
42
|
*/
|
|
32
43
|
function preserveDevCache(projectRoot, options) {
|
|
33
44
|
const log = (options && options.log) || defaultLogger;
|
|
@@ -43,31 +54,60 @@ function preserveDevCache(projectRoot, options) {
|
|
|
43
54
|
const currentLockfileHash = computeLockfileHash(projectRoot);
|
|
44
55
|
|
|
45
56
|
if (forceClean) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
// escape hatch 的语义是"彻底擦干净", 所以连 provenance 也不留 —— 下次冷启动
|
|
58
|
+
// 因此会多清一次 dist, 这正是显式要求强清的人期望的保守方向。
|
|
59
|
+
cleanDist(distPath);
|
|
49
60
|
writeMarker(markerPath, currentLockfileHash);
|
|
50
61
|
log('INFO', 'preserveDevCache: FORCE_CLEAN_DIST=true, cleaned dist/');
|
|
51
62
|
return { action: 'force_cleaned' };
|
|
52
63
|
}
|
|
53
64
|
|
|
54
65
|
const markerExists = fs.existsSync(markerPath);
|
|
55
|
-
const distExists =
|
|
66
|
+
const distExists = distHasBuildOutput(distPath);
|
|
56
67
|
|
|
57
68
|
if (!markerExists) {
|
|
58
69
|
writeMarker(markerPath, currentLockfileHash);
|
|
59
70
|
if (distExists) {
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
// 无 lockfile 时无从判断 dist 针对哪份依赖, 保守保留(与"依赖没变"同解),
|
|
72
|
+
// 免得没有依赖管理的项目每次冷启动都白白全量重编。
|
|
73
|
+
if (currentLockfileHash === null) {
|
|
74
|
+
log('INFO', 'preserveDevCache: first boot, no lockfile to verify against, dist/ preserved');
|
|
75
|
+
return { action: 'first_boot_preserved' };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const distLockfileHash = readDistProvenance(distPath);
|
|
79
|
+
// 带上 hash 便于现场区分"依赖真变了"和"读不到 provenance" —— 上一版 mtime 方案
|
|
80
|
+
// 之所以能 100% 静默失效这么久, 就是因为日志里看不出判定依据。
|
|
81
|
+
const hashes = `dist=${short(distLockfileHash)} cur=${short(currentLockfileHash)}`;
|
|
82
|
+
|
|
83
|
+
if (distLockfileHash === currentLockfileHash) {
|
|
84
|
+
log('INFO', `preserveDevCache: first boot, dist/ matches current lockfile, preserved (${hashes})`);
|
|
85
|
+
return { action: 'first_boot_preserved' };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
cleanDist(distPath);
|
|
89
|
+
writeDistProvenance(distPath, currentLockfileHash);
|
|
90
|
+
|
|
91
|
+
if (distLockfileHash === null) {
|
|
92
|
+
// dist 里没有 provenance: 本次改动之前编译的老 dist, 或刚被 npm run build 换成
|
|
93
|
+
// 发布产物(build.sh 会 rm -rf dist)。两种都无从确认它针对哪份依赖 → 保守清一次;
|
|
94
|
+
// 清完写入 provenance, 下一次冷启动即可复用。
|
|
62
95
|
log(
|
|
63
96
|
'INFO',
|
|
64
|
-
|
|
97
|
+
`preserveDevCache: first boot but dist provenance missing (cannot verify deps), cleaned dist/ (${hashes})`
|
|
65
98
|
);
|
|
66
|
-
return { action: '
|
|
99
|
+
return { action: 'first_boot_unverifiable_cleaned' };
|
|
67
100
|
}
|
|
68
|
-
log(
|
|
69
|
-
|
|
101
|
+
log(
|
|
102
|
+
'INFO',
|
|
103
|
+
`preserveDevCache: first boot but deps changed (dist built against a different lockfile), cleaned dist/ (${hashes})`
|
|
104
|
+
);
|
|
105
|
+
return { action: 'first_boot_deps_changed_cleaned' };
|
|
70
106
|
}
|
|
107
|
+
// 同样要写 provenance: 接下来编译出的 dist 就是针对当前 lockfile 的。
|
|
108
|
+
// 漏掉这里会让每个应用的**首次冷启动**必然复用失败 —— 新建应用第一次进 dev 走的正是
|
|
109
|
+
// 本分支(marker 无、dist 无), 若不记 provenance, 下次冷启动读不到就得保守全清。
|
|
110
|
+
writeDistProvenance(distPath, currentLockfileHash);
|
|
71
111
|
log('INFO', 'preserveDevCache: first boot, no dist/ (will be created)');
|
|
72
112
|
return { action: 'first_boot_no_dist' };
|
|
73
113
|
}
|
|
@@ -79,9 +119,10 @@ function preserveDevCache(projectRoot, options) {
|
|
|
79
119
|
currentLockfileHash !== null &&
|
|
80
120
|
prevLockfileHash !== currentLockfileHash;
|
|
81
121
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
122
|
+
cleanDist(distPath);
|
|
123
|
+
// 清完就把 provenance 写回: 接下来的编译是针对当前 lockfile 的, 下次冷启动
|
|
124
|
+
// 才有依据判断能不能复用。不写的话首次启动分支永远读不到 provenance, 退化成每次全清。
|
|
125
|
+
writeDistProvenance(distPath, currentLockfileHash);
|
|
85
126
|
writeMarker(markerPath, currentLockfileHash);
|
|
86
127
|
|
|
87
128
|
if (depsChanged) {
|
|
@@ -97,55 +138,93 @@ function preserveDevCache(projectRoot, options) {
|
|
|
97
138
|
}
|
|
98
139
|
|
|
99
140
|
/**
|
|
100
|
-
*
|
|
141
|
+
* dist/ 里是否有真实编译产物。
|
|
101
142
|
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
143
|
+
* 判据用 server 入口而不是"目录非空": 清完 dist 会留下 provenance, 发布流程还会留下
|
|
144
|
+
* dist/node_modules、dist/dist 之类,靠"非空"会把这些空壳当成有产物 → 日志谎报 preserved。
|
|
145
|
+
* server/main.js 是 run.sh 实际启动的入口, 它在才算真编译过 —— 顺带让"编译中途失败"
|
|
146
|
+
* 留下的半成品被识别为无产物, 而不是被当成可复用。
|
|
104
147
|
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* - buildinfo 不存在 → 返回 true (dist 里没编译过 → 保守清)
|
|
108
|
-
* - `git checkout` 之类 touch lockfile 但内容没改 → 误判 stale 多清一次 (可接受, 安全方向)
|
|
148
|
+
* 出错方向偏向"有产物": 只有 ENOENT 才算真没有。EACCES / ENOTDIR 等情况下返回 false
|
|
149
|
+
* 会直接跳过清理分支, 让一个该清的 stale dist 被静默留下 —— 宁可多清一次。
|
|
109
150
|
*/
|
|
110
|
-
function
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
151
|
+
function distHasBuildOutput(distPath) {
|
|
152
|
+
try {
|
|
153
|
+
fs.statSync(path.join(distPath, 'server', 'main.js'));
|
|
154
|
+
return true;
|
|
155
|
+
} catch (err) {
|
|
156
|
+
return !(err && err.code === 'ENOENT');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function cleanDist(distPath) {
|
|
161
|
+
if (fs.existsSync(distPath)) {
|
|
162
|
+
fs.rmSync(distPath, { recursive: true, force: true });
|
|
122
163
|
}
|
|
123
|
-
|
|
164
|
+
}
|
|
124
165
|
|
|
166
|
+
function readDistProvenance(distPath) {
|
|
125
167
|
try {
|
|
126
|
-
const
|
|
127
|
-
path.join(
|
|
128
|
-
|
|
129
|
-
|
|
168
|
+
const raw = fs.readFileSync(
|
|
169
|
+
path.join(distPath, DIST_PROVENANCE_FILENAME),
|
|
170
|
+
'utf-8'
|
|
171
|
+
);
|
|
172
|
+
const data = JSON.parse(raw);
|
|
173
|
+
return typeof data.lockfileHash === 'string' ? data.lockfileHash : null;
|
|
130
174
|
} catch {
|
|
131
|
-
return
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 记下"接下来编译出的 dist 是针对这份 lockfile 的"。
|
|
181
|
+
*
|
|
182
|
+
* 在 preserveDevCache 决策时写, 而不是等编译完 —— 此处没有编译完成的 hook。
|
|
183
|
+
* 因此 provenance 表达的是"接下来要针对哪份 lockfile 编译", 而非"已经编译完了"。
|
|
184
|
+
*
|
|
185
|
+
* 编译中途失败/被打断时它仍声称匹配, 但不会放大成 stale binding, 有三层理由:
|
|
186
|
+
* 1. 清理发生在写 provenance 之前, 所以残留一定是"新依赖的半成品", 不可能是"旧依赖的产物" ——
|
|
187
|
+
* 本模块要防的类别没被打开;
|
|
188
|
+
* 2. server/main.js 缺失时 distHasBuildOutput 判为无产物, 半成品走 no_dist 分支而不是被复用;
|
|
189
|
+
* 3. tsbuildinfo 就在 dist 内, 与产物同清同留, 不会出现"buildinfo 说完整、产物却缺"的错配。
|
|
190
|
+
*/
|
|
191
|
+
function writeDistProvenance(distPath, lockfileHash) {
|
|
192
|
+
try {
|
|
193
|
+
fs.mkdirSync(distPath, { recursive: true });
|
|
194
|
+
fs.writeFileSync(
|
|
195
|
+
path.join(distPath, DIST_PROVENANCE_FILENAME),
|
|
196
|
+
JSON.stringify({ lockfileHash: lockfileHash ?? null }),
|
|
197
|
+
'utf-8'
|
|
198
|
+
);
|
|
199
|
+
} catch {
|
|
200
|
+
// provenance 写失败只会让下次冷启动多清一次 dist, 不阻塞 dev 启动
|
|
132
201
|
}
|
|
133
202
|
}
|
|
134
203
|
|
|
204
|
+
/**
|
|
205
|
+
* 把项目里**所有**存在的 lockfile 一起 hash。
|
|
206
|
+
*
|
|
207
|
+
* 不能只取第一个命中的: 多种 lockfile 共存时(如历史残留 yarn.lock + 沙箱实际在用的
|
|
208
|
+
* package-lock.json), 只 hash 前者会让 `npm install pkg@2` 完全不改变 hash →
|
|
209
|
+
* 冷启动判"依赖没变"→ 保留旧依赖编译的 dist, 正是本模块要防的 stale binding。
|
|
210
|
+
* 原 mtime 方案取的是所有 lockfile 的 max(mtime), 对这种组合是免疫的, 换 hash 不能反而变窄。
|
|
211
|
+
* 文件名也一起 hash, 这样"删掉一个 lockfile"也算依赖变动。
|
|
212
|
+
*/
|
|
135
213
|
function computeLockfileHash(projectRoot) {
|
|
136
|
-
const candidates = ['
|
|
214
|
+
const candidates = ['package-lock.json', 'pnpm-lock.yaml', 'yarn.lock'];
|
|
215
|
+
const hash = crypto.createHash('sha1');
|
|
216
|
+
let found = false;
|
|
137
217
|
for (const name of candidates) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
218
|
+
try {
|
|
219
|
+
const content = fs.readFileSync(path.join(projectRoot, name));
|
|
220
|
+
hash.update(name);
|
|
221
|
+
hash.update(content);
|
|
222
|
+
found = true;
|
|
223
|
+
} catch {
|
|
224
|
+
// 该 lockfile 不存在 / 读不了, 跳过
|
|
146
225
|
}
|
|
147
226
|
}
|
|
148
|
-
return null;
|
|
227
|
+
return found ? hash.digest('hex') : null;
|
|
149
228
|
}
|
|
150
229
|
|
|
151
230
|
function readMarkerHash(markerPath) {
|
|
@@ -162,7 +241,7 @@ function writeMarker(markerPath, lockfileHash) {
|
|
|
162
241
|
try {
|
|
163
242
|
fs.writeFileSync(
|
|
164
243
|
markerPath,
|
|
165
|
-
JSON.stringify({ lockfileHash: lockfileHash
|
|
244
|
+
JSON.stringify({ lockfileHash: lockfileHash ?? null }),
|
|
166
245
|
'utf-8'
|
|
167
246
|
);
|
|
168
247
|
} catch {
|
|
@@ -170,6 +249,11 @@ function writeMarker(markerPath, lockfileHash) {
|
|
|
170
249
|
}
|
|
171
250
|
}
|
|
172
251
|
|
|
252
|
+
/** hash 只取前 8 位进日志, 够区分且不刷屏; null 显式打成 none 而不是空 */
|
|
253
|
+
function short(hash) {
|
|
254
|
+
return typeof hash === 'string' ? hash.slice(0, 8) : 'none';
|
|
255
|
+
}
|
|
256
|
+
|
|
173
257
|
function defaultLogger(level, msg) {
|
|
174
258
|
// eslint-disable-next-line no-console
|
|
175
259
|
console.log(`[${level}] [main] ${msg}`);
|