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: determine duplicate function precedence based on extension #5617

Merged
merged 7 commits into from
Apr 12, 2023
20 changes: 18 additions & 2 deletions src/lib/functions/registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import runtimes from './runtimes/index.mjs'

const ZIP_EXTENSION = '.zip'

// Used to determine the precedence of duplicate functions
const EXTENSIONS_BY_PRECEDENCE = ['.js', '.zip', '.mjs', '.cjs', '.go', '.ts', '.tsx', '.mts', '.cts', '.rs']

export class FunctionsRegistry {
constructor({ capabilities, config, debug = false, isConnected = false, projectRoot, settings, timeouts }) {
this.capabilities = capabilities
Expand Down Expand Up @@ -181,6 +184,19 @@ export class FunctionsRegistry {
return await listFunctions(...args)
}

// Check if a function has a duplicate with a preceding extension
hasPrecedingDuplicate({ mainFile, name }) {
const registeredDupe = this.functions.get(name)
khendrikse marked this conversation as resolved.
Show resolved Hide resolved
if (!registeredDupe) return false
const dupeExtension = extname(registeredDupe.mainFile)
const dupeExtIndex = EXTENSIONS_BY_PRECEDENCE.indexOf(dupeExtension)

const extension = extname(mainFile)
const extIndex = EXTENSIONS_BY_PRECEDENCE.indexOf(extension)

return dupeExtIndex < extIndex
}

async scan(relativeDirs) {
const directories = relativeDirs.filter(Boolean).map((dir) => (isAbsolute(dir) ? dir : join(this.projectRoot, dir)))

Expand Down Expand Up @@ -221,8 +237,8 @@ export class FunctionsRegistry {
return
}

// If this function has already been registered, we skip it.
if (this.functions.has(name)) {
// If this function has already been registered and this function shouldn't precede it by extension, we skip it.
if (this.functions.has(name) && this.hasPrecedingDuplicate({ mainFile, name })) {
return
}

Expand Down
81 changes: 80 additions & 1 deletion tests/unit/lib/functions/registry.test.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
import { expect, test, vi } from 'vitest'
import { mkdir, mkdtemp, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'

import { describe, expect, test, vi } from 'vitest'

import { FunctionsRegistry } from '../../../../src/lib/functions/registry.mjs'
import { watchDebounced } from '../../../../src/utils/command-helpers.mjs'

const duplicateFunctions = [
{
filename: 'hello.js',
content: `exports.handler = async (event) => ({ statusCode: 200, body: JSON.stringify({ message: 'Hello World from .js' }) })`,
},
{
filename: 'hello.ts',
content: `exports.handler = async (event) => ({ statusCode: 200, body: JSON.stringify({ message: 'Hello World from .ts' }) })`,
},
{
filename: 'hello/main.go',
subDir: 'hello',
content: `package main
import (
"fmt"
)

func main() {
fmt.Println("Hello, world from a go function!")
}
`,
},
{
filename: 'hello2.ts',
content: `exports.handler = async (event) => ({ statusCode: 200, body: JSON.stringify({ message: 'Hello World from .ts' }) })`,
},
{
filename: 'hello2/main.go',
subDir: 'hello2',
content: `package main
import (
"fmt"
)

func main() {
fmt.Println("Hello, world from a go function!")
}
`,
},
]

vi.mock('../../../../src/utils/command-helpers.mjs', async () => {
const helpers = await vi.importActual('../../../../src/utils/command-helpers.mjs')

Expand Down Expand Up @@ -35,6 +80,40 @@ test('registry should only pass functions config to zip-it-and-ship-it', async (
await prepareDirectoryScanStub.mockRestore()
})

describe('the registry handles duplicate functions based on extension precedence', () => {
test('where .js takes precedence over .go, and .go over .ts', async () => {
const projectRoot = await mkdtemp(join(tmpdir(), 'functions-extension-precedence'))
const functionsDirectory = join(projectRoot, 'functions')
await mkdir(functionsDirectory)

duplicateFunctions.forEach(async (func) => {
if (func.subDir) {
const subDir = join(functionsDirectory, func.subDir)
await mkdir(subDir)
}
const file = join(functionsDirectory, func.filename)
await writeFile(file, func.content)
})
const functionsRegistry = new FunctionsRegistry({
projectRoot,
config: {},
timeouts: { syncFunctions: 1, backgroundFunctions: 1 },
settings: { port: 8888 },
})
const prepareDirectoryScanStub = vi.spyOn(FunctionsRegistry, 'prepareDirectoryScan').mockImplementation(() => {})
const setupDirectoryWatcherStub = vi.spyOn(functionsRegistry, 'setupDirectoryWatcher').mockImplementation(() => {})

await functionsRegistry.scan([functionsDirectory])
const { functions } = functionsRegistry

expect(functions.get('hello').runtime.name).toBe('js')
expect(functions.get('hello2').runtime.name).toBe('go')

await setupDirectoryWatcherStub.mockRestore()
await prepareDirectoryScanStub.mockRestore()
})
})

test('should add included_files to watcher', async () => {
const registry = new FunctionsRegistry({})
const func = {
Expand Down