Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 2a493c1

Browse files
author
William Lee
authored
Support TildeSlash expand (#8589)
1 parent 72d9c0f commit 2a493c1

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Microsoft.DotNet.Cli.Utils/EnvironmentProvider.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class EnvironmentProvider : IEnvironmentProvider
1414
private static char[] s_pathSeparator = new char[] { Path.PathSeparator };
1515
private static char[] s_quote = new char[] { '"' };
1616
private IEnumerable<string> _searchPaths;
17+
private readonly Lazy<string> _userHomeDirectory = new Lazy<string>(() => Environment.GetEnvironmentVariable("HOME") ?? string.Empty);
1718
private IEnumerable<string> _executableExtensions;
1819

1920
public IEnumerable<string> ExecutableExtensions
@@ -45,7 +46,8 @@ private IEnumerable<string> SearchPaths
4546
searchPaths.AddRange(Environment
4647
.GetEnvironmentVariable("PATH")
4748
.Split(s_pathSeparator)
48-
.Select(p => p.Trim(s_quote)));
49+
.Select(p => p.Trim(s_quote))
50+
.Select(p => ExpandTildeSlash(p)));
4951

5052
_searchPaths = searchPaths;
5153
}
@@ -54,6 +56,19 @@ private IEnumerable<string> SearchPaths
5456
}
5557
}
5658

59+
private string ExpandTildeSlash(string path)
60+
{
61+
const string tildeSlash = "~/";
62+
if (path.StartsWith(tildeSlash, StringComparison.Ordinal) && !string.IsNullOrEmpty(_userHomeDirectory.Value))
63+
{
64+
return Path.Combine(_userHomeDirectory.Value, path.Substring(tildeSlash.Length));
65+
}
66+
else
67+
{
68+
return path;
69+
}
70+
}
71+
5772
public EnvironmentProvider(
5873
IEnumerable<string> extensionsOverride = null,
5974
IEnumerable<string> searchPathsOverride = null)

0 commit comments

Comments
 (0)