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 require symlinks for yarn workspaces #15

Merged
merged 11 commits into from
Jul 13, 2019
Merged

Conversation

styfle
Copy link
Member

@styfle styfle commented Jul 12, 2019

Fixes #14

@styfle styfle requested review from guybedford and lucleray July 12, 2019 15:33
src/resolve-dependency.js Outdated Show resolved Hide resolved
@codecov-io
Copy link

codecov-io commented Jul 12, 2019

Codecov Report

Merging #15 into master will increase coverage by 0.03%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #15      +/-   ##
=========================================
+ Coverage   89.46%   89.5%   +0.03%     
=========================================
  Files          10      10              
  Lines         940     943       +3     
=========================================
+ Hits          841     844       +3     
  Misses         99      99
Impacted Files Coverage Δ
src/resolve-dependency.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6099031...ba73e33. Read the comment docs.

@styfle
Copy link
Member Author

styfle commented Jul 12, 2019

@guybedford What am I missing here?

const real = fs.realpathSync(resolved);
if (resolved !== real) {
// this is likely a symlink
job.emitFile(resolved, 'symlink', parent);
Copy link
Member

Choose a reason for hiding this comment

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

If you emit the path including the symlink, you probably don't need to also emit the real path (because the real path is not required).

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand.

Both the symlink and the real file need to be emitted because the symlink is just a pointer to the real file. Both are necessary.

Copy link
Member

@lucleray lucleray Jul 12, 2019

Choose a reason for hiding this comment

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

For example, you could have this path (not real) /symlink-folder/file and the real path is /folder/file.

If you add /symlink-folder/file to the file list, you're not actually listing anything real (not even a file that contains a pointer).

That means that:

  1. the consumer of the list (eg. @now/node) would need to follow symlinks when copying files
  2. the consumer of the list doesn't need to copy the real file anymore
  3. you avoid having to detect or copy symlinks. You create real files instead of "virtual" ones when you copy.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I see what you mean. My code is immediately emitting the symlink even though there are some cases when the real file will not be emitted (starts with 'node:`, etc).

@guybedford Do you have a solution for this one?

Copy link
Contributor

Choose a reason for hiding this comment

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

It will always be a symlink on this code path because of the early return for the node: URL format.

Note that a resolve-dependency is called as part of the emitDependency routine before it itself calls emitFile. Any file emitted by resolve-dependency means that file was necessary for the resolution process, not that that was the resolved file. The emitFile capability was added to resolve-dependency specifically just to emit package.json files (and so this way we don't emit all package.json files, only those that are actually needed by resolution). Emitting package.json + symlinks exactly fits the model.

Copy link
Member

Choose a reason for hiding this comment

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

I'm talking about the case where the symlink is directed at a folder and not a file (which is what yarn workspaces is doing). In that case, with the code above, you emit both a "virtual" file and a real file. Is that the expected behaviour? Could you add a test for that?

Copy link
Contributor

Choose a reason for hiding this comment

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

The Node.js resolver as written here will only ever resolve a file, not a folder. The statting specifically checks for file existence.

Copy link
Member Author

Choose a reason for hiding this comment

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

I confirmed this fixes yarn workspaces with an integration test here: https://github.com/zeit/now-builders/pull/759

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I explained it wrong. I'm talking about the case where the file itself is not the symlink, but the symlink is located higher in the folder structure. In this case, if we emit the "virtual" file path, we don't need to emit the real file path.

It works but I think we emit some files twice when only one is necessary 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, that makes complete sense, but this project is just a "tracer" of the file system. It is up to now node to correctly emit the traced filesystem. And yes, it doesn't due to a lack of symlink support both for files and deeper folders as you describe.

Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

The code is fixing require resolution, while the test is testing asset emission, so I think we just need to change the test case here to ensure the correct coverage.

test/unit/fs-emission-symlink/input.js Outdated Show resolved Hide resolved
@styfle styfle requested a review from guybedford July 13, 2019 17:58
Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

The approach as written here looks great to me.

const real = fs.realpathSync(resolved);
if (resolved !== real) {
// this is likely a symlink
job.emitFile(resolved, 'symlink', parent);
Copy link
Contributor

Choose a reason for hiding this comment

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

It will always be a symlink on this code path because of the early return for the node: URL format.

Note that a resolve-dependency is called as part of the emitDependency routine before it itself calls emitFile. Any file emitted by resolve-dependency means that file was necessary for the resolution process, not that that was the resolved file. The emitFile capability was added to resolve-dependency specifically just to emit package.json files (and so this way we don't emit all package.json files, only those that are actually needed by resolution). Emitting package.json + symlinks exactly fits the model.

@styfle styfle changed the title Fix symlink emission for yarn workspaces Fix require symlinks for yarn workspaces Jul 13, 2019
@styfle styfle merged commit 16a1f73 into master Jul 13, 2019
@styfle styfle deleted the node-modules-symlinks branch July 13, 2019 18:20
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.

Failing to trace node_modules containing symlinks (yarn workspaces)
4 participants