Skip to content

fs/promises: ensure options.flag is defaulted to 'r' in readFile - #20268

Closed
bdistin wants to merge 4 commits into
nodejs:masterfrom
bdistin:fix-readfile-flag-default
Closed

fs/promises: ensure options.flag is defaulted to 'r' in readFile#20268
bdistin wants to merge 4 commits into
nodejs:masterfrom
bdistin:fix-readfile-flag-default

Conversation

@bdistin

@bdistinbdistin commented Apr 24, 2018

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Member

@nodejs/fs

@TrottTrott added the fs Issues and PRs related to the fs subsystem / file system. label Apr 24, 2018
@vsemozhetbytvsemozhetbyt added the promises Issues and PRs related to ECMAScript promises. label Apr 24, 2018
@bdistin
bdistinforce-pushed the fix-readfile-flag-default branch from 76a8dbb to 58b874dCompareApril 25, 2018 01:22
@ronkorving

Copy link
Copy Markdown
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?

Comment threadlib/fs/promises.js Outdated

Copy link
Copy Markdown
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
Copy Markdown
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
bdistinforce-pushed the fix-readfile-flag-default branch from 58b874d to c64b578CompareApril 29, 2018 21:44
@bdistin

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
Contributor

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

@ChALkeRChALkeR added the experimental Issues and PRs related to experimental features. label May 8, 2018
@bdistin

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
Member

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

@bdistin
bdistinforce-pushed the fix-readfile-flag-default branch 4 times, most recently from 3e0d004 to f5a462fCompareMay 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
Copy Markdown
ContributorAuthor

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

@vsemozhetbyt

Copy link
Copy Markdown
Contributor

@bdistin

Copy link
Copy Markdown
ContributorAuthor

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

bzoz commented May 14, 2018

Copy link
Copy Markdown
Contributor

@bdistin

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

bump? @nodejs/fs ??

@joyeecheungjoyeecheung left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

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

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

This comment was marked as resolved.

@joyeecheungjoyeecheung 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
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

@bdistin

bdistin commented May 27, 2018

Copy link
Copy Markdown
ContributorAuthor

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
Copy Markdown
Member

@BridgeARBridgeAR 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
Copy Markdown
Member

Landed in 2cd3e61 🎉

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>
@MylesBorinsMylesBorins mentioned this pull request Jun 6, 2018
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.experimentalIssues and PRs related to experimental features.fsIssues and PRs related to the fs subsystem / file system.promisesIssues and PRs related to ECMAScript promises.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

11 participants

@bdistin@Trott@ronkorving@BridgeAR@apapirovski@trivikr@vsemozhetbyt@bzoz@joyeecheung@jasnell@ChALkeR