Skip to content

Conversation

@jamesdaniels
Copy link
Member

@jamesdaniels jamesdaniels commented May 14, 2025

What's new

Pretty simple one, wire up FIREBASE_CONFIG, FIREBASE_WEBAPP_CONFIG, and a few other important environment variables to the App Hosting emulator to support SDK autoinit—from there trip @firebase/utils postinstall step, if present. This is intended to fail open and rely on the JS SDK error messages, if the developer is relying on autoinit.

The only "meh" bit is cross referencing the apphosting emulator config with the new .apphosting config entry, to look up the backendId. We should consider aligning these more in the future.

Cases tested

  • Missing/invalid project id
  • Missing/invalid backend id
  • Multiple apphosting firebase.json entries
  • No network access
  • Multiple versions of Firebase
  • Versions of Firebase that don't have the postinstall step

Known issues

  • PNPM still has issues, logging a warning for now
  • Sometimes the NextJS build cache can lead to old values being picked up—the developer can work around this with with an rm -rf .next/cache

I will document these limitations. IMO the best solution to both of these is to use autoinit via Cookie—like Web Frameworks does—but that will take a bit more work as we'll need to implement a proxy, I will address post-I/O.

Discovered issues

While working on this feature I discovered a couple issues with the App Hosting emulator, namely that the child process isn't always cleanly terminated and never is when using emulators:exec. I'll file bugs and if other folk don't have bandwidth I can work on that while addressing the above known issues

Copy link
Contributor

@blidd-google blidd-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some comments / nits but otherwise LGTM

): Promise<void> {
const npmLs = spawnSync("npm", ["ls", "@firebase/util", "--json", "--long"], {
cwd: rootDirectory,
shell: process.platform === "win32",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to run npm ls in a shell if the user is on windows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that pattern elsewhere in the CLI, seems to be working fine on mac/linux. Ideally though in an upcoming release I can ditch this in favor of cookie config.

const apphostingEmulatorConfig = options.config.src.emulators?.[Emulators.APPHOSTING];

if (listenForEmulator.apphosting) {
const rootDirectory = apphostingEmulatorConfig?.rootDirectory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the root directory here relate to the root directory specified per-backend by the user in firebase.json?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to put some thought into that, the main difference IMO is that emulatorConfig rootDir is relative to the json. I think my matcher works well enough for now but as firebase init apphosting is new I might be missing some cases. Will play around.

@jamesdaniels jamesdaniels enabled auto-merge (squash) May 15, 2025 19:56
@jamesdaniels jamesdaniels merged commit b86a9a6 into master May 15, 2025
50 checks passed
@github-project-automation github-project-automation bot moved this from Approved [PR] to Done in [Cloud] Extensions + Functions May 15, 2025
@jamesdaniels jamesdaniels deleted the jamesdaniels_appHostingEmulatorAutoInit branch May 15, 2025 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants