Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix flutter build web ignoring --web-define flag#182079

Merged
auto-submit[bot] merged 8 commits intoflutter:masterfrom
mkucharski17:fix-web-define-build-command
Feb 12, 2026
Merged

Fix flutter build web ignoring --web-define flag#182079
auto-submit[bot] merged 8 commits intoflutter:masterfrom
mkucharski17:fix-web-define-build-command

Conversation

@mkucharski17
Copy link
Contributor

@mkucharski17 mkucharski17 commented Feb 9, 2026

Summary

flutter build web --web-define=KEY=VALUE silently ignored the --web-define flag. The option was registered via usesWebDefineOption() but the values were never extracted or passed through the build pipeline. flutter run was unaffected as it correctly calls extractWebDefines().

This PR threads web-define values through the full flutter build web chain:

  • build_web.dart: Call extractWebDefines() and pass the result to buildWeb().
  • compile.dart: Add kWebDefinePrefix constant and webDefines parameter to buildWeb(), storing prefixed entries in Environment.defines.
  • web.dart: In WebTemplatedFiles.build(), extract web-define entries from the environment and pass them to both withSubstitutions() calls (bootstrap + index.html).

Related to #175805.
Fixes #182076

Test plan

  • Added 2 tests in build_web_test.dart:
    • Verifies --web-define values are passed to Environment.defines with webDefine: prefix.
    • Verifies builds succeed without --web-define and no prefixed entries are present.
  • Added 3 tests in web_test.dart:
    • WebTemplatedFiles substitutes web-define variables in index.html.
    • WebTemplatedFiles substitutes web-define variables in flutter_bootstrap.js.
    • WebTemplatedFiles works correctly with no web-define variables.
  • All existing tests in build_web_test.dart (24/24), web_test.dart (619/619), and web_template_test.dart (16/16) pass.

The `--web-define` CLI option was registered in `BuildWebCommand` via
`usesWebDefineOption()` but the values were never extracted or passed
through the build pipeline. This caused `flutter build web
--web-define=KEY=VALUE` to silently ignore the defines, while
`flutter run` worked correctly.

The fix threads web-define values through the full build chain:

1. `build_web.dart`: Call `extractWebDefines()` and pass result to
   `buildWeb()`.
2. `compile.dart`: Add `kWebDefinePrefix` constant and `webDefines`
   parameter to `buildWeb()`, storing prefixed entries in
   `Environment.defines`.
3. `web.dart`: In `WebTemplatedFiles.build()`, extract web-define
   entries from the environment and pass them to both
   `withSubstitutions()` calls (bootstrap + index.html).
@google-cla
Copy link

google-cla bot commented Feb 9, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Feb 9, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request fixes an issue where flutter build web ignored the --web-define flag. The changes correctly thread the web-define values through the build pipeline, from command-line parsing to the web template substitution. The implementation is clean and well-tested. I have one minor suggestion regarding adding documentation to a modified public method to align with the repository's style guide.

@mkucharski17 mkucharski17 force-pushed the fix-web-define-build-command branch from c935562 to 0a58482 Compare February 9, 2026 15:46
@mkucharski17 mkucharski17 force-pushed the fix-web-define-build-command branch from 9a01cce to 34794db Compare February 9, 2026 15:48
@bkonyi bkonyi requested review from bkonyi and mdebbar February 9, 2026 16:01
Copy link
Contributor

@bkonyi bkonyi left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution, @mkucharski17! Just a couple of comments, but overall this looks good to me.

@mkucharski17 mkucharski17 requested a review from bkonyi February 10, 2026 12:47
Copy link
Contributor

@mdebbar mdebbar left a comment

Choose a reason for hiding this comment

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

Thanks for contributing this fix!

Overall, the fix looks good to me!

I only have one suggestion: In order to prevent a similar issue from happening in the future, I'm thinking we should make the webDefines parameter required in buildWeb and in withSubstitutions. That would ensure all callers remember to pass webDefines. Thoughts?

@mdebbar mdebbar added cp: beta cherry pick this pull request to beta release candidate branch cp: stable cherry pick this pull request to stable release candidate branch labels Feb 12, 2026
@mdebbar mdebbar removed cp: beta cherry pick this pull request to beta release candidate branch cp: stable cherry pick this pull request to stable release candidate branch labels Feb 12, 2026
@flutter-dashboard
Copy link

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Changes reported for pull request #182079 at sha 2a3a400

