simondotm / nx-firebase

Firebase plugin for Nx Monorepos
https://www.npmjs.com/package/@simondotm/nx-firebase
MIT License
175 stars 31 forks source link

.env is not being included in build for deployment, but .env.local and secret.local are #170

Closed stevebrowndotco closed 7 months ago

stevebrowndotco commented 8 months ago

Hi there,

Many thanks for this awesome nx project

I am using version 2.1.2

When I build and deploy my firebase function, the dist/apps/function folder seems to build with .secret.local and .env.local and .env is not there

This seems to be the opposite of what is said here:

https://github.com/simondotm/nx-firebase/blob/main/docs/nx-firebase-functions-environment.md

"The firebase CLI will deploy .env and/or .env. files along with function code, and they can be version controlled." and ".env.local and .secret.local files are excluded from deployment by using an functions.ignore rule in firebase.json."

The issue that I have, regardless, is that when I console log process.env.MY_VAR in production, it comes up as undefined in google cloud logs

Is this a bug or is there something I am doing wrong? The functions block in my firebase.json looks like this (and I have made no changes to this file)

  "functions": [
    {
      "codebase": "functions",
      "source": "dist/apps/functions",
      "runtime": "nodejs16",
      "ignore": [
        "*.local"
      ],
      "logs": false
    }
  ],
simondotm commented 7 months ago

@stevebrowndotco thanks for reporting this. It should indeed copy .env files to dist as part of the build step. For example here is one of my projects dist: image

A few things to check then:

  1. There's definitely a .env file in your firebase app project environment folder?
  2. Your functions project.json has the following glob in its config for assets?
        ...
        "assets": [
          "apps/functions/src/assets",
          {
            "glob": "**/*",
            "input": "apps/firebase/environment",
            "output": "."
          }
        ],
       ....

(assuming your main firebase app project was named firebase)

stevebrowndotco commented 7 months ago

Hi @simondotm thank you for checking

  1. There is definitely a .env file in the firebase/environment folder yes:
Screenshot 2023-11-06 at 16 31 39
  1. Yes glob looks the same:
Screenshot 2023-11-06 at 16 32 17
simondotm commented 7 months ago

@stevebrowndotco In your first image, .env file in your environment folder is greyed by vscode, which normally indicates it is being affected by .gitignore

Please could you check you dont have .env in your .gitignore ? This would explain it, since Nx assets copy respects those rules and would not copy .env in this case. (The reason why .secret.local gets copied even though .gitignore'd is because the plugin adds an exception for this in .nxignore)

stevebrowndotco commented 7 months ago

Hi @simondotm many thanks for looking into that. You are correct, it is because it is ignored in git! So I suppose the issue I have is now beyond the scope of this repo but more of an "nx" issue.

This is because I actually have a use case for not wanting .env in source control because of security reasons. I wonder if there is a way to leverage changing the behaviour in the nx config though...

simondotm commented 7 months ago

@stevebrowndotco If your .env files are intentionally in .gitignore but you still want to copy them to dist when building, then try adding:

!.env.*

to .nxignore

That should do it.

stevebrowndotco commented 7 months ago

Hi @simondotm that did it. Thanks for taking a look even though it wasn't strictly an issue with your library