firebase / firebase/firebase-tools

firebase-tools emulators fails silently when firebase client code is initialized with a different project config

Open
#2,874 12 comments 1 reaction 0 assignees View on GitHub
polish
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [REQUIRED] Environment info

**firebase-tools:** @8.16.2

**Platform:** Windows 10

### [REQUIRED] Test case

I've described the best I could. I hope it helps to improve the package.

### [REQUIRED] Steps to reproduce

I was getting strange behaviors from firebase Firestore emulator. It seems that the problem was that `firebase-tools` had a different selected project as from the project that was being initialized by my client code with: `firebase.initializeApp(config);`.

What I mean is:

`firebase.initializeApp(config)` was pointing to a `.env` file that had the config for a `project-old` keys.

**.env**

```
FIREBASE_APP_API_KEY="OLD_KEY"
FIREBASE_APP_AUTH_DOMAIN="project-old.firebaseapp.com"
FIREBASE_APP_DATABASE_URL="https://project-old.firebaseio.com"
FIREBASE_APP_PROJECT_ID="project-old"
FIREBASE_APP_STORAGE_BUCKET="project-old.appspot.com"
FIREBASE_APP_MESSAGING_SENDER_ID="OLD_ID"
FIREBASE_APP_ID="OLD_ID"
```

And `firebase-tools` was set to: `project-new`. So, when I ran `firebase projects:list`, this is what I was getting:

![image](https://user-images.githubusercontent.com/43407798/100607224-8b014700-3302-11eb-9bad-bf0b407b405d.png)

### [REQUIRED] Expected behavior

I was expecting to get some warning about that difference between projects.

### [REQUIRED] Actual behavior

My client code was able to read data from Firestore emulator. But **only the initial data**. The one that was initialized with the emulator. It was unable to read any new data from the Firestore. For example: I would go to the emulators UI on `localhost:4000` and update some data. Even after refreshing my app, the app would still read the `initialData` and not the updated data. Also, no real time `onSnapshot` listeners were activated.

Ideally, I think that `firebase-tools` should be aware that its emulator is being called by a different firebase app, that was initialized on another project, and would show some kind of warning or error.

I feel that the emulator was failing silently and it was hard to find out what was wrong. No warnings or errors were being shown.

When I updated my client code to `initializeApp(config)` to the `project-new` keys, everything started to work fine again.

This is how I'm initializing the emulators:

```
firebase emulators:start --import=./src/firebase/emulatorData
```

This will initialize the emulators with the data from `./src/firebase/emulatorData`

And on my client code I have:

**firebase.ts**

```
firebase.initializeApp(config);

if (DEVELOPMENT) {
console.log(">>> From firebase.ts: activating emulators...");
firebase.functions().useFunctionsEmulator("http://localhost:5001");
firebase.firestore().settings({
host: "localhost:8080",
ssl: false
});
}
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the mismatch between the project in firebase.ts and the project selected by firebase emulators:start --import=./src/firebase/emulatorData. Trace how the emulator handles client connections and determine where a project mismatch could be detected; done means a clear warning or error is shown instead of silent stale reads and missing onSnapshot updates.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
cli, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.