firebase cli cannot deploy functions when using pnpm, turborepo along with [email protected] #5145
Description
Environment info
firebase-tools: v11.15.0
firebase-functions: v4.0.1
Platform: macos (local dev), ubuntu (ci)
Test case
Steps to reproduce
Deploy firebase functions for nodejs with firebase-tools and firebase-functions versions noted above.
Expected behavior
Cloud functions deploy successfully
Actual behavior
Firebase functions triggers are not parsed, resulting in no functions being deployed. If you are using the --force flag, your functions will be deleted.
tldr; I upgraded from [email protected] to [email protected], resulting in my function triggers not being parsed and all my firebase functions being deleted (and an outage for our customers 🔥). I think firebase-tools should prevent users from deploying with firebase-functions@4
I thought it was safe for me to upgrade to firebase-functions@v4 as their release notes did not mention any incompatibility with the current version of firebase-tools. Upon upgrading my CI did a deploy, deleting all my functions.
The cause is that my function triggers were no longer recognised by firebase-tools, and I have set my CI to deploy with --force, resulting in no functions deployed and all my functions being deleted.
I traced the source of the issue, being that firebase-functions@v4 no longer adds __trigger to the cloud functions triggers, but firebase-tools uses this property to discover function triggers in extractTriggers.js. This is mentioned in the release notes for firebase-functions "Removed __trigger object on function handlers.", although it's not immediately obvious that means that function triggers will no longer be recognised at all.
I did notice that the peerDependencies of firebase-tools does not allow firebase-functions@4 but unfortunately my tools did not prevent the upgrade (I accepted the upgrade from renovate bot after reading the release notes and deeming it a safe upgrade).
I would like to suggest that my issue could have been avoided if firebase-tools prevented users from deploying with firebase-functions@v4. There is already a check to make sure an incompatible (lower) version of firebase-functions is not used.
Activity
google-oss-bot commentedon Oct 19, 2022
This issue does not have all the information required by the template. Looks like you forgot to fill out some sections. Please update the issue with more information.
inlined commentedon Oct 19, 2022
This is indeed an unforeseen and tragic issue. The code to use HTTP for discovery was added a long time ago, but we only switched everyone to that code in 10.9.0. In the future, please update your CLI if you are depending on a breaking change version of Functions. To protect others from this mistake we're going to add triggers back.
taeold commentedon Oct 20, 2022
@MarcPorciuncula It looks like you are using the latest version of the CLI, yet our code failed to detect this :(.
Are you by chance using tools like npm/yarn workspaces? It looks like the CLI should have detected that you are using v4 and above, yet decided to fallback to using
__triggerannotation (which no longer exists on v4).Thanks for filing the issue and I'm very sorry to hear about the outage. We will make it a priority to prevent this issue for more developers.
MarcPorciuncula commentedon Oct 20, 2022
Hey @taeold, thank you for looking into this. We are using
[email protected]in a monorepo/workspaces configuration.bartgrundeken commentedon Oct 24, 2022
I am having the same issue, using an NX Monorepo with npm. However, before deploying, functions are bundled into js with all the local libraries in the monorepo.
atsjo commentedon Oct 25, 2022
I have the same problem, using yarn (both 1 and 3) in a nx monorepo.
mpsq commentedon Oct 25, 2022
Same issue with npm workspaces.
[-]firebase-tools does not prevent deploying with incompatible firebase-functions@v4[/-][+]firebase cli cannot deploy functions when using pnpm, turborepo along with [email protected][/+]taeold commentedon Nov 7, 2022
We reverted back the change that broke many users in v4.0.2 of the firebase functions sdk.
We are working on a long term solution to make the function deployment aware of workspaces and other dev toolings that changes where npm packages are installed (e.g. pnpm).
In the meantime, please give 4.0.2 a try. Would appreciate your feedbacks.
taeold commentedon Nov 7, 2022
Hi @MarcPorciuncula! When you have time, can you share a little more about how your proejct is set up?
AFAIK, the Google Cloud Functions platform only support npm and yarn. I'm curious how you got it to work with your pnpm monorepo setup.
MarcPorciuncula commentedon Nov 7, 2022
Hi @taeold, before deploying to Cloud Functions for Firebase we bundle our source code into a format more close to a typical npm setup. This is the only way we've been able to reliably deploy to Cloud Functions for Firebase, and we used to use a similar mechanism when we previously used yarn (classic) and yarn (2.x, 3.x).
For context, one of the the main reasons we use a pnpm monorepo setup is so we can share code by making packages that are not published to the registry, but are referenced and linked locally by pnpm (workspace dependencies, which use a
workspace:*version specifier). To get a Cloud Functions for Firebase package to deploy, we use a special bundling and deployment process that emulates a regular single package npm setup.Bundling process:
The result is a self contained package that can be deployed to Cloud Functions for Firebase. Note this output package does not contain any references to the rest of the monorepo.
Deployment process:
functions.sourceinfirebase.jsonto the build output folder.pnpm install --shamefully-hoistinside the build output folder, this installs dependencies in a folder structure that is (mostly) equivalent to an npm or yarn install.firebase deploy, otherwise loading the code for trigger parsing can crash.npm installthat will be performed by Cloud Build, as the Node.js Cloud Functions buildpack does not have support for pnpm.firebase deploy --only functions:<namespace>in the main package.Here is an example of the file structure for context.
Let me know if I can provide anything further.
FYI I noticed there are a few other tools being mentioned in this thread, to be clear my setup only uses pnpm, the notable differences from a regular npm setup being the use of workspace dependencies and the node_modules folder structure used by pnpm. I am not using turborepo, nx, or any other monorepo build tool.
turnyi commentedon Sep 12, 2023
@MarcPorciuncula Im stuck with the same problem, could you share a small package json and better example on how did you resolved this?
MarcPorciuncula commentedon Sep 14, 2023
Hey @402Martin we're no longer having this issue, it was a while ago so I don't remember what the resolution was.
If it helps you, I have made an older version of our bundling strategy (the one mentioned in my last comment) public which you can browse and are free to copy https://github.com/propps-au/repack. The main thing is a rollup output plugin which bundles in any local workspace dependencies into the output and rewrites the package.json so it only includes public dependencies which are available via npm. There are examples of use in the repo. We are using this bundling strategy with
[email protected]and[email protected]successfully.russellwheatley commentedon Nov 10, 2023
@MarcPorciuncula - thanks for letting us know you are no longer experiencing this issue, and providing a bundling strategy solution to help folks with this issue.