Skip to content

Commit 51f20a4

Browse files
authored
fix: module resolution returns early and accurate error messages on failure (#534)
1 parent 41e9e8e commit 51f20a4

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

Diff for: src/loader.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ export async function getUserFunction(
9393
try {
9494
const functionModulePath = getFunctionModulePath(codeLocation);
9595
if (functionModulePath === null) {
96-
console.error('Provided code is not a loadable module.');
96+
console.error(
97+
`Provided code location '${codeLocation}' is not a loadable module.` +
98+
'\nDid you specify the correct location for the module defining ' +
99+
'your function?'
100+
);
97101
return null;
98102
}
99103

@@ -174,16 +178,17 @@ export async function getUserFunction(
174178
* @return Resolved path or null.
175179
*/
176180
function getFunctionModulePath(codeLocation: string): string | null {
177-
let path: string | null = null;
178181
try {
179-
path = require.resolve(codeLocation);
182+
return require.resolve(codeLocation);
180183
} catch (ex) {
181-
try {
182-
// TODO: Decide if we want to keep this fallback.
183-
path = require.resolve(codeLocation + '/function.js');
184-
} catch (ex) {
185-
return path;
186-
}
184+
// Ignore exception, this means the function was not found here.
185+
}
186+
187+
try {
188+
return require.resolve(codeLocation + '/function.js');
189+
} catch (ex) {
190+
// Ignore exception, this means the function was not found here.
187191
}
188-
return path;
192+
193+
return null;
189194
}

Diff for: test/loader.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,17 @@ describe('loading function', () => {
9393
}
9494
}
9595

96-
it('fails to load a function that does not exist', async () => {
97-
FunctionRegistry.http('function', () => {
98-
return 'PASS';
99-
});
96+
it('fails to load a module that does not exist', async () => {
97+
const loadedFunction = await loader.getUserFunction(
98+
process.cwd() + '/test/data/does_not_exist',
99+
'functionDoesNotExist',
100+
'http'
101+
);
100102

103+
assert.strictEqual(loadedFunction, null);
104+
});
105+
106+
it('fails to load a function that does not exist', async () => {
101107
const loadedFunction = await loader.getUserFunction(
102108
process.cwd() + '/test/data/with_main',
103109
'functionDoesNotExist',

0 commit comments

Comments
 (0)