firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.02k stars 937 forks source link

Empty/bad firebase-export-metadata.json crashes emulators #5076

Open dereekb opened 2 years ago

dereekb commented 2 years ago

[REQUIRED] Environment info

firebase-tools: 11.14.0

Platform: macOS (w/ Docker Container)

[REQUIRED] Test case

Add an empty firebase-export-metadata.json to the exports directory.

[REQUIRED] Steps to reproduce

At some point during usage, the firebase-export-metadata.json file ended up being blank instead of being valid JSON.

[REQUIRED] Expected behavior

It seems like this file doesn't even always exist, so I'd expect behavior to be that if it cannot be read/is invalid JSON it continues as if nothing is there, or deletes it, or prints a better error.

[REQUIRED] Actual behavior

During startup the emulators crash with no helpful info as to what the cause was.

Terminal:

⚠  emulators: You are not currently authenticated so some features may not work correctly. Please run firebase login to authenticate the CLI.
i  emulators: Starting emulators: auth, functions, firestore, hosting, pubsub, storage
i  emulators: Shutting down emulators.
i  hub: Stopping emulator hub

Error: An unexpected error has occurred.

firebase-debug.log

[debug] [2022-10-06T15:28:20.092Z] ----------------------------------------------------------------------
[debug] [2022-10-06T15:28:20.093Z] Command:       /usr/local/bin/node /code/node_modules/.bin/firebase --project=default emulators:start --import=/root/data/emulators --export-on-exit
[debug] [2022-10-06T15:28:20.093Z] CLI Version:   11.14.0
[debug] [2022-10-06T15:28:20.093Z] Platform:      linux
[debug] [2022-10-06T15:28:20.093Z] Node Version:  v16.15.1
[debug] [2022-10-06T15:28:20.093Z] Time:          Thu Oct 06 2022 15:28:20 GMT+0000 (Coordinated Universal Time)
[debug] [2022-10-06T15:28:20.094Z] ----------------------------------------------------------------------
[debug] 
[debug] [2022-10-06T15:28:20.134Z] Object ".functions" in "firebase.json" has unknown property: {"additionalProperty":"engines"}
[debug] [2022-10-06T15:28:20.134Z] Field ".functions" in "firebase.json" is possibly invalid: should be array
[debug] [2022-10-06T15:28:20.135Z] Field ".functions" in "firebase.json" is possibly invalid: should match some schema in anyOf
[debug] [2022-10-06T15:28:20.144Z] > 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"]
[debug] Failed to authenticate, have you run firebase login?
[warn] ⚠  emulators: You are not currently authenticated so some features may not work correctly. Please run firebase login to authenticate the CLI. 
[debug] [2022-10-06T15:28:20.209Z] openjdk version "11.0.16" 2022-07-19

[debug] [2022-10-06T15:28:20.210Z] OpenJDK Runtime Environment (build 11.0.16+8-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Debian-1deb11u1, mixed mode)

[debug] [2022-10-06T15:28:20.215Z] Parsed Java major version: 11
[info] i  emulators: Starting emulators: auth, functions, firestore, hosting, pubsub, storage {"metadata":{"emulator":{"name":"hub"},"message":"Starting emulators: auth, functions, firestore, hosting, pubsub, storage"}}
[debug] [2022-10-06T15:28:20.226Z] [hub] writing locator at /tmp/hub-hellosubsapp.json
[info] i  emulators: Shutting down emulators. {"metadata":{"emulator":{"name":"hub"},"message":"Shutting down emulators."}}
[info] i  hub: Stopping emulator hub {"metadata":{"emulator":{"name":"hub"},"message":"Stopping emulator hub"}}
[debug] [2022-10-06T15:28:20.238Z] SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at findExportMetadata (/code/node_modules/firebase-tools/lib/emulator/controller.js:205:21)
    at Object.startAll (/code/node_modules/firebase-tools/lib/emulator/controller.js:290:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async /code/node_modules/firebase-tools/lib/commands/emulators-start.js:32:43
[error] 
[error] Error: An unexpected error has occurred.

I eventually figured it out by checking the files in the emulators folder and adding content (just "{}") to firebase-export-metadata.json.

I didn't have any emulator data that was exported at the time, and imagine it got corrupted potentially due to the unsafe shutdowns seen in #3092.

christhompsongoogle commented 2 years ago

This looks related to some other issues we've been seeing, namely where the emulator suite gets killed (SIGKILL) the export doesn't get a chance to complete as intended.

Question: Does this repro reliably or is it intermittent?

dereekb commented 2 years ago

It was the first time I'd have firebase-export-metadata.json get "corrupted" like that, but yes I'd agree it is related to SIGKILL coming in and triggering it.

If you overwrite firebase-export-metadata.json to be an empty file then you can reproduce it.