Skip to content

Commit

Permalink
fix: remove default fallback when target function does not exist (#497)
Browse files Browse the repository at this point in the history
Functions framework will no longer load a default function named
'function' if one is exported and the configured target can
not be found. This behavior is unexpected and inconsistent with
other function framework implementations.
  • Loading branch information
garethgeorge authored Jan 5, 2023
1 parent d8ce21c commit f3325d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function getUserFunction(
return registeredFunction;
}

let userFunction = functionTarget
const userFunction = functionTarget
.split('.')
.reduce((code, functionTargetPart) => {
if (typeof code === 'undefined') {
Expand All @@ -131,18 +131,12 @@ export async function getUserFunction(
}
}, functionModule);

// TODO: do we want 'function' fallback?
if (typeof userFunction === 'undefined') {
// eslint-disable-next-line no-prototype-builtins
if (functionModule.hasOwnProperty('function')) {
userFunction = functionModule['function'];
} else {
console.error(
`Function '${functionTarget}' is not defined in the provided ` +
'module.\nDid you specify the correct target function to execute?'
);
return null;
}
console.error(
`Function '${functionTarget}' is not defined in the provided ` +
'module.\nDid you specify the correct target function to execute?'
);
return null;
}

if (typeof userFunction !== 'function') {
Expand Down
14 changes: 14 additions & 0 deletions test/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ describe('loading function', () => {
}
}

it('fails to load a function that does not exist', async () => {
FunctionRegistry.http('function', () => {
return 'PASS';
});

const loadedFunction = await loader.getUserFunction(
process.cwd() + '/test/data/with_main',
'functionDoesNotExist',
'http'
);

assert.strictEqual(loadedFunction, null);
});

it('loads a declaratively registered function', async () => {
FunctionRegistry.http('registeredFunction', () => {
return 'PASS';
Expand Down

0 comments on commit f3325d3

Please sign in to comment.