Prova di costruisci.sh con Git Bash su Windows, mai col lanciatore di WSL
Dal PATH di PowerShell il primo bash è C:\Windows\System32\bash.exe, il lanciatore di WSL: senza distribuzione fallisce («execvpe(/bin/bash) failed») e la prova falliva con lui. Su Windows ora si cerca prima Git Bash (accanto a git sul PATH, poi nelle cartelle del suo installatore), poi un bash sul PATH che non stia nella cartella di Windows né fra gli alias di WindowsApps; senza bash la prova è inconcludente invece di passare in silenzio. Su Linux si usa come prima il primo bash sul PATH. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f7de7f5642
commit
815bd44a62
2 files changed
+56
-7
No files matched your search
@@ -112,10 +112,10 @@ public sealed partial class InstallerTests
|
||||
StringAssert.Contains(text, "100 * 1024 * 1024");
|
||||
Assert.DoesNotContain("\r\n", text, "Unix line ends: bash would choke on CR");
|
||||
|
||||
var bash = FindOnPath("bash");
|
||||
var bash = FindBash();
|
||||
if (bash is null)
|
||||
{
|
||||
return;
|
||||
Assert.Inconclusive("Manca bash (su Windows: Git Bash): la prova della sintassi di costruisci.sh si salta.");
|
||||
}
|
||||
|
||||
using var process = Process.Start(new ProcessStartInfo(bash, ["-n", script]) { RedirectStandardError = true })!;
|
||||
@@ -124,21 +124,69 @@ public sealed partial class InstallerTests
|
||||
Assert.AreEqual(0, process.ExitCode, errors);
|
||||
}
|
||||
|
||||
private static string? FindOnPath(string name)
|
||||
private static string? FindOnPath(string name) => OnPath(name).FirstOrDefault();
|
||||
|
||||
/// <summary>
|
||||
/// A bash that checks the script on this system. Elsewhere than Windows, the first on PATH. On Windows Git Bash
|
||||
/// first (next to git on PATH, then where its installer puts it), else any bash on PATH except the WSL launcher:
|
||||
/// <c>System32\bash.exe</c> and the <c>WindowsApps</c> alias start a Linux distribution, which may be missing
|
||||
/// («execvpe(/bin/bash) failed») and does not see the script by its Windows path.
|
||||
/// </summary>
|
||||
private static string? FindBash()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
return FindOnPath("bash");
|
||||
}
|
||||
|
||||
var roots = new List<string>();
|
||||
if (FindOnPath("git") is { } git)
|
||||
{
|
||||
// git.exe sits in <Git>\cmd, <Git>\bin or <Git>\mingw64\bin.
|
||||
for (var folder = Path.GetDirectoryName(git); folder is not null && roots.Count < 3; folder = Path.GetDirectoryName(folder))
|
||||
{
|
||||
roots.Add(folder);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var variable in new[] { "ProgramW6432", "ProgramFiles", "ProgramFiles(x86)" })
|
||||
{
|
||||
if (Environment.GetEnvironmentVariable(variable) is { Length: > 0 } programs)
|
||||
{
|
||||
roots.Add(Path.Combine(programs, "Git"));
|
||||
}
|
||||
}
|
||||
|
||||
roots.Add(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs", "Git"));
|
||||
var gitBash = roots
|
||||
.SelectMany(root => new[] { Path.Combine(root, "bin", "bash.exe"), Path.Combine(root, "usr", "bin", "bash.exe") })
|
||||
.FirstOrDefault(path => File.Exists(path) && !IsWslLauncher(path));
|
||||
return gitBash ?? OnPath("bash").FirstOrDefault(path => !IsWslLauncher(path));
|
||||
}
|
||||
|
||||
private static bool IsWslLauncher(string path)
|
||||
{
|
||||
var full = Path.GetFullPath(path);
|
||||
var windows = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
|
||||
return (windows.Length > 0 && full.StartsWith(Path.TrimEndingDirectorySeparator(windows) + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
|
||||
|| full.Contains(@"\Microsoft\WindowsApps\", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>Every file called <paramref name="name"/> on PATH, in order (on Windows only <c>.exe</c>: the others do not start).</summary>
|
||||
private static IEnumerable<string> OnPath(string name)
|
||||
{
|
||||
var candidates = OperatingSystem.IsWindows() ? new[] { name + ".exe" } : new[] { name, name + ".exe" };
|
||||
foreach (var folder in (Environment.GetEnvironmentVariable("PATH") ?? string.Empty).Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
foreach (var candidate in new[] { name, name + ".exe" })
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
var path = Path.Combine(folder, candidate);
|
||||
if (File.Exists(path))
|
||||
{
|
||||
return path;
|
||||
yield return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"^\s*(?:Delete|RMDir)(?:\s+/\w+)*\s+""(?<target>[^""]+)""", RegexOptions.Multiline)]
|
||||
|
||||
Reference in new issue
Block a user