waf / CSharpRepl

A command line C# REPL with syntax highlighting – explore the language, libraries and nuget packages interactively.
https://fuqua.io/CSharpRepl/
Mozilla Public License 2.0
2.93k stars 113 forks source link

csharprepl fails with "Could not determine the .NET SDK to use." #245

Closed mardukbp closed 1 year ago

mardukbp commented 1 year ago

Version

0.5.1

What happened?

Executing csharprepl.exe fails to find the .NET SDK despite of the following facts:

dotnet --list-sdks
7.0.203 [E:\dotnet7-sdk-x64\sdk]
 dotnet --list-runtimes
Microsoft.AspNetCore.App 7.0.5 [E:\dotnet7-sdk-x64\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 7.0.5 [E:\dotnet7-sdk-x64\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 7.0.5 [E:\dotnet7-sdk-x64\shared\Microsoft.WindowsDesktop.App]

The culprit is this naive and unreliable way to find the .NET SDK installation: https://github.com/waf/CSharpRepl/blob/5052c9c77ecb87835c02bd90e1514e4512978072/CSharpRepl.Services/Roslyn/References/DotNetInstallationLocator.cs#L32

As shown above, parsing the output of dotnet --list-sdks is a reliable way to find the absolute path to the .NET SDK installation.

Thanks for developing this awesome REPL!

waf commented 1 year ago

Thanks for this, I appreciate the high-quality bug report.

Yeah, the installation logic really needs to be improved, either using the dotnet command line or https://github.com/microsoft/MSBuildLocator

waf commented 1 year ago

@mardukbp I'm looking into this -- mind sharing the full exception message? It should have some debugging information that's useful. I'm expecting the exception message to mention where it tried looking and some fallback information.

I've pushed a branch here https://github.com/waf/CSharpRepl/pull/250 that might help out with this, but I don't want to merge it until I fully understand how it's failing. Thanks!

mardukbp commented 1 year ago

I did some experiments and have good news :) It turns out that if the environment variable DOTNET_ROOT is defined and points to the .NET 7 SDK and in addition the SDK is in the PATH, then csharprepl.exe finds the SDK!

If DOTNET_ROOT is correctly set but the SDK is not in the PATH, the following error is thrown at startup:

A compatible .NET SDK was not found.

Requested SDK version: 7.0.105
global.json file: C:\Users\mardu\global.json

Installed SDKs:
No .NET SDKs were found.

Install the [7.0.105] .NET SDK or update [C:\Users\mardu\global.json] to match an installed SDK.

Download a .NET SDK:
https://aka.ms/dotnet/download

Learn about SDK resolution:
https://aka.ms/dotnet/sdk-not-found
One or more errors occurred. (Failed to find an appropriate version of .NET Core MSBuild. Call to hostfxr_resolve_sdk2 failed. There may be more details in stderr.)

If the SDK is in the PATH and DOTNET_ROOT is not defined then one gets the following error message:

You must install .NET to run this application.

App: C:\Users\mardu\.dotnet\tools\csharprepl.exe
Architecture: x64
App host version: 7.0.5
.NET location: Not found

Learn about runtime installation:
https://aka.ms/dotnet/app-launch-failed

Download the .NET runtime:
https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=win10-x64&apphost_version=7.0.5

Hope that helps :)

waf commented 1 year ago

That's great info, thanks! I'll set up my environment similarly and play around with this too. Thanks for your investigation.

mardukbp commented 4 months ago

@waf This issue has not been solved. Just tried the latest version 0.6.7+d2955f13e74.