Skip to content

feat(cli): inject function slug into served fns - #6345

Open
raulb wants to merge 7 commits into
developfrom
raulb/inject-function-slug-env-var
Open

feat(cli): inject function slug into served fns#6345
raulb wants to merge 7 commits into
developfrom
raulb/inject-function-slug-env-var

Conversation

@raulb

@raulb raulb commented Aug 26, 2026

Copy link
Copy Markdown
Member

Summary

Adds SUPABASE_FUNCTION_SLUG to every locally served Edge Function.

The value always matches the requested function name. This works in both the legacy and next stacks.

Context

Shared function folders

  • Edge Runtime reuses workers by their service path.
  • Two functions can share one source folder. They must not share one worker environment.
  • The CLI now gives each shared function a stable and unique worker path.
  • The CLI still passes the real source file as the entrypoint. Imports and file access remain unchanged.
  • Functions in separate folders keep their current service path.
  • The change does not use queues, owner maps, or forceCreate.

Linked issue

Fixes AI-1129

  • The linked issue is open and carries the open-for-contribution label, or I am a Supabase maintainer.

Checklist

  • The PR title follows Conventional Commits.
  • Tests were added or updated for the change.
  • From the repository root, pnpm check:all passes, and all required workspace checks pass.

@raulb
raulb requested a review from a team as a code owner August 26, 2026 16:53
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch from e264980 to 2989c69 Compare August 26, 2026 16:56
@raulb raulb self-assigned this Aug 26, 2026
@raulb
raulb requested a review from kallebysantos August 26, 2026 16:56

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e264980dc6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/cli/src/shared/functions/serve.main.ts
@github-actions

github-actions Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Supabase CLI preview

npx --yes https://pkg.pr.new/supabase/cli/supabase@4694df409039fad4fc20d989c1f39600a8db0dea

Preview package for commit 4694df4.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 961e6ac204

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch from 961e6ac to c1103de Compare August 27, 2026 09:53

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c1103de0cf

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch from c1103de to 954c972 Compare August 27, 2026 10:01

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c24a039af9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
Comment thread apps/cli/src/shared/functions/serve.main.ts
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch from bddf73e to cbc9aec Compare August 27, 2026 10:41
Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
Comment thread packages/stack/src/services/edge-runtime-main.ts Outdated
raulb added a commit to supabase/supabase that referenced this pull request Aug 28, 2026
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Feature (self-hosted Edge Functions)

## What is the current behavior?

The self-hosted Edge Functions router
(`docker/volumes/functions/main/index.ts`) doesn't tell a function which
slug a request resolved to. As a result, `@supabase/server`'s
`withOAuthProtectedResource` can't derive its canonical resource URL and
falls back to reconstructing it from the request path against the
internal `api-gw` origin, so the advertised OAuth Protected Resource is
/wrong for self-hosted deployments.

## What is the new behavior?

`main/index.ts` now injects `SUPABASE_FUNCTION_SLUG: service_name` per
request (after the `Deno.env.toObject()` snapshot, so nothing in the
container env can shadow it).

Combined with the operator's `SUPABASE_PUBLIC_URL`, the advertised
resource is the correct external
`{SUPABASE_PUBLIC_URL}/functions/v1/{slug}`, not the internal
`http://api-gw:8000`.

Verified on the docker stack: the slug is injected per-function, the
resource origin resolves to `SUPABASE_PUBLIC_URL`, and the `401`
`www-authenticate` carries the right `resource_metadata`.

## Additional context

Fixes AI-1128

Companion to `@supabase/server` [PR
#117](supabase/server#117) and the [CLI slug
injection](supabase/cli#6345)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Edge workers now receive the correct function slug in their runtime
environment, improving per-function request handling.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch from 43b08e3 to 91f7dfe Compare August 28, 2026 11:54
@raulb
raulb requested a review from jgoux August 28, 2026 11:55

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 91f7dfed2c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch 2 times, most recently from 91e1cd3 to abc0a94 Compare August 28, 2026 12:30

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: abc0a94e32

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
Comment thread packages/stack/src/services/edge-runtime-main.ts Outdated
Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
Comment thread packages/stack/src/services/edge-runtime-main.ts Outdated
Comment thread packages/stack/src/services/edge-runtime-main.ts Outdated
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch from abc0a94 to fa6ffc9 Compare August 31, 2026 14:40
@raulb
raulb requested a review from jgoux August 31, 2026 14:40
@raulb
raulb force-pushed the raulb/inject-function-slug-env-var branch from fa6ffc9 to 10eb8ec Compare August 31, 2026 14:44

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 10eb8ec2ea

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread apps/cli/src/shared/functions/serve.main.ts Outdated
Comment thread apps/cli/src/shared/functions/serve-main-offline.e2e.test.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4694df4090

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

} catch (error) {
lastError = error;
}
await Bun.sleep(250);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replace startup polling with readiness synchronization

In the offline Docker test, this fixed sleep turns cold-worker startup into wall-clock polling; on slow CI it delays every retry and, if the worker remains unavailable, eventually throws without the runtime/container diagnostics needed to investigate the failure. Synchronize on an explicit worker or container readiness signal instead, retaining stderr/stdout diagnostics; the repository explicitly treats startup polling delays and arbitrary sleeps as blocking.

AGENTS.md reference: AGENTS.md:L318-L322

Useful? React with 👍 / 👎.

fetchFunctionWhenReady(`${stack.url}/functions/v1/shared-beta`),
]);
const reusedAlpha = await fetchFunctionWhenReady(`${stack.url}/functions/v1/shared-alpha`);
const nested = await fetchFunctionWhenReady(`${stack.url}/functions/v1/nested-worker-path`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Budget for the sequential nested cold start

On a slow Docker host, the parallel alpha/beta cold starts may consume most of fetchFunctionWhenReady's 20-second allowance, after which this separately awaited nested function can consume another 20 seconds; the enclosing test has only a 30-second timeout. Vitest can therefore cancel an otherwise healthy run before the nested readiness helper finishes, so start all cold workers within one shared phase or give the test a guard timeout covering both phases.

AGENTS.md reference: AGENTS.md:L318-L322

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants