Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase emulators:start --only hosting:<target> is not working #2103

Closed
ChromeQ opened this issue Apr 6, 2020 · 9 comments
Closed

Firebase emulators:start --only hosting:<target> is not working #2103

ChromeQ opened this issue Apr 6, 2020 · 9 comments

Comments

@ChromeQ
Copy link

ChromeQ commented Apr 6, 2020

[REQUIRED] Environment info

firebase-tools: 7.11.0

Platform: Ubuntu 19.10

[REQUIRED] Test case

npx firebase emulators:start --only hosting:staging
i  emulators: Starting emulators: 
⚠  Not starting the hosting:staging emulator, make sure you have run firebase init.
✔  All emulators started, it is now safe to connect.

[REQUIRED] Steps to reproduce

Here is a sample firebase.json and .firebaserc files to show targets are set up:

// .firebaserc
{
  "projects": {
    "default": "test-project-dev",
    "staging": "test-project-dev",
    "prod": "test-project-prod"
  },
  "targets": {
    "test-project-dev": {
      "hosting": {
        "staging": [
          "test-project-dev"
        ]
      }
    },
    "test-project-prod": {
      "hosting": {
        "prod": [
          "test-project-prod"
        ]
      }
    }
  }
}
// firebase.json
{
    "emulators": {
      "firestore": {
        "port": 5002
      },
      "functions": {
        "port": 5001
      },
      "hosting": {
        "port": 5000
      }
    },
    "hosting": [
      {
        "target": "staging",
        "public": "build",
        "ignore": [
          "firebase.json",
          "**/.*",
          "**/node_modules/**"
        ],
        "headers": [ ... ],
        "redirects": [ ... ],
        "rewrites": [ ... ]
      },
      {
        "target": "prod",
        "public": "build",
        "ignore": [
          "firebase.json",
          "**/.*",
          "**/node_modules/**"
        ],
        "headers": [ ... ],
        "rewrites": [ ... ]
      }
    ]
}

Below is the output of other firebase commands to show the targets are set up correctly:

npx firebase target
Resource targets for test-project-dev:

[ hosting ]
staging (test-project-dev)

npx firebase use prod
Now using alias prod (test-project-prod)
npx firebase target
Resource targets for test-project-prod:

[ hosting ]
prod (test-project-prod)

When running npx firebase emulators:start --only hosting without a target then it starts both targets successfully. I'm trying to get it to run with just one at a time.

npx firebase emulators:start --only hosting
i  emulators: Starting emulators: hosting
i  hosting[staging]: Serving hosting files from: build
✔  hosting[staging]: Local server: http://localhost:5000
i  hosting[prod]: Serving hosting files from: build
✔  hosting[prod]: Local server: http://localhost:5005
✔  hosting: Emulator started at http://localhost:5000
✔  All emulators started, it is now safe to connect.

[REQUIRED] Expected behavior

I would expect this to start the emulator with only the target specified. But it closes the emulators immediately

[REQUIRED] Actual behavior

@google-oss-bot
Copy link
Contributor

This issue does not have all the information required by the template. Looks like you forgot to fill out some sections. Please update the issue with more information.

@samtstern
Copy link
Contributor

@ChromeQ thanks! This seems like a straightforward bug. For now your best workaround is to use firebase serve for advanced Hosting commands like this one.

@adam-remotesocial
Copy link
Contributor

Hi there! I'm running into this bug now and just wanted to detail an issue with the workaround.

I've tried to work around it by:

  • starting the other emulators with firebase emulators:start --only database,firestore,functions
  • starting hosting for just the target I want with firebase serve --only hosting:target
  • When I initializeApp() I then update functions and firestore to point to emulators:
    firebase.functions().useFunctionsEmulator('http://localhost:5001');
    firebase.firestore().settings({
        host: 'localhost:8080',
        ssl: false,
    });

But there is no way I've found to alter the databaseURL after hosting has already set the options in initializeApp(), and hosting doesn't know about the RTDB emulator.

Next experiment is using a firestoreConfig globbal variable that I'm not sure where it came from and explicitly changing the databaseURL in initializeApp without changing any other config. Hopefully that works. I do wish the bug would be fixed though. This has made my dev setup extra complex with the separate shell commands I have to run together.

Cheers!

@adam-remotesocial
Copy link
Contributor

adam-remotesocial commented Oct 18, 2020

^ ok fresh mind now and I'm realizing that calling initializeApp with firebaseConfig is not possible because the initializeApp call happens inside of init.js which is controlled and served by the hosting emulator and/or firebase serve. So either I'm missing something (very likely!) or it seems there's no workaround for this bug unless I start proxying the init.js file and transforming it which feels kinda terrible.

EDIT: Another option is generating a custom config that only has the target I want and then firebase --config ./.generated/config.json emulators:start, even if I cry a little inside at the thought.

@samtstern
Copy link
Contributor

@adam-remotesocial this is a huge pain and we're working on it as we speak:
firebase/firebase-js-sdk#3904

Once that PR is merged and released (a few weeks, probably) you'll be able to call firebase.database().useEmulator('locahost', 8080) after initializeApp() as long as you haven't yet used the database to access any data.

And then once that is all in place we'll be able to modify the magic __init.js file to use those APIs for you, so it's even more magic :-)

@adam-remotesocial
Copy link
Contributor

Oh wow that's great. Thank you!

@ChromeQ
Copy link
Author

ChromeQ commented Mar 2, 2021

@ChromeQ thanks! This seems like a straightforward bug. For now your best workaround is to use firebase serve for advanced Hosting commands like this one.

Hi @samtstern, now that the other issues raised are fixed and merged in firebase/firebase-js-sdk#3904, is this original issue still a "straightforward bug"? Been running into this issue again when trying to emulate and debug an issue in prod vs staging targets as outlined in the desc.

Thanks

@samtstern
Copy link
Contributor

@ChromeQ yes I would still consider this a bug, we did add the magic __init.js stuff but I think emulators:start still has issues with multiple Hosting targets.

@ChromeQ
Copy link
Author

ChromeQ commented Mar 15, 2021

I see v9.6.0 mentions

Fixes issue where the Firebase Hosting emulator would fail to start with --only filters using targets (#2820).

It sounds like the bug raised in this issue, and I've updated to the latest version and it seems to be fixed 🎉
I think I can close this issue now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants