Repository navigation
Reduce usage of context getters and globals in the flutter tool #47161
Description
Activity
- addedc: contributor-productivityTeam-specific productivity, code health, technical debt.Team-specific productivity, code health, technical debt.toolAffects the "flutter" command-line tool. See also t: labels.Affects the "flutter" command-line tool. See also t: labels.
on Dec 16, 2019 - addedP1High-priority issues at the top of the work listHigh-priority issues at the top of the work list
on May 29, 2020 Sorry for the late discussion. Just saw this now.
This is making it more difficult than necessary to unit test tooling code. Besides mocking foo, we must also ensure the context injection is correct which leads to a significant amount of boilerplate configuration for each test case. Mistakes in this boilerplate can lead to tests silently doing the wrong thing.
Doesn't this describe dependency injection in general? I would expect dependency injection to make unit testing easier rather than harder. If an implicit dependency supplier is the identified issue (I have trouble following through the upstream discussions), should we just solve that? e.g. no dependencies are fulfilled through the context unless you set them up. Then we can still avoid multilayer construction injection which forces us to leak knowledge of descendent dependencies in classes just to pass injections around.
You are of course perfectly free to disagree, but this was the decision that myself, @christopherfujino , @jmagman , and @zanderso came to several months ago.
we can still avoid multilayer construction injection which forces us to leak knowledge of descendent dependencies in classes just to pass injections around.
You don't need to leak implementation details if you inject the classes you need instead of constructing them, but unwinding the context injection requires working from the bottom up, so in the interim there are some cases where we inject interfaces just to construct other types.
- added 6 commits that reference this issue
on Oct 3, 2020 - added a commit that references this issue
on Oct 27, 2020 Moving from January beta milestone since this is a long-term code health issue
26 remaining items
- added a commit that references this issue
on Aug 12, 2026 - added a commit that references this issue
on Aug 19, 2026 - added a commit that references this issue
on Aug 27, 2026 - added a commit that references this issue
on Aug 28, 2026 - added a commit that references this issue
on Aug 28, 2026 - added 7 commits that reference this issue
on Sep 2, 2026 - added a commit that references this issue
on Sep 29, 2026
Internal: b/139713315
The flutter_tool currently makes heavy usage of context getter globals to inject objects throughout the tool for both configuration and testability issues. For example:
This is making it more difficult than necessary to unit test tooling code. Besides mocking foo, we must also ensure the context injection is correct which leads to a significant amount of boilerplate configuration for each test case. Mistakes in this boilerplate can lead to tests silently doing the wrong thing.
Previously I attempted to clean this up with the Testbed class, but this hasn't succeeded it simplifying the test cases.
Instead, we should gradually reduce our usage of these context getters which removes the need for the test configuration and makes it more explicit which instances are being used for tests. For example, rewriting the snippet above becomes:
To ensure we don't regress in usage of context getters, we can use the forthcoming
testWithoutContexttest method that throws ifcontext.getis invoked. See also #45739cc @zanderso