firebase / firebase-functions

Firebase SDK for Cloud Functions
https://firebase.google.com/docs/functions/
MIT License
1.03k stars 202 forks source link

listParam isn't loaded successfully in the emulator #1605

Open tzappia opened 2 months ago

tzappia commented 2 months ago

Related issues

https://github.com/firebase/firebase-functions/issues/1523

[REQUIRED] Version info

node:

v22.8.0

firebase-functions:

5.1.1

firebase-tools:

13.16.0

firebase-admin:

12.4.0

[REQUIRED] Test case

import { beforeUserCreated } from "firebase-functions/v2/identity";
import { myList } from './params.js';

export const beforecreated = beforeUserCreated((event) => {
  console.log('My list', myList.value());
});

In params.js

export const myList = defineList('MY_LIST', { default: [] });

In env.local

MY_LIST=something,somethingElse

[REQUIRED] Steps to reproduce

Run a function that uses a listParam, in this case I try to create a user so the beforeUserCreated blocking function is invoked.

[REQUIRED] Expected behavior

console log shows "My list [something, somethingElse]"

[REQUIRED] Actual behavior

>  {"severity":"ERROR","message":"Unhandled error SyntaxError: Unexpected token 's', \"something,somethingElse\" is not valid JSON\n    at JSON.parse (<anonymous>)\n    at ListParam.runtimeValue (/Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/params/types.js:378:26)\n    at ListParam.value (/Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/params/types.js:39:21)\n    at file:///Users/tzappia/workspace/hrvst/functions/lib/triggers/users.js:50:49\n    at wrappedHandler (/Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/v2/providers/identity.js:58:39)\n    at /Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/common/providers/identity.js:458:28\n    at /Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/common/onInit.js:33:16\n    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n    at async runFunction (/Users/tzappia/.nvm/versions/node/v22.8.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)\n    at async runHTTPS (/Users/tzappia/.nvm/versions/node/v22.8.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:531:5)"}

Were you able to successfully deploy your functions?

I was able to successfully deploy and the listParam seems to work fine in a production environment. Is the emulator parsing the .env file differently in the emulator vs. production? Seems to be a similar behaviour in the linked bug report that was closed.

google-oss-bot commented 2 months ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

exaby73 commented 2 months ago

Hello @tzappia. Could you try wrapping the value in [] and trying again?. So MY_LIST=something,somethingElse will become MY_LIST=[something,somethingElse]

tzappia commented 2 months ago

@exaby73 that works in the emulator, but I'm not willing to try it in production (don't want to break my live deployment!) As noted in the related issues, I expect it to fail in production.

BenJackGill commented 1 month ago

I did some testing.

For the emulator to work with defineList the .env file must have:

  1. Entire list wrapped in brackets.
  2. Each word wrapped in double quotes. Single quotes do not work.
  3. Separated by a comma. Using a space between each list item has does not effect the outcome. ["oranges and pears", "apples"] (with a space between list items) and ["oranges and pears","apples"] (without a space between list items) both work the same.

Like this:

EXAMPLE_LIST=["oranges and pears", "apples"]

But this style does not work in production.

It creates one list item of ["oranges and pears", and another list item of "apples"] which is obviously wrong. It seems to be splitting at the , and including the brackets and quotes and the spaces between each list item.

To make it work in production we need this in the .env file:

EXAMPLE_LIST=oranges and pears,apples

But as the original issue has raised, this format does not work with the emulator.