Skip to content

Commit 7d34204

Browse files
fix(node-resolve): handle "package.json" being in path (#927)
* fix(node-resolve): handle "package.json" being in path * apply fix in a few more places too * chore: fix linting * chore(repo): use origin/main as a comparator for filtering tests Co-authored-by: shellscape <andrew@shellscape.org>
1 parent ceb882f commit 7d34204

12 files changed

Lines changed: 57 additions & 18 deletions

File tree

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
**/test/**/output
55
packages/commonjs/test/fixtures
66
packages/typescript/test/fixtures/syntax-error
7+
8+
# temporary workaround for eslint bug where package.json is a directory
9+
packages/node-resolve/test/fixtures/package-json-in-path

.github/workflows/node-windows.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ jobs:
2727
- name: Checkout Commit
2828
uses: actions/checkout@v1
2929

30+
- name: Checkout Master
31+
run: git branch -f master origin/master
32+
3033
- name: Setup Node
3134
uses: actions/setup-node@v1
3235
with:
@@ -39,4 +42,4 @@ jobs:
3942
run: pnpm install --ignore-scripts
4043

4144
- name: run tests
42-
run: pnpm ci:test --filter ...[${{ github.sha }}]
45+
run: pnpm ci:test --filter "...[origin/master]"

.github/workflows/validate.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
with:
3131
node-version: ${{ matrix.node }}
3232

33+
- name: Checkout Master
34+
run: git branch -f master origin/master
35+
3336
- name: Install pnpm
3437
run: npm install pnpm -g
3538

@@ -56,4 +59,4 @@ jobs:
5659
run: pnpm lint:js
5760

5861
- name: Run Tests
59-
run: pnpm ci:coverage --filter ...[${{ github.sha }}]
62+
run: pnpm ci:coverage --filter "...[origin/master]"

packages/node-resolve/src/fs.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ export const readFile = promisify(fs.readFile);
77
export const realpath = promisify(fs.realpath);
88
export { realpathSync } from 'fs';
99
export const stat = promisify(fs.stat);
10-
export async function exists(filePath) {
10+
11+
export async function fileExists(filePath) {
1112
try {
12-
await access(filePath);
13-
return true;
13+
const res = await stat(filePath);
14+
return res.isFile();
1415
} catch {
1516
return false;
1617
}

packages/node-resolve/src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import deepMerge from 'deepmerge';
66
import isModule from 'is-module';
77

88
import { isDirCached, isFileCached, readCachedFile } from './cache';
9-
import { exists, readFile, realpath } from './fs';
9+
import { fileExists, readFile, realpath } from './fs';
1010
import resolveImportSpecifiers from './resolveImportSpecifiers';
1111
import { getMainFields, getPackageName, normalizeInput } from './util';
1212
import handleDeprecatedOptions from './deprecated-options';
@@ -207,8 +207,8 @@ export function nodeResolve(opts = {}) {
207207
}
208208

209209
if (hasPackageEntry && !preserveSymlinks) {
210-
const fileExists = await exists(location);
211-
if (fileExists) {
210+
const exists = await fileExists(location);
211+
if (exists) {
212212
location = await realpath(location);
213213
}
214214
}
@@ -228,7 +228,7 @@ export function nodeResolve(opts = {}) {
228228
}
229229
}
230230

231-
if (options.modulesOnly && (await exists(location))) {
231+
if (options.modulesOnly && (await fileExists(location))) {
232232
const code = await readFile(location, 'utf-8');
233233
if (isModule(code)) {
234234
return {

packages/node-resolve/src/package/utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* eslint-disable no-await-in-loop */
22
import path from 'path';
33
import fs from 'fs';
4-
import { promisify } from 'util';
54

6-
const fileExists = promisify(fs.exists);
5+
import { fileExists } from '../fs';
76

87
function isModuleDir(current, moduleDirs) {
98
return moduleDirs.some((dir) => current.endsWith(dir));

packages/node-resolve/src/resolveImportSpecifiers.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import fs from 'fs';
22
import { promisify } from 'util';
33
import { fileURLToPath, pathToFileURL } from 'url';
44

5+
import { dirname } from 'path';
6+
57
import resolve from 'resolve';
68

79
import { getPackageInfo, getPackageName } from './util';
8-
import { exists, realpath } from './fs';
10+
import { fileExists, realpath } from './fs';
911
import { isDirCached, isFileCached, readCachedFile } from './cache';
1012
import resolvePackageExports from './package/resolvePackageExports';
1113
import resolvePackageImports from './package/resolvePackageImports';
@@ -26,7 +28,7 @@ async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectori
2628
try {
2729
const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
2830
const pkgJson = JSON.parse(await readFile(pkgJsonPath, 'utf-8'));
29-
return { pkgJsonPath, pkgJson };
31+
return { pkgJsonPath, pkgJson, pkgPath: dirname(pkgJsonPath) };
3032
} catch (_) {
3133
return null;
3234
}
@@ -114,12 +116,11 @@ async function resolveId({
114116
const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
115117

116118
if (result && result.pkgJson.exports) {
117-
const { pkgJson, pkgJsonPath } = result;
119+
const { pkgJson, pkgJsonPath, pkgPath } = result;
118120
try {
119121
const subpath =
120122
pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
121-
const pkgDr = pkgJsonPath.replace('package.json', '');
122-
const pkgURL = pathToFileURL(pkgDr);
123+
const pkgURL = pathToFileURL(`${pkgPath}/`);
123124

124125
const context = {
125126
importer,
@@ -157,7 +158,7 @@ async function resolveId({
157158
}
158159

159160
if (!preserveSymlinks) {
160-
if (await exists(location)) {
161+
if (await fileExists(location)) {
161162
location = await realpath(location);
162163
}
163164
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { main } from "dependency/dist/something.js";
2+
3+
export default main();

packages/node-resolve/test/fixtures/package-json-in-path/package.json/node_modules/dependency/dist-esm/something.js

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

packages/node-resolve/test/fixtures/package-json-in-path/package.json/node_modules/dependency/package.json

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

0 commit comments

Comments
 (0)