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: use w flag for writeFileSync with utf8 encoding when flag not specified #50990

Conversation

MuriloKakazu
Copy link
Contributor

@MuriloKakazu MuriloKakazu commented Dec 1, 2023

PR #49884 seems to have accidentally changed the behavior for fs.writeFileSync with utf-8 encoding when the file does not exist, as compared to previous node versions.

On a low level, it seems we are not passing the O_CREAT flag to uvlib anymore.

Examples:

In node 16.16.0: ✅

❯ nvm use 16.16
Now using node v16.16.0 (npm v8.11.0)
❯ node
Welcome to Node.js v16.16.0.
Type ".help" for more information.
> fs.writeFileSync('./test.txt', 'test', {encoding: 'utf8'});
undefined
> fs.readFileSync('./test.txt');
<Buffer 74 65 73 74>

In node 21.2.0: ✅

❯ nvm use 21.2
Now using node v21.2.0 (npm v10.2.3)
❯ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> fs.writeFileSync('./test.txt', 'test', {encoding: 'utf8'});
undefined
> fs.readFileSync('./test.txt');
<Buffer 74 65 73 74>

In node 21.3.0 (currently latest): ❌

❯ nvm use 21.3
Now using node v21.3.0 (npm v10.2.4)
❯ node
Welcome to Node.js v21.3.0.
Type ".help" for more information.
> fs.writeFileSync('./test.txt', 'test', {encoding: 'utf8'});
Uncaught Error: ENOENT: no such file or directory, open './test.txt'
    at Object.writeFileSync (node:fs:2352:20) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: './test.txt'
}

Currently, a workaround for 21.3.0 is to pass the w flag (which includes O_CREAT) explicitly when calling writeFileSync. e.g:

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

This PR will just set the w flag back as the default value when it is not specified, so its the same behavior from previous node versions.

Fixes #50989

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Dec 1, 2023
@MuriloKakazu MuriloKakazu changed the title fix: defaults to write flag when not specified for writeFileSync fs: use w flag for writeFileSync with utf8 encoding when flag not specified Dec 1, 2023
@MuriloKakazu MuriloKakazu force-pushed the fix/default-flag-fs-write-file-sync branch from b5305d0 to b5283b6 Compare December 1, 2023 07:07
@anonrig
Copy link
Member

anonrig commented Dec 1, 2023

Can you add a test?

@MuriloKakazu MuriloKakazu force-pushed the fix/default-flag-fs-write-file-sync branch from b5283b6 to aaa4edd Compare December 1, 2023 16:46
@MuriloKakazu
Copy link
Contributor Author

@anonrig Done :)

@lpinca lpinca added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 1, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 1, 2023
@nodejs-github-bot
Copy link
Collaborator

Copy link

@chenrui333 chenrui333 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. Thanks for the quick turnaround!

Homebrew/homebrew-core#156024

@bricss
Copy link

bricss commented Dec 2, 2023

It would be great to get this fixed asap with semver patch release at least 👮

