Skip to content

Functions discovery breaks with yarn/npm/pnpm workspaces #5517

Closed

Description

@andrewkrippner

[REQUIRED] Version info

node:
v16.15.0

firebase-functions:
v4.0.1

firebase-tools:
v11.15.0

firebase-admin:
v11.2.0

[REQUIRED] Test case

firebase init functions
>Don't set up a default project 
>Typescript
>ESLint: No
>Install Deps: No

Add {
    "projects": {
        "default": "default"
    }
} to /.firebaserc

Uncomment helloWorld function

yarn set version berry
Add [nodeLinker: node-modules](https://yarnpkg.com/configuration/yarnrc#nodeLinker) in your .yarnrc.yml file
Add "workspaces": ["functions"] to /package.json

cd functions
yarn build
cd ..

yarn up [email protected]

[REQUIRED] Steps to reproduce

firebase functions:shell

[REQUIRED] Expected behavior

Functions found by firebase and run in emulator and deploy properly, as they do before yarn up [email protected] is ran

[REQUIRED] Actual behavior

i functions: Loaded functions:
No functions emulated.

Were you able to successfully deploy your functions?

No, I receive the error:

The following functions are found in your project but do not exist in your local source code:
*

Activity

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

    @google-oss-bot
    Collaborator

    I found a few problems with this issue:

    • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
    • This issue does not seem to follow the issue template. Make sure you provide all the required information.
  2. atsjo commented on Oct 25, 2022

    @atsjo

    I get the same problem, but it doesn't seem like a yarn 2 problem, as it's the same with yarn 1 (in my nx repo). Still staying on 3.24.1, not sure where the fix belongs, in firebase-tools or in firebase-functions.

    #5145

  3. MathiasGilson commented on Oct 25, 2022

    @MathiasGilson

    same issue here I had to roll back to [email protected] to for firebase serve to detect my functions

    otherwise, I get a
    ✔ functions: Loaded functions definitions from source: .

  4. changed the title [-]Functions not found using v4 and Yarn 2[/-] [+]firebase-tools does not find functions while using yarn workspaces (firebase-functions v4)[/+] on Oct 25, 2022
  5. swissspidy commented on Oct 28, 2022

    @swissspidy

    Same issue here with npm workspaces (not yarn)

  6. miguel-leon commented on Oct 31, 2022

    @miguel-leon

    It turns out the problem is that it started looking for a node_modules in the same folder as the functions source folder.
    So if the node_modules is in an upper level for shared usage, this issue happens.
    I confirmed it by the fact that adding a symbolic link in the functions source folder to the actual location of node_modules, got it working again.

  7. inlined commented on Oct 31, 2022

    @inlined
    Member

    I'm making this the master bug for a series of issues with the same underlying cause:

    During functions v1, we simply require your code and look for __trigger. During v2, we now load your code as an HTTPS server and read a manifest. In the 4.0 release we removed support for __trigger because it has not been used since the first public release of firebase-functions/v2.

    Unfortunately this uncovered an very bad issue: our code to decide whether to use the v1 strategy or the v2 strategy depends on npm list, which isn't returning accurate results when customers are using workspaces. This tells the CLI to fall back to the v1 strategy, which the 4.0 SDK does not support. To fix this, we have two parallel prongs of attack:

    1. We're adding __trigger back to the 4.0 SDK because it provides a safety net for many of our customers. This change should roll out soon. This will not be enough to use all of v2 though because v2's format is necessary to represent the additional complexity of v2. Thus:
    2. We're still in the early phases of research for adding workspace awareness if not strong support. This will let us detect that you have the 4.0 functions SDK and launch the webserver that hosts your code and tells us what to support.
  8. changed the title [-]firebase-tools does not find functions while using yarn workspaces (firebase-functions v4)[/-] [+]Functions discovery breaks with yarn/npm/pnpm workspaces[/+] on Oct 31, 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. self-assigned this
    on Nov 7, 2022
  11. andrewkrippner commented on Nov 7, 2022

    @andrewkrippner
    Author

    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.

    My functions deploy now after upgrading to v4.0.2

  12. michaelangeloio commented on Nov 12, 2022

    @michaelangeloio

    FYI I use the following set up with NX (this in in apps/server/project.json:

        "emulate": {
          "executor": "nx:run-commands",
          "options": {
            "command": "echo && firebase emulators:start --import ./apps/server/emulator/staging --export-on-exit"
          }
        },
        "serve": {
          "executor": "nx:run-commands",
          "options": {
            "commands": [
              {
                "command": "nx run server:build --with-deps && nx run server:build --watch"
              },
              {
                "command": "sleep 3 && nx run server:emulate"
              }
            ],
            "parallel": true
          }
        },
    

    For running the functions locally.

    And then to detect properly, I use the following in firebase.json, where dist is the compiled JS from the build NX target.

      "functions": [
        {
          "source": "apps/server/dist",
          "predeploy": [],
          "runtime": "nodejs16"
        }
      ],
    
  13. DibyodyutiMondal commented on Feb 1, 2023

    @DibyodyutiMondal

    Update as of

    After the issue firebase-tools #5448 was resolved, the new error is:
    ⬢ functions: Failed to load function definition from source: FirebaseError: Failed to find location of Firebase Functions SDK. Please file a bug on Github (https://github.com/firebase/firebase-tools/).

    I see that work is in progress at:

    // Location of the binary included in the Firebase Functions SDK
    // differs depending on the developer's setup and choice of package manager.
    //
    // We'll try few routes in the following order:
    //
    // 1. $SOURCE_DIR/node_modules/.bin/firebase-functions
    // 2. node_modules closest to the resolved path ${require.resolve("firebase-functions")}
    //
    // (1) works for most package managers (npm, yarn[no-hoist],pnpm).
    // (2) handles cases where developer prefers monorepo setup or bundled function code.
    const sourceNodeModulesPath = path.join(this.sourceDir, "node_modules");
    const sdkPath = require.resolve("firebase-functions", { paths: [this.sourceDir] });
    const sdkNodeModulesPath = sdkPath.substring(0, sdkPath.lastIndexOf("node_modules") + 12);

    const sourceNodeModulesPath = path.join(this.sourceDir, "node_modules");
    const sdkPath = require.resolve("firebase-functions", { paths: [this.sourceDir] });
    const sdkNodeModulesPath = sdkPath.substring(0, sdkPath.lastIndexOf("node_modules"https://github.com/firebase/firebase-tools/commit/accea7abda3cc9fa6bb91368e4895faf95281c60 + 12);

    In my specific case I use pnpm and nx
    The above variables reolve to:

    sourceNodeModulesPath= '<output_path>/node_modules'
    sdkPath = '<workspace_root>/node_modules/.pnpm/[email protected][email protected]/node_modules/firebase-functions/lib/v1/index.js'
    sdkNodeModulesPath  = '<workspace_root>/node_modules/.pnpm/[email protected][email protected]/node_modules'

    As you can see, the sdkPath is complicated. I believe this happened because of pnpm's unique way of structuring packages which require.resolve does not expect.

    I have found a solution specifically for pnpm which requires that firebase-functions must be listed as either a dependency or devDependency at the workspace level, otherwise pnpm will not create the symlink for the 'normal' node_modules path

    The fix for me has been to remove the .pnpm path and everything that follows

    Proposed code changes: add a replace chain to the decalaration for sdkPath (this line):

        const sdkPath = require.resolve("firebase-functions", { paths: [this.sourceDir] })
          .replace(/\/\.pnpm\/.*/, '');

    For those who are not willing to wait (like me), you can manually go to
    <workspace_root>/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/index.js.
    The line to change is in the serveAdmin function.

  14. diginikkari commented on Feb 5, 2023

    @diginikkari

    I can confirm this. I got this error also after updating to [email protected]. After fixing sdkPath manually as @DibyodyutiMondal suggested functions were loaded successfully.

  15. taeold commented on Feb 6, 2023

    @taeold
    Contributor

    @DibyodyutiMondal thank you for your wonderful, detailed writeup! And thank you @diginikkari for confirming the fix.

    Crafting a fix based on your findings, and I'm curious if your Firebase project directory == workspace directory. I'm trying to come up with a semi-rebost way to detect the workspace directory, and wondering if I can rely on project directoy being the same as the workspace directory.

  16. diginikkari commented on Feb 6, 2023

    @diginikkari

    in my case my firebase.json is located in my (NX) monorepo root. Functions are build to dist/functions/. Functions config in firebase.json is following:

      "functions": {
        "predeploy": ["pnpm nx run functions:lint", "pnpm nx run functions:build"],
        "source": "dist/apps/functions"
      },
    
  17. DibyodyutiMondal commented on Feb 6, 2023

    @DibyodyutiMondal

    For me workspace directory != functions directory

    • apps/cloud-functiions - ts source code
    • dist/apps/cloud-funtions - js output
    • firebase.json - config
    • node_modules - the only node_modules in the entire workspace. All projects in the workspace use this folder for imports, i.e, projects don't have separate node_modules inside their folders.

    All the above are relative to workspace root

    As far as I can tell, using require.resolve and works quite well. It's simply pnpm's way of symlinking that's causing it get confused. I don't know how yarn workspaces are structured. I'd think the easiest way to deal with this would be be allow us to set an 'sdkPath' in firebase.json and nudge firebase in the right direction.

    Yes, it means that users may have an extra setting to think about, but if we keep the current behaviour as a fallback in case that config is not provided, then majority of users can go on as if nothing happened and people who have unusual project structures will also find it very easy. We simply specify the folder path and move on.

  18. added a commit that references this issue on Feb 14, 2023
    9aef0f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions