Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fs: use w flag for writeFileSync with utf8 encoding when flag not specified#50990

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@MuriloKakazu
Copy link
Contributor

@MuriloKakazuMuriloKakazu commentedDec 1, 2023
edited
Loading

PR#49884 seems to have accidentally changed the behavior forfs.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 theO_CREAT flag touvlib anymore.

Examples:

In node 16.16.0: ✅

❯ nvm use 16.16Now using node v16.16.0 (npm v8.11.0)❯ nodeWelcome 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.2Now using node v21.2.0 (npm v10.2.3)❯ nodeWelcome 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.3Now using node v21.3.0 (npm v10.2.4)❯ nodeWelcome 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 thew flag (which includesO_CREAT) explicitly when callingwriteFileSync. e.g:

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

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

Fixes#50989

bricss, silentroach, isabelarochass, jakesjews, jeremymeng, vladaviedov, and ljharb reacted with thumbs up emojisilentroach, isabelarochass, bricss, chenrui333, and anagami reacted with rocket emoji
@nodejs-github-botnodejs-github-bot added fsIssues and PRs related to the fs subsystem / file system. needs-ciPRs that need a full CI run. labelsDec 1, 2023
@MuriloKakazuMuriloKakazu changed the titlefix: defaults to write flag when not specified for writeFileSyncfs: use w flag for writeFileSync with utf8 encoding when flag not specifiedDec 1, 2023
@MuriloKakazuMuriloKakazuforce-pushed thefix/default-flag-fs-write-file-sync branch fromb5305d0 tob5283b6CompareDecember 1, 2023 07:07
@anonrig
Copy link
Member

Can you add a test?

@MuriloKakazuMuriloKakazuforce-pushed thefix/default-flag-fs-write-file-sync branch fromb5283b6 toaaa4eddCompareDecember 1, 2023 16:46
@MuriloKakazu
Copy link
ContributorAuthor

@anonrig Done :)

Akif9748 reacted with thumbs up emoji

@lpincalpinca added the request-ciAdd this label to start a Jenkins CI on a PR. labelDec 1, 2023
@github-actionsgithub-actionsbot removed the request-ciAdd this label to start a Jenkins CI on a PR. labelDec 1, 2023
@nodejs-github-bot
Copy link
Collaborator

Copy link

@chenrui333chenrui333 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
Contributor

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

@nodejs-github-bot
Copy link
Collaborator

@anonriganonrig added author readyPRs that have at least one approval, no pending requests for changes, and a CI started. commit-queueAdd this label to land a pull request using GitHub Actions. labelsDec 2, 2023
@nodejs-github-botnodejs-github-bot added commit-queue-failedAn error occurred while landing this pull request using GitHub Actions. and removed commit-queueAdd this label to land a pull request using GitHub Actions. labelsDec 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:mainLabels     fs, author ready, needs-ciCommits    2 - fs: use default w flag for writeFileSync with utf8 encoding - fs: add tests for writeFileSync with no flagCommitters 1 - Murilo Kakazu PR-URL: https://github.com/nodejs/node/pull/50990Reviewed-By: Luigi Pinca Reviewed-By: Yagiz Nizipli ------------------------------ Generated metadata ------------------------------PR-URL: https://github.com/nodejs/node/pull/50990Reviewed-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 50990From 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 appliedThere 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 Pincaluigipinca@gmail.com
Reviewed-By: Yagiz Nizipliyagiz.nizipli@sentry.io

[detached HEAD 7d6cf7982e] fs: use default w flag for writeFileSync with utf8 encoding
Author: Murilo Kakazuyuri.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 Pincaluigipinca@gmail.com
Reviewed-By: Yagiz Nizipliyagiz.nizipli@sentry.io

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

Successfully rebased and updated refs/heads/main.

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

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

@anonriganonrig added commit-queueAdd this label to land a pull request using GitHub Actions. commit-queue-squashAdd this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed commit-queue-failedAn error occurred while landing this pull request using GitHub Actions. labelsDec 3, 2023
@nodejs-github-botnodejs-github-bot removed the commit-queueAdd this label to land a pull request using GitHub Actions. labelDec 3, 2023
@nodejs-github-botnodejs-github-bot merged commit7bfb087 intonodejs:mainDec 3, 2023
@nodejs-github-bot
Copy link
Collaborator

Landed in7bfb087

bricss reacted with hooray emoji

targos pushed a commit that referenced this pull requestDec 4, 2023
PR-URL:#50990Reviewed-By: Luigi Pinca <luigipinca@gmail.com>Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
targos added a commit that referenced this pull requestDec 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)#50990PR-URL:#51043
@targostargos mentioned this pull requestDec 4, 2023
ljharb added a commit to import-js/eslint-plugin-import that referenced this pull requestDec 4, 2023
targos added a commit to targos/node that referenced this pull requestDec 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#50990PR-URL:nodejs#51043
targos added a commit that referenced this pull requestDec 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)#50990PR-URL:#51043
@kvakilkvakil mentioned this pull requestDec 21, 2023
UlisesGascon pushed a commit that referenced this pull requestJan 3, 2024
PR-URL:#50990Reviewed-By: Luigi Pinca <luigipinca@gmail.com>Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
UlisesGascon pushed a commit that referenced this pull requestJan 9, 2024
PR-URL:#50990Reviewed-By: Luigi Pinca <luigipinca@gmail.com>Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@lpincalpincalpinca approved these changes

@anonriganonriganonrig approved these changes

+1 more reviewer

@chenrui333chenrui333chenrui333 left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.commit-queue-squashAdd this label to instruct the Commit Queue to squash all the PR commits into the first one.fsIssues and PRs related to the fs subsystem / file system.needs-ciPRs 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

6 participants

@MuriloKakazu@anonrig@nodejs-github-bot@bricss@lpinca@chenrui333

[8]ページ先頭

©2009-2025 Movatter.jp