Files
supabase/apps/studio/lib/api/self-hosted/functions/fileSystemStore.ts
supabase-vercel-tedd[bot]andAli Waseem 0e29dfda1b 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 <waseema393@gmail.com>
2026-11-12 06:58:24 -06:00

94 lines
3.0 KiB
TypeScript

import type { Dirent } from 'node:fs'
import { readdir, stat } from 'node:fs/promises'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { FunctionArtifact, FunctionFileEntry } from './types'
export class FileSystemFunctionsArtifactStore {
/** A store without a folder path has no functions. */
constructor(private folderPath: string | undefined) {}
async getFunctions(): Promise<FunctionArtifact[]> {
const dirEntries = await this.readFunctionsFolder()
const functionsFolders = dirEntries.filter((dir) => dir.isDirectory() && dir.name !== 'main')
const functionsArtifacts = await Promise.all(
functionsFolders.map(parseFolderToFunctionArtifact)
)
return functionsArtifacts.filter((f) => f !== undefined)
}
async getFunctionBySlug(slug: string): Promise<FunctionArtifact | undefined> {
const dirEntries = await this.readFunctionsFolder()
const functionFolder = dirEntries.find(
(dir) => dir.isDirectory() && dir.name !== 'main' && dir.name === slug
)
if (!functionFolder) return
return parseFolderToFunctionArtifact(functionFolder)
}
async getFileEntriesBySlug(slug: string): Promise<Array<FunctionFileEntry>> {
if (!this.folderPath || slug === 'main') return []
const functionFolderPath = path.resolve(this.folderPath, slug)
if (!functionFolderPath.startsWith(path.resolve(this.folderPath) + path.sep)) return []
const entries = await readdir(functionFolderPath, {
recursive: true,
withFileTypes: true,
})
const fileEntries = await Promise.all(
entries
.filter((entry) => entry.isFile())
.map(async (entry) => {
const absolutePath = path.join(entry.parentPath, entry.name)
const fileStat = await stat(absolutePath)
return {
absolutePath,
relativePath: path.relative(functionFolderPath, absolutePath),
size: fileStat.size,
}
})
)
return fileEntries
}
/** Reads the functions folder. A folder that does not exist yet has no functions. */
private async readFunctionsFolder(): Promise<Dirent[]> {
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(
folder: Dirent
): Promise<FunctionArtifact | undefined> {
const folderPath = path.join(folder.parentPath, folder.name)
const files = await readdir(folderPath, { withFileTypes: true })
const entrypoint = files.find((file) => file.isFile() && file.name.startsWith('index'))
if (!entrypoint) return
const entrypointPath = path.join(folderPath, entrypoint.name)
const entrypointStat = await stat(entrypointPath)
return {
slug: folder.name,
entrypoint_path: pathToFileURL(entrypointPath).href,
created_at: entrypointStat.birthtimeMs,
updated_at: entrypointStat.mtimeMs,
}
}