Dotnet tool not executing properly, if I manually log into the agent machine and execute it as the agent it works fine
Right now my project has 2 dotnet runner steps, step one is to install the sonnarscanner and step two is to run it
1) tool update --global dotnet-sonarscanner
2) sonarscanner begin /k:"%SONAR_PROJECT_KEY%" /d:"sonar.host.url=%SONAR_HOST%" /d:sonar.login="%env.SONAR_TOKEN%" /o:"%SONAR_ORG%" /d:"sonar.branch.name=%teamcity.build.branch%"
I am getting the following error on my teamcity build agent. However, when I log into the agent computer and run the command with the same user the agent is running under it works
Starting: /usr/share/dotnet/dotnet sonarscanner begin /k:"abc" /d:"sonar.host.url=https://sonarcloud.io" /d:sonar.login="aaa" /o:"abc" /d:"sonar.branch.name=master"
00:40:14 Starting: sonarscanner begin /k:"abc" /d:"sonar.host.url=https://sonarcloud.io" /d:sonar.login="aaa" /o:"abc" /d:"sonar.branch.name=master"
00:40:14 in directory: /opt/teamcity-agent/work/fb3ac1e67c1f4efe
00:40:14 Could not execute because the specified command or file was not found.
00:40:14 Possible reasons for this include:
00:40:14 * You misspelled a built-in dotnet command.
00:40:14 * You intended to execute a .NET program, but dotnet-sonarscanner does not exist.
00:40:14 * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
00:40:14 Process exited with code 1
00:40:14 Process exited with code 1 (Step: Manual Sonar (.NET))
00:40:14 Step Manual Sonar (.NET) failed

Please sign in to leave a comment.
I ran the command dotnet tool list --global, under the user running the agent and it does show the tool installed. I also ran a which dotnet-sonarscanner and it shows it in /var/lib/teamcityagent/.dotnet/tools/dotnet-sonarscanner which is the user running the agent. Also my first build step was dotnet tool chain command "tool update dotnet-sonarscanner".
I was able to solve the issue, I don't know why it is happening, but instead of running "tool update dotnet-sonarscanner" I now run "tool update --tool-path ./tools dotnet-sonarscanner" this forces the install of the tool into the ./tools directory and then instead of executing it as a dotnet tool I run a command line script referencing the tools executable
./tools/dotnet-sonarscanner begin /k:"%SONAR_PROJECT_KEY%" /d:"sonar.host.url=%SONAR_HOST%" /d:sonar.login="%env.SONAR_TOKEN%" /o:"%SONAR_ORG%" /d:"sonar.branch.name=%teamcity.build.branch%" /d:"sonar.cs.dotcover.reportsPaths=%DOTCOVER_HTML%"
As I said I don't know why, but for some reason when TeamCity executes the build, there must be something in the path or some other environmental setting that doesn't allow it to see the tools
I tried reproducing the issue with TeamCity 2023.05 and a Linux agent (WSL 2), but it worked for me and the tool could be detected right away.