@smoothbricks/nx-plugin 0.7.8 → 0.7.10

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 CHANGED
@@ -1157,21 +1157,29 @@ Concrete targets come from concrete files:
1157
1157
  a target, so a `tsc-js` that claimed its bin failed on a file nothing had written yet. It writes no file beside the
1158
1158
  tsconfig, whose directory a consumer's ttsc transform lists while it compiles. Direct project references and every
1159
1159
  cached output lane are preserved. The config's `outDir` is the target's only output and holds exactly what this emit
1160
- wrote: the compiler rewrites every output it produces in place, so after a successful emit the executor removes each
1161
- file under `outDir` whose inode, ctime and mtime are unchanged since before the emit, and each directory that leaves
1162
- empty. A file an earlier build wrote and this one no longer does (a bin moved to another package, a deleted module) is
1163
- gone before Nx captures the output, so no cache artifact carries it. A failed emit removes nothing, and an `outDir`
1164
- that holds a TypeScript source is refused before the compiler runs. It writes `.build-inputs.json` into the `outDir`:
1165
- the SHA-256 of every file the config's `include` matches less its `exclude`, taken before the emit, and those globs as
1166
- Git pathspecs. Native targets write the same record from rustc's dep-info through `@smoothbricks/build-inputs`: macOS
1167
- uses its `smoo-nx-build-inputs` CLI and Linux's `cargo-zigbuild` executor calls that same collector. A record produced
1168
- under Nx also carries its producer's project, target and optional configuration, taken from the actual task
1169
- environment. Shell entry uses that identity to judge the selected development receipts, including inferred targets
1170
- absent from package manifests, and name their actual cached rebuild. It constructs no Nx graph, shadow output registry
1171
- or second interpolation policy. The record is inside the target's outputs, so a cache hit restores it with them; a
1172
- live developer link (`@smoothbricks/cli`, "Declared Links") checks it to tell a current build from a stale one by
1173
- content. The shell entry that bootstraps the plugin (`tooling/direnv/enter-shell.ts`) keeps its build marker under
1174
- `node_modules/.cache`, not under `outDir`, where the executor would remove it as a file its emit did not write.
1160
+ produced. The emit sets `TTSC_WRITE_IF_CHANGED=1`, with which ttsc writes an output only when its bytes differ (the
1161
+ managed ttsc patch), so an emit after one source edit leaves every output whose bytes did not change as it was, inode
1162
+ and mtime included: rewriting identical bytes is file churn that every copy-on-write clone of the checkout pays for.
1163
+ The emit runs with `--verbose`, whose summary lists every file the emit produced on each of ttsc's lanes (its own
1164
+ host, the direct-tsgo lane, the utility-plugin host and typia's), and after a successful emit the executor removes
1165
+ each file under `outDir` that the list does not name, other than its own `.build-inputs.json`, and each directory that
1166
+ leaves empty. A file an earlier build wrote and this one no longer does (a bin moved to another package, a deleted
1167
+ module) is gone before Nx captures the output, so no cache artifact carries it. A summary that lists no files, or
1168
+ fewer or more than it counts, fails the emit rather than read as one that produced nothing, and a list that names a
1169
+ file the emit did not leave prunes nothing and fails the emit, so Nx caches no output it emptied. Execute bits are set
1170
+ only on a bin that lacks them, since a chmod moves the file's ctime even when its mode stays. A failed emit removes
1171
+ nothing, and an `outDir` that holds a TypeScript source is refused before the compiler runs. It writes
1172
+ `.build-inputs.json` into the `outDir`, only when the record changed: the SHA-256 of every file the config's `include`
1173
+ matches less its `exclude`, taken before the emit, and those globs as Git pathspecs. Native targets write the same
1174
+ record from rustc's dep-info through `@smoothbricks/build-inputs`: macOS uses its `smoo-nx-build-inputs` CLI and
1175
+ Linux's `cargo-zigbuild` executor calls that same collector. A record produced under Nx also carries its producer's
1176
+ project, target and optional configuration, taken from the actual task environment. Shell entry uses that identity to
1177
+ judge the selected development receipts, including inferred targets absent from package manifests, and name their
1178
+ actual cached rebuild. It constructs no Nx graph, shadow output registry or second interpolation policy. The record is
1179
+ inside the target's outputs, so a cache hit restores it with them; a live developer link (`@smoothbricks/cli`,
1180
+ "Declared Links") checks it to tell a current build from a stale one by content. The shell entry that bootstraps the
1181
+ plugin (`tooling/direnv/enter-shell.ts`) keeps its build marker under `node_modules/.cache`, not under `outDir`, where
1182
+ the executor would remove it as a file its emit did not write.
1175
1183
  - `scriptc-<arch>-<family>` (`scriptc-arm64-macos`, `scriptc-x64-macos`, `scriptc-arm64-linux`, `scriptc-x64-linux`) are
1176
1184
  inferred for a package that lists `scriptc` in `devDependencies` (that pin is the compiler the targets run; the
1177
1185
  workspace root lists `scriptc` in `trustedDependencies` so its install links the platform compiler) together with each
@@ -41,7 +41,7 @@
41
41
  "packages/nx-plugin/src/executors/bounded-exec/executor.ts": "49c29558c8b4f3db21a807d6c2e453ca5805ffd8b41f8f79d3837fde33a92396",
42
42
  "packages/nx-plugin/src/executors/bounded-exec/gateway-lease.ts": "f3f73bd92db30e4857f98fb8bd2b9a75b2a5a2a318f094e075ed803c1f9a0117",
43
43
  "packages/nx-plugin/src/executors/bounded-exec/hang-diagnostics.ts": "bcaa9e52384f6c481a9cc2835ba6e8ffe167afa3cb51623fdf8dc3493876f335",
44
- "packages/nx-plugin/src/executors/bounded-exec/host-memory.darwin.ts": "c27868c1240ec9c782f80c7848f5eee1bc59146e3a56a6c8c0fc05bcf82b2931",
44
+ "packages/nx-plugin/src/executors/bounded-exec/host-memory.darwin.ts": "235ea2aaaee024d750ffe8fae5570c04d607b0eeb2fa2f0fc4309812c95692c1",
45
45
  "packages/nx-plugin/src/executors/bounded-exec/host-memory.linux.ts": "d473dc8475066d4914c419b6b803b6937ebb2577348f0dda8f9ed9def60e8584",
46
46
  "packages/nx-plugin/src/executors/bounded-exec/host-memory.ts": "cc7e94023adad2c3645c7a4ba18d07545426ae8d02840a2cf8117494d0915234",
47
47
  "packages/nx-plugin/src/executors/bounded-exec/leaf-peak.ts": "0b1b94992b2cbb9e3759785424ceb202667053c5571edf3a79cdd0b206e54a1d",
@@ -77,7 +77,7 @@
77
77
  "packages/nx-plugin/src/executors/shard-timings/executor.ts": "b5a84156fe2b71022603990a9c3e7721de7995987770b14ae60bd7e8117b1adf",
78
78
  "packages/nx-plugin/src/executors/shard-timings/schema.ts": "e509fc7be7a5752f1636f3006440e747d8381c40a46229627b11d7cdad633f69",
