Running dotnet sdk command from unit tests is not possible without using full path
Steps to reproduce
- Create unit test project
- 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();
- 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
/cc @livarcocc @brthor @piotrpMSFT @pranavkm @victorhurdugaci @davidfowl
Is the solution here to search the path explicitly for dotnet.exe instead of relying on Process.Start resolution rules?
No, I think we should use muxer for running tests, not corehost.
@pakrym Why is this marked as blocking-partner? What is the scenario that you need that you can't run without this fix?
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.
Corehost running the tests is Point-In-Time issue, I don't understand how that is breaking this scenario though?
because process start resolves 'dotnet' to 'dotnet.exe' next to coreshost.exe in shared runtime folder instead of muxer
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.
@schellap @gkhanna79 any concern with renaming the dotnet.exe that gets installed in Shared FX Dir?
@krwq can you pick this issue up?
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?
No I believe the ask is Muxer should print own version with say --hostversion. @piotrpMSFT?
--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.
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.
@eerhardt can you take a look? We need to close on a design. Ask @brthor to send you context.
Chatted with @muratg. We're not going to block rc2 on this, but will continue treating it with priority.
If wanting to use the muxer that spawned the current CLR, please use the muxer class to retrieve that paht.