firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

firebase serve --only functions not hot reloading

Open safarmer opened this issue 8 years ago • 48 comments

Version info

firebase --version 3.18.4

Platform Information

Ubuntu 18.04 x64

Steps to reproduce

  1. Firebase cloud functions project with TypeScript using yarn
  2. yarn build --watch
  3. View a HTTPS function in browser
  4. Update function response
  5. Refresh page in browser

Expected behavior

Browser displays updated content

Actual behavior

The page only updates after restarting the firebase serve command. I have confirmed that the output in lib has been updated.

> firebase serve --only functions --debug                                                                                                                                                             [17:29:27]
[2018-05-01T00:29:30.975Z] ----------------------------------------------------------------------
[2018-05-01T00:29:30.978Z] Command:       /home/shane/.nvm/versions/node/v6.11.5/bin/node /home/shane/.yarn/bin/firebase serve --only functions --debug
[2018-05-01T00:29:30.978Z] CLI Version:   3.18.4
[2018-05-01T00:29:30.979Z] Platform:      linux
[2018-05-01T00:29:30.979Z] Node Version:  v6.11.5
[2018-05-01T00:29:30.979Z] Time:          Mon Apr 30 2018 17:29:30 GMT-0700 (PDT)
[2018-05-01T00:29:30.979Z] ----------------------------------------------------------------------

[2018-05-01T00:29:30.986Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2018-05-01T00:29:30.986Z] > authorizing via signed-in user
[2018-05-01T00:29:30.988Z] >>> HTTP REQUEST GET https://admin.firebase.com/v1/projects/nativ-dev  
  
 Mon Apr 30 2018 17:29:30 GMT-0700 (PDT)
[2018-05-01T00:29:31.433Z] <<< HTTP RESPONSE 200 server=nginx, date=Tue, 01 May 2018 00:29:31 GMT, content-type=application/json; charset=utf-8, content-length=128, connection=close, x-content-type-options=nosniff, strict-transport-security=max-age=31536000; includeSubdomains, cache-control=no-cache, no-store
[2018-05-01T00:29:31.437Z] >>> HTTP REQUEST GET https://admin.firebase.com/v1/database/nativ-dev/tokens  
  
 Mon Apr 30 2018 17:29:31 GMT-0700 (PDT)
[2018-05-01T00:29:31.926Z] <<< HTTP RESPONSE 200 server=nginx, date=Tue, 01 May 2018 00:29:31 GMT, content-type=application/json; charset=utf-8, content-length=256, connection=close, x-content-type-options=nosniff, strict-transport-security=max-age=31536000; includeSubdomains, cache-control=no-cache, no-store
[2018-05-01T00:29:31.927Z] >>> HTTP REQUEST GET https://cloudresourcemanager.googleapis.com/v1/projects/nativ-dev  
  
 Mon Apr 30 2018 17:29:31 GMT-0700 (PDT)
