public FrameworkEnvironment(string projectPath, bool sdkProject) { ToolsPath = LocateToolsPath(); ExtensionsPath = Path.GetFullPath(Path.Combine(ToolsPath, @"..\..\")); SDKsPath = Path.Combine(sdkProject ? DotnetPathResolver.ResolvePath(projectPath) : ExtensionsPath, "Sdks"); RoslynTargetsPath = Path.Combine(ToolsPath, "Roslyn"); }
private static BuildEnvironment CreateFrameworkEnvironment(string projectPath, bool sdkProject) { string msBuildExePath = ToolLocationHelper.GetPathToBuildToolsFile("msbuild.exe", ToolLocationHelper.CurrentToolsVersion); if (string.IsNullOrEmpty(msBuildExePath)) { // Could not find the tools path, possibly due to https://github.com/Microsoft/msbuild/issues/2369 // Try to poll for it. From https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/4649f55f900a324421bad5a714a2584926a02138/src/StructuredLogViewer/MSBuildLocator.cs string programFilesX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86); msBuildExePath = new[] { Path.Combine(programFilesX86, @"Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe"), Path.Combine(programFilesX86, @"Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"), Path.Combine(programFilesX86, @"Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe") } .Where(File.Exists) .FirstOrDefault(); } if (string.IsNullOrEmpty(msBuildExePath)) { throw new InvalidOperationException("Could not locate the tools (msbuild.exe) path"); } string toolsPath = Path.GetDirectoryName(msBuildExePath); string extensionsPath = Path.GetFullPath(Path.Combine(toolsPath, @"..\..\")); string sdksPath = Path.Combine(sdkProject ? DotnetPathResolver.ResolvePath(projectPath) : extensionsPath, "Sdks"); string roslynTargetsPath = Path.Combine(toolsPath, "Roslyn"); return(new BuildEnvironment(msBuildExePath, extensionsPath, sdksPath, roslynTargetsPath)); }
public CoreEnvironment(string projectPath) { string dotnetPath = DotnetPathResolver.ResolvePath(projectPath); ToolsPath = dotnetPath; ExtensionsPath = dotnetPath; SDKsPath = Path.Combine(dotnetPath, "Sdks"); RoslynTargetsPath = Path.Combine(dotnetPath, "Roslyn"); }
// Based on code from OmniSharp // https://github.com/OmniSharp/omnisharp-roslyn/blob/78ccc8b4376c73da282a600ac6fb10fce8620b52/src/OmniSharp.Abstractions/Services/DotNetCliService.cs private static BuildEnvironment CreateCoreEnvironment(string projectPath) { string dotnetPath = DotnetPathResolver.ResolvePath(projectPath); string msBuildExePath = Path.Combine(dotnetPath, "MSBuild.dll"); string sdksPath = Path.Combine(dotnetPath, "Sdks"); string roslynTargetsPath = Path.Combine(dotnetPath, "Roslyn"); return(new BuildEnvironment(msBuildExePath, dotnetPath, sdksPath, roslynTargetsPath)); }
// Based on code from OmniSharp // https://github.com/OmniSharp/omnisharp-roslyn/blob/78ccc8b4376c73da282a600ac6fb10fce8620b52/src/OmniSharp.Abstractions/Services/DotNetCliService.cs private BuildEnvironment CreateCoreEnvironment(EnvironmentOptions options) { // Get paths DotnetPathResolver pathResolver = new DotnetPathResolver(_manager.LoggerFactory); string dotnetPath = pathResolver.ResolvePath(_projectFile.Path, options.DotnetExePath); if (dotnetPath == null) { return(null); } string msBuildExePath = Path.Combine(dotnetPath, "MSBuild.dll"); if (options != null && options.EnvironmentVariables.ContainsKey(EnvironmentVariables.MSBUILD_EXE_PATH)) { msBuildExePath = options.EnvironmentVariables[EnvironmentVariables.MSBUILD_EXE_PATH]; } // Clone the options global properties dictionary so we can add to it Dictionary <string, string> additionalGlobalProperties = new Dictionary <string, string>(options.GlobalProperties); // Required to force CoreCompile target when it calculates everything is already built // This can happen if the file wasn't previously generated (Clean only cleans what was in that file) if (options.TargetsToBuild.Contains("Clean", StringComparer.OrdinalIgnoreCase)) { additionalGlobalProperties.Add(MsBuildProperties.NonExistentFile, Path.Combine("__NonExistentSubDir__", "__NonExistentFile__")); } // Clone the options global properties dictionary so we can add to it Dictionary <string, string> additionalEnvironmentVariables = new Dictionary <string, string>(options.EnvironmentVariables); // (Re)set the enviornment variables that dotnet sets // See https://github.com/dotnet/cli/blob/0a4ad813ff971f549d34ac4ebc6c8cca9a741c36/src/Microsoft.DotNet.Cli.Utils/MSBuildForwardingAppWithoutLogging.cs#L22-L28 // Especially important if a global.json is used because dotnet may set these to the latest, but then we'll call a msbuild.dll for the global.json version if (!additionalEnvironmentVariables.ContainsKey(EnvironmentVariables.MSBuildExtensionsPath)) { additionalEnvironmentVariables.Add(EnvironmentVariables.MSBuildExtensionsPath, dotnetPath); } if (!additionalEnvironmentVariables.ContainsKey(EnvironmentVariables.MSBuildSDKsPath)) { additionalEnvironmentVariables.Add(EnvironmentVariables.MSBuildSDKsPath, Path.Combine(dotnetPath, "Sdks")); } if (!additionalEnvironmentVariables.ContainsKey(EnvironmentVariables.COREHOST_TRACE)) { additionalEnvironmentVariables.Add(EnvironmentVariables.COREHOST_TRACE, "0"); } return(new BuildEnvironment( options.DesignTime, options.Restore, options.TargetsToBuild.ToArray(), msBuildExePath, options.DotnetExePath, additionalGlobalProperties, additionalEnvironmentVariables)); }
// Based on code from OmniSharp // https://github.com/OmniSharp/omnisharp-roslyn/blob/78ccc8b4376c73da282a600ac6fb10fce8620b52/src/OmniSharp.Abstractions/Services/DotNetCliService.cs private BuildEnvironment CreateCoreEnvironment(EnvironmentOptions options) { // Clone the options global properties dictionary so we can add to it Dictionary <string, string> additionalGlobalProperties = options.GlobalProperties.ToDictionary(x => x.Key, x => x.Value); // Tweak targets List <string> targets = new List <string>(options.TargetsToBuild); if (targets.Contains("Restore", StringComparer.OrdinalIgnoreCase) && _projectFile.Virtual) { // NuGet.Targets can't handle virtual project files: // C:\Program Files\dotnet\sdk\2.1.300\NuGet.targets(239,5): error MSB3202: The project file "E:\Code\...\...csproj" was not found. targets.RemoveAll(x => x.Equals("Restore", StringComparison.OrdinalIgnoreCase)); } if (targets.Contains("Clean", StringComparer.OrdinalIgnoreCase)) { // Required to force CoreCompile target when it calculates everything is already built // This can happen if the file wasn't previously generated (Clean only cleans what was in that file) additionalGlobalProperties.Add(MsBuildProperties.NonExistentFile, @"__NonExistentSubDir__\__NonExistentFile__"); } // Get paths string dotnetPath = DotnetPathResolver.ResolvePath(_projectFile.Path); string msBuildExePath = Path.Combine(dotnetPath, "MSBuild.dll"); string extensionsPath = dotnetPath; string sdksPath = Path.Combine(dotnetPath, "Sdks"); string roslynTargetsPath = Path.Combine(dotnetPath, "Roslyn"); if (!BuildEnvironment.IsRunningOnCore) { // If the host is .NET Framework, we need to try and set the MSBuild path to the framework if we can // See https://github.com/Microsoft/msbuild/issues/3510#issuecomment-404671740 if (GetFrameworkMsBuildExePath(out string frameworkMsBuildExePath)) { msBuildExePath = frameworkMsBuildExePath; // Also need to set the Roslyn and Extensions paths to match string toolsPath = Path.GetDirectoryName(msBuildExePath); roslynTargetsPath = Path.Combine(toolsPath, "Roslyn"); extensionsPath = Path.GetFullPath(Path.Combine(toolsPath, @"..\..\")); } } // Required to find and import the Restore target additionalGlobalProperties.Add(MsBuildProperties.NuGetRestoreTargets, $@"{ dotnetPath }\NuGet.targets"); return(new BuildEnvironment( options.DesignTime, targets.ToArray(), msBuildExePath, extensionsPath, sdksPath, roslynTargetsPath, additionalGlobalProperties, options.EnvironmentVariables)); }
private BuildEnvironment CreateFrameworkEnvironment(EnvironmentOptions options) { // Clone the options global properties dictionary so we can add to it Dictionary <string, string> additionalGlobalProperties = options.GlobalProperties.ToDictionary(x => x.Key, x => x.Value); // Tweak targets List <string> targets = new List <string>(options.TargetsToBuild); if (targets.Contains("Restore", StringComparer.OrdinalIgnoreCase) && _projectFile.Virtual) { // NuGet.Targets can't handle virtual project files: // C:\Program Files\dotnet\sdk\2.1.300\NuGet.targets(239,5): error MSB3202: The project file "E:\Code\...\...csproj" was not found. targets.RemoveAll(x => x.Equals("Restore", StringComparison.OrdinalIgnoreCase)); } if (targets.Contains("Clean", StringComparer.OrdinalIgnoreCase)) { // Required to force CoreCompile target when it calculates everything is already built // This can happen if the file wasn't previously generated (Clean only cleans what was in that file) additionalGlobalProperties.Add(MsBuildProperties.NonExistentFile, @"__NonExistentSubDir__\__NonExistentFile__"); } // Get paths if (!GetFrameworkMsBuildExePath(out string msBuildExePath)) { throw new InvalidOperationException("Could not locate the tools (msbuild.exe) path"); } string toolsPath = Path.GetDirectoryName(msBuildExePath); string extensionsPath = Path.GetFullPath(Path.Combine(toolsPath, @"..\..\")); string sdksPath = Path.Combine(_projectFile.UsesSdk ? DotnetPathResolver.ResolvePath(_projectFile.Path) : extensionsPath, "Sdks"); string roslynTargetsPath = Path.Combine(toolsPath, "Roslyn"); // Need to set directories for default code analysis rulset (see https://github.com/dotnet/roslyn/issues/6774) string vsRoot = Path.Combine(extensionsPath, @"..\"); additionalGlobalProperties.Add(MsBuildProperties.CodeAnalysisRuleDirectories, Path.GetFullPath(Path.Combine(vsRoot, @"Team Tools\Static Analysis Tools\FxCop\\Rules"))); additionalGlobalProperties.Add(MsBuildProperties.CodeAnalysisRuleSetDirectories, Path.GetFullPath(Path.Combine(vsRoot, @"Team Tools\Static Analysis Tools\\Rule Sets"))); // This is required to trigger NuGet package resolution and regeneration of project.assets.json additionalGlobalProperties.Add(MsBuildProperties.ResolveNuGetPackages, "true"); // Required to find and import the Restore target additionalGlobalProperties.Add(MsBuildProperties.NuGetRestoreTargets, $@"{ toolsPath }\..\..\..\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets"); return(new BuildEnvironment( options.DesignTime, targets.ToArray(), msBuildExePath, extensionsPath, sdksPath, roslynTargetsPath, additionalGlobalProperties, options.EnvironmentVariables)); }