-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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/promises: ensure options.flag is defaulted to 'r' in readFile #20268
Conversation
@nodejs/fs |
76a8dbb
to
58b874d
Compare
I'm curious if the behavior of |
lib/fs/promises.js
Outdated
@@ -473,6 +473,7 @@ async function appendFile(path, data, options) { | |||
|
|||
async function readFile(path, options) { | |||
options = getOptions(options, { flag: 'r' }); | |||
options.flag = options.flag || 'r'; |
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.
This will actually manipulate the passed in options because getOptions
does not copy the options object.
Please do:
if (options.flag == null) {
options = copyObject(options);
options.flag = 'r';
}
@ronkorving you are right in a way: the |
58b874d
to
c64b578
Compare
No longer manipulates the passed in options. Since options.flag is not needed in readFileHandle(), copied the existing style from writeFile() instead of null check + copyObject(). |
@nodejs/fs Please have a look at this PR. |
I see that the fs/promises API has been moved to fs.promises, causing conflicts in this pr. Should I resolve those conflicts? (This is my first pr to Node, so I am still a bit unsure of the processes expected.) |
3e0d004
to
f5a462f
Compare
When passing {} or { encoding: 'utf8' } as options to readFile, the flag is not defaulted to 'r' unlike normal fs. This fix makes (fs/promises).readFile() act consistently with fs.readFile().
Also given the changes of the fs/promises paradigm, should the pr/commit be named just |
Is there anything more I need to do? I can't explain how the Windows 2016 binary tests fail, and can't see how that would be related to the changes made in this pr. If this is something I need to fix, can anyone shed light on what changes I might need to make? |
Windows CI rerun: https://ci.nodejs.org/job/node-test-commit-windows-fanned/17969/ |
So that CI seemed to have passed, but the checks weren't updated on this pr. Does that mean this is good to go? |
bump? @nodejs/fs ?? |
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.
I would've expected that getOptions
should extend fields of the options if they are not present..but the current fix works as well.
const { promises: fs } = require('fs'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const fn = fixtures.path('empty.txt'); |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
fail on unhandled rejection
common was required, but not assigned to common in order for the requested change to work.
It seems with the test fixes, the tests have uncovered another bug: https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L140-L141 How should I handle that bug? Should I increase the scope of this pr to fix that bug as well? |
.then(assert.ok); | ||
|
||
fs.readFile(fn, 'utf8') | ||
.then(assert.strictEqual.bind(this, '')); |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
fs: return string on empty fs.promises.readfile when encoding provided fs.promises.readfile() would provide a buffer whenever an empty file is read, when it should have returned an empty string when encoding is provided.
Landed in 2cd3e61 🎉 |
When passing {} or { encoding: 'utf8' } as options to readFile, the flag is not defaulted to 'r' unlike normal fs. This fix makes fs.promises.readFile() act consistent with fs.readFile(). It also fixes another issue with fs.promises.readfile() where it returned a Buffer instead of an empty string when encoding is provided. PR-URL: nodejs#20268 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ron Korving <ron@ronkorving.nl> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
When passing {} or { encoding: 'utf8' } as options to readFile, the flag is not defaulted to 'r' unlike normal fs. This fix makes fs.promises.readFile() act consistent with fs.readFile(). It also fixes another issue with fs.promises.readfile() where it returned a Buffer instead of an empty string when encoding is provided. PR-URL: #20268 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ron Korving <ron@ronkorving.nl> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
When passing
{}
or{ encoding: 'utf8' }
as options to readFile, theflag is not defaulted to 'r' unlike normal fs. This fix makes
(fs/promises).readFile() act consistently with fs.readFile().
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes