vue-go-tsc 0.3.2 → 0.3.4

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/bin/vue-go-tsc CHANGED
@@ -253,6 +253,39 @@ function resolveEnv(env, { cpus, totalMem, instances = 1, idleCpus, availableMem
253
253
  return out;
254
254
  }
255
255
 
256
+ // Whether to print the resource budget: with tsc's own performance flags
257
+ // (`--diagnostics`, `--extendedDiagnostics`) or VUE_GO_TSC_VERBOSE. The budget
258
+ // depends on the machine's load at startup, so the same project can run in
259
+ // 30 s on one CI runner and 2 min on another; this line tells which.
260
+ function wantsBudget(args, env = {}) {
261
+ if (env.VUE_GO_TSC_VERBOSE && env.VUE_GO_TSC_VERBOSE !== '0') {
262
+ return true;
263
+ }
264
+ return args.some((arg) => arg === '--diagnostics' || arg === '--extendedDiagnostics');
265
+ }
266
+
267
+ // One line describing the budget `resolveEnv` gave the run and what it was
268
+ // derived from. Values the user set explicitly are marked as such.
269
+ function formatBudget(env, resolved, { cpus, idleCpus, instances = 1, totalMem, availableMem, hardLimit, codegen }) {
270
+ const gib = (bytes) => (Number.isFinite(bytes) ? `${(bytes / GiB).toFixed(1)} GiB` : 'unknown');
271
+ const origin = (name) => (env[name] ? ' (set)' : '');
272
+ const parts = [
273
+ `GOMAXPROCS=${resolved.GOMAXPROCS ?? 'default'}${origin('GOMAXPROCS')}`,
274
+ // An explicit GOMEMLIMIT keeps its own syntax (e.g. `5GiB`).
275
+ `GOMEMLIMIT=${env.GOMEMLIMIT || (resolved.GOMEMLIMIT ? gib(Number(resolved.GOMEMLIMIT)) : 'default')}${origin('GOMEMLIMIT')}`,
276
+ `codegen=${codegen}`,
277
+ ];
278
+ if (codegen === 'volar') {
279
+ parts.push(`workers=${resolved.GOLAR_PLUGIN_WORKERS}${origin('GOLAR_PLUGIN_WORKERS')}`);
280
+ }
281
+ const idle = Number.isFinite(idleCpus) ? `, ${idleCpus.toFixed(1)} idle` : '';
282
+ parts.push(
283
+ `from ${cpus} cores${idle}, ${instances} run(s), RAM ${gib(totalMem)} (${gib(availableMem)} available)` +
284
+ (Number.isFinite(hardLimit) ? `, cgroup limit ${gib(hardLimit)}` : '')
285
+ );
286
+ return `vue-go-tsc: ${parts.join(', ')}`;
287
+ }
288
+
256
289
  function isProcessAlive(pid) {
257
290
  try {
258
291
  process.kill(pid, 0);
@@ -327,30 +360,17 @@ function parseCgroupBytes(text) {
327
360
  return Number.isFinite(value) && value > 0 && value < CGROUP_UNLIMITED ? value : undefined;
328
361
  }
329
362
 
330
- // The tightest cgroup memory limit of this process and the usage counted
331
- // against it: { limit, usage } (usage may be undefined), or undefined when
332
- // there is none (or not on Linux).
333
- //
334
- // process.constrainedMemory() only reads the process's own cgroup, but a CI
335
- // job or container runtime often sets the limit on a parent (the job's slice)
336
- // and leaves the leaf unlimited, so this walks from the process's cgroup up to
337
- // the root and keeps the smallest memory.max / memory.high (v2) or
338
- // memory.limit_in_bytes (v1).
339
- function cgroupMemoryLimit(readFile = (p) => fs.readFileSync(p, 'utf8'), root = '/sys/fs/cgroup') {
363
+ // Call `visit(dir, version)` for every cgroup directory of this process that
364
+ // carries `controller` (cgroup v2 unified hierarchy, or the v1 hierarchy of
365
+ // that controller), from the process's own cgroup up to the root. Returns
366
+ // false when the process's cgroups cannot be read (not on Linux).
367
+ function walkCgroups(readFile, root, controller, visit) {
340
368
  let membership;
341
369
  try {
342
370
  membership = readFile('/proc/self/cgroup');
343
371
  } catch {
344
- return undefined;
372
+ return false;
345
373
  }
346
- let best;
347
- const read = (file) => {
348
- try {
349
- return readFile(file);
350
- } catch {
351
- return undefined;
352
- }
353
- };
354
374
  for (const line of membership.split('\n')) {
355
375
  // <hierarchy-id>:<controllers>:<path>; the path itself may contain ':'.
356
376
  const first = line.indexOf(':');
@@ -362,30 +382,86 @@ function cgroupMemoryLimit(readFile = (p) => fs.readFileSync(p, 'utf8'), root =
362
382
  const controllers = line.slice(first + 1, second).split(',');
363
383
  let dir = line.slice(second + 1);
364
384
  let base;
365
- let files;
385
+ let version;
366
386
  if (id === '0' && controllers.length === 1 && controllers[0] === '') {
367
387
  base = root;
368
- files = { limits: ['memory.max', 'memory.high'], usage: 'memory.current' };
369
- } else if (controllers.includes('memory')) {
370
- base = join(root, 'memory');
371
- files = { limits: ['memory.limit_in_bytes'], usage: 'memory.usage_in_bytes' };
388
+ version = 2;
389
+ } else if (controllers.includes(controller)) {
390
+ base = join(root, controllers.join(','));
391
+ version = 1;
372
392
  } else {
373
393
  continue;
374
394
  }
375
395
  for (;;) {
376
- const cgroupDir = join(base, dir);
377
- for (const name of files.limits) {
378
- const limit = parseCgroupBytes(read(join(cgroupDir, name)));
379
- if (limit !== undefined && (!best || limit < best.limit)) {
380
- best = { limit, usage: parseCgroupBytes(read(join(cgroupDir, files.usage))) };
381
- }
382
- }
396
+ visit(join(base, dir), version);
383
397
  if (dir === '/' || dir === '') {
384
398
  break;
385
399
  }
386
400
  dir = posix.dirname(dir);
387
401
  }
388
402
  }
403
+ return true;
404
+ }
405
+
406
+ function readOptional(readFile, file) {
407
+ try {
408
+ return readFile(file);
409
+ } catch {
410
+ return undefined;
411
+ }
412
+ }
413
+
414
+ // The tightest cgroup memory limit of this process and the usage counted
415
+ // against it: { limit, usage } (usage may be undefined), or undefined when
416
+ // there is none (or not on Linux).
417
+ //
418
+ // process.constrainedMemory() only reads the process's own cgroup, but a CI
419
+ // job or container runtime often sets the limit on a parent (the job's slice)
420
+ // and leaves the leaf unlimited, so this walks from the process's cgroup up to
421
+ // the root and keeps the smallest memory.max / memory.high (v2) or
422
+ // memory.limit_in_bytes (v1).
423
+ function cgroupMemoryLimit(readFile = (p) => fs.readFileSync(p, 'utf8'), root = '/sys/fs/cgroup') {
424
+ let best;
425
+ walkCgroups(readFile, root, 'memory', (cgroupDir, version) => {
426
+ const files =
427
+ version === 2
428
+ ? { limits: ['memory.max', 'memory.high'], usage: 'memory.current' }
429
+ : { limits: ['memory.limit_in_bytes'], usage: 'memory.usage_in_bytes' };
430
+ for (const name of files.limits) {
431
+ const limit = parseCgroupBytes(readOptional(readFile, join(cgroupDir, name)));
432
+ if (limit !== undefined && (!best || limit < best.limit)) {
433
+ best = { limit, usage: parseCgroupBytes(readOptional(readFile, join(cgroupDir, files.usage))) };
434
+ }
435
+ }
436
+ });
437
+ return best;
438
+ }
439
+
440
+ // The tightest cgroup CPU quota of this process, in cores (may be fractional),
441
+ // or undefined when there is none: cpu.max "<quota> <period>" (v2) or
442
+ // cpu.cfs_quota_us / cpu.cfs_period_us (v1), on the process's cgroup or any
443
+ // parent. The Go runtime honors this quota for its default GOMAXPROCS, but the
444
+ // launcher sets GOMAXPROCS itself, so it must apply the quota too: more Go
445
+ // threads than the quota only get the job throttled.
446
+ function cgroupCpuLimit(readFile = (p) => fs.readFileSync(p, 'utf8'), root = '/sys/fs/cgroup') {
447
+ let best;
448
+ walkCgroups(readFile, root, 'cpu', (cgroupDir, version) => {
449
+ let quota;
450
+ let period;
451
+ if (version === 2) {
452
+ [quota, period] = String(readOptional(readFile, join(cgroupDir, 'cpu.max')) ?? '').trim().split(/\s+/).map(Number);
453
+ } else {
454
+ quota = Number(readOptional(readFile, join(cgroupDir, 'cpu.cfs_quota_us')));
455
+ period = Number(readOptional(readFile, join(cgroupDir, 'cpu.cfs_period_us')));
456
+ }
457
+ // "max" (v2) parses to NaN and -1 (v1) means no quota.
458
+ if (Number.isFinite(quota) && quota > 0 && Number.isFinite(period) && period > 0) {
459
+ const cores = quota / period;
460
+ if (best === undefined || cores < best) {
461
+ best = cores;
462
+ }
463
+ }
464
+ });
389
465
  return best;
390
466
  }
391
467
 
@@ -410,6 +486,32 @@ function freeMemory(cgroup = cgroupMemoryLimit()) {
410
486
  return available;
411
487
  }
412
488
 
489
+ // Cores this run may use: the CPUs it may be scheduled on, bounded by the
490
+ // cgroup CPU quota (rounded up: a 2.5-core quota still runs 3 threads at once).
491
+ function usableCpus(available, cpuQuota) {
492
+ if (Number.isFinite(cpuQuota) && cpuQuota > 0) {
493
+ return Math.max(1, Math.min(available, Math.ceil(cpuQuota)));
494
+ }
495
+ return available;
496
+ }
497
+
498
+ // Whether GOMAXPROCS should follow the cores idle right now.
499
+ //
500
+ // On a developer machine, that keeps a typecheck from freezing the editor. In a
501
+ // CI job confined to a cgroup (memory limit or CPU quota), the idle sample
502
+ // reads the whole host (/proc/stat): on a runner busy with other jobs it falls
503
+ // to ~0 and GOMAXPROCS to its floor (1 on an 8-core runner), with a single
504
+ // Volar codegen worker. The run then does the same work on one core and holds
505
+ // its memory reservation several times longer (26 s on an idle runner, 135 s
506
+ // on a busy one for the same Nuxt app), while the kernel already shares the
507
+ // cores fairly between the jobs' cgroups.
508
+ function shouldSampleIdleCpus(env, { memoryLimit, cpuQuota } = {}) {
509
+ if (env.GOMAXPROCS) {
510
+ return false;
511
+ }
512
+ return !(Number.isFinite(memoryLimit) || Number.isFinite(cpuQuota));
513
+ }
514
+
413
515
  // Idle cores right now, sampled over CPU_SAMPLE_MS.
414
516
  function sampleIdleCpus(cpus) {
415
517
  const before = os.cpus();
@@ -436,12 +538,15 @@ async function main() {
436
538
  const { instances, unregister } = registerInstance();
437
539
  process.on('exit', unregister);
438
540
 
439
- const cpus = availableCpus();
541
+ const cgroup = cgroupMemoryLimit();
542
+ const cpuQuota = cgroupCpuLimit();
543
+ const cpus = usableCpus(availableCpus(), cpuQuota);
440
544
  // Sampling costs CPU_SAMPLE_MS of startup; skip it when it cannot matter.
441
- const idleCpus = process.env.GOMAXPROCS ? undefined : await sampleIdleCpus(cpus);
545
+ const idleCpus = shouldSampleIdleCpus(process.env, { memoryLimit: cgroup?.limit, cpuQuota })
546
+ ? await sampleIdleCpus(cpus)
547
+ : undefined;
442
548
 
443
- const cgroup = cgroupMemoryLimit();
444
- const env = resolveEnv(process.env, {
549
+ const resources = {
445
550
  cpus,
446
551
  totalMem: totalMemory(cgroup),
447
552
  instances,
@@ -449,12 +554,18 @@ async function main() {
449
554
  availableMem: freeMemory(cgroup),
450
555
  hardLimit: cgroup?.limit,
451
556
  codegen,
557
+ };
558
+ const env = resolveEnv(process.env, {
559
+ ...resources,
452
560
  volar: {
453
561
  pluginEntry: VOLAR_PLUGIN,
454
562
  nodePath: process.execPath,
455
563
  tsconfig: resolveProjectConfig(args, process.cwd()),
456
564
  },
457
565
  });
566
+ if (wantsBudget(args, process.env)) {
567
+ console.error(formatBudget(process.env, env, resources));
568
+ }
458
569
 
459
570
  // Forward the remaining arguments to the binary
460
571
  const child = spawn(BIN_PATH, args, {
@@ -508,8 +619,13 @@ module.exports = {
508
619
  computeHardGoMemLimit,
509
620
  measureIdleCpus,
510
621
  resolveEnv,
622
+ wantsBudget,
623
+ formatBudget,
511
624
  registerInstance,
512
625
  cgroupMemoryLimit,
626
+ cgroupCpuLimit,
627
+ usableCpus,
628
+ shouldSampleIdleCpus,
513
629
  GiB,
514
630
  DEFAULT_CPU_FRACTION,
515
631
  DEFAULT_MEM_FRACTION,
@@ -14,8 +14,13 @@ const {
14
14
  computeHardGoMemLimit,
15
15
  measureIdleCpus,
16
16
  resolveEnv,
17
+ wantsBudget,
18
+ formatBudget,
17
19
  registerInstance,
18
20
  cgroupMemoryLimit,
21
+ cgroupCpuLimit,
22
+ usableCpus,
23
+ shouldSampleIdleCpus,
19
24
  GiB,
20
25
  DEFAULT_MEM_FRACTION,
21
26
  MIN_MEM_LIMIT,
@@ -301,3 +306,72 @@ test('resolveEnv follows a hard cgroup limit instead of the free RAM', () => {
301
306
  assert.strictEqual(explicit.GOMEMLIMIT, '5GiB');
302
307
  assert.strictEqual(computeHardGoMemLimit(undefined), null);
303
308
  });
309
+
310
+ test('wantsBudget follows the tsc performance flags and VUE_GO_TSC_VERBOSE', () => {
311
+ assert.strictEqual(wantsBudget(['--noEmit']), false);
312
+ assert.strictEqual(wantsBudget(['--noEmit', '--extendedDiagnostics']), true);
313
+ assert.strictEqual(wantsBudget(['--diagnostics']), true);
314
+ assert.strictEqual(wantsBudget([], { VUE_GO_TSC_VERBOSE: '1' }), true);
315
+ assert.strictEqual(wantsBudget([], { VUE_GO_TSC_VERBOSE: '0' }), false);
316
+ });
317
+
318
+ test('formatBudget reports the resolved budget and what it came from', () => {
319
+ const resources = { cpus: 8, idleCpus: 0.4, instances: 2, totalMem: 8 * GiB, availableMem: 6 * GiB, hardLimit: 8 * GiB, codegen: 'volar' };
320
+ const resolved = resolveEnv({}, resources);
321
+ const line = formatBudget({}, resolved, resources);
322
+ assert.strictEqual(
323
+ line,
324
+ `vue-go-tsc: GOMAXPROCS=1, GOMEMLIMIT=${(Number(resolved.GOMEMLIMIT) / GiB).toFixed(1)} GiB, codegen=volar, workers=1, ` +
325
+ 'from 8 cores, 0.4 idle, 2 run(s), RAM 8.0 GiB (6.0 GiB available), cgroup limit 8.0 GiB'
326
+ );
327
+ });
328
+
329
+ test('formatBudget marks explicit values and keeps their syntax', () => {
330
+ const env = { GOMAXPROCS: '4', GOMEMLIMIT: '5GiB' };
331
+ const resources = { cpus: 16, totalMem: 32 * GiB, codegen: 'go' };
332
+ const line = formatBudget(env, resolveEnv(env, resources), resources);
333
+ assert.ok(line.startsWith('vue-go-tsc: GOMAXPROCS=4 (set), GOMEMLIMIT=5GiB (set), codegen=go, from 16 cores, 1 run(s)'), line);
334
+ assert.ok(!line.includes('workers='), 'no workers without the Volar codegen');
335
+ assert.ok(!line.includes('cgroup'), 'no cgroup limit reported when there is none');
336
+ });
337
+
338
+ test('cgroupCpuLimit finds the tightest quota on the cgroup or a parent (v2)', () => {
339
+ const read = fakeCgroupFs({
340
+ '/proc/self/cgroup': '0::/seve.slice/job-42/step\n',
341
+ '/sys/fs/cgroup/seve.slice/job-42/step/cpu.max': 'max 100000\n',
342
+ '/sys/fs/cgroup/seve.slice/job-42/cpu.max': '250000 100000\n',
343
+ '/sys/fs/cgroup/seve.slice/cpu.max': '800000 100000\n',
344
+ });
345
+ assert.strictEqual(cgroupCpuLimit(read), 2.5);
346
+ });
347
+
348
+ test('cgroupCpuLimit reads cgroup v1 and ignores unlimited quotas', () => {
349
+ const v1 = fakeCgroupFs({
350
+ '/proc/self/cgroup': '12:cpu,cpuacct:/docker/abc\n4:memory:/docker/abc\n',
351
+ '/sys/fs/cgroup/cpu,cpuacct/docker/abc/cpu.cfs_quota_us': '200000',
352
+ '/sys/fs/cgroup/cpu,cpuacct/docker/abc/cpu.cfs_period_us': '100000',
353
+ '/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us': '-1',
354
+ '/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us': '100000',
355
+ });
356
+ assert.strictEqual(cgroupCpuLimit(v1), 2);
357
+ const unlimited = fakeCgroupFs({ '/proc/self/cgroup': '0::/\n', '/sys/fs/cgroup/cpu.max': 'max 100000\n' });
358
+ assert.strictEqual(cgroupCpuLimit(unlimited), undefined);
359
+ assert.strictEqual(cgroupCpuLimit(fakeCgroupFs({})), undefined);
360
+ });
361
+
362
+ test('usableCpus applies the CPU quota, rounded up', () => {
363
+ assert.strictEqual(usableCpus(8, undefined), 8);
364
+ assert.strictEqual(usableCpus(8, 2.5), 3);
365
+ assert.strictEqual(usableCpus(8, 0.5), 1);
366
+ assert.strictEqual(usableCpus(4, 16), 4);
367
+ });
368
+
369
+ test('shouldSampleIdleCpus samples only outside a CI job cgroup', () => {
370
+ assert.strictEqual(shouldSampleIdleCpus({}), true, 'developer machine');
371
+ assert.strictEqual(shouldSampleIdleCpus({}, { memoryLimit: 8 * GiB }), false, 'memory-limited job');
372
+ assert.strictEqual(shouldSampleIdleCpus({}, { cpuQuota: 4 }), false, 'CPU-limited job');
373
+ assert.strictEqual(shouldSampleIdleCpus({ GOMAXPROCS: '2' }), false, 'explicit GOMAXPROCS');
374
+ // Without the sample, a job on an 8-core runner keeps half of the cores
375
+ // instead of falling to one whenever another job is running.
376
+ assert.strictEqual(computeGoMaxProcs(8, 1, undefined), 4);
377
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-go-tsc",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "Blazingly fast Vue 3 type checker powered by typescript-go — a Go-native alternative to vue-tsc",
5
5
  "keywords": [
6
6
  "vue",