macOS dotnet CLI path for global tools is bad - Installer issue #23165
Description
Describe the bug
On macOS when a .Net tool is installed globally, it will not be found in the PATH.
The dotnet installer does attempt to make the directory for globally installed tools part of the PATH, but the mechanism the installer is relying on doesn't work as the installer apparently expects.
To Reproduce
- Install dotnet on macOS using the Microsoft Installer from https://dotnet.microsoft.com/en-us/download.
- Install a tool globally, e.g.
dotnet tool install -g Microsoft.dotnet-httprepl
- Assuming zsh which is the default shell on macOS (Catalina and newer), run the command
which httprepl
- The output of the which command will be "httprepl not found" indicating that the httprepl tool can’t be found in the PATH.
Further technical details
Why does this happen?
macOS has a mechanism for adding paths to the PATH environment variable. The files in /etc/paths.d are used by the path_helper tool to build up the PATH environment variable.
The installer for dotnet is adding two files for paths:
- /etc/paths.d/dotnet
- /etc/paths.d/dotnet-cli-tools
The dotnet file contains
/usr/local/share/dotnet
The dotnet-cli-tools file contains
~/.dotnet/tools
However, paths in these files should be literal. The tilde (~) is not expanded to be the current user home directory. Environment variable references are also not expanded.
bash
The default standard shell on macOS Catalina (released in 2019) and newer is zsh. Prior to Catalina the default standard shell was bash. The bash shell appears to expand the tilde at a later point for an interactive shell so this issue may not manifest for bash users. However, the files created in /etc/paths.d by the dotnet installer need to support all shells.
Can't Specify the Current User Home Directory
The tilde can't be expected to be expanded to be the current user home directory. ${HOME} also can’t be used. There is not a way to specify the current user home directory in a file in /etc/paths.d.
I don’t know if not supporting the current user home directory is by design or is an omission.
Given the current functionality, the dotnet installer should not create the /etc/paths.d/dotnet-cli-tools file at all.
Workaround for Individual Users
In the .profile file, the user can either add the following line to add the needed path:
# dotnet-cli-tools
export PATH=$PATH:~/.dotnet/tools
or, assuming the bad path exists in the PATH, add the following ‘fix-up’ to replace any tildes in the PATH (equivalent to bash):
export PATH=${PATH//\~/$HOME}
The .zprofile may be set to source the .profile file with the following line:
[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'