-
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?
[linter] Improve do_not_use_environment lint error message and documentation #60970
Conversation
Thank you for your contribution! This project uses Gerrit for code reviews. Your pull request has automatically been converted into a code review at: https://dart-review.googlesource.com/c/sdk/+/435744 Please wait for a developer to review your code review at the above link; you can speed up the review if you sign into Gerrit and manually add a reviewer that has recently worked on the relevant code. See CONTRIBUTING.md to learn how to upload changes to Gerrit directly. Additional commits pushed to this PR will update both the PR and the corresponding Gerrit CL. After the review is complete on the CL, your reviewer will merge the CL (automatically closing this PR). |
71af238
to
8fbffe0
Compare
…ntation Replaces confusing "Invalid use of an environment declaration" message with clear, actionable guidance that explains WHY environment constructors should be avoided and WHAT to use instead. Changes: - Problem message: Now includes specific method name and explains it creates "hidden global state" - Correction message: Suggests Platform.environment for runtime access - Documentation: Added comprehensive examples showing both bad and good patterns - Implementation: Pass method name to error message for better specificity - Tests: Fixed test to properly validate message contains specific method name Before: "Invalid use of an environment declaration." After: "Avoid using environment values like 'bool.fromEnvironment' which create hidden global state." The new message clearly explains: 1. WHAT is wrong (creates hidden global state) 2. WHY it's problematic (unpredictable across environments) 3. WHAT to do instead (use Platform.environment) Fixes dart-lang#59203
f5c0df9
to
bf3933c
Compare
https://dart-review.googlesource.com/c/sdk/+/435744 has been updated with the latest commits from this pull request. |
1 similar comment
https://dart-review.googlesource.com/c/sdk/+/435744 has been updated with the latest commits from this pull request. |
Per code review feedback, using a throw statement makes it clear to readers that this case cannot be reached under normal circumstances.
https://dart-review.googlesource.com/c/sdk/+/435744 has been updated with the latest commits from this pull request. |
1 similar comment
https://dart-review.googlesource.com/c/sdk/+/435744 has been updated with the latest commits from this pull request. |
looks like you still have many failing tests. |
i need to relook at my local development environment |
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.
The changes here (but possibly the existing lint too, to some degree) feels like tey misunderstand what the fromEnvironment
constructors do.
If the goal is to avoid using the compilation environment for anything, then just say that.
If the goal is to catch users who mistakenly think that .fromEnvironment
reads the process environment, say that it doesn't.
But saying that Platform.environment['APPENGINE_RUNTIME']
is a GOOD replacement for String.fromEnvironment('APPENGINE_RUNTIME')
is misleading and confusing.
@@ -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.", |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds misleading. Using Platform.environment
is runtime access to something completely different.
Maybe it's what the user wanted, but it's not a just a switch from compile-time to runtime access.
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 comment
The 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.
They do not. They access the compilation environment which is guaranteed to contain, fx,
bool.fromEnvironment('dart.library.core')
. There is nothing wrong with using this, it's a recommended way of knowing whether a platform library is available.
So const String.fromEnvironment('APPENGINE_RUNTIME')
will not find a process environment entry like export APPENGINE_RUNTIME=foobar
, but it will reflect a dart -DAPPENGINE_RUNTIME=foobar
passed to the compiler at compile time.
That's working as intended.
const loggingLevel = String.fromEnvironment('LOGGING_LEVEL'); | ||
``` | ||
|
||
**GOOD:** |
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.
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.
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 comment
The 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.
Not really, I don't understand why that's a problem. Or how it creates hidden global state. Or what that even is. The explanation has to be much more fundamental. Either the user knows what Consider:
|
Summary
Improves the
do_not_use_environment
lint rule error message and documentation to be more helpful and actionable.Problem
The current error message is confusing and unhelpful:
Solution
After: "Avoid using environment values like 'bool.hasEnvironment' which create hidden global state."
Key Improvements:
Specific Method Names: Error message now includes the exact method being used (e.g., "bool.hasEnvironment", "String.fromEnvironment")
Clear Explanation: Explains that environment constructors create "hidden global state" which makes code hard to understand and maintain
Actionable Guidance: Suggests using
Platform.environment
for runtime access insteadComprehensive Documentation:
Better Test Coverage: Added tests for all variants and edge cases
Examples
Before
After
Documentation Improvements
Added clear examples in the lint documentation:
BAD:
GOOD:
Files Changed
pkg/linter/messages.yaml
- Updated message and documentationpkg/linter/lib/src/rules/do_not_use_environment.dart
- Pass method name to errorpkg/linter/lib/src/lint_codes.g.dart
- Generated code updatepkg/linter/test/rules/do_not_use_environment_test.dart
- Enhanced test coverageTest Plan
bool.hasEnvironment('APPENGINE_RUNTIME')
)Fixes #59203