From 7010df7ec2f466f6371db0dd7a5e220d9c77db53 Mon Sep 17 00:00:00 2001 From: Renze Yu Date: Thu, 17 Oct 2019 13:23:38 +0800 Subject: [PATCH] fix: error extracting metadata when VSINSTALLDIR is set (#5204) --- .../MsBuildEnvironmentScope.cs | 10 +--------- test/docfx.Tests/MetadataCommandTest.cs | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.DocAsCode.Metadata.ManagedReference/MsBuildEnvironmentScope.cs b/src/Microsoft.DocAsCode.Metadata.ManagedReference/MsBuildEnvironmentScope.cs index c121b645162..dc863e8adb4 100644 --- a/src/Microsoft.DocAsCode.Metadata.ManagedReference/MsBuildEnvironmentScope.cs +++ b/src/Microsoft.DocAsCode.Metadata.ManagedReference/MsBuildEnvironmentScope.cs @@ -14,7 +14,6 @@ namespace Microsoft.DocAsCode.Metadata.ManagedReference public class MSBuildEnvironmentScope : IDisposable { - private const string VSInstallDirKey = "VSINSTALLDIR"; private const string MSBuildExePathKey = "MSBUILD_EXE_PATH"; private readonly EnvironmentScope _innerScope; @@ -25,13 +24,6 @@ public MSBuildEnvironmentScope() private EnvironmentScope GetScope() { - var vsInstallDirEnv = Environment.GetEnvironmentVariable(VSInstallDirKey); - if (!string.IsNullOrEmpty(vsInstallDirEnv)) - { - Logger.LogInfo($"Environment variable {VSInstallDirKey} is set to {vsInstallDirEnv}, it is used as the inner compiler."); - return null; - } - var msbuildExePathEnv = Environment.GetEnvironmentVariable(MSBuildExePathKey); if (!string.IsNullOrEmpty(msbuildExePathEnv)) @@ -80,7 +72,7 @@ private EnvironmentScope GetScope() MSBuildLocator.RegisterInstance(latest); return new EnvironmentScope(new Dictionary { - [VSInstallDirKey] = latest.VisualStudioRootPath, + ["VSINSTALLDIR"] = latest.VisualStudioRootPath, ["VisualStudioVersion"] = latest.Version.ToString(2), }); } diff --git a/test/docfx.Tests/MetadataCommandTest.cs b/test/docfx.Tests/MetadataCommandTest.cs index 5c644488ee1..58f10d003ea 100644 --- a/test/docfx.Tests/MetadataCommandTest.cs +++ b/test/docfx.Tests/MetadataCommandTest.cs @@ -3,6 +3,7 @@ namespace Microsoft.DocAsCode.Tests { + using System; using System.Collections.Generic; using System.IO; @@ -48,6 +49,25 @@ public void TestMetadataCommandFromCSProject() CheckResult(); } + [Fact] + [Trait("Related", "docfx")] + [Trait("Language", "CSharp")] + public void TestMetadataCommandFromCSProjectWithVsinstalldirEnvSet() + { + var envName = "VSINSTALLDIR"; + var originalValue = Environment.GetEnvironmentVariable(envName); + Environment.SetEnvironmentVariable(envName, "c:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise"); + + try + { + TestMetadataCommandFromCSProject(); + } + finally + { + Environment.SetEnvironmentVariable(envName, originalValue); + } + } + [Fact] [Trait("Related", "docfx")] [Trait("Language", "CSharp")]