Running dotnet sdk command from unit tests is not possible without using full path
See original GitHub issueSteps 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
Issue Analytics
- State:
- Created 7 years ago
- Comments:17 (16 by maintainers)
Top Results From Across the Web
dotnet test command - .NET CLI
The dotnet test command is used to execute unit tests in a given project.
Read more >Visual Studio unable to run .NET Core tests
When you run an SDK command, the SDK uses the latest installed version. dotnet searches for a global.json file interactively reverse-navigating ...
Read more >Unable to locate the .NET SDK: The Reasons - Hamid Mosalla
When you tell to use an absolute path to the correct version of dotnet, you write a command that does not work. It...
Read more >Problems with .NET unit testing
Check a path to dotnet executable via Settings -> Build, Execution, Deployment -> Toolset and Build (find screenshot below); Go to the directory...
Read more >Live unit testing does not work with Source Generators
When I run the live unit tests I receive “Build completed with failures. Errors were logged under {file path}”. The errors in that...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
If wanting to use the muxer that spawned the current CLR, please use the muxer class to retrieve that paht.