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

Firestore Emulator doesn't update with @google-cloud/firestore, and vice versa

Open Tohaker opened this issue 3 years ago • 6 comments

[REQUIRED] Environment info

firebase-tools: 9.18.0

Platform: Windows 10

[REQUIRED] Test case

Full repo example available here

import { EventFunction } from "@google-cloud/functions-framework/build/src/functions";
import { Firestore } from "@google-cloud/firestore";

export const test: EventFunction = async (_data, _context) => {
  const firestore = new Firestore();

  await firestore
    .collection("channels")
    .doc("new-doc")
    .create({ hello: "world" });

  // No file is created in the UI

  const channelsCollection = await firestore.collection('channels').get();
  channelsCollection.forEach((doc) =>console.log(doc.id));

  // Only the files present at emulator startup are returned
};

[REQUIRED] Steps to reproduce

Run the function with the following command:

FIRESTORE_EMULATOR_HOST=localhost:8080 functions-framework --source=build/src/ --target=test --signature-type=event --port 3000

Run the emulator

firebase emulators:start --project janitor --import=./firebase-data --export-on-exit

[REQUIRED] Expected behavior

When the function writes a file to the Firestore, it appears in the UI. When a new file is added in the UI, it should log the name of that file.

[REQUIRED] Actual behavior

Even between restarts of both function and emulator;

  • Files written are not persisted (with export flag added)
  • New files added in the UI while the emulator is running do not get read by the function, even between function restarts (whole command restart)
  • New files written are read back by the function, even between function restarts, but never reflected in the UI
[2021-09-25T12:20:30.900Z] > 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"]
i  emulators: Starting emulators: firestore, storage {"metadata":{"emulator":{"name":"hub"},"message":"Starting emulators: firestore, storage"}}
[2021-09-25T12:20:31.007Z] [hub] writing locator at C:\Users\miles\AppData\Local\Temp\hub-janitor.json
i  firestore: Importing data from C:\Users\miles\source\repos\carto\janitor\firebase-data\firestore_export\firestore_export.overall_export_metadata {"metadata":{"emulator":{"name":"firestore"},"message":"Importing data from C:\\Users\\miles\\source\\repos\\carto\\janitor\\firebase-data\\firestore_export\\firestore_export.overall_export_metadata"}}
!  firestore: Did not find a Cloud Firestore rules file specified in a firebase.json config file. 
{"metadata":{"emulator":{"name":"firestore"},"message":"Did not find a Cloud Firestore rules file 
specified in a firebase.json config file."}}
!  firestore: The emulator will default to allowing all reads and writes. Learn more about this option: https://firebase.google.com/docs/emulator-suite/install_and_configure#security_rules_configuration. {"metadata":{"emulator":{"name":"firestore"},"message":"The emulator will default to allowing all reads and writes. Learn more about this option: https://firebase.google.com/docs/emulator-suite/install_and_configure#security_rules_configuration."}}
[2021-09-25T12:20:31.018Z] Ignoring unsupported arg: projectId {"metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: projectId"}}
[2021-09-25T12:20:31.018Z] Ignoring unsupported arg: auto_download {"metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: auto_download"}}
[2021-09-25T12:20:31.019Z] Starting Firestore Emulator with command {"binary":"java","args":["-Dgoogle.cloud_firestore.debug_log_level=FINE","-Duser.language=en","-jar","C:\\Users\\miles\\.cache\\firebase\\emulators\\cloud-firestore-emulator-v1.13.1.jar","--host","localhost","--port",8080,"--seed_from_export","C:\\Users\\miles\\source\\repos\\carto\\janitor\\firebase-data\\firestore_export\\firestore_export.overall_export_metadata"],"optionalArgs":["port","webchannel_port","host","rules","functions_emulator","seed_from_export"],"joinArgs":false} {"metadata":{"emulator":{"name":"firestore"},"message":"Starting Firestore Emulator with command {\"binary\":\"java\",\"args\":[\"-Dgoogle.cloud_firestore.debug_log_level=FINE\",\"-Duser.language=en\",\"-jar\",\"C:\\\\Users\\\\miles\\\\.cache\\\\firebase\\\\emulators\\\\cloud-firestore-emulator-v1.13.1.jar\",\"--host\",\"localhost\",\"--port\",8080,\"--seed_from_export\",\"C:\\\\Users\\\\miles\\\\source\\\\repos\\\\carto\\\\janitor\\\\firebase-data\\\\firestore_export\\\\firestore_export.overall_export_metadata\"],\"optionalArgs\":[\"port\",\"webchannel_port\",\"host\",\"rules\",\"functions_emulator\",\"seed_from_export\"],\"joinArgs\":false}"}}
i  firestore: Firestore Emulator logging to firestore-debug.log {"metadata":{"emulator":{"name":"firestore"},"message":"Firestore Emulator logging to \u001b[1mfirestore-debug.log\u001b[22m"}}     
[2021-09-25T12:20:32.161Z] Sep 25, 2021 1:20:32 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketServer start
INFO: Started WebSocket server on ws://localhost:49316
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:20:32 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketServer start\r\nINFO: Started WebSocket server on 
ws://localhost:49316\r\n"}}
[2021-09-25T12:20:32.186Z] API endpoint: http://localhost: {"metadata":{"emulator":{"name":"firestore"},"message":"API endpoint: http://localhost:"}}
[2021-09-25T12:20:32.187Z] 8080
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:   

   export FIRESTORE_EMULATOR_HOST=localhost:8080

Dev App Server is now running.

 {"metadata":{"emulator":{"name":"firestore"},"message":"8080\nIf you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:\r\n\n   export FIRESTORE_EMULATOR_HOST=localhost:8080\n\nDev App Server is now running.\n\r\n"}}
