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
19 changes: 17 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,18 @@ 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
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 +236,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