Skip to content
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

fix: error on scheduled/event-triggered functions with custom route #5344

Merged
merged 9 commits into from
Oct 25, 2023
42 changes: 42 additions & 0 deletions packages/build/src/plugins_core/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resolve } from 'path'
import { NodeBundlerName, RUNTIME, zipFunctions } from '@netlify/zip-it-and-ship-it'
import { pathExists } from 'path-exists'

import { addErrorInfo } from '../../error/info.js'
import { log } from '../../log/logger.js'
import { logBundleResults, logFunctionsNonExistingDir, logFunctionsToBundle } from '../../log/messages/core_steps.js'

Expand All @@ -19,6 +20,45 @@ const getBundlers = (results: Awaited<ReturnType<typeof zipFunctions>> = []) =>
.filter(Boolean) as NodeBundlerName[],
)

// see https://docs.netlify.com/functions/trigger-on-events/#available-triggers
const eventTriggeredFunctions = new Set([
'deploy-building',
'deploy-succeeded',
'deploy-failed',
'deploy-deleted',
'deploy-locked',
'deploy-unlocked',
'submission-created',
'split-test-activated',
'split-test-deactivated',
'split-test-modified',
'identity-validate',
'identity-signup',
'identity-login',
])

const validateCustomRoutes = function (functions: Awaited<ReturnType<typeof zipFunctions>>) {
for (const { routes, name, schedule } of functions) {
if (!routes || routes.length === 0) continue

if (schedule) {
const error = new Error(
`Scheduled functions must not specify a custom path. Please remove the "path" configuration. Learn more about scheduled functions at https://ntl.fyi/custom-path-scheduled-functions.`,
)
addErrorInfo(error, { type: 'resolveConfig' })
throw error
}

if (eventTriggeredFunctions.has(name.toLowerCase().replace('-background', ''))) {
const error = new Error(
`Event-triggered functions must not specify a custom path. Please remove the "path" configuration or pick a different name for the function. Learn more about event-triggered functions at https://ntl.fyi/custom-path-event-triggered-functions.`,
)
addErrorInfo(error, { type: 'resolveConfig' })
throw error
}
}
}

const zipFunctionsAndLogResults = async ({
buildDir,
childEnv,
Expand Down Expand Up @@ -53,6 +93,8 @@ const zipFunctionsAndLogResults = async ({
const sourceDirectories = [internalFunctionsSrc, functionsSrc].filter(Boolean)
const results = await zipItAndShipIt.zipFunctions(sourceDirectories, functionsDist, zisiParameters)

validateCustomRoutes(results)

const bundlers = Array.from(getBundlers(results))

logBundleResults({ logs, results })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => new Response('foo')

export const config = {
path: "/deploy-succeeded",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default () => new Response('Hello, world!')

export const config = {
path: "/daily",
schedule: "@daily"
}
10 changes: 10 additions & 0 deletions packages/build/tests/functions/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ test('Functions: --functionsDistDir', async (t) => {
await removeDir(functionsDistDir)
}
})

test('Functions: custom path on scheduled function', async (t) => {
const output = await new Fixture('./fixtures/custom_path_scheduled').runWithBuild()
t.true(output.includes('Scheduled functions must not specify a custom path.'))
})

test('Functions: custom path on event-triggered function', async (t) => {
const output = await new Fixture('./fixtures/custom_path_event_triggered').runWithBuild()
t.true(output.includes('Event-triggered functions must not specify a custom path.'))
})
Loading