[2021-09-25T12:20:32.319Z] Ignoring unsupported arg: port {"metadata":{"emulator":{"name":"storage"},"message":"Ignoring unsupported arg: port"}}
[2021-09-25T12:20:32.478Z] Temp file directory for storage emulator: C:\Users\miles\AppData\Local\Temp/firebase/storage/blobs {"metadata":{"emulator":{"name":"storage"},"message":"Temp file directory for storage emulator: C:\\Users\\miles\\AppData\\Local\\Temp/firebase/storage/blobs"}}        
[2021-09-25T12:20:32.938Z] Ignoring unsupported arg: auto_download {"metadata":{"emulator":{"name":"ui"},"message":"Ignoring unsupported arg: auto_download"}}
[2021-09-25T12:20:32.938Z] Ignoring unsupported arg: port {"metadata":{"emulator":{"name":"ui"},"message":"Ignoring unsupported arg: port"}}
[2021-09-25T12:20:32.939Z] Starting Emulator UI with command {"binary":"node","args":["C:\\Users\\miles\\.cache\\firebase\\emulators\\ui-v1.6.3\\server.bundle.js"],"optionalArgs":[],"joinArgs":false} {"metadata":{"emulator":{"name":"ui"},"message":"Starting Emulator UI with command {\"binary\":\"node\",\"args\":[\"C:\\\\Users\\\\miles\\\\.cache\\\\firebase\\\\emulators\\\\ui-v1.6.3\\\\server.bundle.js\"],\"optionalArgs\":[],\"joinArgs\":false}"}}
i  ui: Emulator UI logging to ui-debug.log {"metadata":{"emulator":{"name":"ui"},"message":"Emulator UI logging to \u001b[1mui-debug.log\u001b[22m"}}
[2021-09-25T12:20:33.035Z] Web / API server started at localhost:4000
 {"metadata":{"emulator":{"name":"ui"},"message":"Web / API server started at localhost:4000\n"}} 

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:4000                │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ localhost:8080 │ http://localhost:4000/firestore │
├───────────┼────────────────┼─────────────────────────────────┤
│ Storage   │ localhost:9199 │ http://localhost:4000/storage   │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at localhost:4400
  Other reserved ports: 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.

[2021-09-25T12:21:05.121Z] Sep 25, 2021 1:21:05 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:05 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected HTTP/2 connection.\r\n"}}
[2021-09-25T12:21:19.048Z] Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:19.463Z] Sep 25, 2021 1:21:19 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketChannelHandler initChannel
INFO: Connected to new websocket client
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:19 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketChannelHandler initChannel\r\nINFO: Connected to new websocket client\r\n"}}
[2021-09-25T12:21:19.499Z] Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:19.529Z] Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected HTTP/2 connection.\r\n"}}
[2021-09-25T12:21:19.988Z] Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:19 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:20.242Z] Sep 25, 2021 1:21:20 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:20 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:20.407Z] Sep 25, 2021 1:21:20 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:20 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:21.344Z] Sep 25, 2021 1:21:21 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:21 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:25.038Z] Sep 25, 2021 1:21:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:25.268Z] Sep 25, 2021 1:21:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:25.623Z] Sep 25, 2021 1:21:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:25 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:26.225Z] Sep 25, 2021 1:21:26 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:26 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:26.538Z] Sep 25, 2021 1:21:26 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:26 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:27.050Z] Sep 25, 2021 1:21:27 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:27 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:29.090Z] Sep 25, 2021 1:21:29 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:29 PM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\r\nINFO: Detected non-HTTP/2 connection.\r\n"}}      
[2021-09-25T12:21:29.239Z] Sep 25, 2021 1:21:29 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketChannelHandler channelClosed
INFO: Websocket client disconnected
 {"metadata":{"emulator":{"name":"firestore"},"message":"Sep 25, 2021 1:21:29 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketChannelHandler channelClosed\r\nINFO: Websocket client disconnected\r\n"}}

Tohaker avatar Sep 25 '21 12:09 Tohaker

I am also encountering this issue.

1000nettles avatar Sep 29 '21 20:09 1000nettles

For what it's worth, I'm confident this is an issue with the Emulator and not the node packages because I can run the code as expected against a live Firebase project.

Tohaker avatar Oct 02 '21 11:10 Tohaker

@Tohaker Can you try to update your .firebaserc by adding something like this?

CleanShot 2021-11-11 at 18 31 00

The projects must has a key named staging. I renamed it to preview before, and had the issue. After renaming it to staging back, everything worked great again.

judewang avatar Nov 11 '21 09:11 judewang

That didn't seem to help at all, but I'm not sure I'm doing it right. .firebaserc

{
    "projects": {
        "staging": "janitor",
        "production": "janitor"
    },
    "targets": {}
}

I tried the commands firebase emulators:start --import=firebase-data --project=staging and firebase emulators:start --import=firebase-data --project=janitor with identical results, that being adding a new document to Firestore doesn't update the value read by the function. Can you give some more details on what I need?

Tohaker avatar Nov 12 '21 19:11 Tohaker

Thank you! This helped me figure out what was wrong.

The project you are configuring with firebase use needs to match the config given to initializeApp(), even if you then just call connectFirestoreEmulator(). What the project is called probably doesn't matter, just that they are the same.

dkagedal avatar Jan 04 '22 23:01 dkagedal

+1 seeing a similar issue symptomize with the emulator. I am testing using an iOS simulator to read my emulator data, and existing data in firestore emulator database is not present when trying to read from the iOS app.

Turburlar avatar Oct 14 '22 20:10 Turburlar

Last year we added logging for this case where the logs should indicate if the emulators were started with a different project ID from the one specified in the App's requests. If there's still a reproducible error here please reopen.

christhompsongoogle avatar Apr 08 '23 01:04 christhompsongoogle