@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. labels Dec 2, 2023
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Dec 3, 2023
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/50990
✔  Done loading data for nodejs/node/pull/50990
----------------------------------- PR info ------------------------------------
Title      fs: use w flag for writeFileSync with utf8 encoding when flag not specified (#50990)
Author     Murilo Kakazu  (@MuriloKakazu, first-time contributor)
Branch     MuriloKakazu:fix/default-flag-fs-write-file-sync -> nodejs:main
Labels     fs, author ready, needs-ci
Commits    2
 - fs: use default w flag for writeFileSync with utf8 encoding
 - fs: add tests for writeFileSync with no flag
Committers 1
 - Murilo Kakazu 
PR-URL: https://github.com/nodejs/node/pull/50990
Reviewed-By: Luigi Pinca 
Reviewed-By: Yagiz Nizipli 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/50990
Reviewed-By: Luigi Pinca 
Reviewed-By: Yagiz Nizipli 
--------------------------------------------------------------------------------
   ℹ  This PR was created on Fri, 01 Dec 2023 07:05:02 GMT
   ✔  Approvals: 2
   ✔  - Luigi Pinca (@lpinca): https://github.com/nodejs/node/pull/50990#pullrequestreview-1760486535
   ✔  - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/50990#pullrequestreview-1760934016
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2023-12-02T18:04:37Z: https://ci.nodejs.org/job/node-test-pull-request/56046/
- Querying data for job/node-test-pull-request/56046/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
- Bringing origin/main up to date...
From https://github.com/nodejs/node
 * branch                  main       -> FETCH_HEAD
✔  origin/main is now up-to-date
- Downloading patch for 50990
From https://github.com/nodejs/node
 * branch                  refs/pull/50990/merge -> FETCH_HEAD
✔  Fetched commits as 23031d9b0a56..aaa4edda7d2e
--------------------------------------------------------------------------------
Auto-merging lib/fs.js
[main 2fba1f3a18] fs: use default w flag for writeFileSync with utf8 encoding
 Author: Murilo Kakazu 
 Date: Fri Dec 1 03:45:10 2023 -0300
 1 file changed, 3 insertions(+), 3 deletions(-)
[main 934e830937] fs: add tests for writeFileSync with no flag
 Author: Murilo Kakazu 
 Date: Fri Dec 1 13:46:14 2023 -0300
 1 file changed, 16 insertions(+)
   ✔  Patches applied
There are 2 commits in the PR. Attempting autorebase.
Rebasing (2/4)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
fs: use default w flag for writeFileSync with utf8 encoding

PR-URL: #50990
Reviewed-By: Luigi Pinca luigipinca@gmail.com
Reviewed-By: Yagiz Nizipli yagiz.nizipli@sentry.io

[detached HEAD 7d6cf7982e] fs: use default w flag for writeFileSync with utf8 encoding
Author: Murilo Kakazu yuri.kakazu@gmail.com
Date: Fri Dec 1 03:45:10 2023 -0300
1 file changed, 3 insertions(+), 3 deletions(-)
Rebasing (3/4)
Rebasing (4/4)

Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
fs: add tests for writeFileSync with no flag

PR-URL: #50990
Reviewed-By: Luigi Pinca luigipinca@gmail.com
Reviewed-By: Yagiz Nizipli yagiz.nizipli@sentry.io

[detached HEAD 5220933a12] fs: add tests for writeFileSync with no flag
Author: Murilo Kakazu yuri.kakazu@gmail.com
Date: Fri Dec 1 13:46:14 2023 -0300
1 file changed, 16 insertions(+)

Successfully rebased and updated refs/heads/main.

ℹ Add commit-queue-squash label to land the PR as one commit, or commit-queue-rebase to land as separate commits.

https://github.com/nodejs/node/actions/runs/7075388656

@anonrig anonrig added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Dec 3, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Dec 3, 2023
@nodejs-github-bot nodejs-github-bot merged commit 7bfb087 into nodejs:main Dec 3, 2023
65 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 7bfb087

targos pushed a commit that referenced this pull request Dec 4, 2023
PR-URL: #50990
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
targos added a commit that referenced this pull request Dec 4, 2023
Notable changes:

fs:
  * (SEMVER-MINOR) introduce `dirent.parentPath` (Antoine du Hamel) #50976
  * use default w flag for writeFileSync with utf8 encoding (Murilo Kakazu) #50990

PR-URL: #51043
@targos targos mentioned this pull request Dec 4, 2023
ljharb added a commit to import-js/eslint-plugin-import that referenced this pull request Dec 4, 2023
targos added a commit to targos/node that referenced this pull request Dec 5, 2023
Notable changes:

fs:
  * (SEMVER-MINOR) introduce `dirent.parentPath` (Antoine du Hamel) nodejs#50976
  * use default w flag for writeFileSync with utf8 encoding (Murilo Kakazu) nodejs#50990

PR-URL: nodejs#51043
targos added a commit that referenced this pull request Dec 5, 2023
Notable changes:

fs:
  * (SEMVER-MINOR) introduce `dirent.parentPath` (Antoine du Hamel) #50976
  * use default w flag for writeFileSync with utf8 encoding (Murilo Kakazu) #50990

PR-URL: #51043
@kvakil kvakil mentioned this pull request Dec 21, 2023
UlisesGascon pushed a commit that referenced this pull request Jan 3, 2024
PR-URL: #50990
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
UlisesGascon pushed a commit that referenced this pull request Jan 9, 2024
PR-URL: #50990
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fs.writeFileSync regression in v21.3.0 on MacOS
6 participants