The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(studio): sort edge function files by name and select the entrypoint by default (#51796)
Drew McLellanb74d799c3dc8
diff --git a/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.test.ts b/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.test.tsindex 89d299dd0d4..9eac0d4432b 100644--- a/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.test.ts+++ b/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest'-import { formatFunctionBodyToFiles } from './EdgeFunctions.utils'+import { formatFunctionBodyToFiles, getDefaultSelectedFileId } from './EdgeFunctions.utils'describe('formatFunctionBodyToFiles', () => {it('returns no files when there is no entrypoint path', () => {@@ -70,3 +70,123 @@ describe('formatFunctionBodyToFiles', () => {])})})++describe('formatFunctionBodyToFiles sorting', () => {+ const format = (names: string[], entrypoint = 'index.ts') =>+ formatFunctionBodyToFiles({+ functionBody: {+ files: names.map((name) => ({ name, content: '' })),+ metadata: { deno2_entrypoint_path: entrypoint },+ },+ })++ it('sorts files by name', () => {+ const result = format(['utils.ts', 'index.ts', 'deno.json'])+ expect(result.map((f) => f.name)).toEqual(['deno.json', 'index.ts', 'utils.ts'])+ })++ it('sorts numerically so file2 comes before file10', () => {+ const result = format(['file10.ts', 'file2.ts', 'index.ts'])+ expect(result.map((f) => f.name)).toEqual(['file2.ts', 'file10.ts', 'index.ts'])+ })++ it('sorts case-insensitively', () => {+ const result = format(['b.ts', 'A.ts', 'index.ts'])+ expect(result.map((f) => f.name)).toEqual(['A.ts', 'b.ts', 'index.ts'])+ })++ it('sorts by relative name after rewriting nested paths', () => {+ const result = formatFunctionBodyToFiles({+ functionBody: {+ files: [+ { name: 'functions/hello/utils/helper.ts', content: '' },+ { name: 'functions/hello/index.ts', content: '' },+ ],+ metadata: { deno2_entrypoint_path: 'functions/hello/index.ts' },+ },+ })+ expect(result.map((f) => f.name)).toEqual(['index.ts', 'utils/helper.ts'])+ })++ it('assigns unique sequential ids following sorted order', () => {+ const result = format(['c.ts', 'a.ts', 'index.ts', 'b.ts'])+ expect(result.map((f) => [f.id, f.name])).toEqual([+ [1, 'a.ts'],+ [2, 'b.ts'],+ [3, 'c.ts'],+ [4, 'index.ts'],+ ])+ })+})++describe('getDefaultSelectedFileId', () => {+ const files = [+ { id: 1, name: 'a.ts' },+ { id: 2, name: 'handler.ts' },+ { id: 3, name: 'index.ts' },+ ]++ it('selects the entrypoint file', () => {+ expect(getDefaultSelectedFileId(files, 'handler.ts')).toBe(2)+ })++ it('matches on the basename of a nested entrypoint path', () => {+ expect(getDefaultSelectedFileId(files, 'functions/hello/handler.ts')).toBe(2)+ })++ it('matches on the basename of a URL entrypoint', () => {+ expect(+ getDefaultSelectedFileId(files, 'https://edge.supabase.com/deploy/abc123/handler.ts')+ ).toBe(2)+ })++ it('falls back to index.ts when the entrypoint matches no file', () => {+ expect(getDefaultSelectedFileId(files, 'missing.ts')).toBe(3)+ })++ it('falls back to index.ts when there is no entrypoint', () => {+ expect(getDefaultSelectedFileId(files)).toBe(3)+ expect(getDefaultSelectedFileId(files, null)).toBe(3)+ })++ it('falls back to the first file when there is no index.ts', () => {+ expect(getDefaultSelectedFileId(files.slice(0, 2), 'missing.ts')).toBe(1)+ })++ it('returns undefined when there are no files', () => {+ expect(getDefaultSelectedFileId([], 'index.ts')).toBeUndefined()+ })++ it('selects the entrypoint closest to the root when several files match its path', () => {+ const entrypointPath = 'functions/hello/index.ts'++ for (const names of [+ ['index.ts', 'functions/hello/index.ts'],+ ['functions/hello/index.ts', 'index.ts'],+ ]) {+ const files = formatFunctionBodyToFiles({+ functionBody: {+ files: names.map((name) => ({ name, content: name })),+ metadata: { deno2_entrypoint_path: entrypointPath },+ },+ })+ const id = getDefaultSelectedFileId(files, entrypointPath)++ expect(files.map((f) => f.name)).toEqual(['functions/hello/index.ts', 'index.ts'])+ expect(files.find((f) => f.id === id)?.name).toBe('index.ts')+ }+ })++ it('does not match a file name that only shares trailing characters', () => {+ const entrypointPath = 'functions/hello/index.ts'+ const files = formatFunctionBodyToFiles({+ functionBody: {+ files: ['dex.ts', 'index.ts'].map((name) => ({ name, content: name })),+ metadata: { deno2_entrypoint_path: entrypointPath },+ },+ })+ const id = getDefaultSelectedFileId(files, entrypointPath)++ expect(files.find((f) => f.id === id)?.name).toBe('index.ts')+ })+})diff --git a/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.ts b/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.tsindex ddba382e9d8..ba971b27435 100644--- a/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.ts+++ b/apps/studio/components/interfaces/EdgeFunctions/EdgeFunctions.utils.ts@@ -229,12 +229,23 @@ export const getStaticPatterns = (files: Omit<FileData, 'id' | 'content' | 'stat.map(({ name }) => name)}+// Finds the body file the entrypoint refers to: any file whose path is a trailing path of the+// entrypoint. When several match (e.g. `index.ts` and `functions/hello/index.ts` for an entrypoint+// ending in `functions/hello/index.ts`), the one closest to the root wins.+function findEntrypointFileName(entrypoint: string, fileNames: string[]): string | undefined {+ const depth = (name: string) => name.split('/').length++ return fileNames+ .filter((name) => entrypoint === name || entrypoint.endsWith(`/${name}`))+ .sort((a, b) => depth(a) - depth(b))[0]+}+function getBasePath(entrypoint: string | undefined, fileNames: string[]): string {if (!entrypoint) {return '/'}- let candidate = fileNames.find((name) => entrypoint.endsWith(name))+ let candidate = findEntrypointFileName(entrypoint, fileNames)if (candidate) {return dirname(candidate)@@ -284,7 +295,12 @@ export const formatFunctionBodyToFiles = ({}})- return filesWithRelPath.map((file: { name: string; content: string }, index: number) => {+ // sort by displayed (relative) name before assigning ids, so ids follow list order+ const sortedFiles = [...filesWithRelPath].sort((a, b) =>+ a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' })+ )++ return sortedFiles.map((file: { name: string; content: string }, index: number) => {return {id: index + 1,name: file.name,@@ -296,3 +312,16 @@ export const formatFunctionBodyToFiles = ({return []}++// Picks the file to open by default: the function's entrypoint, then `index.ts`, then the first file.+export const getDefaultSelectedFileId = (+ files: Pick<FileData, 'id' | 'name'>[],+ entrypointPath?: string | null+): number | undefined => {+ const entrypointName = entrypointPath?.split('/').pop()+ const entrypointFile = entrypointName+ ? files.find((file) => file.name === entrypointName)+ : undefined++ return (entrypointFile ?? files.find((file) => file.name === 'index.ts') ?? files[0])?.id+}diff --git a/apps/studio/pages/project/[ref]/functions/[functionSlug]/code.tsx b/apps/studio/pages/project/[ref]/functions/[functionSlug]/code.tsxindex 912edd12074..a2707468a50 100644--- a/apps/studio/pages/project/[ref]/functions/[functionSlug]/code.tsx+++ b/apps/studio/pages/project/[ref]/functions/[functionSlug]/code.tsx@@ -7,7 +7,10 @@ import { toast } from 'sonner'import { LogoLoader } from 'ui'import { DeployEdgeFunctionWarningModal } from '@/components/interfaces/EdgeFunctions/DeployEdgeFunctionWarningModal'-import { formatFunctionBodyToFiles } from '@/components/interfaces/EdgeFunctions/EdgeFunctions.utils'+import {+ formatFunctionBodyToFiles,+ getDefaultSelectedFileId,+} from '@/components/interfaces/EdgeFunctions/EdgeFunctions.utils'import { DefaultLayout } from '@/components/layouts/DefaultLayout'import EdgeFunctionDetailsLayout from '@/components/layouts/EdgeFunctionsLayout/EdgeFunctionDetailsLayout'import { DiscardChangesConfirmationDialog } from '@/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog'@@ -74,6 +77,12 @@ const CodePage = () => {: []}, [functionBody, selectedFunction?.entrypoint_path])+ const [selectedFileId, setSelectedFileId] = useState<number>()+ const defaultSelectedFileId = getDefaultSelectedFileId(+ initialFiles,+ functionBody?.metadata?.deno2_entrypoint_path ?? selectedFunction?.entrypoint_path+ )+const { mutate: deployFunction, isPending: isDeploying } = useEdgeFunctionDeployMutation({onSuccess: () => {toast.success('Successfully updated edge function')@@ -190,6 +199,8 @@ const CodePage = () => {<><FileExplorerAndEditorfiles={files}+ selectedFileId={selectedFileId ?? defaultSelectedFileId}+ setSelectedFileId={setSelectedFileId}onFilesChange={(files) => {const formattedFiles: FileData[] = files.map((f) => {const originalFile = initialFiles.find((x) => x.id === f.id)