Skip to content

Commit b925379

Browse files
committed
fs: warn on non-portable mkdtemp() templates
Refs: #26435 PR-URL: #26980 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d11c4be commit b925379

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

lib/fs.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ const {
7373
toUnixTimestamp,
7474
validateOffsetLengthRead,
7575
validateOffsetLengthWrite,
76-
validatePath
76+
validatePath,
77+
warnOnNonPortableTemplate
7778
} = require('internal/fs/utils');
7879
const {
7980
CHAR_FORWARD_SLASH,
@@ -1721,6 +1722,7 @@ function mkdtemp(prefix, options, callback) {
17211722
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
17221723
}
17231724
nullCheck(prefix, 'prefix');
1725+
warnOnNonPortableTemplate(prefix);
17241726
const req = new FSReqCallback();
17251727
req.oncomplete = callback;
17261728
binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, req);
@@ -1733,6 +1735,7 @@ function mkdtempSync(prefix, options) {
17331735
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
17341736
}
17351737
nullCheck(prefix, 'prefix');
1738+
warnOnNonPortableTemplate(prefix);
17361739
const path = `${prefix}XXXXXX`;
17371740
const ctx = { path };
17381741
const result = binding.mkdtemp(path, options.encoding,

lib/internal/fs/promises.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const {
3131
toUnixTimestamp,
3232
validateOffsetLengthRead,
3333
validateOffsetLengthWrite,
34-
validatePath
34+
validatePath,
35+
warnOnNonPortableTemplate
3536
} = require('internal/fs/utils');
3637
const {
3738
parseMode,
@@ -461,6 +462,7 @@ async function mkdtemp(prefix, options) {
461462
throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
462463
}
463464
nullCheck(prefix);
465+
warnOnNonPortableTemplate(prefix);
464466
return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises);
465467
}
466468

lib/internal/fs/utils.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,18 @@ const validatePath = hideStackFrames((path, propName = 'path') => {
432432
}
433433
});
434434

435+
let nonPortableTemplateWarn = true;
436+
437+
function warnOnNonPortableTemplate(template) {
438+
// Template strings passed to the mkdtemp() family of functions should not
439+
// end with 'X' because they are handled inconsistently across platforms.
440+
if (nonPortableTemplateWarn && template.endsWith('X')) {
441+
process.emitWarning('mkdtemp() templates ending with X are not portable. ' +
442+
'For details see: https://nodejs.org/api/fs.html');
443+
nonPortableTemplateWarn = false;
444+
}
445+
}
446+
435447
module.exports = {
436448
assertEncoding,
437449
copyObject,
@@ -448,5 +460,6 @@ module.exports = {
448460
toUnixTimestamp,
449461
validateOffsetLengthRead,
450462
validateOffsetLengthWrite,
451-
validatePath
463+
validatePath,
464+
warnOnNonPortableTemplate
452465
};

test/parallel/test-fs-mkdtemp.js

+5
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ fs.mkdtemp(path.join(tmpdir.path, 'bar.'), common.mustCall(handler));
2929
// Same test as above, but making sure that passing an options object doesn't
3030
// affect the way the callback function is handled.
3131
fs.mkdtemp(path.join(tmpdir.path, 'bar.'), {}, common.mustCall(handler));
32+
33+
const warningMsg = 'mkdtemp() templates ending with X are not portable. ' +
34+
'For details see: https://nodejs.org/api/fs.html';
35+
common.expectWarning('Warning', warningMsg);
36+
fs.mkdtemp(path.join(tmpdir.path, 'bar.X'), common.mustCall(handler));

0 commit comments

Comments
 (0)