From 0b34ee9b4f3d3c7bc05eb62e06ff9253043039e5 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 15 Nov 2026 16:39:53 +0000 Subject: [PATCH 1/3] fs: move DEP0180 to End-of-Life Calling the fs.Stats constructor directly, with or without new, now throws ERR_ILLEGAL_CONSTRUCTOR. fs.Stats instances are only created internally. The class remains exported from node:fs so that instanceof checks continue to work. Internally, instances are constructed through a private symbol. This has been a runtime deprecation since v22.0.0. Refs: https://github.com/nodejs/node/pull/52067 Refs: https://github.com/nodejs/node/pull/51879 Assisted-by: Claude Signed-off-by: James M Snell --- doc/api/deprecations.md | 11 +++++++--- doc/api/fs.md | 4 ++++ lib/internal/fs/utils.js | 10 ++++++--- test/parallel/test-fs-stat.js | 34 +++++++++++------------------- test/parallel/test-fs-watchfile.js | 24 ++++----------------- 5 files changed, 35 insertions(+), 48 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 14b05789727c..b3246b606ef7 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -4073,6 +4073,9 @@ continue to work. -Type: Runtime +Type: End-of-Life -Calling `fs.Stats` class directly with `Stats()` or `new Stats()` is -deprecated due to being internals, not intended for public use. +Calling `fs.Stats` class directly with `Stats()` or `new Stats()` throws an +`ERR_ILLEGAL_CONSTRUCTOR` error. The constructor is internal and not intended +for public use. The class remains exported, so `instanceof fs.Stats` checks +continue to work. ### DEP0181: `Hmac` constructor diff --git a/doc/api/fs.md b/doc/api/fs.md index 673e6b7473ba..ed843f8df77d 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -8003,6 +8003,10 @@ changes: - version: REPLACEME pr-url: https://github.com/nodejs/node/pull/67716 description: Added `Stats.isStats` static method. + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/00000 + description: Calling the constructor directly now throws an + `ERR_ILLEGAL_CONSTRUCTOR` error. - version: v26.2.0 pr-url: https://github.com/nodejs/node/pull/60789 description: Added `Temporal.Instant` support. diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 5cc44d4e6419..22ac504b3fa1 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -34,6 +34,7 @@ const { UVException, codes: { ERR_FS_EISDIR, + ERR_ILLEGAL_CONSTRUCTOR, ERR_INCOMPATIBLE_OPTION_PAIR, ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, @@ -49,7 +50,6 @@ const { isUint8Array, } = require('internal/util/types'); const { - deprecate, isWindows, kEmptyObject, once, @@ -77,6 +77,7 @@ const kPartialAtimeNs = Symbol('partialAtimeNs'); const kPartialMtimeNs = Symbol('partialMtimeNs'); const kPartialCtimeNs = Symbol('partialCtimeNs'); const kPartialBirthtimeNs = Symbol('kPartialBirthtimeNs'); +const kPrivateConstructor = Symbol('kPrivateConstructor'); const assert = require('internal/assert'); const { @@ -703,9 +704,11 @@ BigIntStats.prototype._checkModeProperty = function(property) { return (this.mode & BigInt(S_IFMT)) === BigInt(property); }; -function Stats(dev, mode, nlink, uid, gid, rdev, blksize, +function Stats(privateSymbol, dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atimeS, atimeNs, mtimeS, mtimeNs, ctimeS, ctimeNs, birthtimeS, birthtimeNs) { + if (privateSymbol !== kPrivateConstructor) + throw new ERR_ILLEGAL_CONSTRUCTOR(); FunctionPrototypeCall(StatsBase, this, dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks); this.atimeMs = msFromTimeSpec(atimeS, atimeNs); @@ -758,6 +761,7 @@ function getStatsFromBinding(stats, offset = 0) { ); } return new Stats( + kPrivateConstructor, stats[0 + offset], stats[1 + offset], stats[2 + offset], stats[3 + offset], stats[4 + offset], stats[5 + offset], stats[6 + offset], stats[7 + offset], stats[8 + offset], @@ -1206,7 +1210,7 @@ module.exports = { getStatsFromBinding, stringToFlags, stringToSymlinkType, - Stats: deprecate(Stats, 'fs.Stats constructor is deprecated.', 'DEP0180'), + Stats, toUnixTimestamp, validateBufferArray, validateCpOptions, diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 93bf6573a109..099c1960ddc6 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -156,28 +156,18 @@ fs.open(__filename, 'r', undefined, common.mustCall((err, fd) => { fs.lstat(__filename, undefined, common.mustCall()); { - fs.Stats( - 0, // dev - 0, // mode - 0, // nlink - 0, // uid - 0, // gid - 0, // rdev - 0, // blksize - 0, // ino - 0, // size - 0, // blocks - Date.UTC(1970, 0, 1, 0, 0, 0), // atime - Date.UTC(1970, 0, 1, 0, 0, 0), // mtime - Date.UTC(1970, 0, 1, 0, 0, 0), // ctime - Date.UTC(1970, 0, 1, 0, 0, 0) // birthtime - ); - common.expectWarning({ - DeprecationWarning: [ - ['fs.Stats constructor is deprecated.', - 'DEP0180'], - ] - }); + // The fs.Stats constructor is not intended for public use. + const illegalConstructor = { + code: 'ERR_ILLEGAL_CONSTRUCTOR', + name: 'TypeError', + }; + assert.throws(() => fs.Stats(), illegalConstructor); + assert.throws(() => new fs.Stats(), illegalConstructor); + assert.throws(() => new fs.Stats(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0), illegalConstructor); + + // The class remains exported so that instanceof checks keep working. + assert(fs.statSync(__filename) instanceof fs.Stats); } { diff --git a/test/parallel/test-fs-watchfile.js b/test/parallel/test-fs-watchfile.js index e1fd3f4c3433..f47f32b19b70 100644 --- a/test/parallel/test-fs-watchfile.js +++ b/test/parallel/test-fs-watchfile.js @@ -1,4 +1,5 @@ 'use strict'; +// Flags: --expose-internals const common = require('../common'); const assert = require('assert'); @@ -6,6 +7,7 @@ const fs = require('fs'); const path = require('path'); const tmpdir = require('../common/tmpdir'); +const { getStatsFromBinding } = require('internal/fs/utils'); // Basic usage tests. assert.throws( @@ -31,26 +33,8 @@ assert.throws(() => { }, { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError' }); const enoentFile = tmpdir.resolve('non-existent-file'); -const expectedStatObject = new fs.Stats( - 0, // dev - 0, // mode - 0, // nlink - 0, // uid - 0, // gid - 0, // rdev - 0, // blksize - 0, // ino - 0, // size - 0, // blocks - 0, // atimeS - 0, // atimeNs - 0, // mtimeS - 0, // mtimeNs - 0, // ctime - 0, // ctimeNs - 0, // birthtime - 0, // birthtimeNs -); +// All fields are zero and the date fields are the UNIX epoch. +const expectedStatObject = getStatsFromBinding(new Float64Array(18)); tmpdir.refresh(); From f69777a44937beefdbe7ab9754b5239d53778f5b Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 15 Nov 2026 08:46:42 -0800 Subject: [PATCH 2/3] Apply batched suggestions from code review Co-authored-by: James M Snell --- doc/api/deprecations.md | 2 +- doc/api/fs.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index b3246b606ef7..5e0b81a6db9a 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -4074,7 +4074,7 @@ continue to work.