Skip to content

Commit 9ba8f3e

Browse files
Log SDK environment variable messages only when values differ (#12918)
### Context Structured log viewer was cluttered with repeated non-actionable messages about SDK environment variables (e.g., `DOTNET_HOST_PATH`). These appeared for every project evaluation, reporting normal SDK behavior without providing value to users. ### Changes Made Modified `ProjectInstance.AddSdkResolvedEnvironmentVariable()` to only log messages when an SDK attempts to set an environment variable to a **different value** than what's already set: - **When values match** (common case): No message is logged, eliminating the clutter shown in the original issue - **When values differ** (conflict): A low-importance message is logged showing both the attempted value and the existing value for diagnostic purposes - **Code refactoring**: Extracted duplicate value comparison and logging logic into a helper method `LogIfValueDiffers` to improve code maintainability Updated string resources in `Strings.resx` and all 13 localized `.xlf` files: - `SdkEnvironmentVariableAlreadySet`: Now includes both attempted and existing values in the message - `SdkEnvironmentVariableAlreadySetBySdk`: Now includes both attempted and existing values in the message - Removed `SdkEnvironmentVariableSet` (no longer logging successful sets) The underlying functionality is unchanged—environment variables are still tracked and set correctly, and properties remain visible in the structured log viewer. ### Testing - Built MSBuild successfully - Verified messages no longer appear when values are the same (common case) - Messages would appear when values differ (preserves diagnostic value for conflicts) - Confirmed existing unit tests pass - Verified sample projects build successfully - Confirmed properties are still visible in structured log viewer (DOTNET_HOST_PATH, etc.) ### Notes This implementation balances two concerns: 1. **Eliminates noise**: Repeated messages about the same value being set multiple times no longer clutter logs 2. **Preserves diagnostics**: Actual conflicts (different values) are still logged with full context (both values shown) The messages log at `MessageImportance.Low` to provide diagnostic information without being intrusive in normal builds. Code quality was improved by extracting duplicate logic into a reusable helper method. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Duplicated messages from SDK/environment resolution</issue_title> > <issue_description><img width="2860" height="1772" alt="Image" src="https://github.com/user-attachments/assets/f4d4871f-28e9-4cb5-9f58-42c4a0056979" /> > > There are a ton of these `An SDK attempted to set the environment variable "DOTNET_HOST_PATH"` messages that aren't particularly helpful, and they're not attributed to a project so they clutter up the top level of the viewer tool.</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes #12915 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: YuliiaKovalova <[email protected]>
1 parent 49a6fd4 commit 9ba8f3e

15 files changed

+81
-125
lines changed

src/Build/Instance/ProjectInstance.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,13 +1390,13 @@ public void AddSdkResolvedEnvironmentVariable(string name, string value)
13901390
// If the property has already been set as an environment variable, we do not overwrite it.
13911391
if (_environmentVariableProperties.Contains(name))
13921392
{
1393-
_loggingContext.LogComment(MessageImportance.Low, "SdkEnvironmentVariableAlreadySet", name, value);
1393+
LogIfValueDiffers(_environmentVariableProperties, name, value, "SdkEnvironmentVariableAlreadySet");
13941394
return;
13951395
}
13961396
// If another SDK already set it, we do not overwrite it.
13971397
else if (_sdkResolvedEnvironmentVariableProperties?.Contains(name) == true)
13981398
{
1399-
_loggingContext.LogComment(MessageImportance.Low, "SdkEnvironmentVariableAlreadySetBySdk", name, value);
1399+
LogIfValueDiffers(_sdkResolvedEnvironmentVariableProperties, name, value, "SdkEnvironmentVariableAlreadySetBySdk");
14001400
return;
14011401
}
14021402

@@ -1412,8 +1412,18 @@ public void AddSdkResolvedEnvironmentVariable(string name, string value)
14121412
((IEvaluatorData<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>)this)
14131413
.SetProperty(name, value, isGlobalProperty: false, mayBeReserved: false, loggingContext: _loggingContext, isEnvironmentVariable: true, isCommandLineProperty: false);
14141414
}
1415+
}
14151416

1416-
_loggingContext.LogComment(MessageImportance.Low, "SdkEnvironmentVariableSet", name, value);
1417+
/// <summary>
1418+
/// Helper method to log a message if the attempted value differs from the existing value.
1419+
/// </summary>
1420+
private void LogIfValueDiffers(PropertyDictionary<ProjectPropertyInstance> propertyDictionary, string name, string attemptedValue, string messageResourceName)
1421+
{
1422+
ProjectPropertyInstance existingProperty = propertyDictionary.GetProperty(name);
1423+
if (existingProperty != null && !string.Equals(existingProperty.EvaluatedValue, attemptedValue, StringComparison.Ordinal))
1424+
{
1425+
_loggingContext.LogComment(MessageImportance.Low, messageResourceName, name, attemptedValue, existingProperty.EvaluatedValue);
1426+
}
14171427
}
14181428

14191429
/// <summary>

src/Build/Resources/Strings.resx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,14 +1384,12 @@ Errors: {3}</value>
13841384
<comment>{StrBegin="MSB4238: "}</comment>
13851385
</data>
13861386
<data name="SdkEnvironmentVariableAlreadySet" xml:space="preserve">
1387-
<value>An SDK attempted to set the environment variable "{0}" to "{1}" but it was already set as an environment variable.</value>
1387+
<value>An SDK attempted to set the environment variable "{0}" to "{1}" but it was already set to "{2}" as an environment variable.</value>
13881388
</data>
13891389
<data name="SdkEnvironmentVariableAlreadySetBySdk" xml:space="preserve">
1390-
<value>An SDK attempted to set the environment variable "{0}" to "{1}" but it was already set by another SDK.</value>
1391-
</data>
1392-
<data name="SdkEnvironmentVariableSet" xml:space="preserve">
1393-
<value>An SDK attempted set the environment variable "{0}" to "{1}".</value>
1390+
<value>An SDK attempted to set the environment variable "{0}" to "{1}" but it was already set to "{2}" by another SDK.</value>
13941391
</data>
1392+
13951393
<data name="UnrecognizedParentElement" xml:space="preserve">
13961394
<value>MSB4189: &lt;{1}&gt; is not a valid child of the &lt;{0}&gt; element.</value>
13971395
<comment>{StrBegin="MSB4189: "}</comment>

src/Build/Resources/xlf/Strings.cs.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/Resources/xlf/Strings.de.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/Resources/xlf/Strings.es.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/Resources/xlf/Strings.fr.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/Resources/xlf/Strings.it.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/Resources/xlf/Strings.ja.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/Resources/xlf/Strings.ko.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Build/Resources/xlf/Strings.pl.xlf

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)