3

我有一个 .Net 5 应用程序并想使用 dotnet 格式。首先,我将commitlint、husky​​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 格式 ./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# 项目进行 lint 的正确命令是什么?


更新

我试图改变 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)
  • 为什么对带有多个选项卡的空行显示“无法识别的命令或参数”?
  • 为什么显示帮助部分?
4

1 回答 1

4

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

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

请参考github页面。

于 2021-05-24T06:07:04.513 回答