Skip to content

[v10.x] fs: stop lazy loading stream constructors - #21776

Closed
targos wants to merge 1 commit into
nodejs:v10.x-stagingfrom
targos:fix-fs-mock
Closed

[v10.x] fs: stop lazy loading stream constructors#21776
targos wants to merge 1 commit into
nodejs:v10.x-stagingfrom
targos:fix-fs-mock

Conversation

@targos

Copy link
Copy Markdown
Member

Fixes: #21489

/cc @nodejs/fs @SimenB

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@targostargos added the fs Issues and PRs related to the fs subsystem / file system. label Jul 12, 2018
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-botnodejs-github-bot added the fs Issues and PRs related to the fs subsystem / file system. label Jul 12, 2018

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

Do we need to add a test to make sure these public members of fs are mockable (i.e. if you assign fs.member to something else then assert.strictEqual(fs.member, mockedMember)? It should be less useful for other modules but members of fs are mocked by users all the time in tests.

Comment threadlib/fs.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.

An alternative fix is adding setters here to allow users to override the lazy-loaded variables so we can still avoid loading the internal/fs/streams module when requiring fs.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how it's implemented now. It doesn't work with jest-mock because accessors are ignored.

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.

Ah right, I see that there are setters...so this looks more like a bug of Jest?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maybe. But this was still kind of a sever major change and I'm not sure it's worth to do it.

@joyeecheung

Copy link
Copy Markdown
Member

Should we just wait until Jest fixes the bug instead of working around it ourselves? Looks like Jest just need to consider properties with setters as properties that can be overridden instead of bypassing every property with a getter in the descriptor?

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

Putting a red cross because it does not look like it has to be us fixing the regression, since we already provide setters (also because GitHub does not allow you to dismiss approvals..). This can be dismissed if it's shown that it has to be us fixing this.

Comment threadlib/internal/fs/streams.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to just return require('fs') since require already has a cache?

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.

The require cache is slightly more expensive so we generally use this pattern through core to keep it at cheap as possible. It's likely unnecessary in most cases, but it's a consistent pattern.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, cool. Thank you!

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.

@jasnell we might want to benchmark the influence of that again. It would actually be so clean if we'd just use require directly.

@SimenB

Copy link
Copy Markdown
Member

I'm fine with fixing this in Jest (although I won't have time for it anytime soon, leaving for vacation), but it has over 1.3 million every week, and people are generally slow to update. It also broke in a minor, not a major (and the original PR was originally marked as major before passing CITGM).

That said, nobody has complained to us about it not working, only noticed since our own tests broke 🙂

@BridgeAR

Copy link
Copy Markdown
Member

@joyeecheung I personally would say it was a unintentionally breaking change. It was the reason why I did not move that part out when I was working on improving lazy loading in Node core.

So I am +1 on merging this at least on 10.

@jasnell

Copy link
Copy Markdown
Member

I'm good with this change in v10 but keeping the current behavior in master and 11.

@jasnell

Copy link
Copy Markdown
Member

Specifically, what I'd like to see is this PR rebased against v10.x-staging, rather than against master.

@targos
targos changed the base branch from master to v10.x-stagingJuly 14, 2018 10:43
@targostargos changed the title fs: stop lazy loading stream constructors[v10.x] fs: stop lazy loading stream constructorsJul 14, 2018
@targos

Copy link
Copy Markdown
MemberAuthor

Updated to target v10.x-staging. PTAL

@targos

Copy link
Copy Markdown
MemberAuthor

@joyeecheung are you fine with this landing on v10.x only?

@targos

targos commented Jul 18, 2018

Copy link
Copy Markdown
MemberAuthor

@targos

targos commented Jul 19, 2018

Copy link
Copy Markdown
MemberAuthor

Landed in 484140e

@targostargos closed this Jul 19, 2018
targos added a commit that referenced this pull request Jul 19, 2018
Fixes: #21489
PR-URL: #21776
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@targos
targos deleted the fix-fs-mock branch July 19, 2018 09:09
@targostargos mentioned this pull request Jul 31, 2018
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fsIssues and PRs related to the fs subsystem / file system.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@targos@nodejs-github-bot@joyeecheung@SimenB@BridgeAR@jasnell@cjihrig@vitkarpov