I have a .Net 5 app and want to use dotnet format. First I added commitlint, husky and lint-staged to the repository. The folder structure looks like
.
├── package.json
└── MyCsharpProject
├── MyCsharpProject.sln
└── Assembly1
The configured package.json file looks like
{
"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"
}
}
The hook fails with this output
> 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)
I tried to modify the dotnet command in the package.json file to
dotnet format ./MyCsharpProject/MyCsharpProject.sln
but unfortunately I get multiple errors
> 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'
What is the correct command to simply lint the whole C# project?
Update
I tried to change this line in the package.json from
"*.cs": "dotnet format --include"
to
"*.cs": "dotnet format ./MyCsharpProject/MyCsharpProject.sln"
Now the pre-commit checks work fine. The only thing I'm thinking about is that when I modify a .cs file and add multiple empty lines with some tabs I get this output
> 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)
You need to check your version of dotnet format.
I the new version - v5.1.22507 the feature of format run on solution filter using - dotnet format solution.slnf
.
This will not work prior to the above version.
please refer the github page.