79
79
  "packages/nx-plugin/src/executors/typescript-emit/emit-overrides.ts": "3021aeb756492632d3e5da7a7ae15dded0a9399f73734c0d1fea1c69a2bd4b85",
80
- "packages/nx-plugin/src/executors/typescript-emit/executor.ts": "efea02d08b3112d1927562f09dd8641d8a5e1653af876d89314e35fc381e6a53",
80
+ "packages/nx-plugin/src/executors/typescript-emit/executor.ts": "318e6b8e90b6e811ecb45158586d4ab481fc40eb72950dc4600d5c065acbae86",
81
81
  "packages/nx-plugin/src/executors/typescript-emit/schema.ts": "20edee20fee80198bbfbb743848d4ac3e41e734050abd511fceafcea9cd6da28",
82
82
  "packages/nx-plugin/src/executors/wrangler-deploy/deploy-hash.ts": "0ebcf8d795e703f98e43e6e89c474f71f8bde0e09a807564480dbc73ce30cd8a",
83
83
  "packages/nx-plugin/src/executors/wrangler-deploy/executor.ts": "de8a8b2c069692b1280b779b48069f7c0cd30cd394c416c821d635130c365a5a",
@@ -1 +1 @@
1
- {"version":3,"file":"host-memory.darwin.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.darwin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,kBAAkB,CAAC;AAgJrE,eAAO,MAAM,YAAY,EAAE,UAAgF,CAAC"}
1
+ {"version":3,"file":"host-memory.darwin.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/host-memory.darwin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,kBAAkB,CAAC;AAqIrE,eAAO,MAAM,YAAY,EAAE,UAAgF,CAAC"}
@@ -1,41 +1,28 @@
1
- import { execFile } from 'node:child_process';
2
- import { promisify } from 'node:util';
1
+ import { totalmem } from 'node:os';
3
2
  /**
4
3
  * macOS's memory mechanisms. A run's memory is the host's, `hw.memsize`: macOS confines no job to a memory limit of
5
4
  * its own. Bun reports a leaf's `ru_maxrss` in bytes, as macOS does.
6
5
  */
7
- const run = promisify(execFile);
8
6
  const HW_MEMSIZE = 'hw.memsize';
