Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.writeFileSync regression in v21.3.0 on MacOS #50989

Closed
jeremymeng opened this issue Dec 1, 2023 · 4 comments · Fixed by #50990
Closed

fs.writeFileSync regression in v21.3.0 on MacOS #50989

jeremymeng opened this issue Dec 1, 2023 · 4 comments · Fixed by #50990
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@jeremymeng
Copy link

Version

v21.3.0

Platform

Darwin air@xxx.net 20.3.0 Darwin Kernel Version 20.3.0: Thu Jan 21 00:06:51 PST 2021; root:xnu-7195.81.3~1/RELEASE_ARM64_T8101 arm64

Subsystem

fs

What steps will reproduce the bug?

Run the following code with Node v21.3.0

const { mkdtempSync, writeFileSync } = require("fs")
const { join } = require("path")
const { tmpdir } = require("os")

const testTitle = "azure-arc";
const tempDir = mkdtempSync(join(tmpdir(), testTitle));
const tempFile = join(tempDir, testTitle);
const key = "challenge key";
console.dir({ tempFile })
writeFileSync(tempFile, key, { encoding: "utf8" });

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

V21.2.0 worked as expected

ym@air node-test % nvm use 21.2.0
Now using node v21.2.0 (npm v10.2.3)
ym@air node-test % node fs.js    
{
  tempFile: '/var/folders/hn/r4xrm7195j5gqyr4dj4tmd3r0000gp/T/azure-arclhqY5s/azure-arc'
}
ym@air node-test % cat /var/folders/hn/r4xrm7195j5gqyr4dj4tmd3r0000gp/T/azure-arclhqY5s/azure-arc
challenge key

What do you see instead?

{
  tempFile: '/var/folders/hn/r4xrm7195j5gqyr4dj4tmd3r0000gp/T/azure-arcpSm7cb/azure-arc'
}
node:fs:2352
    return binding.writeFileUtf8(
                   ^

Error: ENOENT: no such file or directory, open '/var/folders/hn/r4xrm7195j5gqyr4dj4tmd3r0000gp/T/azure-arcpSm7cb/azure-arc'
    at writeFileSync (node:fs:2352:20)
    at Object.<anonymous> (/Users/ym/working/node-test/fs.js:12:1)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/var/folders/hn/r4xrm7195j5gqyr4dj4tmd3r0000gp/T/azure-arcpSm7cb/azure-arc'
}

Node.js v21.3.0

Additional information

No response

@MuriloKakazu
Copy link
Contributor

MuriloKakazu commented Dec 1, 2023

It seems the behavior for fs.writeFileSync with utf8 encoding when the file does not exist has been changed on 21.3.0. Its not just a problem with macOS, but every OS.

You can wait on PR #50990 or alternatively pass the w flag explicitly, so the file is created if it does not exist:

fs.writeFileSync('./test.txt', 'test', {encoding: 'utf8', flag: 'w'}); --> works
fs.writeFileSync('./test.txt', 'test', {encoding: 'utf8'}); --> does not work

@jeremymeng
Copy link
Author

Its not just a problem with macOS, but every OS.

I don't know why but our tests are passing on Linux and Windows.

@ljharb
Copy link
Member

ljharb commented Dec 4, 2023

This may be breaking me in eslint-plugin-import as well https://github.com/import-js/eslint-plugin-import/actions/runs/7083055767/job/19274802696

@MuriloKakazu
Copy link
Contributor

It looks like the fix will be released in v21.4.0: #51043

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants