Skip to content

Commit 2b55597

Browse files
Fixed an issue where empty firebase.json files would break init (#9228)
* Fixed an issue where empty firebase.json files would break init * Update src/config.ts Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Adding one test, fixing another badly designed one * Adding test fxiture --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 4ac4bee commit 2b55597

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

src/commands/firestore-operations-describe.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ describe("firestore:operations:describe", () => {
8686

8787
it("should throw a FirebaseError if operation name is invalid", async () => {
8888
const options = { project: "test-project" };
89-
await expect(command.runner()("", options)).to.be.rejectedWith(
89+
await expect(command.runner()("/databases/blah", options)).to.be.rejectedWith(
9090
FirebaseError,
91-
'"" is not a valid operation name.',
91+
'"/databases/blah" is not a valid operation name.',
9292
);
9393
await expect(command.runner()("projects/p/databases/d", options)).to.be.rejectedWith(
9494
FirebaseError,

src/config.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Config } from "./config";
77
import { FIREBASE_JSON_PATH as VALID_CONFIG_PATH } from "./test/fixtures/valid-config";
88
import { FIXTURE_DIR as SIMPLE_CONFIG_DIR } from "./test/fixtures/config-imports";
99
import { FIXTURE_DIR as DUP_TOP_LEVEL_CONFIG_DIR } from "./test/fixtures/dup-top-level";
10+
import { FIREBASE_JSON_PATH as EMPTY_CONFIG_PATH } from "./test/fixtures/empty-config";
1011

1112
describe("Config", () => {
1213
describe("#load", () => {
@@ -17,9 +18,16 @@ describe("Config", () => {
1718
configPath: path.relative(cwd, VALID_CONFIG_PATH),
1819
});
1920
expect(config).to.not.be.null;
20-
if (config) {
21-
expect(config.get("database.rules")).to.eq("config/security-rules.json");
22-
}
21+
expect(config?.get("database.rules")).to.eq("config/security-rules.json");
22+
});
23+
it("should fall back to {} when the file is empty", () => {
24+
const cwd = __dirname;
25+
const config = Config.load({
26+
cwd,
27+
configPath: path.relative(cwd, EMPTY_CONFIG_PATH),
28+
});
29+
expect(config).to.not.be.null;
30+
expect(config?.data).to.deep.eq({});
2331
});
2432
});
2533

src/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import * as _ from "lodash";
66
import * as clc from "colorette";
77
import * as fs from "fs-extra";
88
import * as path from "path";
9-
const cjson = require("cjson");
109

1110
import { detectProjectRoot } from "./detectProjectRoot";
1211
import { FirebaseError } from "./error";
@@ -303,7 +302,10 @@ export class Config {
303302
if (pd) {
304303
try {
305304
const filePath = path.resolve(pd, path.basename(filename));
306-
const data = cjson.load(filePath);
305+
let data: unknown = {};
306+
if (fs.statSync(filePath).size > 0) {
307+
data = loadCJSON(filePath);
308+
}
307309

308310
// Validate config against JSON Schema. For now we just print these to debug
309311
// logs but in a future CLI version they could be warnings and/or errors.

src/test/fixtures/empty-config/firebase.json

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { resolve } from "path";
2+
3+
/**
4+
* A directory containing an empty rebase.json.
5+
*/
6+
export const FIXTURE_DIR = __dirname;
7+
8+
export const FIREBASE_JSON_PATH = resolve(__dirname, "firebase.json");

0 commit comments

Comments
 (0)