Skip to content

fix: omit duplicates from list of bundled functions #3795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
5 changes: 4 additions & 1 deletion packages/build/src/plugins_core/functions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const getRelativeFunctionMainFiles = async function ({ featureFlags, functionsSr

const zisiFeatureFlags = getZisiFeatureFlags(featureFlags)
const functions = await listFunctions(functionsSrc, { featureFlags: zisiFeatureFlags })
return functions.map(({ mainFile }) => relative(functionsSrc, mainFile))
const dedupedFunctions = new Map(functions.map((func) => [func.name, func]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, any reason you are not going with Set? Set ensures no duplicates exist by design :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might have different functions with the same name. By passing an array of entries to Map, we guarantee that the last function with the same name takes precedence, which is the order in which zip-it-and-ship-it outputs the files.

Does that answer the question?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for the explanation :)

const relativeMainFiles = [...dedupedFunctions.values()].map(({ mainFile }) => relative(functionsSrc, mainFile))

return relativeMainFiles
}

export const getUserAndInternalFunctions = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const handler = () => true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const handler = () => true
50 changes: 50 additions & 0 deletions packages/build/tests/core/snapshots/tests.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2276,3 +2276,53 @@ Generated by [AVA](https://avajs.dev).
────────────────────────────────────────────────────────────────␊
(Netlify Build completed in 1ms)`

## Removes duplicate function names from the list of processed functions

> Snapshot 1

`␊
────────────────────────────────────────────────────────────────␊
Netlify Build ␊
────────────────────────────────────────────────────────────────␊
> Version␊
@netlify/build 1.0.0␊
> Flags␊
debug: true␊
repositoryRoot: packages/build/tests/core/fixtures/functions_duplicate_names␊
testOpts:␊
pluginsListUrl: test␊
silentLingeringProcesses: true␊
> Current directory␊
packages/build/tests/core/fixtures/functions_duplicate_names␊
> Config file␊
No config file was defined: using default values.␊
> Resolved config␊
build:␊
publish: packages/build/tests/core/fixtures/functions_duplicate_names␊
publishOrigin: default␊
functionsDirectory: packages/build/tests/core/fixtures/functions_duplicate_names/netlify/functions␊
> Context␊
production␊
────────────────────────────────────────────────────────────────␊
1. Functions bundling ␊
────────────────────────────────────────────────────────────────␊
Packaging Functions from netlify/functions directory:␊
- function_one.js␊
(Functions bundling completed in 1ms)␊
────────────────────────────────────────────────────────────────␊
Netlify Build Complete ␊
────────────────────────────────────────────────────────────────␊
(Netlify Build completed in 1ms)`
Binary file modified packages/build/tests/core/snapshots/tests.js.snap
Binary file not shown.
4 changes: 4 additions & 0 deletions packages/build/tests/core/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ test('Bundles functions from the `.netlify/functions-internal` directory even if
await runFixture(t, 'functions_user_missing')
})

test('Removes duplicate function names from the list of processed functions', async (t) => {
await runFixture(t, 'functions_duplicate_names')
})

test.serial('`rustTargetDirectory` is passed to zip-it-and-ship-it only when running in buildbot', async (t) => {
const fixtureWithConfig = 'functions_config_1'
const fixtureWithoutConfig = 'functions_internal_missing'
Expand Down