Open ithinkthisisnotreal opened 3 years ago
@ithinkthisisnotreal Thanks for giving dotnet-format a try. Looking at your configuration, you need to also pass the --include
options when invoking dotnet-format.
"lint-staged": {
"*.cs": "dotnet format ./TheProject/TheProject.sln --include"
},
Another issue is that dotnet-format expects the include paths to be relative to the workspace (./TheProject/TheProject.sln
). This means you will need to update the lint-staged invocation with the --relative
flag.
This was a very helpful blog post about using dotnet-format and lint-staged - https://blog.johnnyreilly.com/2020/12/22/prettier-your-csharp-with-dotnet-format-and-lint-staged/#lint-staged-integration
@JoeRobich thanks a lot for your help! I updated my package.json, now it looks like
{
"husky": {
"hooks": {
"pre-commit": "lint-staged --relative"
}
},
"lint-staged": {
"*.cs": "dotnet format ./TheProject/TheProject.sln --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"
}
}
I'm not sure if this is correct because I tried to commit the following code violating many C# rules (but I don't have custom rules, I'm using the default ones)
public void AMethod(
string foo
)
{
if(foo == "bar") {
} else {
}
}
private int x = 0;
So this commit shouldn't pass right?
@ithinkthisisnotreal I think this touches on the other issue, that paths should be relative to the workspace opened in dotnet-format, which is in the ./TheProject
folder. This is a limitation we hope to fix in a following release.
lint-staged is likely passing paths relative to the root of your repo. You may be able to work around this by changing the "pre-commit" hook to invoke "./TheProject/lint-staged --relative".
@JoeRobich thanks! I tried your suggested workaround but unfortunately it didn't work for me (maybe for OP). Based on the sample package.json posted above I modified it to this
{
"husky": {
"hooks": {
"pre-commit": "./TheProject/lint-staged --relative"
}
},
"lint-staged": {
"*.cs": "dotnet format ./TheProject/TheProject.sln --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"
}
}
So should we stick to this
https://github.com/dotnet/format/issues/1156#issuecomment-848507819
solution and wait for the update? :)
When using the tool in combination with husky, commitlint and lint-staged I get confusing error reports.
How to reproduce it:
dotnet format .\TheProject\TheProject.sln
It should work fine.
Setup the package.json file
Setup commitlint and husky as described here
https://commitlint.js.org/#/guides-local-setup?id=guide-local-setup
After that setup lint-staged as described here
https://github.com/okonet/lint-staged#installation-and-setup
Your package.json should be simliar to this
You should see this output
There are two questions coming to my mind