0x80 / isolate-package

Isolate a monorepo package with its internal dependencies to form a self-contained directory with a pruned lockfile
MIT License
121 stars 13 forks source link

Failed to load function definition from source #25

Closed Psycarlo closed 12 months ago

Psycarlo commented 1 year ago

Any clue on why the following is occuring?

Failed to load function definition from source: FirebaseError: Failed to find location of Firebase Functions SDK. Please file a bug on Github

This is when I try to run with emulators.

My setup:

firebase.json

...
"functions": {
  "source": "./apps/functions/isolate",
  "predeploy": ["turbo build", "pnpm isolate"],
  "runtime": "nodejs18"
},
...

Note: I have my firebase config files at the root of my monorepo

isolate.config.json

{
  "targetPackagePath": "./apps/functions"
}

apps/functions/package.json

...
"engines": {
  "node": "18"
},
"main": "./dist/index.js",
"files": [
  "dist"
],
"scripts": {
  "build": "tsc"
},
devDependencies: {
  ...
  "isolate-package": "^1.3.3"
}
...

apps/functions/src/index.ts

import { initializeApp } from 'firebase-admin/app'
import { onWordCreated } from './words'

initializeApp({
  projectId:
    process.env.GCLOUD_PROJECT === 'demo-words' ? 'demo-words' : undefined
})

export { onWordCreated }

When I try firebase deploy --only functions, I also get:

Running command: npx isolate npm ERR! could not determine executable to run

0x80 commented 12 months ago

Not sure, but it is likely caused by having "pnpm" as part of your firebase.json config.

Executables in this context, just like package.json scripts, will resolve to installed the node_modules so you should not prefix them with your package manager.

It should be "predeploy": ["turbo build", "isolate"],

Psycarlo commented 12 months ago

Even if with "predeploy": [] , the error is the same

0x80 commented 12 months ago

With "predeploy": [] you shouldn't have a build output, no? Then you are maybe deploying whatever is left in your build output from last time...?

0x80 commented 12 months ago

Also check that you are actually exporting the functions from the root index file. This error looks familiar. I think it happens when firebase really can not find your code. I suspect it is not related to the build process or isolate

Psycarlo commented 12 months ago

I was able to fix this. I changed my firebase.json predeploy to:

"predeploy": ["turbo build", "pnpm --dir \"$RESOURCE_DIR\" isolate"],

And also my functions package.json scripts, contain:

"isolate": "npx isolate"

0x80 commented 12 months ago

Good to hear that you found a workaround! I still wonder what the root of the problem was though, because you shouldn't need to do that.

If it's code that you are willing to share with me, invite me to your repo and I will take a quick look to see if I can spot anything.