george43g / better-firebase-functions

This repo provides functionality for a better way of organising files, imports and function triggers in Firebase Cloud Functions
Mozilla Public License 2.0
179 stars 15 forks source link

Firebase-functions v4 breaks better-firebase-functions: No functions are detected during deployment #47

Open bhr opened 1 year ago

bhr commented 1 year ago

After updating to firebase-functions 4.0.1 and trying to deploy the functions, firebase doesn't find any cloud functions anymore. It shows the following error:

The following functions are found in your project but do not exist in your local source code:
        auth-onCreate(europe-west1)
…
If you are renaming a function or changing its region, it is recommended that you create the new function first before deleting the old one to prevent event loss. For more info, visit https://firebase.google.com/docs/functions/manage-functions#modify

? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. No

index.ts

import { exportFunctions } from 'better-firebase-functions';
import camelCase from 'camelcase';

exportFunctions({
  __filename,
  exports,
  functionDirectoryPath: './api',
  funcNameFromRelPath: myFuncNameAlgo,
});

function myFuncNameAlgo(relPath: string): string {
  const sep = '/';
  const relPathArray = relPath.split(sep); /* ? */
  const fileName = relPathArray.pop(); /* ? */
  const relDirPathFunctionNameChunk = relPathArray.map((pathFragment) => camelCase(pathFragment)).join(sep);
  const fileNameFunctionNameChunk = camelCase(fileName!.split('.')[0]);
  const funcName = relDirPathFunctionNameChunk ? `${relDirPathFunctionNameChunk}${sep}${fileNameFunctionNameChunk}` : fileNameFunctionNameChunk;
  return funcName.split(sep).join('-');
}

With firebase-functions 3.24.1 deployment works and the functions are picked up correctly.

The transpiled JS files look as expected and correctly use exports.default

Sample function (TS->JS)

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const functions = __importStar(require("firebase-functions"));
exports.default = functions
    .region('europe-west1')
    .auth.user()
    .onCreate((user) => {
    …
    return Promise.resolve();
});

Is there any way to debug what's going wrong?

george43g commented 1 year ago

I've not yet had a chance to investigate this - had to take the last few months off work. I need to investigate how useful this repo is given that Firebase Functions now supports ESM modules (import) statements directly and what impact this has on performance. There may still be a benefit to using webpack to minify all the function code as that did increase performance when I initially wrote and tested this library.

paul019 commented 11 months ago

I have the same problem as bhr and would very much appreciate a disclaimer in the README.

george43g commented 11 months ago

I've tested with v4 and it works for me - however my whole dir is compiled with webpack before deployment (shouldn't change things) - I'll investigate further what's causing the issue.

cgossain commented 9 months ago

For me after upgrading from 4.0.0 to 6.0.0 none of my functions are being detected anymore. In this case it deleted all my staging environment functions.

cgossain commented 9 months ago

Btw, I downgraded back to 4.0.0 and it's working again.

Also v2 functions seem to not get picked up properly with this lib, I was hoping to upgrade to 6.0.0 in case it was fixed there.

george43g commented 9 months ago

Just short on time - if anyone can diagnose the issues and make a PR, that would be great. Otherwise I’ll get around to it as soon as I can.

G

On Wed, 4 Oct 2023 at 12:22 pm, Christian Gossain @.***> wrote:

Btw, I downgraded back to 4.0.0 and it's working again.

Also v2 functions seem to not get picked up properly with this lib, I was hoping to upgrade to 6.0.0 in case it was fixed there.

— Reply to this email directly, view it on GitHub https://github.com/george43g/better-firebase-functions/issues/47#issuecomment-1745984662, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUEHPRWZBX2OTKRQSHOSW3X5S26TAVCNFSM6AAAAAARJ7Y7U2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONBVHE4DINRWGI . You are receiving this because you commented.Message ID: @.***>

andr3wV commented 5 months ago

Same problem for me -- no functions are being detected. I tried figuring out what the problem was, but am struggling to figure it out

oconnorjoseph commented 4 months ago

Experiencing the same issue. Downgrading to 4.0.0 resolves it for both v1 and v2 functions.

roomman commented 2 months ago

Experiencing the same issue. Downgrading to 4.0.0 resolves it for both v1 and v2 functions.

Likewise here.

george43g commented 2 months ago

Has anyone figured out why version 4 works while the newer ones doesn't work?

On Thu, 11 Apr 2024 at 10:06 pm, Simon Zimmerman @.***> wrote:

Experiencing the same issue. Downgrading to 4.0.0 resolves it for both v1 and v2 functions.

Likewise here.

— Reply to this email directly, view it on GitHub https://github.com/george43g/better-firebase-functions/issues/47#issuecomment-2049549445, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUEHPVPMUZFFAQDRXYJEKDY4Z4FBAVCNFSM6AAAAAARJ7Y7U2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBZGU2DSNBUGU . You are receiving this because you commented.Message ID: @.***>

andr3wV commented 2 months ago

Just don't use this library, it has been abandoned. No point in having your plan rely on it.

george43g commented 2 months ago

Issue is not reproducible and I haven't seen anyone else attempt a PR with a fix yet. I only have so much time to contribute to this these days

G

On Fri, 12 Apr 2024 at 12:35 am, Andrew Vittiglio @.***> wrote:

Just don't use this library, it has been abandoned. No point in having your plan rely on it.

— Reply to this email directly, view it on GitHub https://github.com/george43g/better-firebase-functions/issues/47#issuecomment-2049839480, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUEHPUF6X2AJQUJN56G37DY42NR3AVCNFSM6AAAAAARJ7Y7U2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANBZHAZTSNBYGA . You are receiving this because you commented.Message ID: @.***>

KyleSmith0905 commented 2 months ago

I got the issue as well, I cloned the project locally to debug and found out the glob matching couldn't find the files.

I made the following change and it fixed.

exportFunctions({
    __filename,
    exports,
    enableLogger: true,
+   searchGlob: '**/*.js',
});

Now I see: 👍

functions: Loaded functions definitions from source: ...
functions[us-central1-...]: firestore function initialized.

Something else might be going on since this doesn't seem like something that would change between firebase versions?

george43g commented 2 months ago

Wow that’s a weird one. I remember writing it in TS but using wallaby to debug live / performance test it - so wrote the default glob to match both ts files and js files, depending on the environment (eg ts-node would return ts file extensions) you may need the glob to cover both cases.

At some point there was a compile or node version compatibility error (or maybe security patch, this was a while ago, could check the commit history) where the glob version was updated. Might be a breaking change with glob - but the best default value imho should match both ts and js (its not regex but glob notation).

Is it possible newer version of glob had a breaking change with glob notation?

G

On Thu, 18 Apr 2024 at 8:40 am, Kyle Smith @.***> wrote:

I got the issue as well, I cloned the project locally to debug and found out the glob matching couldn't find the files.

I made the following change and it fixed.

exportFunctions({ __filename, exports, enableLogger: true,+ searchGlob: '*/.js', });

Now I see: 👍

functions: Loaded functions definitions from source: ... functions[us-central1-...]: firestore function initialized.

Something else might be going on since this doesn't seem like something that would change between firebase versions?

— Reply to this email directly, view it on GitHub https://github.com/george43g/better-firebase-functions/issues/47#issuecomment-2062597057, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUEHPVDUIDNDVSM5JTSQADY5325DAVCNFSM6AAAAAARJ7Y7U2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRSGU4TOMBVG4 . You are receiving this because you commented.Message ID: @.***>

george43g commented 2 months ago

I’ve also had questions regarding jsm / export/import syntax thats now natively supported by node. As this is also a performance solution, I haven't benchmarked if native import X from Y beats the classic require(). Functions and sub function group detection is built to work identically to how FF used to do it, which was simply reading the main entry point’s module.exports object. If this has changed, it may be related to the issue and warrant further updates.

I personally use webpack to compile, minify and have its own runtime to squeeze in dependencies into the same js function file (saves on js parse/eval time) but separated per function (used the entry points feature of webpack to achieve this. So you’d still output separate js for each function to minimise how much js is parsed in cold start and only load the dependencies needed for that particular function invocation, and keep it in global scope while the function is warm.

I never got a chance to publish the webpack plugin that goes with this - I didnt want to include it in this bundle as again, for performance I keep the bundle as small as possible with minimal external dependencies.

Its been a few months since I checked the latest of the js ecosystem - would users want this as well (as its a further speedup) or would it need compatibility with multiple bundlers that are also popular nowadays? At the time Firebase didnt have any docs on bundling or compiling your functions directory but it works and does lead to further performance improvements.

G

On Thu, 18 Apr 2024 at 10:19 am, George G @.***> wrote:

Wow that’s a weird one. I remember writing it in TS but using wallaby to debug live / performance test it - so wrote the default glob to match both ts files and js files, depending on the environment (eg ts-node would return ts file extensions) you may need the glob to cover both cases.

At some point there was a compile or node version compatibility error (or maybe security patch, this was a while ago, could check the commit history) where the glob version was updated. Might be a breaking change with glob - but the best default value imho should match both ts and js (its not regex but glob notation).

Is it possible newer version of glob had a breaking change with glob notation?

G

On Thu, 18 Apr 2024 at 8:40 am, Kyle Smith @.***> wrote:

I got the issue as well, I cloned the project locally to debug and found out the glob matching couldn't find the files.

I made the following change and it fixed.

exportFunctions({ __filename, exports, enableLogger: true,+ searchGlob: '*/.js', });

Now I see: 👍

functions: Loaded functions definitions from source: ... functions[us-central1-...]: firestore function initialized.

Something else might be going on since this doesn't seem like something that would change between firebase versions?

— Reply to this email directly, view it on GitHub https://github.com/george43g/better-firebase-functions/issues/47#issuecomment-2062597057, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACUEHPVDUIDNDVSM5JTSQADY5325DAVCNFSM6AAAAAARJ7Y7U2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRSGU4TOMBVG4 . You are receiving this because you commented.Message ID: @.***>