@flutter-dashboard flutter-dashboard bot added the will affect goldens Changes to golden files label Feb 12, 2026
@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 12, 2026
@auto-submit auto-submit bot added this pull request to the merge queue Feb 12, 2026
Merged via the queue into flutter:master with commit 6f1d81b Feb 12, 2026
144 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 14, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 14, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 14, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 14, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 15, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 15, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 15, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 16, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 16, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 16, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 17, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 17, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 17, 2026
flutteractionsbot pushed a commit to flutteractionsbot/flutter that referenced this pull request Feb 18, 2026
…2273)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

This PR fixes a problem introduced in
flutter#175805 that caused a build to
exception to be thrown if variables like `{{this}}` were defined in the
web `index.html`, but were not explicitly set by
`--web-define=this=VALUE`.

Resolves flutter#182243 

Note: As per flutter#182076, this will
always show the warning if ran with `flutter build web`, whereas
`flutter run -d ...` works fine, regardless of whether you've set the
variable or not. That is set to be fixed in
flutter#182079, and does not have to do
with this PR.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: Navaron Bracke <[email protected]>
flutteractionsbot pushed a commit to flutteractionsbot/flutter that referenced this pull request Feb 18, 2026
…2273)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

This PR fixes a problem introduced in
flutter#175805 that caused a build to
exception to be thrown if variables like `{{this}}` were defined in the
web `index.html`, but were not explicitly set by
`--web-define=this=VALUE`.

Resolves flutter#182243 

Note: As per flutter#182076, this will
always show the warning if ran with `flutter build web`, whereas
`flutter run -d ...` works fine, regardless of whether you've set the
variable or not. That is set to be fixed in
flutter#182079, and does not have to do
with this PR.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: Navaron Bracke <[email protected]>
rickhohler pushed a commit to rickhohler/flutter that referenced this pull request Feb 19, 2026
## Summary

`flutter build web --web-define=KEY=VALUE` silently ignored the
`--web-define` flag. The option was registered via
`usesWebDefineOption()` but the values were never extracted or passed
through the build pipeline. `flutter run` was unaffected as it correctly
calls `extractWebDefines()`.

This PR threads web-define values through the full `flutter build web`
chain:

- **`build_web.dart`**: Call `extractWebDefines()` and pass the result
to `buildWeb()`.
- **`compile.dart`**: Add `kWebDefinePrefix` constant and `webDefines`
parameter to `buildWeb()`, storing prefixed entries in
`Environment.defines`.
- **`web.dart`**: In `WebTemplatedFiles.build()`, extract web-define
entries from the environment and pass them to both `withSubstitutions()`
calls (bootstrap + index.html).

Related to flutter#175805. 
Fixes flutter#182076

## Test plan

- Added 2 tests in `build_web_test.dart`:
- Verifies `--web-define` values are passed to `Environment.defines`
with `webDefine:` prefix.
- Verifies builds succeed without `--web-define` and no prefixed entries
are present.
- Added 3 tests in `web_test.dart`:
- `WebTemplatedFiles` substitutes web-define variables in `index.html`.
- `WebTemplatedFiles` substitutes web-define variables in
`flutter_bootstrap.js`.
  - `WebTemplatedFiles` works correctly with no web-define variables.
- All existing tests in `build_web_test.dart` (24/24), `web_test.dart`
(619/619), and `web_template_test.dart` (16/16) pass.

---------

Co-authored-by: Mouad Debbar <[email protected]>
rickhohler pushed a commit to rickhohler/flutter that referenced this pull request Feb 19, 2026
…2273)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

This PR fixes a problem introduced in
flutter#175805 that caused a build to
exception to be thrown if variables like `{{this}}` were defined in the
web `index.html`, but were not explicitly set by
`--web-define=this=VALUE`.

Resolves flutter#182243 

Note: As per flutter#182076, this will
always show the warning if ran with `flutter build web`, whereas
`flutter run -d ...` works fine, regardless of whether you've set the
variable or not. That is set to be fixed in
flutter#182079, and does not have to do
with this PR.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: Navaron Bracke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cp: beta cherry pick this pull request to beta release candidate branch cp: stable cherry pick this pull request to stable release candidate branch tool Affects the "flutter" command-line tool. See also t: labels. will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[web] --web-define flag not working with flutter build web (works with flutter run) on flutter 3.41.0-0.2.pre

3 participants