Skip to content

Commit a6aa304

Browse files
Nicholas Papecpojer
Nicholas Pape
authored andcommitted
Update node_modules_path to resolve symlinks to real paths (jestjs#5085)
* Update node_modules_path to follow symlinks * Update whitespace to make prettier happy * Update CHANGELOG.md * Update CHANGELOG.md * Update node_modules_paths.js * Code Review feedback * Use `realpath()` in `jest-util` * Fix a lint error * Add missing dependency in jest-resolve * Fix import statement * Fix a lint warning * Fix lint issues caused by editor * Add test * passing tests * Make lint happy * Make lint happy * Make lint happy * AppVeyor should keep symlinks * Update appveyor.yml * Fix imports * fixed the rel/abs path test suite that was failing with realpath * Update CHANGELOG.md * Update package.json
1 parent 83a51cc commit a6aa304

File tree

10 files changed

+87
-5
lines changed

10 files changed

+87
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## master
22

3+
### Fixes
4+
5+
* `[jest-resolve]` Update node module resolution algorithm to correctly handle
6+
symlinked paths ([#5085](https://github.com/facebook/jest/pull/5085))
7+
38
## 22.4.2
49

510
### Fixes

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ init:
66
# debugging Appveyor build. More info:
77
# https://www.appveyor.com/docs/how-to/rdp-to-build-worker/
88
- ps: iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
9+
- git config --global core.symlinks true
910

1011
install:
1112
- ps: Install-Product node $env:nodejs_version x64

packages/jest-resolve/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"main": "build/index.js",
1010
"dependencies": {
1111
"browser-resolve": "^1.11.2",
12-
"chalk": "^2.0.1"
12+
"chalk": "^2.0.1",
13+
"realpath-native": "^1.0.0"
1314
},
1415
"devDependencies": {
1516
"jest-haste-map": "^22.4.2"

packages/jest-resolve/src/__mocks__/bar/node_modules/bar/index.js

Whitespace-only changes.

packages/jest-resolve/src/__mocks__/bar/node_modules/foo

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/jest-resolve/src/__mocks__/foo/node_modules/dep/index.js

Whitespace-only changes.

packages/jest-resolve/src/__mocks__/foo/node_modules/foo/index.js

Whitespace-only changes.

packages/jest-resolve/src/__tests__/resolve.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ describe('resolveModule', () => {
129129
require.resolve('../__mocks__/mockJsxDependency.native.jsx'),
130130
);
131131
});
132+
133+
it('is possible to resolve node modules by resolving their realpath', () => {
134+
const resolver = new Resolver(moduleMap, {
135+
extensions: ['.js'],
136+
});
137+
const src = require.resolve(
138+
'../../src/__mocks__/bar/node_modules/foo/index.js',
139+
);
140+
const resolved = resolver.resolveModule(src, 'dep');
141+
expect(resolved).toBe(
142+
require.resolve('../../src/__mocks__/foo/node_modules/dep/index.js'),
143+
);
144+
});
132145
});
133146

134147
describe('getMockModule', () => {
@@ -203,6 +216,16 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
203216
map: [],
204217
mocks: [],
205218
});
219+
220+
// Mocking realpath to function the old way, where it just looks at
221+
// pathstrings instead of actually trying to access the physical directory.
222+
// This test suite won't work otherwise, since we cannot make assumptions
223+
// about the test environment when it comes to absolute paths.
224+
jest.doMock('realpath-native', () => {
225+
return {
226+
sync: dirInput => dirInput,
227+
};
228+
});
206229
});
207230

