The issue involves complex fs logic and a regression in handling flush options.
The issue involves a regression where the `flush` option is ignored when writing/appending strings, particularly when `encoding` is specified. The fix requires understanding and modifying the `getOptions` logic in the fs module, which is complex and prone to bugs. The problem is test-focused, but the solution may involve broader changes to ensure consistency.
#50009 and #49884 conflict and were merged independently
This was exposed in https://github.com/nodejs/node/pull/60539 CI run
https://github.com/nodejs/node/blob/dbe45b767b0bb9c984206edd4ad0b5658d7c2fe4/test/parallel/test-fs-write-file-flush.js#L26-L37 fails if encoding is specified:
diff --git a/test/parallel/test-fs-write-file-flush.js b/test/parallel/test-fs-write-file-flush.js
index 98a8d637c5f..c28e536c528 100644
--- a/test/parallel/test-fs-write-file-flush.js
+++ b/test/parallel/test-fs-write-file-flush.js
@@ -26,7 +26,7 @@ test('synchronous version', async (t) => {
await t.test('performs flush', (t) => {
const spy = t.mock.method(fs, 'fsyncSync');
const file = nextFile();
- fs.writeFileSync(file, data, { flush: true });
+ fs.writeFileSync(file, data, { flush: true, encoding: 'utf8' });
const calls = spy.mock.calls;
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].result, undefined);
Here, if options are set to e.g. { flush: true } (or any other object), they do not default to those values
https://github.com/nodejs/node/blob/dbe45b767b0bb9c984206edd4ad0b5658d7c2fe4/lib/fs.js#L2322-L2327
As getOptions does not merge options with the default ones, unless it's just an encoding string:
https://github.com/nodejs/node/blob/dbe45b767b0bb9c984206edd4ad0b5658d7c2fe4/lib/internal/fs/utils.js#L318-L339
And the options.encoding === 'utf8' codepath was not called when options were set but options.encoding was undefined, as in flush testcases, resulting in the slow path
The bug was still triggered if both encoding and flush were set though
#60539 fixes that accidental slow path when options object without encoding is passed
But it exposes the flush bug even when encoding is not set
And fast path doesn't handle options.flush at all
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!