cli icon indicating copy to clipboard operation
cli copied to clipboard

--env-file switch in functions serve fails, and .env ingestion in general is failing

Open lululeon opened this issue 1 year ago • 12 comments

Describe the bug The command supabase functions serve --env-file ./.env yields:

Invalid env name: SUPABASE_PERSONAL_ACCESS_TOKEN. Env names cannot start with SUPABASE_.

This command was resorted to only because when using the bare supabase functions serve command, there were runtime errors as follows:

  1. Initially, the edge function attempted to use process.env but it wasn't accessible with npm: prefixed dotenv pkg (perhaps I did sthg wrong), therefore:
  2. Switched to deno version of dotenv, with import { config } from 'https://deno.land/x/[email protected]/mod.ts' and a call to config() prior to Deno.env.get() calls, which failed with:
    [Error] TypeError: Deno.readFileSync is not a function
        at parseFile (https://deno.land/x/[email protected]/mod.ts:100:55)
        at config (https://deno.land/x/[email protected]/mod.ts:41:16)
        at Object.handler (file:///home/deno/functions/faq-embeddings-agent/index.ts:62:3)
        at eventLoopTick (ext:core/01_core.js:182:7)
        at async handleHttp (ext:sb_core_main_js/js/http.js:96:17)
    
  3. therefore, switched to import { configAsync } from 'https://deno.land/x/[email protected]/mod.ts' and a call to await configAsync(), which failed with:
    [Error] TypeError: Deno.readFile is not a function
        at parseFileAsync (https://deno.land/x/[email protected]/mod.ts:108:61)
        at configAsync (https://deno.land/x/[email protected]/mod.ts:48:22)
        at Object.handler (file:///home/deno/functions/faq-embeddings-agent/index.ts:62:9)
        at eventLoopTick (ext:core/01_core.js:182:7)
        at async handleHttp (ext:sb_core_main_js/js/http.js:96:17)
    

Hope that provides some context.

To Reproduce Run the command above.

Expected behavior .env file ingested without protest

Screenshots If applicable, add screenshots to help explain your problem. image

System information Rerun the failing command with --create-ticket flag (preferred).

  • Ticket ID: 58a7490ab6074b7497087a7417ecd5fa

Additional context If applicable, add any other context about the problem here.

  • Browser [e.g. chrome, safari]
    • n/a
  • Version of supabase-js [e.g. v2.22.0]
    • 2.39.3
  • Version of Node.js [e.g. v16.20.0]
  • 20.11.0

lululeon avatar Feb 20 '24 18:02 lululeon

We don't allow env vars prefixed with SUPABASE_. Are you able to rename the env var to something else? For eg. APP_ACCESS_TOKEN.

If you want to avoid specifying --env-file flag, you can create supabase/functions/.env file. Those variables will be automatically injected into edge runtime container when serving locally. Remember that the same naming restrictions apply.

sweatybridge avatar Apr 15 '24 09:04 sweatybridge

@sweatybridge I arrived at this error while looking at this page in the Supabase docs: https://supabase.com/docs/guides/functions/examples/send-emails

It specifically cites .env and it doesn't appear to be within the supabase/functions/ directory. Therefore, I added the RESEND_API_KEY to my local .env file in project root and got the error shown.

As-is, the error is also misleading or hard to understand. Invalid env name: SUPABASE_URL. Env names cannot start with SUPABASE_. — Is it really an env name? That seems like the wrong term to use. Rather, Env vars cannot start with... makes more sense IMO.

galaxyblur avatar Apr 22 '24 02:04 galaxyblur

Rather, Env vars cannot start with... makes more sense IMO.

Yes, thanks for the suggestion. We are planning to change it so that env vars prefixed with supabase_ are automatically ignored with a warning. Hoping to land it this week

sweatybridge avatar Apr 22 '24 03:04 sweatybridge

@sweatybridge

Rather, Env vars cannot start with... makes more sense IMO.

Yes, thanks for the suggestion. We are planning to change it so that env vars prefixed with supabase_ are automatically ignored with a warning. Hoping to land it this week

Any updates?

colemilne54 avatar May 05 '24 21:05 colemilne54

@colemilne54 this is released to beta channel.

npx supabase@beta functions serve

sweatybridge avatar May 05 '24 22:05 sweatybridge

Bro so lame we can't start them with SUPABASE_ wut?! Delete that enforcement. I should be able to access my SUPABASE_JWT_SECRET from my .env.local from my NextJS project. This is wacko!

daveycodez avatar Dec 14 '24 03:12 daveycodez

I think this is strange to enforce because all the reserved secrets in edge functions secret management dashboard start with "SUPABASE_". So essentially I need to duplicate secrets?

jallen avatar Feb 07 '25 15:02 jallen

I might be missing something, but this makes it a little tricky to locally run edge functions that are pointed to cloud supabase instead of local supabase. It seems like I have to have a duplicate set of secrets which I point to when running locally.

tfrank11 avatar Mar 14 '25 02:03 tfrank11

I might be missing something, but this makes it a little tricky to locally run edge functions that are pointed to cloud supabase instead of local supabase. It seems like I have to have a duplicate set of secrets which I point to when running locally.

I have the same question. I am working on the edge functions and found out I cannot use SUPABASE_* as env var. could anyone explain more details about this enforcement?

shtse8 avatar Mar 16 '25 02:03 shtse8

I would have thought that the supabase CLI was simply ignoring env vars prefixed with SUPABASE_ because it was injecting it's own for the local supabase docker container. But that also doesn't appear to be the case. What's the point of ignoring these envs if not to provide the local values in their place? I too have resorted to duplicating env names (with different namings). Make it make sense please :)

zeluspudding avatar Apr 07 '25 22:04 zeluspudding

This is definitely confusing on the part of Supabase. Since the built-in vars will be present in a deployment, you can just set a local only env var and have it be the fallback like so:

    const supabaseUrl =
      Deno.env.get("SUPABASE_URL") ?? Deno.env.get("LOCAL_DEV_SUPABASE_URL")!
    const supabaseKey =
      Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ??
      Deno.env.get("LOCAL_DEV_SUPABASE_SERVICE_ROLE_KEY")!

hichana avatar Apr 16 '25 05:04 hichana

This is definitely confusing on the part of Supabase. Since the built-in vars will be present in a deployment, you can just set a local only env var and have it be the fallback like so:

const supabaseUrl =
  Deno.env.get("SUPABASE_URL") ?? Deno.env.get("LOCAL_DEV_SUPABASE_URL")!
const supabaseKey =
  Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ??
  Deno.env.get("LOCAL_DEV_SUPABASE_SERVICE_ROLE_KEY")!

It should be the other way around as SUPABASE_URL defaults to http://kong:8000.

const supabaseUrl =Deno.env.get("LOCAL_DEV_SUPABASE_URL") ??
      Deno.env.get("SUPABASE_URL")!
const supabaseServiceRoleKey =
Deno.env.get("LOCAL_DEV_SUPABASE_SERVICE_ROLE_KEY")??
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") !

anil1kuppa avatar May 16 '25 03:05 anil1kuppa