Skip to content

firebase cli cannot deploy functions when using pnpm, turborepo along with [email protected] #5145

Closed

Description

@MarcPorciuncula

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

  1. google-oss-bot commented on Oct 19, 2022

    @google-oss-bot
    Collaborator

    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.

  2. inlined commented on Oct 19, 2022

    @inlined
    Member

    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.

  3. taeold commented on Oct 20, 2022

    @taeold
    Contributor

    @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 __trigger annotation (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.

  4. MarcPorciuncula commented on Oct 20, 2022

    @MarcPorciuncula
    Author

    Hey @taeold, thank you for looking into this. We are using [email protected] in a monorepo/workspaces configuration.

  5. bartgrundeken commented on Oct 24, 2022

    @bartgrundeken

    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.

  6. atsjo commented on Oct 25, 2022

    @atsjo

    I have the same problem, using yarn (both 1 and 3) in a nx monorepo.

  7. mpsq commented on Oct 25, 2022

    @mpsq

    Same issue with npm workspaces.

  8. changed the title [-]firebase-tools does not prevent deploying with incompatible firebase-functions@v4[/-] [+]firebase cli cannot deploy functions when using pnpm, turborepo along with [email protected][/+] on Nov 7, 2022
  9. taeold commented on Nov 7, 2022

    @taeold
    Contributor

    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.

  10. taeold commented on Nov 7, 2022

    @taeold
    Contributor

    Hey @taeold, thank you for looking into this. We are using [email protected] in a monorepo/workspaces configuration.

    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.

  11. added
    Needs: Author Feedback
    on Nov 7, 2022
  12. MarcPorciuncula commented on Nov 7, 2022

    @MarcPorciuncula
    Author

    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:

    • Regular rollup stuff including compiling typescript and bundling individual modules into one (or few) files. We keep any npm dependencies external as we find bundling them in can have unintended runtime effects.
    • Bundle in any workspace dependencies so we don't need to depend on them at runtime (we can't anyway because they are not published to a registry).
    • In the build output, we create copy of the package.json
      • We remove all dev dependencies, as Cloud Build does a production only install when we deploy.
      • We remove all workspace dependencies, as they have already been bundled in.
      • We add all transitive dependencies found through all workspace dependencies.

    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:

    • We point functions.source in firebase.json to the build output folder.
    • We run pnpm install --shamefully-hoist inside the build output folder, this installs dependencies in a folder structure that is (mostly) equivalent to an npm or yarn install.
      • We have to do this so that all dependencies are present when when we run firebase deploy, otherwise loading the code for trigger parsing can crash.
      • This is also a dry run for the npm install that will be performed by Cloud Build, as the Node.js Cloud Functions buildpack does not have support for pnpm.
    • We run firebase deploy --only functions:<namespace> in the main package.

    Here is an example of the file structure for context.

    - node_modules/ (uses the pnpm node_modules structure with symlinks to workspace packages in the monorepo)
    - package.json (makes use of workspace:* specifiers only available in a monorepo)
    - firebase.json (functions.source is set to 'dist/')
    - src/
      - index.ts
    - dist/
      - node_modules/ (installed at deploy time using pnpm install --shamefully-hoist)
      - package.json (normal package.json usable by npm)
      - index.js (includes bundled in code from other workspace packages)
    

    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.

  13. added and removed
    Needs: Author Feedback
    on Nov 7, 2022
  14. turnyi commented on Sep 12, 2023

    @turnyi

    @MarcPorciuncula Im stuck with the same problem, could you share a small package json and better example on how did you resolved this?

  15. MarcPorciuncula commented on Sep 14, 2023

    @MarcPorciuncula
    Author

    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.

  16. russellwheatley commented on Nov 10, 2023

    @russellwheatley
    Member

    @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.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions