sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Running dotnet sdk command from unit tests is not possible without using full path

Open pakrym opened this issue 10 years ago • 17 comments

Steps to reproduce

  1. Create unit test project
  2. Add following code into test
 var startInfo = new ProcessStartInfo
                    {
                        FileName = "dotnet",
                        Arguments = "--version",
                        UseShellExecute = false,
                        CreateNoWindow = true,
                        RedirectStandardError = true,
                        RedirectStandardOutput = true,
                        WorkingDirectory =  AppContext.BaseDirectory
                    };
                    var hostProcess = new Process() { StartInfo = startInfo };
                    hostProcess.ErrorDataReceived += (sender, dataArgs) => { Console.WriteLine(dataArgs.Data ?? string.Empty); };
                    hostProcess.OutputDataReceived += (sender, dataArgs) => { Console.WriteLine(dataArgs.Data ?? string.Empty); };
                    hostProcess.Start();
                    Console.WriteLine(hostProcess.MainModule.FileName);
                    hostProcess.BeginErrorReadLine();
                    hostProcess.BeginOutputReadLine();
                    hostProcess.WaitForExit();
  1. dotnet test

Expected behavior

Current dotnet version gets displayed along with muxer dotnet.exe path.

Actual behavior

dotnet.exe from shared runtime directory is resolved and it throws an error.

C:\Users\pakrym\AppData\Local\Microsoft\dotnet\shared\Microsoft.NETCore.App\1.0.0-rc2-23929\dotnet.exe
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly '--version' or one of its dependencies. The system cannot find the file specified.

Cause

When dotnet test runs it uses corehost from shared host directory to run xunit runner, and when test tries to start dotnet publish dotnet.exe gets resolved to one inside shared runtime folder because of rules below.

Resolution order:
    The directory from which the application loaded.
    The current directory for the parent process.
    The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
    The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
    The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
    The directories that are listed in the PATH environment variable. Note that this function does not search the per-application path specified by the App Paths registry key. To include this per-application path in the search sequence, use the ShellExecute function.

Environment data

dotnet --version output:

1.0.0-beta-002115

pakrym avatar Mar 30 '16 19:03 pakrym

/cc @livarcocc @brthor @piotrpMSFT @pranavkm @victorhurdugaci @davidfowl

pakrym avatar Mar 30 '16 19:03 pakrym

Is the solution here to search the path explicitly for dotnet.exe instead of relying on Process.Start resolution rules?

brthor avatar Mar 30 '16 19:03 brthor

No, I think we should use muxer for running tests, not corehost.

pakrym avatar Mar 30 '16 19:03 pakrym

@pakrym Why is this marked as blocking-partner? What is the scenario that you need that you can't run without this fix?

livarcocc avatar Mar 30 '16 19:03 livarcocc

ASP.NET cli gate runs dotnet publish in it's test, and so do many of other ASP.NET projects. We have temporary workaround for gate.

pakrym avatar Mar 30 '16 19:03 pakrym

Corehost running the tests is Point-In-Time issue, I don't understand how that is breaking this scenario though?

brthor avatar Mar 30 '16 19:03 brthor

because process start resolves 'dotnet' to 'dotnet.exe' next to coreshost.exe in shared runtime folder instead of muxer

pakrym avatar Mar 30 '16 19:03 pakrym

I don't love this solution. If we're saying that the Muxer should always win then we shouldn't call the thing in the Shared FX dir dotnet.exe. We should call it dotnet.{version}.exe so there is no opportunity for ambiguity.

TheRealPiotrP avatar Mar 30 '16 20:03 TheRealPiotrP

@schellap @gkhanna79 any concern with renaming the dotnet.exe that gets installed in Shared FX Dir?

TheRealPiotrP avatar Mar 30 '16 20:03 TheRealPiotrP

@krwq can you pick this issue up?

TheRealPiotrP avatar Mar 31 '16 06:03 TheRealPiotrP

I believe changing the name of dotnet.exe in the shared FX is not the right solution. It will break the scenario of allowing folks to run dotnet directly from the SharedFX folder.

Also, what is this "corehost.exe" thing and is it in SharedFX (and if so, why)?

Is this problem happening because muxer (i.e. dotnet.exe) in the SharedFX location does not respect "--version"? @schellap is that what you expect?

gkhanna79 avatar Mar 31 '16 14:03 gkhanna79

No I believe the ask is Muxer should print own version with say --hostversion. @piotrpMSFT?

schellap avatar Mar 31 '16 16:03 schellap

--version is irrelevant in this issue. Issues is that when running via corehost.exe from runtime folder dotnet command name get resolved to runtimes dotnet.exe even if muxer is the only one on the path.

pakrym avatar Mar 31 '16 17:03 pakrym

The XUnit runner is using "corehost" in the shared fx directory to start the xunit runner. This is a separate but related issue that I'll be working on.

Although this issue exists I don't believe it is the same as the issue you are presenting @pakrym. That issue is that anytime you are running an app from shared fx directory (using "corehost"), Process.Start will default to the present dotnet.exe instead of the muxer.

The proper fix here should be to search the path for dotnet.exe the same way we do in the cli Utils instead of relying on the results of a raw Process.Start invocation to find the muxer for you.

brthor avatar Mar 31 '16 21:03 brthor

@eerhardt can you take a look? We need to close on a design. Ask @brthor to send you context.

TheRealPiotrP avatar Apr 05 '16 07:04 TheRealPiotrP

Chatted with @muratg. We're not going to block rc2 on this, but will continue treating it with priority.

TheRealPiotrP avatar Apr 06 '16 22:04 TheRealPiotrP

If wanting to use the muxer that spawned the current CLR, please use the muxer class to retrieve that paht.

TheRealPiotrP avatar Jan 19 '17 09:01 TheRealPiotrP