question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
TheRealPiotrPcommented, Mar 30, 2016

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.

0reactions
TheRealPiotrPcommented, Jan 19, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found