Make SdkResolver-provided environment variables take precedence over ambient environment - #12655
Conversation
|
Hello @@copilot, I noticed that you’re changing an .swr file or any file under src/Package/MSBuild.VSSetup.. Please make sure to validate this change by an experimental VS insertion. This is accomplished by pushing to an exp/* branch, which requires write permissions to this repo. |
|
The package vuln thing that's plaguing other builds is hitting this one too, so it's hard to tell if this change actually passes tests. |
c24c943 to
5b852f6
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR fixes the precedence order for SDK resolver environment variables, making them override ambient environment variables from the host process as originally intended, rather than being overridden by them.
Key Changes:
- Modified
ProjectInstance.csandProject.csto allow SDK-resolved environment variables to override ambient environment variables - Fixed a serialization bug in
SdkResult.cswhere_environmentVariablesToAddwasn't being translated for out-of-proc scenarios - Added logging when SDK variables override ambient ones through a new resource string
SdkEnvironmentVariableOverridingAmbient
Reviewed Changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| ProjectInstance.cs | Removed check preventing SDK env vars from overriding ambient vars; added logic to remove conflicting ambient vars and log overrides |
| Project.cs | Removed check preventing SDK env vars from overriding ambient vars; SDK values now always override ambient values |
| SdkResult.cs | Fixed serialization bug by adding translation for _environmentVariablesToAdd; updated Equals and GetHashCode to include the field |
| Strings.resx | Added new log message resource string for SDK variable overrides |
| Strings.*.xlf | Added translations (marked as "new") for the new log message across all localization files |
| SdkResultEvaluation_Tests.cs | Added two tests verifying SDK env vars override ambient vars and first SDK wins when multiple SDKs set the same variable |
|
@copilot retarget this PR to |
I've improved the implementation (commit 79626cc) to align with
Regarding retargeting to
Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
79626cc to
e1a79ed
Compare
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
Context
The SdkResolver system allows SDK resolvers to provide environment variables to the MSBuild engine when an SDK is resolved. These variables were previously overridden by ambient environment variables from the host process, preventing the resolver from being the source of truth. This PR fixes that precedence order.
Changes Made
ProjectInstance.cs —
AddSdkResolvedEnvironmentVariable:_environmentVariablePropertiesbefore setting the new valueLogIfValueDiffersprivate helper (matchingmain's pattern) that logs the old and new values only when they differErrorUtilities.VerifyThrowArgumentLength(name)andErrorUtilities.VerifyThrowArgumentNull(value)Project.cs —
AddSdkResolvedEnvironmentVariable:SetPropertyto override ambient valuesErrorUtilities.VerifyThrowArgumentLength(name)andErrorUtilities.VerifyThrowArgumentNull(value)SdkResult.cs:
_environmentVariablesToAddwas not included inTranslate, breaking out-of-proc scenariosEqualsandGetHashCodeto include_environmentVariablesToAddStrings.resx and XLF files:
SdkEnvironmentVariableAlreadySetandSdkEnvironmentVariableAlreadySetBySdkto 3-arg format ({0}= name,{1}= new value,{2}= existing value)SdkEnvironmentVariableOverridingAmbientwith 3-arg format for diagnostics when an SDK overrides an ambient valueSdkEnvironmentVariableSetstringTesting
Two new tests were added in
SdkResultEvaluation_Tests.cs:SdkResolverEnvironmentVariablesOverrideAmbientEnvironmentVariables— verifies that an SDK-resolved env var takes precedence over an ambient env var with the same nameFirstSdkEnvironmentVariableWinsOverSubsequentSdks— verifies that among multiple SDKs, the first one to set a given variable winsAll 14
SdkResultEvaluationtests pass.Notes
New precedence order (highest to lowest):
Null
nameorvalueparameters toAddSdkResolvedEnvironmentVariableare rejected early with appropriate exceptions, matching the validation pattern used elsewhere in MSBuild property-setting code.Original prompt
Fixes #12654
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.