Skip to content

Fix bucket name being reused between instances of different buckets - #496

Open
minhdanh wants to merge 4 commits into
Baseflow:mainfrom
minhdanh:develop
Open

minhdanh wants to merge 4 commits into
Baseflow:mainfrom
minhdanh:develop

Conversation

@minhdanh

@minhdanh minhdanh commented Sep 8, 2025 •

Copy link
Copy Markdown
Contributor

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

This fixes an issue in which a subsequent FirebaseCacheManager instance cannot load or upload files correctly because the bucket name was set and cached in the first FirebaseCacheManager instance.
It also updates packages to latest versions.

⤵️ What is the current behavior?

In my case when I try to read a file from another bucket I see error " [firebase_storage/object-not-found] No object exists at the desired reference." although the file is there.

🆕 What is the new behavior (if this is a feature change)?

If we're initializing a FirebaseCacheManager with a new instance, it makes sure to use another cache key so that files from the other bucket can be read.

💥 Does this PR introduce a breaking change?

No

🐛 Recommendations for testing

Hmm, not sure

📝 Links to relevant issues/docs

🤔 Checklist before submitting

  • All projects build
  • Follows style guide lines (code style guide)
  • Relevant documentation was updated
  • Rebased onto current develop

@rickdijk rickdijk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add a test that two managers with different buckets get different cache keys / file services.

_retryOptions = retryOptions;
return _instance;
final cacheKey = bucket ?? defaultKey;
if (_instances.containsKey(cacheKey)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When an instance already exists, a new retryOptions argument is silently ignored. Worth documenting, or only accepting options on first create.

@rickdijk
rickdijk changed the base branch from develop to main September 16, 2026 00:21
@rickdijk

Copy link
Copy Markdown
Contributor

main has moved since you opened this. 3d64fa6 added a smoke test in test/flutter_cache_manager_firebase_test.dart that checks FirebaseCacheManager.key, which your pull request renames to defaultKey, so you are better placed than we are to resolve it. Since key is public, renaming it would also break apps that use it; keeping key alongside the per-bucket instances would avoid that. Could you merge main into your branch? The other conflicts are formatting in firebase_cache_manager.dart (3cd1694), which dart format . takes care of, and dependency lines in pubspec.yaml, where main already allows firebase_storage 13 and uses flutter_lints ^6.0.0, so taking main's side there is fine. Happy to help if anything is unclear.

_bucket = bucket;
_retryOptions = retryOptions;
return _instance;
final cacheKey = bucket ?? key;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Important: the raw bucket name is now the Config cache key, and that key becomes both the sqflite/JSON database name and the cache directory name under the app's temp dir. A bucket whose name matches another cache manager's key in the app (for example libCachedImageData, or firebaseCache itself) shares that manager's database and files, and bucket: '' gives key '', so the files land in the temp dir root and the database is named .db. Users who already passed bucket: also find their cache under a new name and the old firebaseCache entries left behind.

Fix: namespace the key, for example:

Suggested change
final cacheKey = bucket ?? key;
final cacheKey = bucket == null ? key : '${key}_$bucket';

(and update the test expectations to match).

});
});

tearDown(() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit (optional): _instances is a static cache that outlives each test, but this tearDown deletes the temp directory those instances were built against. A second test that calls FirebaseCacheManager(bucket: 'bucket-a') gets the cached instance whose path_provider directory is gone, so it fails or writes outside the mock. Fix: use unique bucket names per test, or add a @visibleForTesting way to clear _instances and call it here.

static String? _bucket;
static final Map<String, FirebaseCacheManager> _instances = {};

factory FirebaseCacheManager({RetryOptions? retryOptions, String? bucket}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit (optional): the factory now returns one shared instance per bucket, and retryOptions only takes effect on the first call for that bucket; later calls with different options get the existing instance unchanged. Neither is documented. AGENTS.md asks for "Public API documented with /// doc comments where applicable". Fix: add a /// comment on the factory that says both.

@rickdijk rickdijk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Criterion Verdict Reason
Adds real value pass Apps reading from more than one bucket got object-not-found because every instance shared the first bucket.
Really fixes it pass One instance per bucket, built from its own arguments; the new test fails on main and passes here.
Breaks something concern Public API is unchanged, but the bucket name is now the raw cache key (see the inline comment on the factory).
Version bump ready fail pubspec.yaml is still 2.1.4 with no CHANGELOG entry.
Covered by tests pass returns separate instances per bucket covers keys and file services; ran locally, passes.
Contribution guidelines concern PR description is from the old template.

Recommendation: close to mergeable. Namespace the cache key and add the version bump; the rest is optional.

  • Version bump missing. This fix ships in the published package, so it needs version: 2.1.5 in flutter_cache_manager_firebase/pubspec.yaml and a matching ## [2.1.5] - 2026-12-01 entry at the top of its CHANGELOG.md (for example * Fixes FirebaseCacheManager reusing the first bucket for every instance). 2.2.0 is already claimed by two other open PRs, so 2.1.5 is the free patch number. Per AGENTS.md a maintainer can add this while merging if you prefer.
  • PR description. It still uses the old template: the checklist says "Rebased onto current develop" and lacks the "Version bumped and dated CHANGELOG entry added" item, the body still says it updates packages (it no longer does), and "Recommendations for testing" can now point to the new unit test.
  • The earlier change request for a test is met by FirebaseCacheManager returns separate instances per bucket.

🤖 Generated with Claude Code

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