GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: use w flag for writeFileSync with utf8 encoding when flag not specified by MuriloKakazu · Pull Request #50990 · nodejs/node

Conversation

@MuriloKakazu

@MuriloKakazu MuriloKakazu commented Dec 1, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss, silentroach, isabelarochass, jakesjews, jeremymeng, vladaviedov, and ljharb reacted with thumbs up emoji silentroach, isabelarochass, bricss, chenrui333, and anagami reacted with rocket emoji
All reactions
  • 👍 7 reactions
  • 🚀 5 reactions
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. 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
This was referenced Dec 1, 2023
Temporarily pin NodeTestVersion of v21 to 21.2.0 Azure/azure-sdk-for-js#27951
Merged
fs.writeFileSync regression in v21.3.0 on MacOS #50989
Closed
@anonrig

anonrig commented Dec 1, 2023

Copy link
Copy Markdown
Member

Can you add a test?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

MuriloKakazu added 2 commits December 1, 2023 13:29
@MuriloKakazu
fs: use default w flag for writeFileSync with utf8 encoding
c323642
@MuriloKakazu
fs: add tests for writeFileSync with no flag
aaa4edd
@MuriloKakazu
MuriloKakazu force-pushed the fix/default-flag-fs-write-file-sync branch from b5283b6 to aaa4edd Compare December 1, 2023 16:46
@MuriloKakazu

MuriloKakazu commented Dec 1, 2023

Copy link
Copy Markdown
Contributor Author

@anonrig Done :)

Akif9748 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lpinca
lpinca approved these changes Dec 1, 2023
View reviewed changes
@lpinca lpinca added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 1, 2023
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 1, 2023
@nodejs-github-bot

nodejs-github-bot commented Dec 1, 2023

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/56038/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chenrui333 chenrui333 mentioned this pull request Dec 1, 2023
node 21.3.0 Homebrew/homebrew-core#156024
Merged
1 task done
chenrui333
chenrui333 reviewed Dec 1, 2023
View reviewed changes

@chenrui333 chenrui333 left a comment

Copy link
Copy Markdown

There was a problem hiding this 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@okwergot okwergot mentioned this pull request Dec 2, 2023
use w flag for writeFileSync with utf8 encoding startupjs/startupjs#1118
Merged
@bricss

bricss commented Dec 2, 2023

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented Dec 2, 2023

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/56046/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

anonrig
anonrig approved these changes Dec 2, 2023
View reviewed changes
@anonrig anonrig added author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. labels Dec 2, 2023
@nodejs-github-bot nodejs-github-bot added commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. labels Dec 3, 2023
@nodejs-github-bot

nodejs-github-bot commented Dec 3, 2023

Copy link
Copy Markdown
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 [email protected]
Reviewed-By: Yagiz Nizipli [email protected]

[detached HEAD 7d6cf7982e] fs: use default w flag for writeFileSync with utf8 encoding
Author: Murilo Kakazu [email protected]
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 [email protected]
Reviewed-By: Yagiz Nizipli [email protected]

[detached HEAD 5220933a12] fs: add tests for writeFileSync with no flag
Author: Murilo Kakazu [email protected]
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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anonrig anonrig added commit-queue PRs queued for automated landing through the Commit Queue. commit-queue-squash PRs the Commit Queue should land as one squashed commit. and removed commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. labels Dec 3, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Dec 3, 2023
@nodejs-github-bot
nodejs-github-bot merged commit 7bfb087 into nodejs:main Dec 3, 2023
@nodejs-github-bot

nodejs-github-bot commented Dec 3, 2023

Copy link
Copy Markdown
Collaborator

Landed in 7bfb087

bricss reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

targos pushed a commit that referenced this pull request Dec 4, 2023
@MuriloKakazu @targos
fs: use default w flag for writeFileSync with utf8 encoding
7245486
PR-URL: #50990
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
targos added a commit that referenced this pull request Dec 4, 2023
@targos
2023-12-05, Version 21.4.0 (Current)
67ada40
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
v21.4.0 release proposal #51043
Merged
ljharb added a commit to import-js/eslint-plugin-import that referenced this pull request Dec 4, 2023
@ljharb
[Tests] node v21.3 has a broken fs.writeFile
8c83eaf
See nodejs/node#50990 / nodejs/node#50989
targos added a commit that referenced this pull request Dec 5, 2023
@targos
2023-12-05, Version 21.4.0 (Current)
2eb1808
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
@nfischer nfischer mentioned this pull request Dec 6, 2023
test: make a test more forgiving for systems with non-standard bash and sh paths shelljs/shelljs#1144
Merged
@Cherry Cherry mentioned this pull request Dec 7, 2023
🐛 BUG: EBADF: bad file descriptor, write cloudflare/workers-sdk#4540
Closed
@kvakil kvakil mentioned this pull request Dec 21, 2023
v20.11.0 proposal #51124
Merged
UlisesGascon pushed a commit that referenced this pull request Jan 3, 2024
@MuriloKakazu @UlisesGascon
fs: use default w flag for writeFileSync with utf8 encoding
a42629c
PR-URL: #50990
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
UlisesGascon pushed a commit that referenced this pull request Jan 9, 2024
@MuriloKakazu @UlisesGascon
fs: use default w flag for writeFileSync with utf8 encoding
435f9c9
PR-URL: #50990
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
@richardlau richardlau mentioned this pull request Mar 14, 2024
fs.writeFileSync in Node 20 failed to write file in TMPDIR #52079
Closed
@coderabbitai coderabbitai Bot mentioned this pull request Aug 14, 2026
manual song post creation jtzero/cle-gunners#435
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lpinca lpinca lpinca approved these changes
@anonrig anonrig anonrig approved these changes
+1 more reviewer
@chenrui333 chenrui333 chenrui333 left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-squash PRs the Commit Queue should land as one squashed commit. fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

fs.writeFileSync regression in v21.3.0 on MacOS

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@MuriloKakazu @anonrig @nodejs-github-bot @bricss @lpinca @chenrui333
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.