SonarCloud静态分析工具安装失败求助
已排查所有类似问题,均与当前情况不符,推测是路径配置问题。本地仓库路径正确且已与GitHub同步正常,但安装dotnet-sonarscanner时触发错误,请求协助解决。
日志错误信息
目录: D:\Clients**************Services\Azure%20Training\CICD%20Git%20Hub%20Actions\Repos\PSCIDemoRepo\PSCIDemo.sonar
Mode LastWriteTime Length Name---- ------------- ------ ----d----- 8/29/2023 4:50 PM scanner
Tool 'dotnet-sonarscanner' failed to update due to the following:
Failed to install tool package 'dotnet-sonarscanner': Could not find a part of the path 'D:\Clients*****************Services\Azure%20Training\CICD%20Git%20Hub%20Actions\Repos\PSCIDemoRepo\PSCIDemo.sonar\scanner.store.stage\ks1t1zcl.c3u\dotnet-sonarscanner'.
Tool 'dotnet-sonarscanner' failed to install. This failure may have been caused by:
- 尝试安装预览版本但未使用
--version指定版本- 找到同名包,但该包不是.NET工具
- 无法访问所需NuGet源,可能是网络问题
- 工具名称输入错误
Error: Process completed with exit code 1.
我使用的YAML配置
- name: Install SonarCloud scanner if: steps.cache-sonar-scanner.outputs.cache-hit != 'true' shell: powershell run: | New-Item -Path \Clients\*******Services\Azure%20Training\CICD%20Git%20Hub%20Actions\Repos\PSCIDemoRepo\PSCIDemo\.sonar\scanner -ItemType Directory dotnet tool update dotnet-sonarscanner --version 5.5.3 --tool-path \Clients\*******Services\Azure%20Training\CICD%20Git%20Hub%20Actions\Repos\PSCIDemoRepo\PSCIDemo\.sonar\scanner - name: Build and analyze
SonarCloud生成的建议YAML配置
- name: Install SonarCloud scanner if: steps.cache-sonar-scanner.outputs.cache-hit != 'true' shell: powershell run: | New-Item -Path .\.sonar\scanner -ItemType Directory dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner - name: Build and analyze
解决建议
- 修正路径编码问题:绝对路径中使用的
%20是空格的URL编码,PowerShell中需直接使用空格,将路径中的Azure%20Training改为Azure Training。 - 改用相对路径:放弃硬编码绝对路径,直接采用SonarCloud推荐的
.\.sonar\scanner相对路径,避免环境差异、路径拼写错误引发的问题。 - 排查路径拼写一致性:日志中路径显示为
PSCIDemo.sonar(缺少点),而配置中是PSCIDemo\.sonar,使用相对路径可规避这类解析错误。 - 清理残留后重装:若存在旧安装残留,先执行
dotnet tool uninstall dotnet-sonarscanner --tool-path .\.sonar\scanner,再重新运行安装命令。
内容的提问来源于stack exchange,提问作者user1585204

