From 7c9c040977466b2202d6012f7852f11698193b2c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 09:10:45 +0200 Subject: [PATCH 1/4] chore: update resolve --- packages/jest-resolve/package.json | 2 +- packages/jest-resolve/src/defaultResolver.ts | 2 ++ yarn.lock | 8 ++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/jest-resolve/package.json b/packages/jest-resolve/package.json index ad7c5822dfe2..7dc336af75c3 100644 --- a/packages/jest-resolve/package.json +++ b/packages/jest-resolve/package.json @@ -23,7 +23,7 @@ "jest-pnp-resolver": "^1.2.1", "read-pkg-up": "^7.0.1", "realpath-native": "^2.0.0", - "resolve": "^1.16.1", + "resolve": "^1.17.0", "slash": "^3.0.0" }, "devDependencies": { diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 4e990f8b65c1..30a7d7948881 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -41,6 +41,8 @@ export default function defaultResolver( moduleDirectory: options.moduleDirectory, paths: options.paths, preserveSymlinks: false, + // @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/44137 + realpathSync: realpath, }); try { diff --git a/yarn.lock b/yarn.lock index 8f72804d1d06..5cdbc006208c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12708,10 +12708,10 @@ resolve@1.1.7: resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" integrity sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs= -resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.15.0, resolve@^1.15.1, resolve@^1.16.1, resolve@^1.3.2, resolve@^1.5.0, resolve@^1.8.1: - version "1.16.1" - resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.16.1.tgz#49fac5d8bacf1fd53f200fa51247ae736175832c" - integrity sha512-rmAglCSqWWMrrBv/XM6sW0NuRFiKViw/W4d9EbC4pt+49H8JwHy+mcGmALTEg504AUDcLTvb1T2q3E9AnmY+ig== +resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.15.0, resolve@^1.15.1, resolve@^1.17.0, resolve@^1.3.2, resolve@^1.5.0, resolve@^1.8.1: + version "1.17.0" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.17.0.tgz#b25941b54968231cc2d1bb76a79cb7f2c0bf8444" + integrity sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w== dependencies: path-parse "^1.0.6" From 03533a72d473d7dacef6ef92170a0b70ca177422 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 09:38:36 +0200 Subject: [PATCH 2/4] try-catch the realpath call, otherwise we OOM --- packages/jest-resolve/src/defaultResolver.ts | 34 ++++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 30a7d7948881..94e2ee210e4f 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -22,6 +22,22 @@ type ResolverOptions = { rootDir?: Config.Path; }; +function tolerantRealpath(path: Config.Path): Config.Path { + try { + const resolved = realpath(path); + + if (resolved) { + return resolved; + } + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + + return path; +} + export default function defaultResolver( path: Config.Path, options: ResolverOptions, @@ -42,22 +58,12 @@ export default function defaultResolver( paths: options.paths, preserveSymlinks: false, // @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/44137 - realpathSync: realpath, + realpathSync: tolerantRealpath, }); - try { - // Dereference symlinks to ensure we don't create a separate - // module instance depending on how it was referenced. - const resolved = realpath(result); - - if (resolved) { - return resolved; - } - } catch { - // ignore - } - - return result; + // Dereference symlinks to ensure we don't create a separate + // module instance depending on how it was referenced. + return tolerantRealpath(result); } export function clearDefaultResolverCache(): void { From 2af02324413265ee4995836b8db21ffb5590ae74 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 09:41:49 +0200 Subject: [PATCH 3/4] refactor to helper function --- packages/jest-resolve/src/defaultResolver.ts | 41 +++++++++++--------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index 94e2ee210e4f..2481a8c606c4 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -22,22 +22,6 @@ type ResolverOptions = { rootDir?: Config.Path; }; -function tolerantRealpath(path: Config.Path): Config.Path { - try { - const resolved = realpath(path); - - if (resolved) { - return resolved; - } - } catch (error) { - if (error.code !== 'ENOENT') { - throw error; - } - } - - return path; -} - export default function defaultResolver( path: Config.Path, options: ResolverOptions, @@ -58,12 +42,12 @@ export default function defaultResolver( paths: options.paths, preserveSymlinks: false, // @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/44137 - realpathSync: tolerantRealpath, + realpathSync, }); // Dereference symlinks to ensure we don't create a separate // module instance depending on how it was referenced. - return tolerantRealpath(result); + return realpathSync(result); } export function clearDefaultResolverCache(): void { @@ -105,6 +89,23 @@ function statSyncCached(path: string): IPathType { return IPathType.OTHER; } +function tolerantRealpath(path: Config.Path): Config.Path { + let result: Config.Path | undefined = undefined; + try { + result = realpath(path); + } catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + + if (!result) { + result = path; + } + + return result; +} + /* * helper functions */ @@ -115,3 +116,7 @@ function isFile(file: Config.Path): boolean { function isDirectory(dir: Config.Path): boolean { return statSyncCached(dir) === IPathType.DIRECTORY; } + +function realpathSync(file: Config.Path): Config.Path { + return tolerantRealpath(file); +} From 58c26e54a018adb03718639f209db28234bfe56b Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 23 Apr 2020 10:00:08 +0200 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f1165f46273..ec36a2f6f407 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ ### 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)) + ## 25.4.0 - `[expect]` Support `async function`s in `toThrow` ([#9817](https://github.com/facebook/jest/pull/9817))