Skip to content

Commit

Permalink
Make sure process parameters are correctly quoted
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Sep 27, 2024
1 parent 60e6cba commit 25ad975
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,7 @@ await File.WriteAllTextAsync(projectPath, """
</Project>
""");
var executableName = Path.Join(Path.GetDirectoryName(GetType().Assembly.Location), "NuGetUpdater.Cli.dll");
var executableArgs = string.Join(" ",
[
IEnumerable<string> executableArgs = [
executableName,
"update",
"--repo-root",
Expand All @@ -367,7 +366,7 @@ await File.WriteAllTextAsync(projectPath, """
"--previous-version",
"7.0.1",
"--verbose"
]);
];

// verify base run
var workingDirectory = tempDir.DirectoryPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private static byte[] CreateAssembly(string assemblyName, string assemblyVersion
</Project>
"""
);
var (exitCode, stdout, stderr) = ProcessEx.RunAsync("dotnet", $"msbuild {projectPath} /t:_ReportCurrentSdkVersion").Result;
var (exitCode, stdout, stderr) = ProcessEx.RunAsync("dotnet", ["msbuild", projectPath, "/t:_ReportCurrentSdkVersion"]).Result;
if (exitCode != 0)
{
throw new Exception($"Failed to report the current SDK version:\n{stdout}\n{stderr}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static async Task UpdateLockFileAsync(

await MSBuildHelper.SidelineGlobalJsonAsync(projectDirectory, repoRootPath, async () =>
{
var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", $"restore --force-evaluate {projectPath}", workingDirectory: projectDirectory);
var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", ["restore", "--force-evaluate", projectPath], workingDirectory: projectDirectory);
if (exitCode != 0)
{
logger.Log($" Lock file update failed.\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ await MSBuildHelper.SidelineGlobalJsonAsync(projectDirectory, repoRootPath, asyn
logger.Log($" Adding [{dependencyName}/{newDependencyVersion}] as a top-level package reference.");
// see https://learn.microsoft.com/nuget/consume-packages/install-use-packages-dotnet-cli
var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", $"add {projectPath} package {dependencyName} --version {newDependencyVersion}", workingDirectory: projectDirectory);
var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", ["add", projectPath, "package", dependencyName, "--version", newDependencyVersion], workingDirectory: projectDirectory);
MSBuildHelper.ThrowOnUnauthenticatedFeed(stdout);
if (exitCode != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ internal static async Task<bool> DependenciesAreCoherentAsync(string repoRoot, s
try
{
var tempProjectPath = await CreateTempProjectAsync(tempDirectory, repoRoot, projectPath, targetFramework, packages);
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", $"restore \"{tempProjectPath}\"", workingDirectory: tempDirectory.FullName);
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", ["restore", tempProjectPath], workingDirectory: tempDirectory.FullName);

// NU1608: Detected package version outside of dependency constraint

Expand Down Expand Up @@ -359,7 +359,7 @@ internal static bool UseNewDependencySolver()
try
{
string tempProjectPath = await CreateTempProjectAsync(tempDirectory, repoRoot, projectPath, targetFramework, packages);
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", $"restore \"{tempProjectPath}\"", workingDirectory: tempDirectory.FullName);
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", ["restore", tempProjectPath], workingDirectory: tempDirectory.FullName);

// Add Dependency[] packages to List<PackageToUpdate> existingPackages
List<PackageToUpdate> existingPackages = packages
Expand Down Expand Up @@ -515,7 +515,7 @@ internal static bool UseNewDependencySolver()
try
{
var tempProjectPath = await CreateTempProjectAsync(tempDirectory, repoRoot, projectPath, targetFramework, packages);
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", $"restore \"{tempProjectPath}\"", workingDirectory: tempDirectory.FullName);
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", ["restore", tempProjectPath], workingDirectory: tempDirectory.FullName);
ThrowOnUnauthenticatedFeed(stdOut);

// simple cases first
Expand All @@ -540,7 +540,7 @@ internal static bool UseNewDependencySolver()
foreach ((string PackageName, NuGetVersion packageVersion) in badPackagesAndVersions)
{
// this command dumps a JSON object with all versions of the specified package from all package sources
(exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", $"package search {PackageName} --exact-match --format json", workingDirectory: tempDirectory.FullName);
(exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", ["package", "search", PackageName, "--exact-match", "--format", "json"], workingDirectory: tempDirectory.FullName);
if (exitCode != 0)
{
continue;
Expand Down Expand Up @@ -770,7 +770,7 @@ internal static async Task<Dependency[]> GetAllPackageDependenciesAsync(
var topLevelPackagesNames = packages.Select(p => p.Name).ToHashSet(StringComparer.OrdinalIgnoreCase);
var tempProjectPath = await CreateTempProjectAsync(tempDirectory, repoRoot, projectPath, targetFramework, packages);

var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", $"build \"{tempProjectPath}\" /t:_ReportDependencies", workingDirectory: tempDirectory.FullName);
var (exitCode, stdout, stderr) = await ProcessEx.RunAsync("dotnet", ["build", tempProjectPath, "/t:_ReportDependencies"], workingDirectory: tempDirectory.FullName);
ThrowOnUnauthenticatedFeed(stdout);

if (exitCode == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static async Task<bool> DownloadNuGetPackagesAsync(string repoRoot, str
try
{
var tempProjectPath = await MSBuildHelper.CreateTempProjectAsync(tempDirectory, repoRoot, projectPath, "netstandard2.0", packages, usePackageDownload: true);
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", $"restore \"{tempProjectPath}\"");
var (exitCode, stdOut, stdErr) = await ProcessEx.RunAsync("dotnet", ["restore", tempProjectPath]);

return exitCode == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ namespace NuGetUpdater.Core;

public static class ProcessEx
{
public static Task<(int ExitCode, string Output, string Error)> RunAsync(string fileName, string arguments = "", string? workingDirectory = null)
public static Task<(int ExitCode, string Output, string Error)> RunAsync(string fileName, IEnumerable<string>? arguments = null, string? workingDirectory = null)
{
var tcs = new TaskCompletionSource<(int, string, string)>();

var redirectInitiated = new ManualResetEventSlim();
var process = new Process
{
StartInfo =
StartInfo = new ProcessStartInfo(fileName, arguments ?? Array.Empty<string>())
{
FileName = fileName,
Arguments = arguments,
UseShellExecute = false, // required to redirect output
RedirectStandardOutput = true,
RedirectStandardError = true,
Expand Down

0 comments on commit 25ad975

Please sign in to comment.