Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Command Line App | Process.Start() runs on some machines but not others

Background

I am writing a .NET Core Command Line Application as a CLI for my build system. This part of the build system involves generating a NuGet package from a class library. I am using ProcessStartInfo.cs and Process.cs to make a call to nuget.exe to issue the pack command (nuget.exe location is in the system PATH).

  • NOTE: I cannot use dotnet CLI for the packaging as the class library is not a .NET Core project, so please do not say "Why don't you just use dotnet pack.

Stack

  • C# .NET Core (My CLI)
  • C# .NET 4.5 (Class Library)
  • Thoughtworks GoCD (Build Server)
  • Windows Server 2016 (Build Server OS)
  • Windows 10 (Local Machine OS)

Problem

The issue I am facing is that on my personal machine, when I run my build system CLI, everything works perfectly fine; however, when my build server runs it, the process appears to attempt to start, but then exits without throwing an exception or returning any sort of exit code. Both accounts (Me on my local machine and the account the build server runs under) are local admin accounts.


Code

Cmd.cs (Executes process)

public static class Cmd
{
    public static int Execute(string filename, string arguments)
    {
        var startInfo = new ProcessStartInfo
        {
            CreateNoWindow = true,
            FileName = filename,
            Arguments = arguments,
        };

        using (var process = new Process { StartInfo = startInfo })
        {
            try
            {
                process.Start();
                process.WaitForExit(30000);
                return process.ExitCode;
            }

            catch (Exception exception)
            {
                if (!process.HasExited)
                {
                    process.Kill();
                }

                Console.WriteLine($"Cmd could not execute command {filename} {arguments}:\n{exception.Message}");
                return (int)ExitCode.Exception;
            }
        }
    }
}

Package.cs (uses Cmd.cs)

// Some code before this

// The below three variables are generated/provided and are made up here for show
var version = "1.0.0";
var package = $"MyLib.{version}";
var projectPath = "C:\Some\Made\Up\Path";
///////////

// Real code
var tempPath = $"{Path.Combine(Path.GetTempPath(), "NugetPackages")}";
var packagePath = Path.Combine(tempPath, package);

if (!Directory.Exists(tempPath))
{
    try
    {
        Directory.CreateDirectory(tempPath);
    }

    catch (Exception exception)
    {
        Console.WriteLine($"Could not create directory in user temp file: {exception.Message}");
        return (int) ExitCode.FilePermission;
    }
}

var filename = "nuget.exe";
var arguments = $"pack -Version {version} -OutputDirectory {tempPath} -properties Configuration=Release {projectPath}";
var exitCode = Cmd.Execute(filename, arguments);

if (exitCode != 0)
{
    Console.WriteLine($"Process failed to execute and exited with exit code: {exitCode}");
    return exitCode;
}

Console.WriteLine($"{package} built successfully");

// Some code after this
like image 522
Stephen P. Avatar asked Nov 07 '22 21:11

Stephen P.


1 Answers

I modified my code to display all window output and it appears that the same version of NuGet.exe behaves differently on Windows 10 Pro vs Windows Server 2016... not sure why, but on Windows 10 Pro I can use the -Version tag to replace the $version$ token in the .nuspec, but in Windows Server 2016, I cannot do this and have to use a different token (e.g. $package$) and use the -properties switch to replace $package$ with my version... lame sauce!

So, instead of:

nuget.exe pack -Version 1.0.0 -OutputDirectory C:\Somewhere -properties Configuration=Release MyLib

I have to use

nuget.exe pack -OutputDirectory C:\Somwhere -properties "Configuration=Release;package=1.0.0" MyLib
like image 63
Stephen P. Avatar answered Nov 14 '22 22:11

Stephen P.