如何使用 dotnet 格式格式化子文件夹内的整个项目?

问题描述

我有一个 .Net 5 应用程序,想使用 dotnet 格式。首先,我将 commitlint,huskylint-staged 添加到存储库中。文件夹结构看起来像

.
├── package.json
└── MyCsharpProject
    ├── MyCsharpProject.sln
    └── Assembly1

配置的 package.json 文件看起来像

{
  "lint-staged": {
    "*.cs": "dotnet format --include"
  },"devDependencies": {
    "@commitlint/cli": "^12.1.1","@commitlint/config-conventional": "^12.1.1","husky": "^6.0.0","lint-staged": "^10.5.4"
  },"scripts": {
    "prepare": "husky install"
  }
}

钩子因这个输出失败

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.cs
[STARTED] dotnet format --include
[Failed] dotnet format --include [Failed]
[Failed] dotnet format --include [Failed]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...

✖ dotnet format --include:
  Could not find a MSBuild project file or solution file in '/home/.../my-repository'. Specify which to use with the <workspace> argument.
husky - pre-commit hook exited with code 1 (error)

我尝试将 package.json 文件中的 dotnet 命令修改

dotnet 格式 ./MyCsharpProject/MyCsharpProject.sln

但不幸的是我收到了多个错误

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.cs
[STARTED] dotnet format ./MyCsharpProject/MyCsharpProject.sln
[Failed] dotnet format ./MyCsharpProject/MyCsharpProject.sln [Failed]
[Failed] dotnet format ./MyCsharpProject/MyCsharpProject.sln [Failed]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...

✖ dotnet format ./MyCsharpProject/MyCsharpProject.sln:
Unrecognized command or argument '/home/.../my-repository/MyCsharpProject/Assembly1/SubFolder/AFile.cs'
Unrecognized command or argument '/home/.../my-repository/MyCsharpProject/Assembly1/SubFolder/BFile.cs'
Unrecognized command or argument '/home/.../my-repository/MyCsharpProject/Assembly2/AFile.cs'

简单地 lint 整个 C# 项目的正确命令是什么?


更新

我试图将 package.json 中的这一行从

"*.cs": "dotnet format --include"

"*.cs": "dotnet format ./MyCsharpProject/MyCsharpProject.sln"

现在预提交检查工作正常。我唯一想到的是,当我修改一个 .cs 文件添加多个带有一些选项卡的空行时,我得到了这个输出

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.cs
[STARTED] dotnet format ./MyCsharpProject/MyCsharpProject.sln
[Failed] dotnet format ./MyCsharpProject/MyCsharpProject.sln [Failed]
[Failed] dotnet format ./MyCsharpProject/MyCsharpProject.sln [Failed]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...

✖ dotnet format ./MyCsharpProject/MyCsharpProject.sln:
Unrecognized command or argument '/home/.../my-repository/MyCsharpProject/Assembly1/MyFile.cs'

dotnet-format
  dotnet-format

Usage:
  dotnet-format [options] [<workspace>]

Arguments:
  <workspace>  A path to a solution file,a project file,or a folder containing a solution or project file. If a path is not specified then the current directory is used. [default: ]

Options:
  --no-restore                                                             Doesn't execute an implicit restore before formatting.
  -f,--folder                                                             Whether to treat the `<workspace>` argument as a simple folder of files.
  -w,--fix-whitespace                                                     Run whitespace formatting. Run by default when not applying fixes.
  -s,--fix-style <error|info|warn>                                        Run code style analyzers and apply fixes.
  -a,--fix-analyzers <error|info|warn>                                    Run 3rd party analyzers and apply fixes.
  --diagnostics <diagnostics>                                              A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues. [default: ]
  --include <include>                                                      A list of relative file or folder paths to include in formatting. All files are formatted if empty. [default: ]
  --exclude <exclude>                                                      A list of relative file or folder paths to exclude from formatting. [default: ]
  --check                                                                  Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.
  --report <report-path>                                                   Accepts a file path,which if provided,will produce a json report in the given directory.
  -v,--verbosity <d|detailed|diag|diagnostic|m|minimal|n|normal|q|quiet>  Set the verbosity level. Allowed values are q[uiet],m[inimal],n[ormal],d[etailed],and diag[nostic]
  --binarylog <binary-log-path>                                            Log all project or solution load information to a binary log file.
  --version                                                                Show version information
  -?,-h,--help                                                           Show help and usage information

husky - pre-commit hook exited with code 1 (error)
  • 为什么对带有多个选项卡的空行显示“无法识别的命令或参数”?
  • 为什么会显示帮助部分?

解决方法

您需要检查您的 dotnet 格式版本。 我的新版本 - v5.1.22507 使用 - dotnet format solution.slnf 在解决方案过滤器上运行格式的功能。

在上述版本之前这将不起作用。

请参阅github页面。