[2018-05-01T00:29:32.335Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Tue, 01 May 2018 00:29:32 GMT, server=ESF, cache-control=private, x-xss-protection=1; mode=block, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35", accept-ranges=none, connection=close

=== Serving from '/home/shane/source/firebase'...

i  functions: Preparing to emulate functions.
[2018-05-01T00:29:32.733Z] Fetching environment
[2018-05-01T00:29:32.736Z] >>> HTTP REQUEST GET https://mobilesdk-pa.googleapis.com/v1/projects/766196581577:getServerAppConfig

 Mon Apr 30 2018 17:29:32 GMT-0700 (PDT)
[2018-05-01T00:29:33.255Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Tue, 01 May 2018 00:29:33 GMT, server=ESF, cache-control=private, x-xss-protection=1; mode=block, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, alt-svc=hq=":443"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=":443"; ma=2592000; v="43,42,41,39,35", accept-ranges=none, connection=close
[2018-05-01T00:29:33.256Z] Starting @google-cloud/functions-emulator
[2018-05-01T00:29:34.318Z] Parsing function triggers
i  functions: No HTTPS functions found. Use firebase functions:shell if you would like to emulate other types of functions.
i  functions: No HTTPS functions found. Use firebase functions:shell if you would like to emulate other types of functions.
✔  functions: createCustomerOnCall: http://localhost:5000/nativ-dev/us-central1/createCustomerOnCall
✔  functions: createCustomerHttps: http://localhost:5000/nativ-dev/us-central1/createCustomerHttps
info: User function triggered, starting execution
info: Execution took 19 ms, user function completed successfully
info: User function triggered, starting execution
info: Execution took 1 ms, user function completed successfully

safarmer avatar May 01 '18 00:05 safarmer

I have just tried this on a different machine (Debian Stretch) with a fresh project (firebase init) and the same thing happened with the hello world function. I did notice that if I changed the source before sending a request to the function, but after the function was ready to serve, I could get it to hot reload once and then would stop working again.

safarmer avatar May 01 '18 04:05 safarmer

I have tried creating a new project (firebase init) on a Mac and a Linux machine, It works on Mac and not on Linux.

safarmer avatar May 01 '18 22:05 safarmer

I did some experimenting and I could get the hello world example in JavaScript to hot reload but not the TypeScript example. It seems that firebase serve only watches the index.js in the directory configured in `firebase.json.

To get my project reloading for TypeScript (and I assume more complex JavaScript layouts that have more than just index.js) I had to use Webpack to output a single file functions/lib/index.js and configure Firebase to look for my functions code in functions/list. This then makes hot reload work for both firebase functions:shell and firebase serve.

When I was using tsc (as per the firebase init template) the code would only reload if I modified the resulting index.js file. My current workflow is to run webpack --watch in one terminal with firebase serve --only functions in another. I now get hot reload. It would be good if I could go back to the default from firebase init.

Updated firebase.json

{    
  "functions": {
    "predeploy": "npm --prefix functions run build",
    "source": "functions/list"
  }
}

safarmer avatar May 02 '18 17:05 safarmer

@safarmer What does your functions/package.json look like? Does it have a main field?

laurenzlong avatar May 03 '18 19:05 laurenzlong

I had started using the default generated by firebase init

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc --project tsconfig.json",
    "serve": "yarn run build && firebase serve --only functions",
    "shell": "yarn run build && firebase functions:shell",
    "start": "yarn run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  },
  "private": true
}

safarmer avatar May 03 '18 19:05 safarmer

The simplest way I have to replicate the issue:

  1. firebase init && cd functions && yarn
  2. Uncomment the helloWorld function
  3. mkdir src/handlers && mv src/index.ts src/handlers/helloworld.ts
  4. echo "export * from './handlers/helloworld';" > src/index.ts
  5. In terminal 1: yarn build --watch
  6. In terminal 2: firebase serve --only functions

Visiting the URL in a browser, you see the hello world message. If you change the string returned from the handler in handler.ts, the only way to get the result to change is to restart the firebase serve command.

Adding "source": "functions/lib" to firebase.json make the code hot reload if you change anything in functions/lib but lower lever directories do not trigger a reload. It is possible to run touch lib/injex.js to trigger a reload. For example, changing a handler in src/index.ts triggers reload, but changing src/handlers/helloworld.ts does not.

safarmer avatar May 03 '18 20:05 safarmer

@laurenzlong the best I have so far is (taken from the default result of firebase init) is below. With this, I still need to run touch lib/index.js. Antoher issue with this is that firebase deploy no longer works.

firebase.json

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ],
    "source": "functions/lib"
  }
}

functions/package.json

{
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "devDependencies": {
    "tslint": "^5.8.0",
    "typescript": "^2.5.3"
  },
  "private": true
}

safarmer avatar May 03 '18 20:05 safarmer

@laurenzlong I have a simple example you can clone to repro the issue on Linux: https://github.com/safarmer/firebase-functions

safarmer avatar May 04 '18 22:05 safarmer

Thanks for the repro! I'll take a look next week.

laurenzlong avatar May 04 '18 23:05 laurenzlong

The same problem here :) To me, it seems like firebase serve is not supporting Typescript for now...

I have compared the files you asked to with the ones @safarmer sent and I have the same configurations. Doesn't surprise me as this is the official configuration to use with TypeScript

I think the problem is that firebase serve is not listening for changes in the functions "source" directory and won't run the predeploy scripts, defined in the firebase.json.

Would be great if this would be added. :D

IchordeDionysos avatar May 06 '18 17:05 IchordeDionysos

Ahh and would be great if I could define other predeploy scripts for firebase serve, so I won't have to run for example tslint in testing :)

IchordeDionysos avatar May 06 '18 17:05 IchordeDionysos

@IchordeDionysos I don't think it just TypeScript. I tried to do the same thing as my example in JavaScript and it seemed to do the same thing. I think the issue is with the server only looking at the entrypoint and not the rest of the code. Hopefully the firebase team can shed some light on the issue soon.

safarmer avatar May 07 '18 16:05 safarmer

@laurenzlong manually installing watchman seems to make things work (thanks @tstirrat for the suggestion)

safarmer avatar May 10 '18 20:05 safarmer

Thanks for the detailed repro @safarmer , very helpful. I was able to track down the root of the issue and filed https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/207. Please follow that issue.

laurenzlong avatar May 10 '18 21:05 laurenzlong

For others coming here, install the watchman from source or Linuxbrew, not the npm package

tstirrat avatar May 10 '18 21:05 tstirrat

@laurenzlong I can reproduce this issue in Windows and in particular index.ts change is not being detected. I'm having to manually run tsc. The change is then detected and the function is reloaded. Should I open a separate issue?

quick notes: sometimes info: Worker for helloWorld closed due to file changes. is detected on the ts but lib isnt refreshed. other times its not detected at all. manually running tsc always reloads the function.

dustensalinas avatar Nov 01 '18 23:11 dustensalinas

@tstirrat installing watchman on windows as per instructions doesnt seem to work

galki avatar Nov 13 '18 11:11 galki

What errors are you specifically running into when installing watchman?

kevinajian avatar Nov 20 '18 23:11 kevinajian

No errors when installing watchman. But serving functions doesn't hot reload. I have to stop serving, build, and serve again after every change.

On Wed, Nov 21, 2018 at 1:01 AM Kevin Jian [email protected] wrote:

What errors are you specifically running into when installing watchman?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-tools/issues/758#issuecomment-440460196, or mute the thread https://github.com/notifications/unsubscribe-auth/AHgHbeA_iECsIMH3V9P7NQypsmudF_IQks5uxInggaJpZM4Tta12 .

galki avatar Nov 21 '18 16:11 galki

Reopening due to multiple reports of this still not working on Windows

samtstern avatar Nov 26 '18 19:11 samtstern