Skip to content

Commit 2ad4108

Browse files
vedchaudhariaduh95
authored andcommitted
fs: restore stack traces of fs/promises readFile and writeFile errors
Errors from readFile(), writeFile() and appendFile() have had no stack frames since they started using a single thread pool round trip, as the stack was captured from a C++ callback with no JS frames on it. Capture it after the await instead, like the rest of fs/promises. Refs: #65327 Refs: #65489 Assisted-by: claude:opus-5.5 Signed-off-by: vedchaudhari <vedc2853@gmail.com> PR-URL: #66757 Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
1 parent 9321692 commit 2ad4108

3 files changed

Lines changed: 106 additions & 19 deletions

File tree

‎lib/internal/fs/promises.js‎

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,8 +2226,14 @@ async function writeFile(path, data, options) {
22262226

22272227
if (!flush && !isCustomIterable(data) && data.byteLength <= kWriteFileMaxChunkSize) {
22282228
path = getValidatedPath(path);
2229-
await writeFileInOneRoundTrip(path, stringToFlags(flag, 'options.flag'),
2230-
parseFileMode(options.mode, 'mode', 0o666), data);
2229+
const flagsNumber = stringToFlags(flag, 'options.flag');
2230+
const mode = parseFileMode(options.mode, 'mode', 0o666);
2231+
try {
2232+
await writeFileInOneRoundTrip(path, flagsNumber, mode, data);
2233+
} catch (err) {
2234+
ErrorCaptureStackTrace(err);
2235+
throw err;
2236+
}
22312237
checkAborted(options.signal); // An abort during the write still wins.
22322238
return;
22332239
}
@@ -2244,29 +2250,32 @@ async function writeFile(path, data, options) {
22442250

22452251
/**
22462252
* Open + write + close as one thread pool round trip.
2253+
* Throws synchronously if the permission model denies access, while the
2254+
* caller's frames are still on the stack. The caller is responsible for
2255+
* capturing the stack trace of a rejection, as no JS frames are on the stack
2256+
* when the job completes.
22472257
* @param {string|Buffer} path Validated path
22482258
* @param {number} flagsNumber
22492259
* @param {number} mode
22502260
* @param {ArrayBufferView} data
22512261
* @returns {Promise<void>}
22522262
*/
22532263
function writeFileInOneRoundTrip(path, flagsNumber, mode, data) {
2254-
return new Promise((resolve, reject) => {
2255-
const job = new binding.WriteFileJob(path, flagsNumber, mode, data);
2264+
const job = new binding.WriteFileJob(path, flagsNumber, mode, data);
2265+
const promise = new Promise((resolve, reject) => {
22562266
job.ondone = (err) => {
22572267
if (err != null) {
2258-
ErrorCaptureStackTrace(err, writeFileInOneRoundTrip);
22592268
reject(err);
22602269
} else {
22612270
resolve();
22622271
}
22632272
};
2264-
const accessError = job.run(path);
2265-
if (accessError !== undefined) {
2266-
ErrorCaptureStackTrace(accessError, writeFileInOneRoundTrip);
2267-
reject(accessError);
2268-
}
22692273
});
2274+
const accessError = job.run(path);
2275+
if (accessError !== undefined) {
2276+
throw accessError;
2277+
}
2278+
return promise;
22702279
}
22712280

22722281
function isCustomIterable(obj) {
@@ -2309,7 +2318,15 @@ async function readFile(path, options) {
23092318
// files of up to one chunk; larger and non-regular files (size 0) come
23102319
// back as an open fd + size and are read by readFileHandle() as before.
23112320
path = getValidatedPath(path);
2312-
const { 0: buffer, 1: fd, 2: size } = await readFileInOneRoundTrip(path, stringToFlags(flag));
2321+
const flagsNumber = stringToFlags(flag);
2322+
let result;
2323+
try {
2324+
result = await readFileInOneRoundTrip(path, flagsNumber);
2325+
} catch (err) {
2326+
ErrorCaptureStackTrace(err);
2327+
throw err;
2328+
}
2329+
const { 0: buffer, 1: fd, 2: size } = result;
23132330
if (fd === -1) {
23142331
checkAborted(options.signal); // An abort during the read still wins.
23152332
return options.encoding ? buffer.toString(options.encoding) : buffer;
@@ -2323,28 +2340,31 @@ async function readFile(path, options) {
23232340
}
23242341

23252342
/**
2343+
* Throws synchronously if the permission model denies access, while the
2344+
* caller's frames are still on the stack. The caller is responsible for
2345+
* capturing the stack trace of a rejection, as no JS frames are on the stack
2346+
* when the job completes.
23262347
* @param {string|Buffer} path Validated path
23272348
* @param {number} flagsNumber
23282349
* @returns {Promise<[Buffer|undefined, number, number|undefined]>} [buffer, -1] or [undefined, fd, size]
23292350
*/
23302351
function readFileInOneRoundTrip(path, flagsNumber) {
2331-
return new Promise((resolve, reject) => {
2332-
const job = new binding.ReadFileJob(path, flagsNumber, kReadFileBufferLength);
2352+
const job = new binding.ReadFileJob(path, flagsNumber, kReadFileBufferLength);
2353+
const promise = new Promise((resolve, reject) => {
23332354
job.ondone = (err, buffer, fd, size, closeErr) => {
23342355
const error = err ?? closeErr;
23352356
if (error != null) {
2336-
ErrorCaptureStackTrace(error, readFileInOneRoundTrip);
23372357
reject(error);
23382358
} else {
23392359
resolve([buffer, fd, size]);
23402360
}
23412361
};
2342-
const accessError = job.run(path);
2343-
if (accessError !== undefined) {
2344-
ErrorCaptureStackTrace(accessError, readFileInOneRoundTrip);
2345-
reject(accessError);
2346-
}
23472362
});
2363+
const accessError = job.run(path);
2364+
if (accessError !== undefined) {
2365+
throw accessError;
2366+
}
2367+
return promise;
23482368
}
23492369

23502370
async function* _watch(filename, options = kEmptyObject) {

‎test/parallel/test-fs-promises.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const fs = require('fs');
99
const fsPromises = fs.promises;
1010
const {
1111
access,
12+
appendFile,
1213
chmod,
1314
chown,
1415
copyFile,
@@ -62,6 +63,30 @@ assert.strictEqual(
6263
}
6364
).then(common.mustCall());
6465

66+
assert.rejects(
67+
readFile('this file does not exist'),
68+
{
69+
code: 'ENOENT',
70+
stack: /at async assert\.rejects/
71+
}
72+
).then(common.mustCall());
73+
74+
assert.rejects(
75+
writeFile(path.join('this dir does not exist', 'file'), 'data'),
76+
{
77+
code: 'ENOENT',
78+
stack: /at async assert\.rejects/
79+
}
80+
).then(common.mustCall());
81+
82+
assert.rejects(
83+
appendFile(path.join('this dir does not exist', 'file'), 'data'),
84+
{
85+
code: 'ENOENT',
86+
stack: /at async assert\.rejects/
87+
}
88+
).then(common.mustCall());
89+
6590
assert.rejects(
6691
access(__filename, 8),
6792
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { spawnSync } = require('child_process');
6+
const tmpdir = require('../common/tmpdir');
7+
8+
tmpdir.refresh();
9+
10+
// The permission model denies access before the first await, so the stack
11+
// still has the frames of a caller that does not await the promise.
12+
const { status, stderr } = spawnSync(
13+
process.execPath,
14+
[
15+
'--permission',
16+
'-e',
17+
`
18+
const assert = require('assert');
19+
const { readFile, writeFile, appendFile } = require('fs/promises');
20+
const target = ${JSON.stringify(tmpdir.resolve('file'))};
21+
22+
function readCaller() { return readFile(target); }
23+
function writeCaller() { return writeFile(target, 'data'); }
24+
function appendCaller() { return appendFile(target, 'data'); }
25+
async function awaitAppendCaller() { await appendFile(target, 'data'); }
26+
27+
const cases = [
28+
[readCaller, /at readCaller /],
29+
[writeCaller, /at writeCaller /],
30+
[appendCaller, /at appendCaller /],
31+
[awaitAppendCaller, /at awaitAppendCaller /],
32+
];
33+
let checked = 0;
34+
for (const [caller, stack] of cases) {
35+
assert.rejects(caller(), { code: 'ERR_ACCESS_DENIED', stack })
36+
.then(() => checked++);
37+
}
38+
process.on('exit', () => assert.strictEqual(checked, cases.length));
39+
`,
40+
],
41+
);
42+
assert.strictEqual(status, 0, stderr.toString());

0 commit comments

Comments
 (0)