Skip to content

Convert stdio to TypeScript - #8120

Merged
ry merged 2 commits into
mainfrom
ry/stdio-ts
Nov 2, 2026
Merged

ry merged 2 commits into
mainfrom
ry/stdio-ts

Conversation

@ry

@ry ry commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Part of a broader effort to reduce C++ JSG. Writes to stdout/stderr through node:fs and process.stdout/stderr are line-buffered and logged through console.log by C++ that calls back into JavaScript for every line. That logic is JS-shaped, so this moves it to TypeScript (node-internal:internal_fs_stdio).

Following #8105, the TypeScript implementation is behind a new TS_STDIO autogate, and the C++ one stays in place for rollback. fs-stdio-test.js runs under a C++ test configuration and a TypeScript one, each asserting which implementation it ran. The stdio golden-output tests also run with all autogates on.

With the gate on, output is the same except a partial line flushed outside a request is now logged rather than dropped, and no server-side warning is logged when console.log is missing. No API surface changes.

Writes to the stdio file descriptors through node:fs are line-buffered
and logged through globalThis.console.log by StdioFile in C++. With the
TS_STDIO autogate enabled, that is done by node-internal:internal_fs_stdio
instead, which node:fs calls for fds 0-2 before reaching native code. The
C++ implementation is retained for rollback.

FileSystemModule gains tsStdio(), which reports the autogate, and
stdioPrefix(), since the configured logging prefixes are not otherwise
visible to TypeScript.

fs-stdio-test.js runs under a C++ cell and a TypeScript cell, each
asserting which implementation it runs, and the stdio golden-output tests
gain @all-autogates variants.

With the autogate enabled, behaviour matches the C++ implementation,
except that a partial line flushed outside a request is logged (StdioFile
drops it) and no warning is logged to the server when console.log is
missing. Neither changes the API surface exposed to Workers.
@ry
ry requested review from a team as code owners October 31, 2026 17:34

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 2bff94a2.

This review, of 40543edb

💬 Approved with comments — The gated TypeScript port matches the C++ stdio behaviour on the paths checked. The remaining points are INFO-level: missing tests for two call sites, no warning when console.log is missing, and a small change in behaviour for closed stdio fds.

Findings: 3 info

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 3 info

The port matches C++ on the main paths; three INFO-level gaps.

Recommendation: 💬 Optional Nits

  • [INFO] With the gate on, the stdio branches in writeFileSync and callback writeFile have no test; only writeSync/writevSync is exercised. (src/node/internal/internal_fs_sync.ts:823)
  • [INFO] The TypeScript path drops output without a message when console.log is missing, where the C++ path logs a warning. (src/node/internal/internal_fs_stdio.ts:68)
  • [INFO] isStdioFd bypasses the virtual file system's fd table, so with the gate on a write to fd 1 or 2 after fs.closeSync() succeeds instead of failing. The pull request description does not list this change. (src/node/internal/internal_fs_stdio.ts:92)
View full review

On the paths checked, the TypeScript StdioStream matches the C++ StdioFile behaviour:

  • line splitting;
  • the 4 KiB tail kept for a partial line;
  • the 16 KiB cap on each write;
  • prefix formatting;
  • safe re-entrancy, because #pending is cleared before console.log runs.

Three minor points:

  1. Tests for the new call sites. fs-stdio-test.js only exercises writeSync, which goes through writevSync. These branches have no test under either configuration:

    • writeFileSync(1, ...) (internal_fs_sync.ts:823);
    • callback writeFile(1, ...) (internal_fs_callback.ts:1351-1354);
    • a test where console.log writes back into writeSync, which would lock in the re-entrancy handling.
  2. Missing console.log. internal_fs_stdio.ts:65-68 returns without a message when console or console.log is missing or not a function. The C++ path logs a warning in this case, so a deleted or replaced console.log is harder to diagnose with the gate on.

  3. Closed stdio fds. isStdioFd returns true for fds 0–2 before the virtual file system is consulted, so writeSync(1, ...) after fs.closeSync(1) now succeeds where the C++ path would fail on the closed fd. This is an edge case and it is behind the autogate. The description says only partial-line flushing changes, though, so it should either be listed there or the TypeScript path should respect closed fds.


✅ All other checks passed

🔒 Security: ✅ LGTM

No exploitable issues:

  • Only fds 0–2 are routed to the new path.
  • Buffers are capped and copied.
  • The prefix comes from the isolate, not from user input.
  • Only data the caller wrote to stdout or stderr is logged, as the C++ path already did.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 40543edb

writeFileSync, appendFileSync and callback writeFile reach stdio through
their own branches in node:fs, separate from writeSync's.

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 Approved with comments — The new writeFileToStdio test closes the earlier test-coverage gap. Two INFO notes from the last review still apply to code that has not changed: no warning when console.log is missing, and writes to closed stdio fds succeeding with the gate on.

Findings: 2 info

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 2 info

The earlier test-coverage note is resolved. Two INFO notes on unchanged code remain.

Recommendation: 💬 Optional Nits

  • [INFO] When console.log is missing or not a function, the TypeScript path drops output without any message. The C++ path logs a warning in that case. (src/node/internal/internal_fs_stdio.ts:68)
  • [INFO] isStdioFd does not check the virtual file system's fd table, so with the gate on a write to fd 1 or 2 after fs.closeSync() succeeds instead of failing. The pull request description does not list this change. (src/node/internal/internal_fs_stdio.ts:92)
View full review

🔄 Since last review: 1 finding resolved, 2 preserved, 0 new

Resolved: The new writeFileToStdio test (fs-stdio-test.js) covers writeFileSync(1, ...), appendFileSync(2, ...) and callback writeFile(1, ...). It runs under both the C++ and the TypeScript configuration, so the stdio branches in internal_fs_sync.ts and internal_fs_callback.ts are now tested.

Still open (INFO):

  • internal_fs_stdio.ts:64-68: #log returns without any message when console or console.log is missing or not a function. The C++ path logs a warning in that case, so with the gate on, a deleted or replaced console.log is harder to diagnose.
  • internal_fs_stdio.ts:91-93: isStdioFd returns true for fds 0-2 without checking the virtual file system's fd table. With the gate on, writeSync(1, ...) after fs.closeSync(1) therefore succeeds, where the C++ path fails. The pull request description lists partial-line flushing as the only behaviour change, so either add this change there or make the TypeScript path respect closed fds.

✅ All other checks passed

🔒 Security: ✅ LGTM

Only fds 0–2 are routed to the new path, and only behind the TS_STDIO autogate. Each write is capped at 16 KiB, and a partial line keeps at most 4 KiB. The prefix comes from the isolate, not from user input. The logged data is the caller's own stdout/stderr output, which the C++ path already logged.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 2bff94a2

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.31%. Comparing base (80f7da6) to head (2bff94a).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/workerd/api/filesystem.h 0.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8120      +/-   ##
==========================================
- Coverage   68.45%   68.31%   -0.15%     
==========================================
  Files         726      726              
  Lines      189225   189675     +450     
  Branches    29668    29675       +7     
==========================================
+ Hits       129543   129583      +40     
- Misses      42307    42717     +410     
  Partials    17375    17375              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ry
ry merged commit 0857ae4 into main Nov 2, 2026
38 of 40 checks passed
@ry
ry deleted the ry/stdio-ts branch November 2, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants