You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently I have a build tasks assembly that uses complex logic to try to resolve the currently used version of the .NET SDK but it has a few issues:
if both x86 and x64 .NET SDKs are installed it could randomly select the x86 one when the x64 version should be preferred unless the user is currently using the 64 bit version.
I personally think a way to directly get it from MSBuild is much better for both performance and simplicity just like there is a way to get the current .NET SDK RID (RuntimeIdentifier).
Build tasks targeting netstandard2.0 could benefit from this because RuntimeInformation.RuntimeIdentifier does not exist there for good reason (It does not exist in .NET Framework).
The logic for getting this correct in build tasks is way too complex than need be and has to account not only windows but also MacOS and Linux, and all other systems that might have different paths.
The logic does not handle manual .NET SDK installs in custom locations, vs getting it from MSBuild directly would handle it for free.
As such I am asking here if it is currently possible to get the values that this function would return in a much more accurate way from any existing msbuild properties from the .NET SDK today (Code to this taken from a console application targeting .NET 8, but my build tasks uses a similar logic):
privatestaticstringGetDotNetSdkLocation(stringruntimeIdentifier){varknownDotNetLocations=(OperatingSystem.IsWindows(),OperatingSystem.IsLinux(),OperatingSystem.IsMacOS())switch{(true,false,false)=>[Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),"dotnet","dotnet.exe"),Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),"dotnet","dotnet.exe"),],(false,false,true)=>["/usr/local/share/dotnet/dotnet",],(false,true,false)=>[Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),"share","dotnet","dotnet")],
_ =>Array.Empty<string>(),};varsdkRoot=string.Empty;varenvSdkRoot=Environment.GetEnvironmentVariable("DOTNET_ROOT");if(envSdkRootis not null&&Directory.Exists(envSdkRoot)){sdkRoot=envSdkRoot;}if(string.IsNullOrEmpty(sdkRoot)||!Directory.Exists(sdkRoot)){foreach(varlocinknownDotNetLocations){// for x64/arm64 versions of the .NET SDKif(OperatingSystem.IsWindows()&&(RuntimeInformation.OSArchitecture==Architecture.X64||RuntimeInformation.OSArchitecture==Architecture.Arm64)&&loc.Contains("Program Files (x86)")&&runtimeIdentifier.Equals(RuntimeInformation.RuntimeIdentifier,StringComparison.Ordinal)){// we need the 64 bit sdk location (if installed) instead.varloc2=knownDotNetLocations[Array.IndexOf(knownDotNetLocations,loc)+1];if(File.Exists(loc2)){vardotnet=newFileInfo(loc2);varsdkDir=dotnet.Directory;if(sdkDiris not null){sdkRoot=sdkDir.FullName;}break;}}if(File.Exists(loc)){vardotnet=newFileInfo(loc);varsdkDir=dotnet.Directory;if(sdkDiris not null){sdkRoot=sdkDir.FullName;}break;}}}returnsdkRoot;}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently I have a build tasks assembly that uses complex logic to try to resolve the currently used version of the .NET SDK but it has a few issues:
netstandard2.0
could benefit from this becauseRuntimeInformation.RuntimeIdentifier
does not exist there for good reason (It does not exist in .NET Framework).As such I am asking here if it is currently possible to get the values that this function would return in a much more accurate way from any existing msbuild properties from the .NET SDK today (Code to this taken from a console application targeting .NET 8, but my build tasks uses a similar logic):
Beta Was this translation helpful? Give feedback.
All reactions