Skip to content

Commit 28de572

Browse files
fix(schematics): warn instead of crashing when the post-init firebase.json read fails
The re-read added for the firestore starter files was unguarded, so a firebase.json that firebase-tools left unreadable would abort the whole ng add. Before the starter files moved after the init calls, this same failure degraded gracefully — the files were already staged, and addFirestoreToFirebaseJson warned and let setup finish. Restore that: warn and continue. createFirestoreStarterFiles independently checks the disk for each file it would create, so a missing snapshot costs the firestore-section check, not the collision safety it also relies on. Trimmed the surrounding comments to the two constraints a future edit could silently break — read after init, and run before addFirestoreToFirebaseJson — and moved the rationale here. The reason the read sits after the init calls at all: staging against a stale pre-init snapshot risks the Tree/disk collision the .firebaserc write hit earlier in this PR, where a Tree-staged file collides at commit time with one firebase-tools already wrote to disk and aborts the run. No init call adds a firestore section today, so that specific path is defensive against a future one.
1 parent e9c6ee4 commit 28de572

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/schematics/setup/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,19 @@ export const ngAddSetupProject = (
147147

148148
}
149149

150-
// Re-read firebase.json after the last firebaseTools.init call — init rewrites it on disk
151-
// mid-run, staging files against a stale pre-init copy risks the same Tree/disk collision
152-
// .firebaserc hit earlier in this file (a Tree-staged file colliding with one firebase-tools
153-
// already wrote to disk, which crashes the schematic when the Tree commits). Right now no
154-
// init call adds a firestore section, so this is defensive against a future one that might.
155-
const firebaseJsonAfterInit: FirebaseJSON = JSON.parse(
156-
readFileSync(join(projectRoot, "firebase.json")).toString()
157-
);
158-
// Must run before addFirestoreToFirebaseJson below: that call is what adds the firestore
159-
// section on a normal run. If it ran first, this read would see the section it just added
160-
// and skip creating the files — leaving firebase.json pointing at rules/indexes files that
161-
// were never created.
150+
// Read after the init calls, never before — firebase-tools rewrites firebase.json on disk
151+
// mid-run. A failed read isn't fatal: createFirestoreStarterFiles falls back to checking the
152+
// disk for each file it would create.
153+
let firebaseJsonAfterInit: FirebaseJSON | undefined;
154+
try {
155+
firebaseJsonAfterInit = JSON.parse(
156+
readFileSync(join(projectRoot, "firebase.json")).toString()
157+
);
158+
} catch (e) {
159+
context.logger.warn(`Could not re-read firebase.json after setup (${e.message}).`);
160+
}
161+
// Must run before addFirestoreToFirebaseJson: that call adds the firestore section, and if it
162+
// ran first this snapshot would see it and skip creating the files it points at.
162163
createFirestoreStarterFiles(host, context, features, firebaseJsonAfterInit);
163164

164165
// Both write the real filesystem, after the last firebaseTools.init call — init writes

0 commit comments

Comments
 (0)