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

fix(fslib): ensure path is a string before calling replace(). #4215

Closed
wants to merge 1 commit into from
Closed

fix(fslib): ensure path is a string before calling replace(). #4215

wants to merge 1 commit into from

Conversation

nmoinvaz
Copy link

@nmoinvaz nmoinvaz commented Mar 12, 2022

What's the problem this PR addresses?

Random error when calling yarn run build on Windows 11, node 17.6.0, and yarn 3.2.0.

Failed to compile.

p.replace is not a function

Debugging the issue, here is the value of p and also the stack trace during the failure.

p = 
<Buffer 5c 5c 3f 5c 43 3a 5c 55 73 65  ... 10 more bytes>
Trace: 
    at Object.toPortablePath (.pnp.cjs:20132:15)
    at PosixFS.mapToBase (.pnp.cjs:22644:18)
    at PosixFS.rmdirSync (.pnp.cjs:22561:39)
    at URLFS.rmdirSync (.pnp.cjs:22561:24)
    at _rmdirSync (node:internal/fs/rimraf:260:21)
    at rimrafSync (node:internal/fs/rimraf:193:7)
    at node:internal/fs/rimraf:253:9
    at Array.forEach (<anonymous>)
    at _rmdirSync (node:internal/fs/rimraf:250:7)
    at rimrafSync (node:internal/fs/rimraf:193:7)

How did you fix it?

path in fs.rmdirSync and other Node functions can be string | Buffer | URL. So call .toString() before calling .replace().

Checklist

  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

Copy link
Member

@merceyz merceyz left a comment

Choose a reason for hiding this comment

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

@nmoinvaz
Copy link
Author

Any idea why the CI fails?

@nmoinvaz
Copy link
Author

nmoinvaz commented Mar 14, 2022

Test added.

@nmoinvaz nmoinvaz requested a review from merceyz March 14, 2022 17:56
arcanis
arcanis previously approved these changes Mar 15, 2022
Copy link
Member

@arcanis arcanis left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@merceyz merceyz left a comment

Choose a reason for hiding this comment

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

I'm not sure if this is safe to land so I'll defer to @paul-soporan since he looked into Buffer support in #1584 and knows more about what is required.

Ref #1818

Any idea why the CI fails?

A flaky test, you can ignore it.

packages/yarnpkg-fslib/sources/path.ts Outdated Show resolved Hide resolved
@nmoinvaz
Copy link
Author

Anything else on this commit to do?

@paul-soporan
Copy link
Member

I apologize for the delay, I'll try doing an in-depth review tomorrow.

@paul-soporan paul-soporan added the infra: pending update A bot will merge master into this PR label Mar 20, 2022
@github-actions github-actions bot removed the infra: pending update A bot will merge master into this PR label Mar 20, 2022
Copy link
Member

@paul-soporan paul-soporan left a comment

Choose a reason for hiding this comment

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

Haven't had time to look into the encoding issues yet, but, regarding the API, toPortablePath isn't the right place for this change. It's only meant to accept a Path.

Introducing an invisible layer here means that things might break (Edit: Actually, in this specific case they wouldn't, but still, it gives off the false impression that we fully support Buffers without them being implemented in the right layer) when going through the FS classes that only expect paths.

The right fix would be to create a BufferFS to automatically wrap the patchedFs just like the URLFS does.

@paul-soporan paul-soporan added the infra: pending update A bot will merge master into this PR label Mar 23, 2022
@github-actions github-actions bot removed the infra: pending update A bot will merge master into this PR label Mar 23, 2022
In some instances Node path may be a Buffer instead of a string causing a p.replace is not a function error.
@nmoinvaz
Copy link
Author

nmoinvaz commented Mar 23, 2022

I have made the suggested changes and rebased.

@paul-soporan
Copy link
Member

I'm sorry if that wasn't clear - I meant to say that we should create a BufferFS class inside a new BufferFS file (just like URLFS is implemented), not modify PosixFS. The patchFs function should then be modified to wrap the existing fs with a BufferFS too (

// We wrap the `fakeFs` with a `URLFS` to add support for URL instances
fakeFs = new URLFS(fakeFs);
).

@nmoinvaz nmoinvaz closed this Mar 24, 2022
@nmoinvaz
Copy link
Author

I have closed the pull request. I don't want to see it through.

My workaround is just to delete the build directory before running yarn build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants