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

Firebase functions reloading not working #44

Closed leemhenson closed 9 months ago

leemhenson commented 9 months ago

Hey. So perhaps this is a misconfiguration on my part, but firebase code reloading is not working for me when isolate-package is involved.

Excerpt from firebase.json:

{
  "functions": [
    {
      "source": "./isolate",
      "runtime": "nodejs20",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ],
      "predeploy": [
        "npm run build",
        "npm run isolate"
      ]
    }
  ]
}

So, npm run build causes tsc to emit into ./dist and then npm run isolate bundles that all up into ./isolate. So far so good, the emulators boot up fine and my functions handle requests as expected.

If I now make some changes and run npm run build, obviously nothing will change in the emulators as they are not watching that directory. Then, if I then run npm run isolate again, I would expect the emulators to see the changes and cycle in the new code - but it does not. At this point I have to manually kill and restart the emulators to see the new code changes take effect.

I believe the cause of this behaviour is firebase-tools using chokidar to watch the ./isolate/dist directory. Every time the isolate command is run, it removes the ./isolate directory entirely before replacing it with the new isolated bundle. The removal of the ./isolate/dist directory seems to cause chokidar to stop tracking changes, even though a new ./isolate/dist directory is created shortly after.

There are a couple of unanswered issues in the chokidar issue tracker that talk about this behaviour, so I don't expect it be resolved there or in firebase-tools any time soon. And since this seems to be an issue because of the way the isolate command works, I suspect it would be easiest to fix here - probably by not deleting isolate or isolate/dist. The dist part is specific to my project too I suspect (derived from main in package.json?), so that would need generalising too.

I suppose one other workaround would be to remove isolate from firebase.json entirely, and only use it when deploying but that feels like more of a hack and would mean I was testing with code that wasn't necessarily identical to that which would be in the isolated bundle.

0x80 commented 9 months ago

@leemhenson Live reloading of changes while running the emulators is only possible if isolate is integrated in the firebase-tools, so that the emulators run on the original source code as normal.

For this reason I have forked the firebase tools. It is described in the readme, and also in this article

This is the fork https://github.com/0x80/firebase-tools-with-isolate

You can see a working example in the mono-ts boilerplate at services/api

0x80 commented 9 months ago

I see that it's not super clear from this package's readme. I will add a warning to the "usage" section.

leemhenson commented 9 months ago

@leemhenson Live reloading of changes while running the emulators is only possible if isolate is integrated in the firebase-tools, so that the emulators run on the original source code as normal.

For this reason I have forked the firebase tools. It is described in the readme, and also in this article

This is the fork https://github.com/0x80/firebase-tools-with-isolate

You can see a working example in the mono-ts boilerplate at services/api

Maybe one day the Firebase gods will bless us with first-class monorepo support, but until then: thanks for your work.