fix: handle non-FlutterFire-generated firebase_options.dart gracefully#430
fix: handle non-FlutterFire-generated firebase_options.dart gracefully#430solvejet-team wants to merge 2 commits intoinvertase:mainfrom
Conversation
When firebase_options.dart exists but was not generated by FlutterFire CLI (e.g. a placeholder file using UnimplementedError instead of UnsupportedError), the CLI crashes with "Exception: UnsupportedError not found in <platform>". This fix checks whether the existing file matches the expected FlutterFire format before attempting an in-place update. If it does not match, the file is regenerated from scratch. Fixes invertase#422 Fixes invertase#382
There was a problem hiding this comment.
Code Review
This pull request introduces a verification step to ensure existing configuration files were generated by the FlutterFire CLI before attempting updates, allowing placeholder files to be correctly overwritten. It also includes various code formatting improvements and more descriptive error handling. A review comment suggests a more robust implementation for the file generation check by verifying the presence of the standard CLI header comment.
| /// Checks whether the existing file was generated by FlutterFire CLI | ||
| /// by looking for the expected structure (platform switch/if blocks). | ||
| /// Returns false for placeholder or manually written files. | ||
| bool _isFlutterFireGeneratedFile(File file) { | ||
| final contents = file.readAsStringSync(); | ||
| return contents.contains('DefaultFirebaseOptions') && | ||
| (contents.contains('kIsWeb') || | ||
| contents.contains('defaultTargetPlatform')); | ||
| } |
There was a problem hiding this comment.
The current check for whether a file is generated by FlutterFire CLI is based on the presence of certain keywords. This might be brittle and could lead to false positives if a user's placeholder file happens to contain these keywords.
A more robust method would be to check for the specific comment header // File generated by FlutterFire CLI. that this tool adds to the beginning of every generated file. This is a much stronger and more reliable signal that the file was auto-generated.
/// Checks whether the existing file was generated by FlutterFire CLI
/// by looking for the header comment.
/// Returns false for placeholder or manually written files.
bool _isFlutterFireGeneratedFile(File file) {
final contents = file.readAsStringSync();
return contents.startsWith('// File generated by FlutterFire CLI.');
}
Description
When
firebase_options.dartexists but was not generated by FlutterFire CLI(e.g. a placeholder file using
UnimplementedErrorinstead ofUnsupportedError),the CLI crashes with
Exception: UnsupportedError not found in <platform>.Root Cause
_updateExistingConfigurationFile()assumes the existing file containsUnsupportedErrorthrow blocks. If the file doesn't match, it throwsinstead of falling back gracefully.
Fix
Added
_isFlutterFireGeneratedFile()check inwrite(). If the existingfile wasn't generated by FlutterFire CLI, it regenerates from scratch
instead of attempting an in-place update.
Tests
All 27 existing unit tests pass.
Fixes #422
Fixes #382
Type of Change
feat-- New feature (non-breaking change which adds functionality)fix-- Bug fix (non-breaking change which fixes an issue)!-- Breaking change (fix or feature that would cause existing functionality to change)refactor-- Code refactorci-- Build configuration changedocs-- Documentationchore-- Chore