Skip to content

readFileSync with 'utf-8' option broken by node v20.5.0 #377

@lg250137

Description

@lg250137

Node v20.5.0 introduced an optimization to fs.readFileSync for utf-8 files. Unfortunately, this breaks mock-fs. Here is a minimal test case that works with Node v20.4.0 but breaks with Node v20.5.0:

import mockFs from "mock-fs";
import { test } from "node:test";
import { readFileSync } from "fs";
import assert from "node:assert/strict";

test("mockFs", () => {
    mockFs({ "file": "contents" })
    const fileContents = readFileSync("file", 'utf-8').toString()
    assert.equal(fileContents, "contents")
})

In Node v20.5.0, I get the error Error: ENOENT: no such file or directory, open 'file'.
The above test case also works in Node v20.5.0 if I remove the 'utf-8' option from readFileSync. This issue specifically occurs for readFileSync with 'utf-8' option.

Here is the relevant commit: nodejs/node#48658

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions