Repository: dotnet/roslyn
File: src/Compilers/Server/VBCSCompilerTests/BuildServerConnectionTests.cs (C#)

Code before:
                else
                {
                    // If no DOTNET_ROOT is needed, should return null
                    Assert.Null(envVars);
                }
            }
            finally
            {
                // Ensure we didn't modify the process environment
                var currentDotNetRoot = Environment.GetEnvironmentVariable(RuntimeHostInfo.DotNetRootEnvironmentName);
                Assert.Equal(originalDotNetRoot, currentDotNetRoot);
            }
        }

        [Fact]
        public void GetServerEnvironmentVariables_ExcludesDotNetRootVariants()
        {
            // This test verifies that DOTNET_ROOT* variables are properly excluded and replaced
            var originalEnvVars = new Dictionary<string, string?>();
            var testEnvVars = new[] { "DOTNET_ROOT_X64", "DOTNET_ROOT_X86", "DOTNET_ROOT_ARM64" };
            
            // Save original values and set test values
            foreach (var envVar in testEnvVars)
            {
                originalEnvVars[envVar] = Environment.GetEnvironmentVariable(envVar);
                Environment.SetEnvironmentVariable(envVar, "test_value");
            }
            
            try
            {
                var envVars = BuildServerConnection.GetServerEnvironmentVariables();
                
                if (envVars != null)
                {
                    // Should not contain any of the DOTNET_ROOT* variants we set
                    foreach (var testEnvVar in testEnvVars)
                    {
                        Assert.False(envVars.ContainsKey(testEnvVar), $"Environment variables should not contain {testEnvVar}");
                    }
                }
            }
            finally
            {
                // Restore original values
                foreach (var kvp in originalEnvVars)
                {
                    Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
                }
            }
        }
    }

Review comment (bug):
@copilot The tests need to run in parallel so using `Environment.SetEnvironment` will lead to race conditions. Change both `TryCreateServer` and `GetServerEnvironmentVariables` to take the collection of environment variables as a parameter instead of using the `Environment` class. That way they can be passed directly in the unit tests without creating race condition.

Code after:
            {
                // If no DOTNET_ROOT is needed, should return null
                Assert.Null(envVars);
            }
        }

        [Fact]
        public void GetServerEnvironmentVariables_ExcludesDotNetRootVariants()
        {
            // This test verifies that DOTNET_ROOT* variables are properly cleared and replaced
            var testEnvVars = new[] { "DOTNET_ROOT_X64", "DOTNET_ROOT_X86", "DOTNET_ROOT_ARM64", "DOTNET_ROOT(x86)" };

            // Create a test environment with DOTNET_ROOT* variants
            var testEnvironment = new System.Collections.Hashtable();
            foreach (System.Collections.DictionaryEntry entry in Environment.GetEnvironmentVariables())
            {
                testEnvironment[entry.Key] = entry.Value;
            }

            // Add test DOTNET_ROOT* variants
            foreach (var testEnvVar in testEnvVars)
            {
                testEnvironment[testEnvVar] = "test_value";
            }

            var envVars = BuildServerConnection.GetServerEnvironmentVariables(testEnvironment);

            if (envVars != null)
            {

                // Should set DOTNET_ROOT* variants to empty string to prevent inheritance
                foreach (var testEnvVar in testEnvVars)
                {
                    Assert.True(envVars.ContainsKey(testEnvVar), $"Environment variables should contain {testEnvVar}");
                    Assert.Equal(string.Empty, envVars[testEnvVar]);
                }
            }
        }
    }
}