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: pass custom cached realpath function to resolve #9873

Merged
merged 1 commit into from Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
### Performance

- `[jest-resolve]` Update `resolve` to a version using native `realpath`, which is faster than the default JS implementation ([#9872](https://github.com/facebook/jest/pull/9872))
- `[jest-resolve]` Pass custom cached `realpath` function to `resolve` ([#9873](https://github.com/facebook/jest/pull/9873))

## 25.4.0

Expand Down
20 changes: 17 additions & 3 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -52,6 +52,7 @@ export default function defaultResolver(

export function clearDefaultResolverCache(): void {
checkedPaths.clear();
checkedRealpathPaths.clear();
}

enum IPathType {
Expand Down Expand Up @@ -89,8 +90,14 @@ function statSyncCached(path: string): IPathType {
return IPathType.OTHER;
}

function tolerantRealpath(path: Config.Path): Config.Path {
let result: Config.Path | undefined = undefined;
const checkedRealpathPaths = new Map<string, string>();
function realpathCached(path: Config.Path): Config.Path {
let result = checkedRealpathPaths.get(path);

if (result !== undefined) {
return result;
}

try {
result = realpath(path);
} catch (error) {
Expand All @@ -103,6 +110,13 @@ function tolerantRealpath(path: Config.Path): Config.Path {
result = path;
}

checkedRealpathPaths.set(path, result);

if (path !== result) {
// also cache the result in case it's ever referenced directly - no reason to `realpath` that as well
checkedRealpathPaths.set(result, result);
}

return result;
}

Expand All @@ -118,5 +132,5 @@ function isDirectory(dir: Config.Path): boolean {
}

function realpathSync(file: Config.Path): Config.Path {
return tolerantRealpath(file);
return realpathCached(file);
}