macOS 10.14
I ran across the issue when looking on https://github.com/zeit/next.js/issues/5912 and I tried to extract the main part of the issue and create a repo to reproduce the issue and created a repo:
https://github.com/oBusk/firebase-tools-cwd-issue
So to reproduce:
npm install inside ./functionscd .. (to the root of the repo)npm i -g firebase-tools) firebase deploy or firebase serveBoth functions and hosting is served or deployed.
Two problems show themselves.
âš functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.This seems weird since inside ./functions, firebase-functions very much is installed and available.
âš functions: Error from emulator. Error occurred while parsing your function triggers.
Error: ENOENT: no such file or directory, open './FILE.txt'
at Object.openSync (fs.js:450:3)
at Object.readFileSync (fs.js:350:35)
at Object.<anonymous> (/Users/oscar/Source/firebase-tools-cwd-issue/functions/index.js:7:24)
at Module._compile (internal/modules/cjs/loader.js:723:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:659:17)
at require (internal/modules/cjs/helpers.js:22:18)
This is because the ./ path in the readFileSync() call is resolved from your current working directory, so following the instructions, the root of the repo.
If the code was somehow deployed, it would work, because the deployed functions has the root of the functions directory as cwd, meaning that deployed works differently than firebase-tools.
The workaround is to always run firebase deploy from inside ./functions which feels weird seeing how you're deploying both functions and hosting (and potentially others). I feel like the code for preparing the functions should use functions source as cwd. Thereby being more equal to the deployed runtime. This would maybe also fix the weird warning about firebase-functions not being available. That warning could also be improved without changing cwd by just checking in functions source rather than locally.
Thanks for the detailed report. I'll skimmed this and think I understand what you're asking, but I will have to come back to it soon (I'm working through my post-holiday email).
Okay, so I had some time to play around with this...
The two error message you are seeing are from the same source (as far as I can tell. When serve or deploy are run, they load the functions code to see what functions actually need to be executed. Because the file's content is being read in the top-most scope, it's being executed when the CLI is evaluating what functions exist. Because of how this works, the read fails causing the CLI to show those errors.
Now, there are two fairly simple workarounds for this:
serve or deploy are run, it'll fix that issue.path.resolve to get an absolute path to the file using __dirname. This can be done at the top level as well and not break serve/deploy: fs.readFileSync(path.resolve(__dirname, './FILE.txt'))`
Future work:
If we wanted your sample as-is to work, something probably has to change within the CLI. I'm not sure what it is off hand, but likely the working directory needs to be somehow changed when we require the functions source. I'll leave this open for now, 'cause I'm not sure what the solution is 😄.
Thanks again for the details - I hope one of those two workarounds works for you for the time being.
I have the following directory structure:
|--functions
|--index.js
|--firebase.json
|--index.js
If I run firebase serve --only functions from either ./ or ./functions, it tries to run ./index.js, not ./functions/index.js.
Is this expected behavior? Is there any way to select the directory to serve from? Also, firebase init in ./functions does not create another project directory from which I can run firebase serve --only functions.
Thank you.
Hey @alvin-toffler,
I tried reproducing your issue but firebase serve --only functions was working for me in both cases.
Can you provide more info? What's your version info and are there any debug logs?
For your second question, running firebase init in ./functions will not create another project, since you're already within a Firebase project directory.
Hi @kevinajian, thanks for following up.
I asked because I created the /functions directory within the root of a react-native project, and a couple of times I would run firebase serve --only functions from the root and get hit with a transpiling error like this:
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
Which made me think firebase was loading my react-native index.js file.
I just tried reproing with a fresh directory and I couldn't. I've changed my directory structure and it's fine now. Thanks!
Thanks for your update @alvin-toffler. We're going to close this out but if you encounter any further problems, please don't hesitate to open up a new issue.
Oops, didn't realize @alvin-toffler is not the author of this post. Will keep this open.
Most helpful comment
Okay, so I had some time to play around with this...
The two error message you are seeing are from the same source (as far as I can tell. When
serveordeployare run, they load the functions code to see what functions actually need to be executed. Because the file's content is being read in the top-most scope, it's being executed when the CLI is evaluating what functions exist. Because of how this works, the read fails causing the CLI to show those errors.Now, there are two fairly simple workarounds for this:
serveordeployare run, it'll fix that issue.path.resolveto get an absolute path to the file using__dirname. This can be done at the top level as well and not breakserve/deploy:Future work:
If we wanted your sample as-is to work, something probably has to change within the CLI. I'm not sure what it is off hand, but likely the working directory needs to be somehow changed when we
requirethefunctionssource. I'll leave this open for now, 'cause I'm not sure what the solution is 😄.Thanks again for the details - I hope one of those two workarounds works for you for the time being.