-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[linter] Improve do_not_use_environment lint error message and documentation #60970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -623,8 +623,9 @@ class LinterLintCode extends LintCode { | |
|
||
static const LintCode do_not_use_environment = LinterLintCode( | ||
LintNames.do_not_use_environment, | ||
"Invalid use of an environment declaration.", | ||
correctionMessage: "Try removing the environment declaration usage.", | ||
"Avoid using environment values like '{0}' which create hidden global state.", | ||
correctionMessage: | ||
"Try using 'Platform.environment' for runtime access or remove environment-dependent code.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sounds misleading. Using |
||
); | ||
|
||
static const LintCode document_ignores = LinterLintCode( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4149,22 +4149,32 @@ LintCode: | |
Future<void> createDir(String path) async {} | ||
``` | ||
do_not_use_environment: | ||
problemMessage: "Invalid use of an environment declaration." | ||
correctionMessage: "Try removing the environment declaration usage." | ||
problemMessage: "Avoid using environment values like '{0}' which create hidden global state." | ||
correctionMessage: "Try using 'Platform.environment' for runtime access or remove environment-dependent code." | ||
state: | ||
stable: "2.9" | ||
categories: [errorProne] | ||
hasPublishedDocs: false | ||
deprecatedDetails: |- | ||
Using values derived from the environment at compile-time, creates | ||
Using values derived from environment variables at compile-time creates | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sound like those constructors derive values from the process environment at compile-time. So That's working as intended. |
||
hidden global state and makes applications hard to understand and maintain. | ||
Environment values are resolved at compile-time and become embedded in the | ||
compiled code, making behavior unpredictable across different environments. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These constructors has nothing to do with "different environments" at all, they are, if anything, predictable. |
||
|
||
**DON'T** use `fromEnvironment` or `hasEnvironment` factory constructors. | ||
|
||
**BAD:** | ||
```dart | ||
const loggingLevel = | ||
bool.hasEnvironment('logging') ? String.fromEnvironment('logging') : null; | ||
const bool usingAppEngine = bool.hasEnvironment('APPENGINE_RUNTIME'); | ||
const loggingLevel = String.fromEnvironment('LOGGING_LEVEL'); | ||
``` | ||
|
||
**GOOD:** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not not a GOOD replacement, it is completely different. Unless you expect that this was what the user wanted anyway, it's not a replacement for the bad code. |
||
```dart | ||
import 'dart:io'; | ||
|
||
final bool usingAppEngine = Platform.environment.containsKey('APPENGINE_RUNTIME'); | ||
final String loggingLevel = Platform.environment['LOGGING_LEVEL'] ?? 'info'; | ||
``` | ||
document_ignores: | ||
problemMessage: "Missing documentation explaining why the diagnostic is ignored." | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what "hidden global state" means in this case.
Or how reading the compilation environment "create"s anything.