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/promises: ensure options.flag is defaulted to 'r' in readFile #20268

Closed
wants to merge 4 commits into from

Conversation

bdistin
Copy link
Contributor

@bdistin bdistin commented Apr 24, 2018

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().

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@Trott
Copy link
Member

Trott commented Apr 24, 2018

@nodejs/fs

@Trott Trott added the fs Issues and PRs related to the fs subsystem / file system. label Apr 24, 2018
@vsemozhetbyt vsemozhetbyt added the promises Issues and PRs related to ECMAScript promises. label Apr 24, 2018
@ronkorving
Copy link
Contributor

I'm curious if the behavior of getOptions is as intended. Can someone shed some light on this? Should we not merge these default options into a given object?

@@ -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';
Copy link
Member

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';
}

@BridgeAR
Copy link
Member

@ronkorving you are right in a way: the getOptions changed in a way that breaks with the former behavior. I am looking into that.

@bdistin
Copy link
Contributor Author

bdistin commented Apr 29, 2018

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().

@apapirovski
Copy link
Member

@nodejs/fs Please have a look at this PR.

@ChALkeR ChALkeR added the experimental Issues and PRs related to experimental features. label May 8, 2018
@bdistin
Copy link
Contributor Author

bdistin commented May 9, 2018

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.)

@trivikr
Copy link
Member

trivikr commented May 9, 2018

@bdistin Yes, please update this PR by following instructions in Step 9

@bdistin bdistin force-pushed the fix-readfile-flag-default branch 4 times, most recently from 3e0d004 to f5a462f Compare May 9, 2018 16:48
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().
@bdistin
Copy link
Contributor Author

bdistin commented May 9, 2018

Also given the changes of the fs/promises paradigm, should the pr/commit be named just fs: instead of fs/promises:?

@vsemozhetbyt
Copy link
Contributor

@bdistin
Copy link
Contributor Author

bdistin commented May 14, 2018

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?

@bzoz
Copy link
Contributor

bzoz commented May 14, 2018

@bdistin
Copy link
Contributor Author

bdistin commented May 16, 2018

So that CI seemed to have passed, but the checks weren't updated on this pr. Does that mean this is good to go?

@bdistin
Copy link
Contributor Author

bdistin commented May 26, 2018

bump? @nodejs/fs ??

Copy link
Member

@joyeecheung joyeecheung left a 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.

@joyeecheung
Copy link
Member

@joyeecheung joyeecheung added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 26, 2018
const { promises: fs } = require('fs');
const fixtures = require('../common/fixtures');

const fn = fixtures.path('empty.txt');

This comment was marked as resolved.

@joyeecheung joyeecheung removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 27, 2018
fail on unhandled rejection
@joyeecheung
Copy link
Member

common was required, but not assigned to common in order for the requested change to work.
@joyeecheung
Copy link
Member

@bdistin
Copy link
Contributor Author

bdistin commented May 27, 2018

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
Specifically, readFileHandle() ignores the encoding option when the size of the file to read is 0 and returns a buffer instead of an empty string.

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.

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.
@BridgeAR
Copy link
Member

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 29, 2018
@BridgeAR
Copy link
Member

Landed in 2cd3e61 🎉

@BridgeAR BridgeAR closed this May 30, 2018
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request May 30, 2018
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>
addaleax pushed a commit that referenced this pull request May 31, 2018
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>
@MylesBorins MylesBorins mentioned this pull request Jun 6, 2018
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. experimental Issues and PRs related to experimental features. fs Issues and PRs related to the fs subsystem / file system. promises Issues and PRs related to ECMAScript promises.
Projects
None yet
Development

Successfully merging this pull request may close these issues.