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

Patch FS for module resolution #40501

Closed
pauldraper opened this issue Oct 18, 2021 · 3 comments
Closed

Patch FS for module resolution #40501

pauldraper opened this issue Oct 18, 2021 · 3 comments
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system.

Comments

@pauldraper
Copy link

pauldraper commented Oct 18, 2021

Is your feature request related to a problem? Please describe.

I am customizing module/package.json location, necessary for good interop with Bazel build tool. Namely, I need to transparently customize file locations.

I cannot use symlinks because they change Node.js module resolution algorithm. (I cannot use preserve symlinks, because then hoisting becomes much more difficult, or at least compared to https://nodejs.org/api/modules.html#modules_package_manager_tips.)

I cannot use hardlinks because they may not be on the same file system.

Describe the solution you'd like

Monkeypatch the fs module (or process.binding('fs')), the same approach taken by npm's former tink project https://github.com/npm/tink

However, cjs/loader makes that impossible with code like

const { internalModuleStat } = internalBinding('fs');

Then internalModuleStat is impossible to monkey patch.

I'd like some way of monkeypatching that. Tink is dead, but I assume it had some plan.

Even if that code were

const binding = internalBinding('fs');

fs.internalModuleStat

Describe alternatives you've considered

Reimplement the node module algorithm and override at at different level like Module._load. This seems very complicated, involves package.json parsing, etc. Plus I'd need to repeat with other tools in the ecosystem.

I could LD_PRELOAD inject glibc, but I'd have to do that for each platform.

@pauldraper
Copy link
Author

pauldraper commented Oct 18, 2021

If I monkeypatch path.toNamespacedPath, I think I can get everything to work. (EDIT: Nope, there's more.)

The crux of the matter is that -- unlike most other tools -- Node.js and its ecosystem assign essential behavior to symlinks. So some other mechanism (like in-process patching) is need for transparent file management.

FWIW, in-process FS patching is not unique, if this particular reason is. See https://docs.oracle.com/javase/8/docs/technotes/guides/io/fsp/filesystemprovider.html

@VoltrexKeyva VoltrexKeyva added feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system. labels Oct 18, 2021
@merceyz
Copy link
Member

merceyz commented Oct 19, 2021

Similar to #33423

@pauldraper
Copy link
Author

Indeed. Closing this in favor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

No branches or pull requests

3 participants