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
commented
Dec 1, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
This was referenced Dec 1, 2023
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Akif9748 reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
It would be great to get this fixed asap with semver patch release at least 👮
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
[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
[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
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
bricss reacted with hooray emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
ljharb
added a commit
to import-js/eslint-plugin-import
that referenced
this pull request
Dec 4, 2023
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
Reviewers
lpinca
lpinca approved these changes
anonrig
anonrig approved these changes
+1 more reviewer
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.
Milestone
No milestone
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.
|