From 147b61a5ab870b405156b7de9f2daf6fdb48ad67 Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Tue, 3 Aug 2021 11:43:29 -0700 Subject: [PATCH 1/2] Print a useful error explaining why Runtime Config doesn't work --- spec/v1/config.spec.ts | 17 +++++++++++++++++ src/v1/config.ts | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/spec/v1/config.spec.ts b/spec/v1/config.spec.ts index d1deea4fd..f56b8747b 100644 --- a/spec/v1/config.spec.ts +++ b/spec/v1/config.spec.ts @@ -47,6 +47,23 @@ describe('config()', () => { (config as any).firebaseConfigCache = null; delete process.env.FIREBASE_CONFIG; delete process.env.CLOUD_RUNTIME_CONFIG; + delete process.env.K_CONFIGURATION; + }); + + it('Will never load in GCFv2', () => { + const json = JSON.stringify({ + foo: 'bar', + firebase: {}, + }); + readFileSync + .withArgs('/srv/.runtimeconfig.json') + .returns(Buffer.from(json)); + + process.env.K_CONFIGURATION = 'my-service'; + expect(() => config.config()).to.throw( + Error, + /transition to using environment variables/ + ); }); it('loads config values from .runtimeconfig.json', () => { diff --git a/src/v1/config.ts b/src/v1/config.ts index f0793957d..e6900be77 100644 --- a/src/v1/config.ts +++ b/src/v1/config.ts @@ -26,6 +26,14 @@ import * as path from 'path'; import * as firebase from 'firebase-admin'; export function config(): config.Config { + // K_CONFIGURATION is only set in GCFv2 + if (process.env.K_CONFIGURATION) { + throw new Error( + 'RuntimeConfig is no longer available in Google Cloud Functions v2. ' + + 'Please see the latest documentation for information on how to ' + + 'transition to using environment variables' + ); + } if (typeof config.singleton === 'undefined') { init(); } From 70f9755e8795f2278a86ef445935985511ec04bb Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Wed, 4 Aug 2021 14:05:02 -0700 Subject: [PATCH 2/2] PR feedback --- spec/v1/config.spec.ts | 2 +- src/v1/config.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/v1/config.spec.ts b/spec/v1/config.spec.ts index f56b8747b..7b750032d 100644 --- a/spec/v1/config.spec.ts +++ b/spec/v1/config.spec.ts @@ -50,7 +50,7 @@ describe('config()', () => { delete process.env.K_CONFIGURATION; }); - it('Will never load in GCFv2', () => { + it('will never load in GCFv2', () => { const json = JSON.stringify({ foo: 'bar', firebase: {}, diff --git a/src/v1/config.ts b/src/v1/config.ts index e6900be77..01ca3a4cc 100644 --- a/src/v1/config.ts +++ b/src/v1/config.ts @@ -29,9 +29,9 @@ export function config(): config.Config { // K_CONFIGURATION is only set in GCFv2 if (process.env.K_CONFIGURATION) { throw new Error( - 'RuntimeConfig is no longer available in Google Cloud Functions v2. ' + - 'Please see the latest documentation for information on how to ' + - 'transition to using environment variables' + 'functions.config() is no longer available in Cloud Functions for ' + + 'Firebase v2. Please see the latest documentation for information ' + + 'on how to transition to using environment variables' ); } if (typeof config.singleton === 'undefined') {