Skip to content

Commit

Permalink
fix: add error message if unable to find cron expression for schedule…
Browse files Browse the repository at this point in the history
…d function (#1123)

* fix: add error message if cron expression not found or invalid in isc of scheduled funcs
  • Loading branch information
jenae-janzen committed Jun 27, 2022
1 parent 7e21b63 commit 2309f4b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/runtimes/node/in_source_config/index.ts
Expand Up @@ -23,6 +23,10 @@ export const findISCDeclarationsInPath = async (sourcePath: string): Promise<ISC
}

const imports = ast.body.flatMap((node) => getImports(node, IN_SOURCE_CONFIG_MODULE))

const scheduledFuncsExpected = imports.filter(({ imported }) => imported === 'schedule').length
let scheduledFuncsFound = 0

const getAllBindings = createBindingsMethod(ast.body)
const mainExports = getMainExport(ast.body, getAllBindings)
const iscExports = mainExports
Expand All @@ -34,16 +38,29 @@ export const findISCDeclarationsInPath = async (sourcePath: string): Promise<ISC
}

switch (matchingImport.imported) {
case 'schedule':
return parseSchedule({ args }, getAllBindings)
case 'schedule': {
const parsed = parseSchedule({ args }, getAllBindings)

if (parsed.schedule) {
scheduledFuncsFound += 1
}

return parsed
}
default:
// no-op
}

return null
})
.filter(nonNullable)

if (scheduledFuncsFound < scheduledFuncsExpected) {
throw new Error(
'Warning: unable to find cron expression for scheduled function. `schedule` imported but not called or exported. If you meant to schedule a function, please check that `schedule` is invoked with an appropriate cron expression.',
)
}

const mergedExports: ISCValues = iscExports.reduce((acc, obj) => ({ ...acc, ...obj }), {})

return mergedExports
Expand Down
@@ -0,0 +1,9 @@
import { schedule as nfySchedule } from '@netlify/functions'
import { schedule } from '../node_modules/@netlify/functions/index.js'
// make sure cron expression is found/doesn't error if `schedule` is also imported from another source

schedule()

export const handler = nfySchedule('@daily', () => {
// function handler
})
@@ -0,0 +1,5 @@
const { schedule } = require('@netlify/functions')

module.exports.handler = schedule(null, () => {
// function handler
})
@@ -0,0 +1,4 @@
const { schedule } = require('@netlify/functions')

// Should throw an error that `schedule` is imported but cron expression not found
module.exports.handler = {}
@@ -0,0 +1,5 @@
// eslint-disable-next-line n/no-unsupported-features/es-syntax
import { schedule } from '@netlify/functions'

// Should throw an error that schedule is imported but cron expression not found
export const handler = {}
20 changes: 18 additions & 2 deletions tests/main.js
Expand Up @@ -2633,7 +2633,7 @@ testMany(
'Finds in-source config declarations using the `schedule` helper',
['bundler_default', 'bundler_esbuild', 'bundler_nft'],
async (options, t) => {
const FUNCTIONS_COUNT = 11
const FUNCTIONS_COUNT = 12
const { files } = await zipFixture(t, join('in-source-config', 'functions'), {
opts: options,
length: FUNCTIONS_COUNT,
Expand All @@ -2647,6 +2647,22 @@ testMany(
},
)

testMany(
'Throws error when `schedule` helper is imported but cron expression not found',
['bundler_default', 'bundler_esbuild', 'bundler_nft'],
async (options, t) => {
const rejected = (error) => {
t.true(error.message.startsWith('Warning: unable to find cron expression for scheduled function.'))
}

const FUNCTIONS_COUNT = 3
await zipFixture(t, join('in-source-config', 'functions_missing_cron_expression'), {
opts: options,
length: FUNCTIONS_COUNT,
}).catch(rejected)
},
)

test('listFunctions surfaces schedule config property', async (t) => {
const functions = await listFunctions(join(FIXTURES_DIR, 'many-functions'), {
config: {
Expand All @@ -2663,7 +2679,7 @@ test('listFunctions includes in-source config declarations', async (t) => {
const functions = await listFunctions(join(FIXTURES_DIR, 'in-source-config', 'functions'), {
parseISC: true,
})
const FUNCTIONS_COUNT = 11
const FUNCTIONS_COUNT = 12
t.is(functions.length, FUNCTIONS_COUNT)
functions.forEach((func) => {
t.is(func.schedule, '@daily')
Expand Down

1 comment on commit 2309f4b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⏱ Benchmark results

largeDepsEsbuild: 7s

largeDepsNft: 33.4s

largeDepsZisi: 50.6s

Please sign in to comment.