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

How to use dotnet format to format the whole project inside a subfolder?

我有一个 .Net 5 应用程序并且想使用 dotnet 格式。首先,我将 commitlint, husky and lint-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 format ./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'

简单检查整个 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 页面。