1- using System ;
2- using System . Collections . Generic ;
3- using System . IO ;
4- using System . Linq ;
1+ using System . IO ;
52using System . Runtime . InteropServices ;
63using Buildalyzer . Construction ;
74using Microsoft . Build . Utilities ;
@@ -23,19 +20,19 @@ internal EnvironmentFactory(IAnalyzerManager manager, IProjectFile projectFile)
2320 _logger = _manager . LoggerFactory ? . CreateLogger < EnvironmentFactory > ( ) ;
2421 }
2522
26- public BuildEnvironment GetBuildEnvironment ( ) =>
23+ public BuildEnvironment ? GetBuildEnvironment ( ) =>
2724 GetBuildEnvironment ( null , null ) ;
2825
29- public BuildEnvironment GetBuildEnvironment ( string targetFramework ) =>
26+ public BuildEnvironment ? GetBuildEnvironment ( string ? targetFramework ) =>
3027 GetBuildEnvironment ( targetFramework , null ) ;
3128
32- public BuildEnvironment GetBuildEnvironment ( EnvironmentOptions options ) =>
29+ public BuildEnvironment ? GetBuildEnvironment ( EnvironmentOptions ? options ) =>
3330 GetBuildEnvironment ( null , options ) ;
3431
35- public BuildEnvironment GetBuildEnvironment ( string targetFramework , EnvironmentOptions options )
32+ public BuildEnvironment ? GetBuildEnvironment ( string ? targetFramework , EnvironmentOptions ? options )
3633 {
3734 options ??= new EnvironmentOptions ( ) ;
38- BuildEnvironment buildEnvironment ;
35+ BuildEnvironment ? buildEnvironment ;
3936
4037 // Use the .NET Framework if that's the preference
4138 // ...or if this project file uses a target known to require .NET Framework
@@ -57,18 +54,20 @@ public BuildEnvironment GetBuildEnvironment(string targetFramework, EnvironmentO
5754
5855 // Based on code from OmniSharp
5956 // https://github.com/OmniSharp/omnisharp-roslyn/blob/78ccc8b4376c73da282a600ac6fb10fce8620b52/src/OmniSharp.Abstractions/Services/DotNetCliService.cs
60- private BuildEnvironment CreateCoreEnvironment ( EnvironmentOptions options )
57+ private BuildEnvironment ? CreateCoreEnvironment ( EnvironmentOptions options )
6158 {
6259 // Get paths
63- DotnetPathResolver pathResolver = new DotnetPathResolver ( _manager . LoggerFactory ) ;
64- string dotnetPath = pathResolver . ResolvePath ( _projectFile . Path , options . DotnetExePath ) ;
65- if ( dotnetPath == null )
60+ var resolver = new DotNetInfoResolver ( _manager . LoggerFactory ) ;
61+ var info = resolver . Resolve ( IO . IOPath . Parse ( _projectFile . Path ) , IO . IOPath . Parse ( options . DotnetExePath ) ) ;
62+
63+ if ( ( info . BasePath ?? info . Runtimes . Values . FirstOrDefault ( ) ) is not { } dotnetPath )
6664 {
65+ _logger ? . LogWarning ( "Could not locate SDK path in `{DotnetPath} --info` results" , options . DotnetExePath ) ;
6766 return null ;
6867 }
6968
7069 string msBuildExePath = Path . Combine ( dotnetPath , "MSBuild.dll" ) ;
71- if ( options != null && options . EnvironmentVariables . ContainsKey ( EnvironmentVariables . MSBUILD_EXE_PATH ) )
70+ if ( options . EnvironmentVariables . ContainsKey ( EnvironmentVariables . MSBUILD_EXE_PATH ) )
7271 {
7372 msBuildExePath = options . EnvironmentVariables [ EnvironmentVariables . MSBUILD_EXE_PATH ] ;
7473 }
@@ -121,7 +120,7 @@ private BuildEnvironment CreateCoreEnvironment(EnvironmentOptions options)
121120 options . WorkingDirectory ) ;
122121 }
123122
124- private BuildEnvironment CreateFrameworkEnvironment ( EnvironmentOptions options )
123+ private BuildEnvironment ? CreateFrameworkEnvironment ( EnvironmentOptions options )
125124 {
126125 // Clone the options global properties dictionary so we can add to it
127126 Dictionary < string , string > additionalGlobalProperties = new Dictionary < string , string > ( options . GlobalProperties ) ;
@@ -135,7 +134,7 @@ private BuildEnvironment CreateFrameworkEnvironment(EnvironmentOptions options)
135134 }
136135
137136 string msBuildExePath ;
138- if ( options != null && options . EnvironmentVariables . ContainsKey ( EnvironmentVariables . MSBUILD_EXE_PATH ) )
137+ if ( options . EnvironmentVariables . ContainsKey ( EnvironmentVariables . MSBUILD_EXE_PATH ) )
139138 {
140139 msBuildExePath = options . EnvironmentVariables [ EnvironmentVariables . MSBUILD_EXE_PATH ] ;
141140 }
@@ -194,7 +193,7 @@ private bool GetFrameworkMsBuildExePath(out string msBuildExePath)
194193 return ! string . IsNullOrEmpty ( msBuildExePath ) ;
195194 }
196195
197- private bool OnlyTargetsFramework ( string targetFramework )
196+ private bool OnlyTargetsFramework ( string ? targetFramework )
198197 => targetFramework == null
199198 ? _projectFile . TargetFrameworks . TrueForAll ( IsFrameworkTargetFramework )
200199 : IsFrameworkTargetFramework ( targetFramework ) ;
0 commit comments