Skip to content

Commit 937ed9f

Browse files
committed
fix: improve error messages when files are missing or mismatched
1 parent d592501 commit 937ed9f

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

src/utils/get-source-path.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const tryExtensions = async (
99
) => {
1010
for (const extension of extensions) {
1111
const pathWithExtension = pathWithoutExtension + extension;
12+
console.log("checking for path", pathWithExtension);
1213
if (await fsExists(pathWithExtension)) {
1314
return {
1415
extension,
@@ -36,22 +37,23 @@ export const getSourcePath = async (
3637
) => {
3738
const sourcePathUnresolved = source + exportEntry.outputPath.slice(dist.length);
3839

39-
for (const distExtension of distExtensions) {
40-
if (exportEntry.outputPath.endsWith(distExtension)) {
41-
const sourcePath = await tryExtensions(
42-
sourcePathUnresolved.slice(0, -distExtension.length),
43-
extensionMap[distExtension],
44-
);
45-
46-
if (sourcePath) {
47-
return {
48-
input: sourcePath.path,
49-
srcExtension: sourcePath.extension,
50-
distExtension,
51-
};
52-
}
40+
const distExtension = distExtensions.find((distExtension) => exportEntry.outputPath.endsWith(distExtension));
41+
if (distExtension) {
42+
const sourcePathWithoutExtension = sourcePathUnresolved.slice(0, -distExtension.length);
43+
const sourcePath = await tryExtensions(
44+
sourcePathWithoutExtension,
45+
extensionMap[distExtension],
46+
);
47+
48+
if (sourcePath) {
49+
return {
50+
input: sourcePath.path,
51+
srcExtension: sourcePath.extension,
52+
distExtension,
53+
};
5354
}
55+
throw new Error(`Could not find matching source file for export path: ${stringify(exportEntry.outputPath)}; Expected: ${sourcePathWithoutExtension}[${extensionMap[distExtension].join('|')}]`);
5456
}
5557

56-
throw new Error(`Could not find matching source file for export path ${stringify(exportEntry.outputPath)}`);
58+
throw new Error(`Package.json output path contains invalid extension: ${stringify(exportEntry.outputPath)}; Expected: ${distExtensions.join(', ')}`);
5759
};

tests/specs/error-cases.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,50 @@ export default testSuite(({ describe }, nodePath: string) => {
106106
expect(pkgrollProcess.stderr).toMatch('Ignoring entry outside of ./dist/ directory: package.json#main="/dist/main.js"');
107107
expect(pkgrollProcess.stderr).toMatch('No export entries found in package.json');
108108
});
109+
110+
test('cannot find matching source file', async () => {
111+
await using fixture = await createFixture({
112+
...packageFixture(),
113+
'package.json': createPackageJson({
114+
name: 'pkg',
115+
main: 'dist/missing.js',
116+
module: 'dist/missing.mjs',
117+
}),
118+
});
119+
120+
const pkgrollProcess = await pkgroll(
121+
[],
122+
{
123+
cwd: fixture.path,
124+
nodePath,
125+
reject: false,
126+
},
127+
);
128+
expect(pkgrollProcess.exitCode).toBe(1);
129+
expect(pkgrollProcess.stderr).toMatch('Could not find matching source file for export path');
130+
expect(pkgrollProcess.stderr).toMatch('Expected: ./src/missing[.js|.ts|.tsx|.mts|.cts]');
131+
});
132+
133+
test('unexpected extension', async () => {
134+
await using fixture = await createFixture({
135+
...packageFixture(),
136+
'package.json': createPackageJson({
137+
name: 'pkg',
138+
main: 'dist/index.foo',
139+
}),
140+
});
141+
142+
const pkgrollProcess = await pkgroll(
143+
[],
144+
{
145+
cwd: fixture.path,
146+
nodePath,
147+
reject: false,
148+
},
149+
);
150+
expect(pkgrollProcess.exitCode).toBe(1);
151+
expect(pkgrollProcess.stderr).toMatch('Error: Package.json output path contains invalid extension');
152+
expect(pkgrollProcess.stderr).toMatch('Expected: .d.ts, .d.mts, .d.cts, .js, .mjs, .cjs');
153+
});
109154
});
110155
});

0 commit comments

Comments
 (0)