From 0e29dfda1bebeeffa70249ab4c6c59495cfed0cf Mon Sep 17 00:00:00 2001 From: "supabase-vercel-tedd[bot]" <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> Date: Thu, 12 Nov 2026 06:58:24 -0600 Subject: [PATCH] fix(studio): return no edge functions when the self-hosted functions folder is missing (#53229) ## Problem In self-hosted and local Studio, `FileSystemFunctionsArtifactStore` reads the folder set by `EDGE_FUNCTIONS_MANAGEMENT_FOLDER` with `readdir`. When that folder does not exist yet (for example, a fresh project before any function is created), `readdir` throws `ENOENT`, the `/api/v1/projects/[ref]/functions` route returns a 500, and the Edge Functions page shows "Failed to retrieve edge functions" instead of its empty state. ## Solution - Add a private `readFunctionsFolder()` helper that returns an empty list when the folder is missing (`ENOENT`) and rethrows every other error. - `getFunctions()` now returns `[]` and `getFunctionBySlug()` returns `undefined` for a missing folder. - Add `fileSystemStore.test.ts`, which uses real temp folders to cover the missing folder, a non-ENOENT error (path is a file), and the normal listing (skips `main` and folders without an `index` entrypoint). No change to the hosted platform path. ## Review instructions This only affects self-hosted Studio, so the Vercel preview cannot show it. Check it with: 1. `cd apps/studio && pnpm exec vitest run lib/api/self-hosted/functions/`: all tests pass. 2. Optional, locally: run self-hosted Studio with `EDGE_FUNCTIONS_MANAGEMENT_FOLDER` pointing to a folder that does not exist, open Edge Functions, and see the empty state instead of a 500. Checks run: the Vitest command above, `pnpm turbo run typecheck --filter='...[origin/master]'`, ESLint and Prettier on the changed files. ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) [FE-4923](https://linear.app/supabase/issue/FE-4923/fix-studio-edge-functions-500-error-when-local-functions-folder-does) --------- Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> Co-authored-by: Ali Waseem --- .../functions/fileSystemStore.test.ts | 69 +++++++++++++++++++ .../self-hosted/functions/fileSystemStore.ts | 21 ++++-- .../api/self-hosted/functions/index.test.ts | 8 +-- .../lib/api/self-hosted/functions/index.ts | 14 ++-- 4 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 apps/studio/lib/api/self-hosted/functions/fileSystemStore.test.ts diff --git a/apps/studio/lib/api/self-hosted/functions/fileSystemStore.test.ts b/apps/studio/lib/api/self-hosted/functions/fileSystemStore.test.ts new file mode 100644 index 00000000000..65962dc062f --- /dev/null +++ b/apps/studio/lib/api/self-hosted/functions/fileSystemStore.test.ts @@ -0,0 +1,69 @@ +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import path from 'node:path' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' + +import { FileSystemFunctionsArtifactStore } from './fileSystemStore' + +describe('FileSystemFunctionsArtifactStore', () => { + let rootDir: string + + beforeEach(async () => { + rootDir = await mkdtemp(path.join(tmpdir(), 'functions-store-')) + }) + + afterEach(async () => { + await rm(rootDir, { recursive: true, force: true }) + }) + + describe('when the functions folder does not exist', () => { + it('getFunctions returns an empty list', async () => { + const store = new FileSystemFunctionsArtifactStore(path.join(rootDir, 'missing')) + + await expect(store.getFunctions()).resolves.toEqual([]) + }) + + it('getFunctionBySlug returns undefined', async () => { + const store = new FileSystemFunctionsArtifactStore(path.join(rootDir, 'missing')) + + await expect(store.getFunctionBySlug('hello')).resolves.toBeUndefined() + }) + }) + + describe('when no functions folder is configured', () => { + const store = new FileSystemFunctionsArtifactStore(undefined) + + it('getFunctions returns an empty list', async () => { + await expect(store.getFunctions()).resolves.toEqual([]) + }) + + it('getFunctionBySlug returns undefined', async () => { + await expect(store.getFunctionBySlug('hello')).resolves.toBeUndefined() + }) + + it('getFileEntriesBySlug returns an empty list', async () => { + await expect(store.getFileEntriesBySlug('hello')).resolves.toEqual([]) + }) + }) + + it('rethrows errors other than a missing folder', async () => { + const filePath = path.join(rootDir, 'not-a-folder') + await writeFile(filePath, '') + const store = new FileSystemFunctionsArtifactStore(filePath) + + await expect(store.getFunctions()).rejects.toMatchObject({ code: 'ENOTDIR' }) + }) + + it('lists functions that have an index entrypoint, skipping main', async () => { + await mkdir(path.join(rootDir, 'hello')) + await writeFile(path.join(rootDir, 'hello', 'index.ts'), '') + await mkdir(path.join(rootDir, 'main')) + await writeFile(path.join(rootDir, 'main', 'index.ts'), '') + await mkdir(path.join(rootDir, 'no-entrypoint')) + const store = new FileSystemFunctionsArtifactStore(rootDir) + + const functions = await store.getFunctions() + + expect(functions.map((f) => f.slug)).toEqual(['hello']) + }) +}) diff --git a/apps/studio/lib/api/self-hosted/functions/fileSystemStore.ts b/apps/studio/lib/api/self-hosted/functions/fileSystemStore.ts index f5b783866a8..71f93b51e99 100644 --- a/apps/studio/lib/api/self-hosted/functions/fileSystemStore.ts +++ b/apps/studio/lib/api/self-hosted/functions/fileSystemStore.ts @@ -6,10 +6,11 @@ import { pathToFileURL } from 'node:url' import { FunctionArtifact, FunctionFileEntry } from './types' export class FileSystemFunctionsArtifactStore { - constructor(private folderPath: string) {} + /** A store without a folder path has no functions. */ + constructor(private folderPath: string | undefined) {} async getFunctions(): Promise { - const dirEntries = await readdir(this.folderPath, { withFileTypes: true }) + const dirEntries = await this.readFunctionsFolder() const functionsFolders = dirEntries.filter((dir) => dir.isDirectory() && dir.name !== 'main') const functionsArtifacts = await Promise.all( @@ -20,7 +21,7 @@ export class FileSystemFunctionsArtifactStore { } async getFunctionBySlug(slug: string): Promise { - const dirEntries = await readdir(this.folderPath, { withFileTypes: true }) + const dirEntries = await this.readFunctionsFolder() const functionFolder = dirEntries.find( (dir) => dir.isDirectory() && dir.name !== 'main' && dir.name === slug @@ -31,7 +32,7 @@ export class FileSystemFunctionsArtifactStore { } async getFileEntriesBySlug(slug: string): Promise> { - if (slug === 'main') return [] + if (!this.folderPath || slug === 'main') return [] const functionFolderPath = path.resolve(this.folderPath, slug) if (!functionFolderPath.startsWith(path.resolve(this.folderPath) + path.sep)) return [] @@ -57,6 +58,18 @@ export class FileSystemFunctionsArtifactStore { return fileEntries } + + /** Reads the functions folder. A folder that does not exist yet has no functions. */ + private async readFunctionsFolder(): Promise { + if (!this.folderPath) return [] + + try { + return await readdir(this.folderPath, { withFileTypes: true }) + } catch (error) { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return [] + throw error + } + } } async function parseFolderToFunctionArtifact( diff --git a/apps/studio/lib/api/self-hosted/functions/index.test.ts b/apps/studio/lib/api/self-hosted/functions/index.test.ts index 05af849d15f..9d15eda86a9 100644 --- a/apps/studio/lib/api/self-hosted/functions/index.test.ts +++ b/apps/studio/lib/api/self-hosted/functions/index.test.ts @@ -37,12 +37,12 @@ describe('api/self-hosted/functions/index', () => { expect(util.assertSelfHosted).toHaveBeenCalled() }) - it('should throw error if EDGE_FUNCTIONS_MANAGEMENT_FOLDER is not set', () => { + it('should create a store without a folder if EDGE_FUNCTIONS_MANAGEMENT_FOLDER is not set', () => { delete process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER - expect(() => getFunctionsArtifactStore()).toThrow( - 'EDGE_FUNCTIONS_MANAGEMENT_FOLDER is required' - ) + getFunctionsArtifactStore() + + expect(fileSystemStore.FileSystemFunctionsArtifactStore).toHaveBeenCalledWith(undefined) }) it('should create FileSystemFunctionsArtifactStore with correct path', () => { diff --git a/apps/studio/lib/api/self-hosted/functions/index.ts b/apps/studio/lib/api/self-hosted/functions/index.ts index 34d8847c397..79ccf9a24bf 100644 --- a/apps/studio/lib/api/self-hosted/functions/index.ts +++ b/apps/studio/lib/api/self-hosted/functions/index.ts @@ -1,14 +1,14 @@ -import assert from 'node:assert' - import { assertSelfHosted } from '../util' import { FileSystemFunctionsArtifactStore } from './fileSystemStore' +/** + * Without EDGE_FUNCTIONS_MANAGEMENT_FOLDER the store has no folder, so it lists no functions + * and the dashboard shows the empty state. + */ export function getFunctionsArtifactStore() { assertSelfHosted() - assert( - process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER, - 'EDGE_FUNCTIONS_MANAGEMENT_FOLDER is required' - ) - return new FileSystemFunctionsArtifactStore(process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER) + return new FileSystemFunctionsArtifactStore( + process.env.EDGE_FUNCTIONS_MANAGEMENT_FOLDER || undefined + ) }