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(resolve): use escalade to find package.json #10781

Merged
merged 2 commits into from
Nov 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781))
- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"jest-util": "^26.6.2",
"jest-validate": "^26.6.2",
"prompts": "^2.0.1",
"yargs": "^15.4.1"
"yargs": "^16.0.3"
},
"devDependencies": {
"@jest/test-utils": "^26.6.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"jest-runtime": "^26.6.3",
"jest-validate": "^26.6.2",
"repl": "^0.1.3",
"yargs": "^15.4.1"
"yargs": "^16.0.3"
},
"devDependencies": {
"@jest/test-utils": "^26.6.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"dependencies": {
"@jest/types": "^26.6.2",
"chalk": "^4.0.0",
"escalade": "^3.1.1",
"graceful-fs": "^4.2.4",
"jest-pnp-resolver": "^1.2.2",
"jest-util": "^26.6.2",
"read-pkg-up": "^7.0.1",
"resolve": "^1.18.1",
"slash": "^3.0.0"
},
Expand Down
32 changes: 26 additions & 6 deletions packages/jest-resolve/src/shouldLoadAsEsm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
import {dirname, extname} from 'path';
// @ts-expect-error: experimental, not added to the types
import {SyntheticModule} from 'vm';
import readPkgUp = require('read-pkg-up');
import escalade from 'escalade/sync';
import {readFileSync} from 'graceful-fs';
import type {Config} from '@jest/types';

const runtimeSupportsVmModules = typeof SyntheticModule === 'function';

const cachedFileLookups = new Map<string, boolean>();
const cachedDirLookups = new Map<string, boolean>();
const cachedChecks = new Map<string, boolean>();

export function clearCachedLookups(): void {
cachedFileLookups.clear();
cachedDirLookups.clear();
cachedChecks.clear();
}