9
- async function readRunMemory() {
10
- let stdout;
11
- try {
12
- ({ stdout } = await run('sysctl', ['-n', HW_MEMSIZE]));
13
- }
14
- catch (error) {
15
- return {
16
- ok: false,
17
- error: {
18
- operation: 'read-run-memory',
19
- code: 'unreadable',
20
- subject: HW_MEMSIZE,
21
- cause: error instanceof Error ? error.message : String(error),
22
- },
23
- };
24
- }
25
- const value = stdout.trim();
26
- const bytes = Number(value);
27
- if (!/^\d+$/.test(value) || !Number.isSafeInteger(bytes) || bytes === 0) {
28
- return {
7
+ /**
8
+ * `hw.memsize`, read in this process: `os.totalmem()` is that sysctl under Node (libuv) and Bun alike. A child `sysctl`
9
+ * cost each run a process, and under `bun test` its exit sometimes never settled, so the pool a run serves never
10
+ * started: eight concurrent runs of the pool-silence cases at load ~50 hung there 6 times out of 24.
11
+ */
12
+ function readRunMemory() {
13
+ const bytes = totalmem();
14
+ if (!Number.isSafeInteger(bytes) || bytes <= 0) {
15
+ return Promise.resolve({
29
16
  ok: false,
30
17
  error: {
31
18
  operation: 'read-run-memory',
32
19
  code: 'malformed',
33
20
  subject: HW_MEMSIZE,
34
- cause: `sysctl printed ${JSON.stringify(value)}, not a byte count`,
21
+ cause: `os.totalmem() answered ${String(bytes)}, not a byte count`,
35
22
  },
36
- };
23
+ });
37
24
  }
38
- return { ok: true, memory: { bytes, source: HW_MEMSIZE } };
25
+ return Promise.resolve({ ok: true, memory: { bytes, source: HW_MEMSIZE } });
39
26
  }
40
27
  /**
41
28
  * Bun's FFI, as the peak waiter reaches it. Node, which runs the executor, loads this module too and has no `bun:ffi`;
@@ -10,10 +10,34 @@ export interface CompilerInvocation {
10
10
  command: 'ttsc';
11
11
  args: string[];
12
12
  cwd: string;
13
+ /** Variables set over the caller's environment for this run. */
14
+ env: Readonly<Record<string, string>>;
13
15
  }
14
- export type CompilerRunner = (invocation: CompilerInvocation) => Promise<boolean>;
16
+ /** What a compiler run answers: whether it succeeded and, when it did, every file its emit produced (absolute). */
17
+ export type CompilerOutcome = {
18
+ readonly success: false;
19
+ } | {
20
+ readonly success: true;
21
+ readonly emitted: ReadonlySet<string>;
22
+ };
23
+ export type CompilerRunner = (invocation: CompilerInvocation) => Promise<CompilerOutcome>;
15
24
  export default function typescriptEmitExecutor(options: TypeScriptEmitOptions, context: TypeScriptEmitContext): Promise<TypeScriptEmitResult>;
16
25
  /** Runs with the caller's compiler and task identity; only the Nx entry reads its environment. */
17
26
  export declare function runTypeScriptEmit(options: TypeScriptEmitOptions, context: TypeScriptEmitContext, runner: CompilerRunner, producer?: BuildInputProducer): Promise<TypeScriptEmitResult>;
27
+ /** ttsc's `--verbose` output, read: the files its emit named, and the rest of the output to show as ttsc wrote it. */
28
+ export interface VerboseEmit {
29
+ readonly emitted: ReadonlySet<string>;
30
+ readonly output: string;
31
+ }
32
+ /**
33
+ * Reads the emitted-file list of ttsc's `--verbose` summary. ttsc's own host and its direct-tsgo lane
34
+ * (cmd/ttsc/build.go, BuildExecution `verboseBuildSummary`), its utility-plugin host (utility/host.go, given the
35
+ * header and list by the managed ttsc patch) and a plugin's own transform host (typia's `ttsc-typia`) all print it
36
+ * in one shape under their own label: a header `// <label>: tsconfig=<config> cwd=<cwd>
37
+ * … emit=true …`, then `// <label>: emitted=<count> files`, then one ` + <path>` line per file, relative to that
38
+ * cwd. Pruning trusts this list, so output without one, or a list longer or shorter than its count, is refused rather
39
+ * than read as an emit that produced nothing.
40
+ */
41
+ export declare function readVerboseEmit(stdout: string): VerboseEmit;
18
42
  export {};
19
43
  //# sourceMappingURL=executor.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/executors/typescript-emit/executor.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,kBAAkB,EASxB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEzD,UAAU,qBAAqB;IAC7B,IAAI,EAAE,MAAM,CAAC;CACd;AAED,UAAU,oBAAoB;IAC5B,OAAO,EAAE,OAAO,CAAC;CAClB;AAQD,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,cAAc,GAAG,CAAC,UAAU,EAAE,kBAAkB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAElF,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC5C,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,oBAAoB,CAAC,CAG/B;AAED,kGAAkG;AAClG,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EAAE,qBAAqB,EAC9B,MAAM,EAAE,cAAc,EACtB,QAAQ,CAAC,EAAE,kBAAkB,GAC5B,OAAO,CAAC,oBAAoB,CAAC,CAwB/B"}
1
+ {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/executors/typescript-emit/executor.ts"],"names":[],"mappings":"AAKA,OAAO,EAEL,KAAK,kBAAkB,EASxB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEzD,UAAU,qBAAqB;IAC7B,IAAI,EAAE,MAAM,CAAC;CACd;AAED,UAAU,oBAAoB;IAC5B,OAAO,EAAE,OAAO,CAAC;CAClB;AAQD,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,gEAAgE;IAChE,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACvC;AAED,mHAAmH;AACnH,MAAM,MAAM,eAAe,GACvB;IAAE,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAA;CAAE,GAC3B;IAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CAAE,CAAC;AAEtE,MAAM,MAAM,cAAc,GAAG,CAAC,UAAU,EAAE,kBAAkB,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC;AAE1F,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC5C,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,oBAAoB,CAAC,CAG/B;AAED,kGAAkG;AAClG,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EAAE,qBAAqB,EAC9B,MAAM,EAAE,cAAc,EACtB,QAAQ,CAAC,EAAE,kBAAkB,GAC5B,OAAO,CAAC,oBAAoB,CAAC,CAiD/B;AAoKD,sHAAsH;AACtH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAiC3D"}
@@ -1,9 +1,9 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { once } from 'node:events';
3
3
  import { realpathSync } from 'node:fs';
4
- import { chmod, lstat, readdir, rm, rmdir, stat } from 'node:fs/promises';
4
+ import { chmod, lstat, readdir, realpath, rm, rmdir, stat } from 'node:fs/promises';
5
5
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
6
- import { buildInputProducer, buildInputRecord, gitWorkspaceRoot, globPathspec, pathspecFiles, workspacePath, writeBuildInputs, } from '@smoothbricks/build-inputs';
6
+ import { BUILD_INPUTS_FILE, buildInputProducer, buildInputRecord, gitWorkspaceRoot, globPathspec, pathspecFiles, workspacePath, writeBuildInputs, } from '@smoothbricks/build-inputs';
7
7
  import { readJsonFile } from 'nx/src/devkit-exports.js';
8
8
  import { EMIT_OVERRIDES } from './emit-overrides.js';
9
9
  export default function typescriptEmitExecutor(options, context) {
@@ -19,17 +19,43 @@ export async function runTypeScriptEmit(options, context, runner, producer) {
19
19
  // Taken before the emit, so a source edited while it runs is recorded with its earlier bytes and reads as changed.
20
20
  const inputs = outputDirectory === null ? null : typeScriptInputs(tsConfigPath, tsConfig, producer);
21
21
  const previous = outputDirectory === null ? null : await snapshotOutput(outputDirectory);
22
- const javascriptSucceeded = await runner({
22
+ const compiled = await runner({
23
23
  command: 'ttsc',
24
- args: ['-p', tsConfigPath, '--emit', ...EMIT_OVERRIDES],
24
+ args: ['-p', tsConfigPath, '--emit', '--verbose', ...EMIT_OVERRIDES],
25
25
  cwd,
26
+ // ttsc then leaves an output that already holds its bytes alone (the managed ttsc patch, driver/rewrite.go). Only a
27
+ // caller that prunes by the emitted-file list may ask: one that finds stale outputs by which files the emit did not
28
+ // touch, as this executor did through 0.7.9, would remove every unchanged output.
29
+ env: { TTSC_WRITE_IF_CHANGED: '1' },
26
30
  });
27
- if (!javascriptSucceeded) {
31
+ if (!compiled.success) {
28
32
  return { success: false };
29
33
  }
30
- // Before the execute bits: their chmod moves a bin's ctime, as a write does.
31
- if (previous !== null)
32
- await removeUnwritten(previous);
34
+ if (previous !== null) {
35
+ // Spelled as the snapshot spells outDir's files: a temp directory reached as /var is /private/var to the compiler.
36
+ const produced = new Set();
37
+ const missing = [];
38
+ for (const file of compiled.emitted) {
39
+ const real = await realpath(file).catch((error) => {
40
+ if (errorCode(error) === 'ENOENT')
41
+ return null;
42
+ throw error;
43
+ });
44
+ if (real === null)
45
+ missing.push(file);
46
+ else
47
+ produced.add(real);
48
+ }
49
+ // A list that names an output the emit did not leave cannot tell this emit's outputs from an earlier build's, so it
50
+ // removes nothing, and the failed task leaves Nx nothing to cache.
51
+ if (missing.length > 0) {
52
+ console.error(`ttsc listed ${String(missing.length)} emitted file(s) that do not exist, so ${outputDirectory} was not pruned: ${missing.slice(0, 5).join(', ')}`);
53
+ return { success: false };
54
+ }
55
+ if (inputs !== null)
56
+ produced.add(join(previous.root, BUILD_INPUTS_FILE));
57
+ await removeUnproduced(previous, produced);
58
+ }
33
59
  await makePackageBinsExecutable(cwd, options.executableOutputs ?? []);
34
60
  if (outputDirectory !== null && inputs !== null)
35
61
  writeBuildInputs(outputDirectory, inputs);
@@ -59,7 +85,7 @@ function emitOutputDirectory(tsConfigPath, tsConfig) {
59
85
  * snapshot refuses before the compiler runs rather than let the removal take them.
60
86
  */
61
87
  async function snapshotOutput(outputDirectory) {
62
- const files = new Map();
88
+ const files = new Set();
63
89
  const directories = [];
64
90
  const root = await lstat(outputDirectory).catch((error) => {
65
91
  if (errorCode(error) === 'ENOENT')
@@ -67,10 +93,11 @@ async function snapshotOutput(outputDirectory) {
67
93
  throw error;
68
94
  });
69
95
  if (root === null)
70
- return { files, directories };
96
+ return { root: outputDirectory, files, directories };
71
97
  // Removal follows outDir's own path only: through a link it would reach files outside the project.
72
98
  if (!root.isDirectory())
73
99
  throw new Error(`${outputDirectory} is the emit's outDir but is not a directory`);
100
+ const real = await realpath(outputDirectory);
74
101
  const visit = async (directory) => {
75
102
  for (const name of await readdir(directory)) {
76
103
  const path = join(directory, name);
@@ -83,28 +110,23 @@ async function snapshotOutput(outputDirectory) {
83
110
  if (/\.[cm]?tsx?$/.test(name) && !/\.d\.[cm]?ts$/.test(name)) {
84
111
  throw new Error(`${outputDirectory} is the emit's outDir but holds the TypeScript source ${path}`);
85
112
  }
86
- files.set(path, { ino: stats.ino, ctimeNs: stats.ctimeNs, mtimeNs: stats.mtimeNs });
113
+ files.add(path);
87
114
  }
88
115
  };
89
- await visit(outputDirectory);
90
- return { files, directories };
116
+ await visit(real);
117
+ return { root: real, files, directories };
91
118
  }
92
119
  /**
93
- * Removes what the emit left as it was: each file whose identity is still the one it had before the emit, then
94
- * each directory that removal emptied. The compiler truncates and rewrites every output of a non-incremental emit,
95
- * unchanged bytes included (TypeScript-Go's writer opens each output O_TRUNC, and the transform hosts ttsc loads write
96
- * through it), so an untouched file is one this emit no longer produces. The executor tests hold that contract against
97
- * the real compiler by emitting twice.
120
+ * Removes what an earlier build left and this emit no longer produces: each file outDir held before the emit that is
121
+ * not in `produced`, then each directory that removal emptied. The emit opts ttsc into writing an output only when
122
+ * its bytes differ (`TTSC_WRITE_IF_CHANGED`, the managed ttsc patch's `driver/rewrite.go`), so an output this emit
123
+ * produced unchanged keeps its file, inode and mtime included, and only the compiler's own list of what it emitted
124
+ * tells it from a file an earlier build left. The executor tests hold that against the real compiler by emitting twice
125
+ * and by deleting a module, and hold an identity-pruning emit without the opt-in to keeping every output.
98
126
  */
99
- async function removeUnwritten({ files, directories }) {
100
- for (const [path, before] of files) {
101
- const now = await lstat(path, { bigint: true }).catch((error) => {
102
- if (errorCode(error) === 'ENOENT')
103
- return null;
104
- throw error;
105
- });
106
- const untouched = now !== null && now.ino === before.ino && now.ctimeNs === before.ctimeNs && now.mtimeNs === before.mtimeNs;
107
- if (untouched)
127
+ async function removeUnproduced({ files, directories }, produced) {
128
+ for (const path of files) {
129
+ if (!produced.has(path))
108
130
  await rm(path, { force: true });
109
131
  }
110
132
  for (const directory of [...directories].reverse()) {
@@ -165,23 +187,91 @@ async function makePackageBinsExecutable(cwd, outputs) {
165
187
  const outputStat = await stat(outputPath);
166
188
  // TypeScript creates emitted files with the process default mode. A package
167
189
  // bin that loses its execute bits is present but unusable through
168
- // node_modules/.bin, so restore the semantic guarantee declared by `bin`.
169
- await chmod(outputPath, outputStat.mode | 0o111);
190
+ // node_modules/.bin, so restore the semantic guarantee declared by `bin`. A
191
+ // chmod moves the file's ctime even when the mode stays, so a bin that kept
192
+ // its bits is left alone.
193
+ if ((outputStat.mode & 0o111) !== 0o111)
194
+ await chmod(outputPath, outputStat.mode | 0o111);
170
195
  }
171
196
  }
197
+ /**
198
+ * The `--verbose` summary lines that name an emit's files, as the transform host that emitted prints them under its own
199
+ * label (`// ttsc:`, `// ttsc-typia build:`): its header, its count and one line per file.
200
+ */
201
+ const VERBOSE_SUMMARY = /^\/\/ [^:]+: tsconfig=.* cwd=(.*?) (?:sites=\d+ )?emit=true(?: .*)?$/;
202
+ const VERBOSE_COUNT = /^\/\/ [^:]+: emitted=(\d+) files$/;
203
+ const VERBOSE_FILE = /^ {2}\+ (.+)$/;
204
+ /**
205
+ * Reads the emitted-file list of ttsc's `--verbose` summary. ttsc's own host and its direct-tsgo lane
206
+ * (cmd/ttsc/build.go, BuildExecution `verboseBuildSummary`), its utility-plugin host (utility/host.go, given the
207
+ * header and list by the managed ttsc patch) and a plugin's own transform host (typia's `ttsc-typia`) all print it
208
+ * in one shape under their own label: a header `// <label>: tsconfig=<config> cwd=<cwd>
209
+ * … emit=true …`, then `// <label>: emitted=<count> files`, then one ` + <path>` line per file, relative to that
210
+ * cwd. Pruning trusts this list, so output without one, or a list longer or shorter than its count, is refused rather
211
+ * than read as an emit that produced nothing.
212
+ */
213
+ export function readVerboseEmit(stdout) {
214
+ const emitted = new Set();
215
+ const output = [];
216
+ const lines = stdout.split('\n');
217
+ let summaries = 0;
218
+ let cwd = null;
219
+ for (let index = 0; index < lines.length; index += 1) {
220
+ const line = lines[index] ?? '';
221
+ output.push(line);
222
+ const header = VERBOSE_SUMMARY.exec(line);
223
+ if (header !== null) {
224
+ cwd = header[1] ?? null;
225
+ continue;
226
+ }
227
+ const count = VERBOSE_COUNT.exec(line);
228
+ if (count === null || cwd === null)
229
+ continue;
230
+ const expected = Number(count[1]);
231
+ for (let listed = 0; listed < expected; listed += 1) {
232
+ index += 1;
233
+ const file = VERBOSE_FILE.exec(lines[index] ?? '')?.[1];
234
+ if (file === undefined) {
235
+ throw new Error(`ttsc --verbose: the summary counts ${expected} emitted files but lists ${listed}`);
236
+ }
237
+ emitted.add(resolve(cwd, file));
238
+ }
239
+ if (VERBOSE_FILE.test(lines[index + 1] ?? '')) {
240
+ throw new Error(`ttsc --verbose: the summary counts ${expected} emitted files but lists more`);
241
+ }
242
+ summaries += 1;
243
+ cwd = null;
244
+ }
245
+ if (summaries === 0)
246
+ throw new Error('ttsc --verbose: the emit printed no emitted-file summary');
247
+ return { emitted, output: output.join('\n') };
248
+ }
172
249
  async function runCompiler(invocation) {
173
250
  const child = spawn(invocation.command, invocation.args, {
174
251
  cwd: invocation.cwd,
175
- stdio: 'inherit',
252
+ env: { ...process.env, ...invocation.env },
253
+ stdio: ['inherit', 'pipe', 'inherit'],
176
254
  windowsHide: true,
177
255
  });
256
+ let stdout = '';
257
+ child.stdout.setEncoding('utf8');
258
+ child.stdout.on('data', (chunk) => {
259
+ stdout += chunk;
260
+ });
178
261
  try {
179
262
  // eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: a TypeScript emit lasts as long as its work; the operator's interrupt or the enclosing runner's bound ends a hung one
180
- await once(child, 'exit');
181
- return child.exitCode === 0 && child.signalCode === null;
263
+ await once(child, 'close');
182
264
  }
183
265
  catch (error) {
266
+ process.stdout.write(stdout);
184
267
  console.error(`${invocation.command}: ${error instanceof Error ? error.message : String(error)}`);
185
- return false;
268
+ return { success: false };
269
+ }
270
+ if (child.exitCode !== 0 || child.signalCode !== null) {
271
+ process.stdout.write(stdout);
272
+ return { success: false };
186
273
  }
274
+ const read = readVerboseEmit(stdout);
275
+ process.stdout.write(read.output);
276
+ return { success: true, emitted: read.emitted };
187
277
  }
@@ -90,6 +90,48 @@ index f6ee81fb1af41deea022b92e838a7c4665eb9778..112faf913ce38b1f4624b38db3783937
90
90
  }
91
91
 
92
92
  // forceNoEmit sets noEmit so the program type-checks without writing files.
93
+ diff --git a/driver/rewrite.go b/driver/rewrite.go
94
+ index 79be74d24e879da216ad680d23ecfeb5f11a29db..fb57c1ac6ef61f72abe75fd118915c7b632c5bb6 100644
95
+ --- a/driver/rewrite.go
96
+ +++ b/driver/rewrite.go
97
+ @@ -317,22 +317,28 @@ func (p *Program) emitsBuildInfo() bool {
98
+ }
99
+
100
+ // DefaultWriteFile is the default disk writer used when EmitAll's caller does not
101
+ -// supply a custom WriteFile callback. Existing files are normally truncated; a
102
+ -// watch child setting TTSC_WATCH_WRITE_IF_CHANGED=1 preserves byte-identical
103
+ -// regular output files instead. A failed write can leave partial output.
104
+ -// Requested creation modes are subject to the native platform and umask, and
105
+ -// do not replace existing file permissions.
106
+ +// supply a custom WriteFile callback. Existing files are normally truncated,
107
+ +// which a caller that finds stale outputs by file identity relies on: one that
108
+ +// removes each outDir file the emit did not touch (smoo's typescript-emit
109
+ +// executor through nx-plugin 0.7.9) would otherwise remove every unchanged
110
+ +// output. A watch child setting TTSC_WATCH_WRITE_IF_CHANGED=1, or a caller that
111
+ +// prunes its outDir by the emitted-file list and sets TTSC_WRITE_IF_CHANGED=1,
112
+ +// preserves byte-identical regular output files instead, inode and mtime
113
+ +// included: rewriting identical bytes is file churn every copy-on-write clone of
114
+ +// the output pays for. A failed write can leave partial output. Requested
115
+ +// creation modes are subject to the native platform and umask, and do not
116
+ +// replace existing file permissions.
117
+ //
118
+ -// @evidence contracts/common.md#principled-implementation The watch-only opt-in compares an existing writable regular output before deciding whether native writing is needed; all other writes use the original directory and file operations.
119
+ +// @evidence contracts/common.md#principled-implementation An opted-in emit compares an existing writable regular output before deciding whether native writing is needed; all other writes use the original directory and file operations.
120
+ // @evidence contracts/common.md#clear-and-simple-design One opt-in decision selects the unchanged-output check or the original writer.
121
+ // @evidence contracts/common.md#prohibited-implementation-shortcuts Native file identity and bytes decide whether to skip; unknown state falls back to the original writer and its errors.
122
+ -// @evidence contracts/common.md#meaningful-documentation Native prose states normal truncation, watch opt-in, partial failure and permission behavior.
123
+ +// @evidence contracts/common.md#meaningful-documentation Native prose states normal truncation, who relies on it, both opt-ins, partial failure and permission behavior.
124
+ // @evidence contracts/portability.md#os-neutral-implementation filepath.Dir, os.MkdirAll and os.WriteFile preserve native path and permission behavior; the optional check uses native file identity rather than path spelling.
125
+ -// @evidence contracts/performance.md#efficient-algorithms Normal writes retain one directory creation and file write. Watch writes add metadata and streamed comparison of up to the existing output size, then write only when content differs or safe comparison fails; no prior-output cache is retained.
126
+ +// @evidence contracts/performance.md#efficient-algorithms Normal writes retain one directory creation and file write. Opted-in writes add metadata and streamed comparison of up to the existing output size, then write only when content differs or safe comparison fails; no prior-output cache is retained.
127
+ // @evidenceExclude contracts/performance.md#reuse-equivalent-work Each emit write is requested independently; this writer owns no cross-emit reuse coordinator.
128
+ // @evidence contracts/performance.md#bound-retention-and-release-resources The optional comparison closes its one read/write handle before return or fallback; os.WriteFile owns and closes the later write handle. No output buffer or handle survives this call, while created directories and partial or completed files remain on disk.
129
+ func DefaultWriteFile(fileName, text string) error {
130
+ - return writeFileWithWatchMode(fileName, text, os.Getenv("TTSC_WATCH_WRITE_IF_CHANGED") == "1")
131
+ + return writeFileWithWatchMode(fileName, text, os.Getenv("TTSC_WATCH_WRITE_IF_CHANGED") == "1" || os.Getenv("TTSC_WRITE_IF_CHANGED") == "1")
132
+ }
133
+
134
+ func defaultWriteFile(fileName, text string) error {
93
135
  diff --git a/lib/internal/javascriptRuntimeCapabilities.js b/lib/internal/javascriptRuntimeCapabilities.js
94
136
  --- a/lib/internal/javascriptRuntimeCapabilities.js
95
137
  +++ b/lib/internal/javascriptRuntimeCapabilities.js
@@ -644,3 +686,46 @@ diff --git a/lib/plugin/internal/source/waitForPluginBinary.js b/lib/plugin/inte
644
686
  if (clock - startedAt >= opts.timeoutMs) {
645
687
  throw new Error(`ttsc: timed out after ${(0, formatDuration_1.formatDuration)(clock - startedAt)} waiting for ` +
646
688
  `${opts.lockInfo.label} "${opts.lockInfo.pluginName}" at ${opts.lockDir}; ` +
689
+ diff --git a/utility/host.go b/utility/host.go
690
+ index fab9fcc72897ec54540a5f5e93dfe115bba102a5..9d72bb780e5e104f6a41889a712efe677bdfe3d8 100644
691
+ --- a/utility/host.go
692
+ +++ b/utility/host.go
693
+ @@ -7,6 +7,7 @@ import (
694
+ "io"
695
+ "os"
696
+ "path/filepath"
697
+ + "sort"
698
+ "strings"
699
+
700
+ shimcompiler "github.com/microsoft/typescript-go/shim/compiler"
701
+ @@ -234,7 +235,10 @@ func RunBuildWithIO(args []string, stdout, stderr io.Writer) int {
702
+ opts.quiet = false // --verbose overrides the default --quiet=true
703
+ }
704
+ if !opts.quiet {
705
+ - fmt.Fprintf(opts.stdout, "// ttsc utility: plugins=%d emit=%v\n", len(entries), !opts.noEmit)
706
+ + // The header and the " + <path>" list below are the summary shape ttsc's
707
+ + // build lane (cmd/ttsc/build.go) and ttsc-typia print, so a caller that
708
+ + // reads the emitted files from --verbose reads this lane the same way.
709
+ + fmt.Fprintf(opts.stdout, "// ttsc utility: tsconfig=%s cwd=%s emit=%v plugins=%d\n", opts.tsconfig, opts.cwd, !opts.noEmit, len(entries))
710
+ }
711
+ writeFile, finishPreamble := makeSourcePreambleWriteFile(prog)
712
+ var snapshot func() map[string][]string
713
+ @@ -284,6 +288,18 @@ func RunBuildWithIO(args []string, stdout, stderr io.Writer) int {
714
+ }
715
+ if !opts.quiet {
716
+ fmt.Fprintf(opts.stdout, "// ttsc utility: emitted=%d files\n", len(emittedFiles))
717
+ + listed := make([]string, 0, len(emittedFiles))
718
+ + for fileName := range emittedFiles {
719
+ + listed = append(listed, fileName)
720
+ + }
721
+ + sort.Strings(listed)
722
+ + for _, fileName := range listed {
723
+ + rel := fileName
724
+ + if relative, err := filepath.Rel(opts.cwd, fileName); err == nil {
725
+ + rel = relative
726
+ + }
727
+ + fmt.Fprintln(opts.stdout, " +", rel)
728
+ + }
729
+ }
730
+ return 0
731
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.7.8",
3
+ "version": "0.7.10",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -474,6 +474,7 @@
474
474
  "^dependencyDefault",
475
475
  "bunToolchain",
476
476
  "{workspaceRoot}/packages/cowshed/crates/cowshed-core/src/port_zero.cjs",
477
+ "{workspaceRoot}/packages/lmao-ttsc/plugin.cjs",
477
478
  {
478
479
  "dependentTasksOutputFiles": "**/test-stream/archive.tar.zst",
479
480
  "transitive": false
@@ -501,6 +502,7 @@
501
502
  "^dependencyDefault",
502
503
  "bunToolchain",
503
504
  "{workspaceRoot}/packages/cowshed/crates/cowshed-core/src/port_zero.cjs",
505
+ "{workspaceRoot}/packages/lmao-ttsc/plugin.cjs",
504
506
  {
505
507
  "dependentTasksOutputFiles": "**/test-stream/archive.tar.zst",
506
508
  "transitive": false
@@ -528,6 +530,7 @@
528
530
  "^dependencyDefault",
529
531
  "bunToolchain",
530
532
  "{workspaceRoot}/packages/cowshed/crates/cowshed-core/src/port_zero.cjs",
533
+ "{workspaceRoot}/packages/lmao-ttsc/plugin.cjs",
531
534
  {
532
535
  "dependentTasksOutputFiles": "**/test-stream/archive.tar.zst",
533
536
  "transitive": false
@@ -555,6 +558,7 @@
555
558
  "^dependencyDefault",
556
559
  "bunToolchain",
557
560
  "{workspaceRoot}/packages/cowshed/crates/cowshed-core/src/port_zero.cjs",
561
+ "{workspaceRoot}/packages/lmao-ttsc/plugin.cjs",
558
562
  {
559
563
  "dependentTasksOutputFiles": "**/test-stream/archive.tar.zst",
560
564
  "transitive": false
@@ -650,9 +654,9 @@
650
654
  },
651
655
  "dependencies": {
652
656
  "@nx/js": "24.0.3",
653
- "@smoothbricks/build-inputs": "0.0.2",
657
+ "@smoothbricks/build-inputs": "0.0.3",
654
658
  "@smoothbricks/cargo-retain": "0.0.2",
655
- "@smoothbricks/cowshed": "0.4.6",
659
+ "@smoothbricks/cowshed": "0.4.8",
656
660
  "@smoothbricks/nextest-extract": "0.0.2",
657
661
  "jsonc-parser": "^3.3.1",
658
662
  "minimatch": "10.2.5",
@@ -665,7 +669,7 @@
665
669
  "yaml": "^2.9.0"
666
670
  },
667
671
  "devDependencies": {
668
- "@smoothbricks/lmao": "0.6.0",
672
+ "@smoothbricks/lmao": "0.6.1",
669
673
  "@smoothbricks/validation": "0.1.13",
670
674
  "fast-check": "^4.4.0"
671
675
  }
@@ -1,5 +1,4 @@
1
- import { execFile } from 'node:child_process';
2
- import { promisify } from 'node:util';
1
+ import { totalmem } from 'node:os';
3
2
  import type { HostMemory, RunMemoryReading } from './host-memory.js';
4
3
 
5
4
  /**
@@ -7,38 +6,27 @@ import type { HostMemory, RunMemoryReading } from './host-memory.js';
7
6
  * its own. Bun reports a leaf's `ru_maxrss` in bytes, as macOS does.
8
7
  */
9
8
 
10
- const run = promisify(execFile);
11
9
  const HW_MEMSIZE = 'hw.memsize';
12
10
 
13
- async function readRunMemory(): Promise<RunMemoryReading> {
14
- let stdout: string;
15
- try {
16
- ({ stdout } = await run('sysctl', ['-n', HW_MEMSIZE]));
17
- } catch (error) {
18
- return {
19
- ok: false,
20
- error: {
21
- operation: 'read-run-memory',
22
- code: 'unreadable',
23
- subject: HW_MEMSIZE,
24
- cause: error instanceof Error ? error.message : String(error),
25
- },
26
- };
27
- }
28
- const value = stdout.trim();
29
- const bytes = Number(value);
30
- if (!/^\d+$/.test(value) || !Number.isSafeInteger(bytes) || bytes === 0) {
31
- return {
11
+ /**
12
+ * `hw.memsize`, read in this process: `os.totalmem()` is that sysctl under Node (libuv) and Bun alike. A child `sysctl`
13
+ * cost each run a process, and under `bun test` its exit sometimes never settled, so the pool a run serves never
14
+ * started: eight concurrent runs of the pool-silence cases at load ~50 hung there 6 times out of 24.
15
+ */
16
+ function readRunMemory(): Promise<RunMemoryReading> {
17
+ const bytes = totalmem();
18
+ if (!Number.isSafeInteger(bytes) || bytes <= 0) {
19
+ return Promise.resolve({
32
20
  ok: false,
33
21
  error: {
34
22
  operation: 'read-run-memory',
35
23
  code: 'malformed',
36
24
  subject: HW_MEMSIZE,
37
- cause: `sysctl printed ${JSON.stringify(value)}, not a byte count`,
25
+ cause: `os.totalmem() answered ${String(bytes)}, not a byte count`,
38
26
  },
39
- };
27
+ });
40
28
  }
41
- return { ok: true, memory: { bytes, source: HW_MEMSIZE } };
29
+ return Promise.resolve({ ok: true, memory: { bytes, source: HW_MEMSIZE } });
42
30
  }
43
31
 
44
32
  /**
@@ -1,9 +1,10 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { once } from 'node:events';
3
3
  import { realpathSync } from 'node:fs';
4
- import { chmod, lstat, readdir, rm, rmdir, stat } from 'node:fs/promises';
4
+ import { chmod, lstat, readdir, realpath, rm, rmdir, stat } from 'node:fs/promises';
5
5
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
6
6
  import {
7
+ BUILD_INPUTS_FILE,
7
8
  type BuildInputProducer,
8
9
  type BuildInputRecord,
9
10
  buildInputProducer,
@@ -36,9 +37,16 @@ export interface CompilerInvocation {
36
37
  command: 'ttsc';
37
38
  args: string[];
38
39
  cwd: string;
40
+ /** Variables set over the caller's environment for this run. */
41
+ env: Readonly<Record<string, string>>;
39
42
  }
40
43
 
41
- export type CompilerRunner = (invocation: CompilerInvocation) => Promise<boolean>;
44
+ /** What a compiler run answers: whether it succeeded and, when it did, every file its emit produced (absolute). */
45
+ export type CompilerOutcome =
46
+ | { readonly success: false }
47
+ | { readonly success: true; readonly emitted: ReadonlySet<string> };
48
+
49
+ export type CompilerRunner = (invocation: CompilerInvocation) => Promise<CompilerOutcome>;
42
50
 
43
51
  export default function typescriptEmitExecutor(
44
52
  options: TypeScriptEmitOptions,
@@ -63,17 +71,42 @@ export async function runTypeScriptEmit(
63
71
  // Taken before the emit, so a source edited while it runs is recorded with its earlier bytes and reads as changed.
64
72
  const inputs = outputDirectory === null ? null : typeScriptInputs(tsConfigPath, tsConfig, producer);
65
73
  const previous = outputDirectory === null ? null : await snapshotOutput(outputDirectory);
66
- const javascriptSucceeded = await runner({
74
+ const compiled = await runner({
67
75
  command: 'ttsc',
68
- args: ['-p', tsConfigPath, '--emit', ...EMIT_OVERRIDES],
76
+ args: ['-p', tsConfigPath, '--emit', '--verbose', ...EMIT_OVERRIDES],
69
77
  cwd,
78
+ // ttsc then leaves an output that already holds its bytes alone (the managed ttsc patch, driver/rewrite.go). Only a
79
+ // caller that prunes by the emitted-file list may ask: one that finds stale outputs by which files the emit did not
80
+ // touch, as this executor did through 0.7.9, would remove every unchanged output.
81
+ env: { TTSC_WRITE_IF_CHANGED: '1' },
70
82
  });
71
- if (!javascriptSucceeded) {
83
+ if (!compiled.success) {
72
84
  return { success: false };
73
85
  }
74
86
 
75
- // Before the execute bits: their chmod moves a bin's ctime, as a write does.
76
- if (previous !== null) await removeUnwritten(previous);
87
+ if (previous !== null) {
88
+ // Spelled as the snapshot spells outDir's files: a temp directory reached as /var is /private/var to the compiler.
89
+ const produced = new Set<string>();
90
+ const missing: string[] = [];
91
+ for (const file of compiled.emitted) {
92
+ const real = await realpath(file).catch((error: unknown) => {
93
+ if (errorCode(error) === 'ENOENT') return null;
94
+ throw error;
95
+ });
96
+ if (real === null) missing.push(file);
97
+ else produced.add(real);
98
+ }
99
+ // A list that names an output the emit did not leave cannot tell this emit's outputs from an earlier build's, so it
100
+ // removes nothing, and the failed task leaves Nx nothing to cache.
101
+ if (missing.length > 0) {
102
+ console.error(
103
+ `ttsc listed ${String(missing.length)} emitted file(s) that do not exist, so ${outputDirectory} was not pruned: ${missing.slice(0, 5).join(', ')}`,
104
+ );
105
+ return { success: false };
106
+ }
107
+ if (inputs !== null) produced.add(join(previous.root, BUILD_INPUTS_FILE));
108
+ await removeUnproduced(previous, produced);
109
+ }
77
110
  await makePackageBinsExecutable(cwd, options.executableOutputs ?? []);
78
111
  if (outputDirectory !== null && inputs !== null) writeBuildInputs(outputDirectory, inputs);
79
112
 
@@ -100,18 +133,12 @@ function emitOutputDirectory(tsConfigPath: string, tsConfig: TsConfigJson): stri
100
133
  }
101
134
 
102
135
  /**
103
- * A file as writes change it: a write moves its mtime and, on POSIX, its ctime (Windows reports birth time there), and a
104
- * replacement gives it another inode.
136
+ * What outDir held before the emit: its files and its directories, each directory before those inside it, all spelled
137
+ * from outDir's real path.
105
138
  */
106
- interface FileIdentity {
107
- readonly ino: bigint;
108
- readonly ctimeNs: bigint;
109
- readonly mtimeNs: bigint;
110
- }
111
-
112
- /** What outDir held before the emit: its files by identity, and its directories, each before those inside it. */
113
139
  interface OutputSnapshot {
114
- readonly files: ReadonlyMap<string, FileIdentity>;
140
+ readonly root: string;
141
+ readonly files: ReadonlySet<string>;
115
142
  readonly directories: readonly string[];
116
143
  }
117
144
 
@@ -120,15 +147,16 @@ interface OutputSnapshot {
120
147
  * snapshot refuses before the compiler runs rather than let the removal take them.
121
148
  */
122
149
  async function snapshotOutput(outputDirectory: string): Promise<OutputSnapshot> {
123
- const files = new Map<string, FileIdentity>();
150
+ const files = new Set<string>();
124
151
  const directories: string[] = [];
125
152
  const root = await lstat(outputDirectory).catch((error: unknown) => {
126
153
  if (errorCode(error) === 'ENOENT') return null;
127
154
  throw error;
128
155
  });
129
- if (root === null) return { files, directories };
156
+ if (root === null) return { root: outputDirectory, files, directories };
130
157
  // Removal follows outDir's own path only: through a link it would reach files outside the project.
131
158
  if (!root.isDirectory()) throw new Error(`${outputDirectory} is the emit's outDir but is not a directory`);
159
+ const real = await realpath(outputDirectory);
132
160
  const visit = async (directory: string): Promise<void> => {
133
161
  for (const name of await readdir(directory)) {
134
162
  const path = join(directory, name);
@@ -141,29 +169,24 @@ async function snapshotOutput(outputDirectory: string): Promise<OutputSnapshot>
141
169
  if (/\.[cm]?tsx?$/.test(name) && !/\.d\.[cm]?ts$/.test(name)) {
142
170
  throw new Error(`${outputDirectory} is the emit's outDir but holds the TypeScript source ${path}`);
143
171
  }
144
- files.set(path, { ino: stats.ino, ctimeNs: stats.ctimeNs, mtimeNs: stats.mtimeNs });
172
+ files.add(path);
145
173
  }
146
174
  };
147
- await visit(outputDirectory);
148
- return { files, directories };
175
+ await visit(real);
176
+ return { root: real, files, directories };
149
177
  }
150
178
 
151
179
  /**
152
- * Removes what the emit left as it was: each file whose identity is still the one it had before the emit, then
153
- * each directory that removal emptied. The compiler truncates and rewrites every output of a non-incremental emit,
154
- * unchanged bytes included (TypeScript-Go's writer opens each output O_TRUNC, and the transform hosts ttsc loads write
155
- * through it), so an untouched file is one this emit no longer produces. The executor tests hold that contract against
156
- * the real compiler by emitting twice.
180
+ * Removes what an earlier build left and this emit no longer produces: each file outDir held before the emit that is
181
+ * not in `produced`, then each directory that removal emptied. The emit opts ttsc into writing an output only when
182
+ * its bytes differ (`TTSC_WRITE_IF_CHANGED`, the managed ttsc patch's `driver/rewrite.go`), so an output this emit
183
+ * produced unchanged keeps its file, inode and mtime included, and only the compiler's own list of what it emitted
184
+ * tells it from a file an earlier build left. The executor tests hold that against the real compiler by emitting twice
185
+ * and by deleting a module, and hold an identity-pruning emit without the opt-in to keeping every output.
157
186
  */
158
- async function removeUnwritten({ files, directories }: OutputSnapshot): Promise<void> {
159
- for (const [path, before] of files) {
160
- const now = await lstat(path, { bigint: true }).catch((error: unknown) => {
161
- if (errorCode(error) === 'ENOENT') return null;
162
- throw error;
163
- });
164
- const untouched =
165
- now !== null && now.ino === before.ino && now.ctimeNs === before.ctimeNs && now.mtimeNs === before.mtimeNs;
166
- if (untouched) await rm(path, { force: true });
187
+ async function removeUnproduced({ files, directories }: OutputSnapshot, produced: ReadonlySet<string>): Promise<void> {
188
+ for (const path of files) {
189
+ if (!produced.has(path)) await rm(path, { force: true });
167
190
  }
168
191
  for (const directory of [...directories].reverse()) {
169
192
  await rmdir(directory).catch((error: unknown) => {
@@ -237,23 +260,96 @@ async function makePackageBinsExecutable(cwd: string, outputs: readonly string[]
237
260
  const outputStat = await stat(outputPath);
238
261
  // TypeScript creates emitted files with the process default mode. A package
239
262
  // bin that loses its execute bits is present but unusable through
240
- // node_modules/.bin, so restore the semantic guarantee declared by `bin`.
241
- await chmod(outputPath, outputStat.mode | 0o111);
263
+ // node_modules/.bin, so restore the semantic guarantee declared by `bin`. A
264
+ // chmod moves the file's ctime even when the mode stays, so a bin that kept
265
+ // its bits is left alone.
266
+ if ((outputStat.mode & 0o111) !== 0o111) await chmod(outputPath, outputStat.mode | 0o111);
267
+ }
268
+ }
269
+
270
+ /**
271
+ * The `--verbose` summary lines that name an emit's files, as the transform host that emitted prints them under its own
272
+ * label (`// ttsc:`, `// ttsc-typia build:`): its header, its count and one line per file.
273
+ */
274
+ const VERBOSE_SUMMARY = /^\/\/ [^:]+: tsconfig=.* cwd=(.*?) (?:sites=\d+ )?emit=true(?: .*)?$/;
275
+ const VERBOSE_COUNT = /^\/\/ [^:]+: emitted=(\d+) files$/;
276
+ const VERBOSE_FILE = /^ {2}\+ (.+)$/;
277
+
278
+ /** ttsc's `--verbose` output, read: the files its emit named, and the rest of the output to show as ttsc wrote it. */
279
+ export interface VerboseEmit {
280
+ readonly emitted: ReadonlySet<string>;
281
+ readonly output: string;
282
+ }
283
+
284
+ /**
285
+ * Reads the emitted-file list of ttsc's `--verbose` summary. ttsc's own host and its direct-tsgo lane
286
+ * (cmd/ttsc/build.go, BuildExecution `verboseBuildSummary`), its utility-plugin host (utility/host.go, given the
287
+ * header and list by the managed ttsc patch) and a plugin's own transform host (typia's `ttsc-typia`) all print it
288
+ * in one shape under their own label: a header `// <label>: tsconfig=<config> cwd=<cwd>
289
+ * … emit=true …`, then `// <label>: emitted=<count> files`, then one ` + <path>` line per file, relative to that
290
+ * cwd. Pruning trusts this list, so output without one, or a list longer or shorter than its count, is refused rather
291
+ * than read as an emit that produced nothing.
292
+ */
293
+ export function readVerboseEmit(stdout: string): VerboseEmit {
294
+ const emitted = new Set<string>();
295
+ const output: string[] = [];
296
+ const lines = stdout.split('\n');
297
+ let summaries = 0;
298
+ let cwd: string | null = null;
299
+ for (let index = 0; index < lines.length; index += 1) {
300
+ const line = lines[index] ?? '';
301
+ output.push(line);
302
+ const header = VERBOSE_SUMMARY.exec(line);
303
+ if (header !== null) {
304
+ cwd = header[1] ?? null;
305
+ continue;
306
+ }
307
+ const count = VERBOSE_COUNT.exec(line);
308
+ if (count === null || cwd === null) continue;
309
+ const expected = Number(count[1]);
310
+ for (let listed = 0; listed < expected; listed += 1) {
311
+ index += 1;
312
+ const file = VERBOSE_FILE.exec(lines[index] ?? '')?.[1];
313
+ if (file === undefined) {
314
+ throw new Error(`ttsc --verbose: the summary counts ${expected} emitted files but lists ${listed}`);
315
+ }
316
+ emitted.add(resolve(cwd, file));
317
+ }
318
+ if (VERBOSE_FILE.test(lines[index + 1] ?? '')) {
319
+ throw new Error(`ttsc --verbose: the summary counts ${expected} emitted files but lists more`);
320
+ }
321
+ summaries += 1;
322
+ cwd = null;
242
323
  }
324
+ if (summaries === 0) throw new Error('ttsc --verbose: the emit printed no emitted-file summary');
325
+ return { emitted, output: output.join('\n') };
243
326
  }
244
327
 
245
- async function runCompiler(invocation: CompilerInvocation): Promise<boolean> {
328
+ async function runCompiler(invocation: CompilerInvocation): Promise<CompilerOutcome> {
246
329
  const child = spawn(invocation.command, invocation.args, {
247
330
  cwd: invocation.cwd,
248
- stdio: 'inherit',
331
+ env: { ...process.env, ...invocation.env },
332
+ stdio: ['inherit', 'pipe', 'inherit'],
249
333
  windowsHide: true,
250
334
  });
335
+ let stdout = '';
336
+ child.stdout.setEncoding('utf8');
337
+ child.stdout.on('data', (chunk: string) => {
338
+ stdout += chunk;
339
+ });
251
340
  try {
252
341
  // eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: a TypeScript emit lasts as long as its work; the operator's interrupt or the enclosing runner's bound ends a hung one
253
- await once(child, 'exit');
254
- return child.exitCode === 0 && child.signalCode === null;
342
+ await once(child, 'close');
255
343
  } catch (error) {
344
+ process.stdout.write(stdout);
256
345
  console.error(`${invocation.command}: ${error instanceof Error ? error.message : String(error)}`);
257
- return false;
346
+ return { success: false };
347
+ }
348
+ if (child.exitCode !== 0 || child.signalCode !== null) {
349
+ process.stdout.write(stdout);
350
+ return { success: false };
258
351
  }
352
+ const read = readVerboseEmit(stdout);
353
+ process.stdout.write(read.output);
354
+ return { success: true, emitted: read.emitted };
259
355
  }