Skip to content

Commit 331dd1c

Browse files
committed
fix: Fix main module resolution in case pkg.main leads to broken path
1 parent 2469fe9 commit 331dd1c

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

get-resolver/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ module.exports = function (extensions, confirmFile, resolvePackageMain) {
2222
var mainPath = join(path, result);
2323
return resolveFile(mainPath).then(function (mainResult) {
2424
if (mainResult) return mainResult;
25-
return resolveFile(join(mainPath, "index"), 0);
25+
return resolveFile(join(mainPath, "index"), 0).then(function (mainDirResult) {
26+
if (mainDirResult) return mainDirResult;
27+
return resolveFile(join(path, "index"), 0);
28+
});
2629
});
2730
}
2831
return resolveFile(join(path, "index"), 0);

test/__playground/node_modules/broken-main/index.js

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

test/__playground/node_modules/broken-main/package.json

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

test/resolve/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ module.exports = (t, a) =>
164164
t(playgroundDir, "nested/elo").then(unexpected, error => {
165165
a(isModuleNotFoundError(error, "nested/elo"), true);
166166
}),
167+
t(playgroundDir, "broken-main").then(value => {
168+
a.deep(value, {
169+
targetPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`),
170+
realPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`)
171+
});
172+
}),
167173
t(`${ playgroundDir }/node_modules/outer`, "outer3").then(value => {
168174
a.deep(value, {
169175
targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`),

test/resolve/sync.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ module.exports = (t, a) => {
121121
targetPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`),
122122
realPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`)
123123
});
124+
a.deep(t(playgroundDir, "broken-main"), {
125+
targetPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`),
126+
realPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`)
127+
});
124128
try {
125129
t(playgroundDir, "nested/elo");
126130
throw new Error("Unexpected");

0 commit comments

Comments
 (0)