export default function cachedShouldLoadAsEsm(path: Config.Path): boolean {
Expand Down Expand Up @@ -67,12 +70,29 @@ function shouldLoadAsEsm(path: Config.Path): boolean {
}

function cachedPkgCheck(cwd: Config.Path): boolean {
// TODO: can we cache lookups somehow?
const pkg = readPkgUp.sync({cwd, normalize: false});

if (!pkg) {
const pkgPath = escalade(cwd, (_dir, names) => {
if (names.includes('package.json')) {
// will be resolved into absolute
return 'package.json';
}
return false;
});
if (!pkgPath) {
return false;
}

return pkg.packageJson.type === 'module';
let hasModuleField = cachedChecks.get(pkgPath);
if (hasModuleField != null) {
return hasModuleField;
}

try {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
hasModuleField = pkg.type === 'module';
} catch {
hasModuleField = false;
}

cachedChecks.set(pkgPath, hasModuleField);
return hasModuleField;
}
2 changes: 1 addition & 1 deletion packages/jest-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"jest-validate": "^26.6.2",
"slash": "^3.0.0",
"strip-bom": "^4.0.0",
"yargs": "^15.4.1"
"yargs": "^16.0.3"
},
"devDependencies": {
"@jest/test-utils": "^26.6.2",
Expand Down
67 changes: 59 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5918,6 +5918,17 @@ __metadata:
languageName: node
linkType: hard

"cliui@npm:^7.0.2":
version: 7.0.3
resolution: "cliui@npm:7.0.3"
dependencies:
string-width: ^4.2.0
strip-ansi: ^6.0.0
wrap-ansi: ^7.0.0
checksum: aa05598ac7178428d41188e73fcafd25dd17ddba6aba2f99d5c7805fc6655d503e46a0a56e4e173dbda9a4adf0001c484cd586f50324365e0c83cfa6c58a6520
languageName: node
linkType: hard

"clone-deep@npm:^4.0.1":
version: 4.0.1
resolution: "clone-deep@npm:4.0.1"
Expand Down Expand Up @@ -9448,7 +9459,7 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"get-caller-file@npm:^2.0.1":
"get-caller-file@npm:^2.0.1, get-caller-file@npm:^2.0.5":
version: 2.0.5
resolution: "get-caller-file@npm:2.0.5"
checksum: 9dd9e1e2591039ee4c38c897365b904f66f1e650a8c1cb7b7db8ce667fa63e88cc8b13282b74df9d93de481114b3304a0487880d31cd926dfda6efe71455855d
Expand Down Expand Up @@ -11545,7 +11556,7 @@ fsevents@^1.2.7:
jest-util: ^26.6.2
jest-validate: ^26.6.2
prompts: ^2.0.1
yargs: ^15.4.1
yargs: ^16.0.3
bin:
jest: ./bin/jest.js
languageName: unknown
Expand Down Expand Up @@ -11893,7 +11904,7 @@ fsevents@^1.2.7:
jest-runtime: ^26.6.3
jest-validate: ^26.6.2
repl: ^0.1.3
yargs: ^15.4.1
yargs: ^16.0.3
bin:
jest-repl: ./bin/jest-repl.js
languageName: unknown
Expand Down Expand Up @@ -11921,11 +11932,11 @@ fsevents@^1.2.7:
"@types/graceful-fs": ^4.1.3
"@types/resolve": ^1.17.0
chalk: ^4.0.0
escalade: ^3.1.1
graceful-fs: ^4.2.4
jest-haste-map: ^26.6.2
jest-pnp-resolver: ^1.2.2
jest-util: ^26.6.2
read-pkg-up: ^7.0.1
resolve: ^1.18.1
slash: ^3.0.0
languageName: unknown
Expand Down Expand Up @@ -12011,7 +12022,7 @@ fsevents@^1.2.7:
jest-validate: ^26.6.2
slash: ^3.0.0
strip-bom: ^4.0.0
yargs: ^15.4.1
yargs: ^16.0.3
bin:
jest-runtime: ./bin/jest-runtime.js
languageName: unknown
Expand Down Expand Up @@ -17374,14 +17385,14 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"safe-buffer@npm:5.1.2, safe-buffer@npm:^5.0.1, safe-buffer@npm:^5.1.1, safe-buffer@npm:^5.1.2, safe-buffer@npm:~5.1.0, safe-buffer@npm:~5.1.1":
"safe-buffer@npm:5.1.2, safe-buffer@npm:~5.1.0, safe-buffer@npm:~5.1.1":
version: 5.1.2
resolution: "safe-buffer@npm:5.1.2"
checksum: 2708587c1b5e70a5e420714ceb59f30f5791c6e831d39812125a008eca63a4ac18578abd020a0776ea497ff03b4543f2b2a223a7b9073bf2d6c7af9ec6829218
languageName: node
linkType: hard

"safe-buffer@npm:>=5.1.0, safe-buffer@npm:^5.2.0":
"safe-buffer@npm:>=5.1.0, safe-buffer@npm:^5.0.1, safe-buffer@npm:^5.1.1, safe-buffer@npm:^5.1.2, safe-buffer@npm:^5.2.0":
version: 5.2.1
resolution: "safe-buffer@npm:5.2.1"
checksum: 0bb57f0d8f9d1fa4fe35ad8a2db1f83a027d48f2822d59ede88fd5cd4ddad83c0b497213feb7a70fbf90597a70c5217f735b0eb1850df40ce9b4ae81dd22b3f9
Expand Down Expand Up @@ -20056,6 +20067,17 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"wrap-ansi@npm:^7.0.0":
version: 7.0.0
resolution: "wrap-ansi@npm:7.0.0"
dependencies:
ansi-styles: ^4.0.0
string-width: ^4.1.0
strip-ansi: ^6.0.0
checksum: 09939dd775ae565bb99a25a6c072fe3775a95fa71751b5533c94265fe986ba3e3ab071a027ab76cf26876bd9afd10ac3c2d06d7c4bcce148bf7d2d9514e3a0df
languageName: node
linkType: hard

"wrappy@npm:1":
version: 1.0.2
resolution: "wrappy@npm:1.0.2"
Expand Down Expand Up @@ -20276,6 +20298,13 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"y18n@npm:^5.0.2":
version: 5.0.5
resolution: "y18n@npm:5.0.5"
checksum: a7d41b0cccca1c98ebab270a944df48eb3b5352d3be0affb8afc8369823f6aa97a5fbead2c5b35e59a5650cb786b2b37627b45be5ff31f02a80dd3b881aceb17
languageName: node
linkType: hard

"yallist@npm:^2.1.2":
version: 2.1.2
resolution: "yallist@npm:2.1.2"
Expand Down Expand Up @@ -20330,6 +20359,13 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"yargs-parser@npm:^20.2.2":
version: 20.2.3
resolution: "yargs-parser@npm:20.2.3"
checksum: 6504b053bbb7ac0a3cd4c0e343447f2a7bb7ee18f3de8865a83874531ac8b8b0661cab4a4603a515224d5f5cfcd26d0d9998642b07a58ceab29eea12878123f7
languageName: node
linkType: hard

"yargs@npm:^14.2.0, yargs@npm:^14.2.2":
version: 14.2.3
resolution: "yargs@npm:14.2.3"
Expand All @@ -20349,7 +20385,7 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"yargs@npm:^15.1.0, yargs@npm:^15.4.1":
"yargs@npm:^15.1.0":
version: 15.4.1
resolution: "yargs@npm:15.4.1"
dependencies:
Expand All @@ -20368,6 +20404,21 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"yargs@npm:^16.0.3":
version: 16.1.0
resolution: "yargs@npm:16.1.0"
dependencies:
cliui: ^7.0.2
escalade: ^3.1.1
get-caller-file: ^2.0.5
require-directory: ^2.1.1
string-width: ^4.2.0
y18n: ^5.0.2
yargs-parser: ^20.2.2
checksum: 19e94e62eb653f8310aa4ed6eab797b1756fea90cdacb38dec45afdd21b8607bf38723ee0fae0b967ca3886ad58b8430eb48b716958231f2f1b950c1c7f11034
languageName: node
linkType: hard

"yargs@npm:^2.3.0":
version: 2.3.0
resolution: "yargs@npm:2.3.0"
Expand Down