'dotnet --version gives "the folder [/usr/local/share/dotnet/host/fxr] does not contain any version-numbered child folders"

When I run dotnet --version in Terminal on my Mac it gives an error:

A fatal error occurred, the folder [/usr/local/share/dotnet/host/fxr] does not contain any version-numbered child folders

It can find the dotnet executable, but --version is not working:

anasmahdi@Anass-Air ~ % dotnet

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.
anasmahdi@Anass-Air ~ % dotnet --version
A fatal error occurred, the folder [/usr/local/share/dotnet/host/fxr] does not contain any version-numbered child folders
anasmahdi@Anass-Air ~ % 


Solution 1:[1]

I had this problem too, on an M1 Mac. Because /usr/local/share/dotnet/ was inside my $PATH (I still don't know why), it was looking at the wrong dotnet executable:

enter image description here

After running

sudo ln -s /usr/local/share/dotnet/x64/dotnet /usr/local/bin/

as described here and restarting the Terminal, the problem went away.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel