Skip to content

Commit c783280

Browse files
committed
Update EnvironmentProvider.cs
1 parent 4d37830 commit c783280

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/Resolvers/Microsoft.DotNet.NativeWrapper/EnvironmentProvider.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,33 @@ private IEnumerable<string> SearchPaths
7575
{
7676
// e.g. on Linux the 'dotnet' command from PATH is a symlink so we need to
7777
// resolve it to get the actual path to the binary
78-
dotnetExeFromPath = GetRealPath(dotnetExeFromPath) ?? dotnetExeFromPath;
78+
dotnetExeFromPath = GetRealPath(dotnetExeFromPath);
7979

80-
static string? GetRealPath(string path)
80+
static string GetRealPath(string path)
8181
{
82-
string fullPath = Path.GetFullPath(path);
83-
if (!File.Exists(fullPath))
84-
return null;
85-
86-
FileInfo info = new(fullPath);
87-
if (info.LinkTarget != null)
82+
FileInfo fileInfo = new(path);
83+
if (fileInfo.LinkTarget != null)
8884
{
89-
var resolvedTarget = info.ResolveLinkTarget(returnFinalTarget: true);
90-
if (resolvedTarget != null)
91-
return resolvedTarget.FullName;
85+
var resolved = fileInfo.ResolveLinkTarget(true);
86+
return resolved?.Exists is true ? resolved.FullName : path;
9287
}
93-
94-
return fullPath;
88+
89+
string invariantPart = string.Empty;
90+
DirectoryInfo? parentDirectory = fileInfo.Directory;
91+
while (parentDirectory is not null)
92+
{
93+
invariantPart = path[parentDirectory.FullName.Length..];
94+
if (parentDirectory.LinkTarget != null)
95+
{
96+
var resolved = parentDirectory.ResolveLinkTarget(true);
97+
if (resolved?.Exists is true)
98+
return Path.Join(resolved.FullName, invariantPart);
99+
}
100+
101+
parentDirectory = parentDirectory.Parent;
102+
}
103+
104+
return path;
95105
}
96106
}
97107
#endif

0 commit comments

Comments
 (0)