feat(package_info_plus): add opt-in compile-time package info accessor#3874
Open
Ortes wants to merge 1 commit into
Open
feat(package_info_plus): add opt-in compile-time package info accessor#3874Ortes wants to merge 1 commit into
Ortes wants to merge 1 commit into
Conversation
Adds an opt-in library, package_info_plus_environment.dart, exposing PackageInfoEnvironment.packageInfo. It returns the *running* app's PackageInfo: on web from compile-time PACKAGE_INFO_PLUS_* dart-defines, on every other platform by delegating to PackageInfo.fromPlatform(). On web there is no reliable runtime source for the running version: version.json is fetched from the server, so it reflects the deployed version rather than the executing (possibly cached) bundle, and the fetch can fail entirely (offline, CORS, hosting rewrites). The compile-time constants are embedded in the bundle and cannot diverge from it. A web build that omits PACKAGE_INFO_PLUS_VERSION fails to compile, so a misleading version can never ship silently. Native builds are unaffected. PackageInfo.fromPlatform() is unchanged and the new library is not exported by the package barrel, so this is purely additive and opt-in. Related: fluttercommunity#2675, fluttercommunity#225, fluttercommunity#456, flutter/flutter#149031.
e31c344 to
13e02bd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
On the web,
PackageInfo.fromPlatform()resolves the version by fetchingversion.jsonfrom the server at runtime (with a cache buster). That file reflects whatever is currently deployed — not the bundle actually executing in the browser. The two states routinely diverge:ClientException: Invalid content-length header [-1]when readingversion.jsonon a Firefox extension #3028), leaving every field empty.This makes the reported version actively misleading rather than merely missing — and the version is the whole point of the plugin. Its two canonical uses, showing the running version and checking whether an update is needed, are both defeated when the value is the server's. An update gate built on it can never fire for the very users it targets, since stale clients self-report as up to date.
This PR
Adds a small opt-in library,
package_info_plus_environment.dart, exposingPackageInfoEnvironment.packageInfo:PackageInfobuilt from compile-timePACKAGE_INFO_PLUS_*defines (flutter build web --dart-define=PACKAGE_INFO_PLUS_VERSION=1.2.3 ...). These are embedded in the running bundle and cannot diverge from it. A web build that omitsPACKAGE_INFO_PLUS_VERSIONfails to compile (akIsWeb-conditionalconstassert), so a misleading version can never ship silently.PackageInfo.fromPlatform(), which reads the installed binary and is already reliable. The defines are not required there.Design choices that keep this safe and non-breaking:
PackageInfo.fromPlatform()is untouched. No behavior change for existing users.package_info_plus_environment.dart. Apps that don't opt in are completely unaffected — no new build requirement is imposed package-wide.flutter analyzeis clean and the existing + new tests pass. The web compile-time enforcement is verified by a compilation test (a web build without the define does not compile) rather than a runtime test.Related Issues
ClientException: Invalid content-length header [-1]when readingversion.jsonon a Firefox extension #3028Checklist
CHANGELOG.mdnor the plugin version inpubspec.yamlfiles.flutter analyze) does not report any problems on my PR.Breaking Change