Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use framework API to resolve symlink.
  • Loading branch information
teo-tsirpanis committed Aug 22, 2025
commit 7692e5c0125651c4a04d00e6a6040ce9075ec10c
17 changes: 2 additions & 15 deletions src/MSBuildLocator/DotNetSdkLocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private static IList<string> ResolveDotnetPathCandidates()
{
if (!IsWindows)
{
hostPath = realpath(hostPath) ?? hostPath;
hostPath = File.ResolveLinkTarget(hostPath, true)?.FullName ?? hostPath;
}

AddIfValid(Path.GetDirectoryName(hostPath));
Expand Down Expand Up @@ -314,19 +314,6 @@ void AddIfValid(string? path)
return dotnetPath;
}

/// <summary>
/// This native method call determines the actual location of path, including
/// resolving symbolic links.
/// </summary>
private static string? realpath(string path)
{
IntPtr ptr = NativeMethods.realpath(path, IntPtr.Zero);
string? result = Marshal.PtrToStringAuto(ptr);
NativeMethods.free(ptr);

return result;
}

private static string? FindDotnetPathFromEnvVariable(string environmentVariable)
{
string? dotnetPath = Environment.GetEnvironmentVariable(environmentVariable);
Expand All @@ -349,7 +336,7 @@ private static void SetEnvironmentVariableIfEmpty(string name, string value)
{
if (!IsWindows)
{
fullPathToDotnetFromRoot = realpath(fullPathToDotnetFromRoot) ?? fullPathToDotnetFromRoot;
fullPathToDotnetFromRoot = File.ResolveLinkTarget(fullPathToDotnetFromRoot, true)?.FullName ?? fullPathToDotnetFromRoot;
return File.Exists(fullPathToDotnetFromRoot) ? Path.GetDirectoryName(fullPathToDotnetFromRoot) : null;
}

Expand Down
6 changes: 0 additions & 6 deletions src/MSBuildLocator/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,5 @@ internal static extern int hostfxr_resolve_sdk2(

[DllImport(HostFxrName, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern int hostfxr_get_available_sdks(string exe_dir, hostfxr_get_available_sdks_result_fn result);

[DllImport("libc", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr realpath(string path, IntPtr buffer);

[DllImport("libc", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
internal static extern void free(IntPtr ptr);
}
}