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

realpath performance improvements + error handling #125

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/workspace/node-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import Workspace from './workspace';
import semverMaxSatisfying from 'semver/ranges/max-satisfying';
import semver from 'semver';

// fs.realpathSync is 70x slower than fs.realpathSync.native:
// https://github.com/nodejs/node/issues/2680
// However, `fs.realpathSync.native` resolves differently in
// Windows network drive, causing file read errors
const fsRealpathSync =
process.platform === 'win32' ? fs.realpathSync : fs.realpathSync.native;

export type RemoteData = {
time: Record<string | 'modified' | 'created', string>;
versions: Array<string>;
Expand All @@ -21,6 +28,7 @@ export default class NodeModule {
_remoteData: RemoteData | null;
_packageJson: PackageJson | null;
_stats: Stats | null;
_realpath: string | null;
_yarnRequiredBy: Set<string> | null;
_symlink: string | null;

Expand All @@ -40,6 +48,7 @@ export default class NodeModule {
this.parent = parent;
this.workspace = workspace;
this._stats = null;
this._realpath = null;
this._remoteData = null;
this._packageJson = null;
this._yarnRequiredBy = null;
Expand Down Expand Up @@ -75,7 +84,21 @@ export default class NodeModule {
}

get realpath() {
return fs.realpathSync(path.join(this.nodeModulesPath, this.name));
if (!this._realpath) {
try {
this._realpath = fsRealpathSync(this.path);
} catch (e: any) {
// When using npm "overwrites" with "@favware/skip-dependency", there could be modules
// that have symlinks to non-existing paths, causing an ENOENT error. Falling back to
// the path when this happen.
if ('code' in e && e.code === 'ENOENT') {
this._realpath = this.path;
}
throw e;
}
}

return this._realpath;
}

get stats() {
Expand Down
Loading