chore: modernize EnvironmentVariableService#1788
Merged
JamieMagee merged 1 commit intomainfrom Apr 27, 2026
Merged
Conversation
91d78bd to
ec11315
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Modernizes EnvironmentVariableService.GetEnvironmentVariable to better align with net8.0 and platform behavior, reducing unnecessary work on Windows while keeping cross-platform case-insensitive lookup behavior.
Changes:
- Updated
IEnvironmentVariableService.GetEnvironmentVariableto returnstring?to reflect the fact it can return null. - Optimized Windows behavior by short-circuiting to
Environment.GetEnvironmentVariable(name)(no enumeration). - Replaced
string.Compare(..., true)withstring.Equals(..., StringComparison.OrdinalIgnoreCase)and removed#nullable disablefrom the implementation file.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.ComponentDetection.Contracts/IEnvironmentVariableService.cs | Updates interface return nullability for GetEnvironmentVariable. |
| src/Microsoft.ComponentDetection.Common/EnvironmentVariableService.cs | Optimizes Windows lookup and modernizes case-insensitive comparison; enables nullable context. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 2
grvillic
approved these changes
Apr 27, 2026
Replace string.Compare(.., true) with StringComparison.OrdinalIgnoreCase. Short-circuit to Environment.GetEnvironmentVariable on Windows, where it is already case-insensitive. Drop #nullable disable on the file. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ec11315 to
0138013
Compare
|
👋 Hi! It looks like you modified some files in the
If none of the above scenarios apply, feel free to ignore this comment 🙂 |
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.
What
Cleans up
EnvironmentVariableService.GetEnvironmentVariable:string.Compare(x, name, true) == 0(a .NET Framework idiom) withstring.Equals(x, name, StringComparison.OrdinalIgnoreCase).Environment.GetEnvironmentVariable(name)on Windows, where the lookup is already case-insensitive — no need to enumerate every environment variable.#nullable disablefrom the file.Why
The original implementation predates net8.0 and always paid the cost of enumerating every environment variable, even on Windows where the platform handles case-insensitivity natively.
Part 4 of a 6-PR cleanup series removing .NET-Framework-era anachronisms.