208231
afterAll(() => {

packages/jest-resolve/src/node_modules_paths.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import type {Path} from 'types/Config';
1313
import path from 'path';
14+
import {sync as realpath} from 'realpath-native';
1415

1516
type NodeModulesPathsOptions = {|
1617
moduleDirectory?: Array<string>,
@@ -37,11 +38,17 @@ export default function nodeModulesPaths(
3738
prefix = '\\\\';
3839
}
3940

40-
const paths = [basedirAbs];
41-
let parsed = path.parse(basedirAbs);
41+
// The node resolution algorithm (as implemented by NodeJS
42+
// and TypeScript) traverses parents of the physical path,
43+
// not the symlinked path
44+
const physicalBasedir = realpath(basedirAbs);
45+
46+
const paths = [physicalBasedir];
47+
let parsed = path.parse(physicalBasedir);
4248
while (parsed.dir !== paths[paths.length - 1]) {
43-
paths.push(parsed.dir);
44-
parsed = path.parse(parsed.dir);
49+
const realParsedDir = realpath(parsed.dir);
50+
paths.push(realParsedDir);
51+
parsed = path.parse(realParsedDir);
4552
}
4653

4754
const dirs = paths

yarn.lock

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4534,6 +4534,10 @@ jest-docblock@^21, jest-docblock@^21.0.0, jest-docblock@^21.2.0:
45344534
version "21.2.0"
45354535
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-21.2.0.tgz#51529c3b30d5fd159da60c27ceedc195faf8d414"
45364536

4537+
jest-get-type@^21.2.0:
4538+
version "21.2.0"
4539+
resolved "https://onedrive.pkgs.visualstudio.com/_packaging/odsp-npm/npm/registry/jest-get-type/-/jest-get-type-21.2.0.tgz#f6376ab9db4b60d81e39f30749c6c466f40d4a23"
4540+
45374541
jest-haste-map@^21:
45384542
version "21.2.0"
45394543
resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-21.2.0.tgz#1363f0a8bb4338f24f001806571eff7a4b2ff3d8"
@@ -4545,6 +4549,39 @@ jest-haste-map@^21:
45454549
sane "^2.0.0"
45464550
worker-farm "^1.3.1"
45474551

4552+
jest-message-util@^21.2.1:
4553+
version "21.2.1"
4554+
resolved "https://onedrive.pkgs.visualstudio.com/_packaging/odsp-npm/npm/registry/jest-message-util/-/jest-message-util-21.2.1.tgz#bfe5d4692c84c827d1dcf41823795558f0a1acbe"
4555+
dependencies:
4556+
chalk "^2.0.1"
4557+
micromatch "^2.3.11"
4558+
slash "^1.0.0"
4559+
4560+
jest-mock@^21.2.0:
4561+
version "21.2.0"
4562+
resolved "https://onedrive.pkgs.visualstudio.com/_packaging/odsp-npm/npm/registry/jest-mock/-/jest-mock-21.2.0.tgz#7eb0770e7317968165f61ea2a7281131534b3c0f"
4563+
4564+
jest-util@^21.2.1:
4565+
version "21.2.1"
4566+
resolved "https://onedrive.pkgs.visualstudio.com/_packaging/odsp-npm/npm/registry/jest-util/-/jest-util-21.2.1.tgz#a274b2f726b0897494d694a6c3d6a61ab819bb78"
4567+
dependencies:
4568+
callsites "^2.0.0"
4569+
chalk "^2.0.1"
4570+
graceful-fs "^4.1.11"
4571+
jest-message-util "^21.2.1"
4572+
jest-mock "^21.2.0"
4573+
jest-validate "^21.2.1"
4574+
mkdirp "^0.5.1"
4575+
4576+
jest-validate@^21.2.1:
4577+
version "21.2.1"
4578+
resolved "https://onedrive.pkgs.visualstudio.com/_packaging/odsp-npm/npm/registry/jest-validate/-/jest-validate-21.2.1.tgz#cc0cbca653cd54937ba4f2a111796774530dd3c7"
4579+
dependencies:
4580+
chalk "^2.0.1"
4581+
jest-get-type "^21.2.0"
4582+
leven "^2.1.0"
4583+
pretty-format "^21.2.1"
4584+
45484585
jquery@^3.2.1:
45494586
version "3.2.1"
45504587
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.2.1.tgz#5c4d9de652af6cd0a770154a631bba12b015c787"
@@ -6200,6 +6237,13 @@ prettier@^1.10.1:
62006237
version "1.10.1"
62016238
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.10.1.tgz#01423fea6957ea23618d37d339ef0e7f7c967fc6"
62026239

6240+
pretty-format@^21.2.1:
6241+
version "21.2.1"
6242+
resolved "https://onedrive.pkgs.visualstudio.com/_packaging/odsp-npm/npm/registry/pretty-format/-/pretty-format-21.2.1.tgz#ae5407f3cf21066cd011aa1ba5fce7b6a2eddb36"
6243+
dependencies:
6244+
ansi-regex "^3.0.0"
6245+
ansi-styles "^3.2.0"
6246+
62036247
pretty-format@^4.2.1:
62046248
version "4.3.1"
62056249
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-4.3.1.tgz#530be5c42b3c05b36414a7a2a4337aa80acd0e8d"

0 commit comments

Comments
 (0)