gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:26:15 25 4
gpt4 key购买 nike

我有一个 .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"
}
}

Hook 失败并显示此输出

> 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 格式 --include"

"*.cs": "dotnet 格式 ./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页面。

关于c# - 如何使用 dotnet 格式在子文件夹中格式化整个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67546915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com