csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Using .gitignore to exclude files

Open bbugh opened this issue 2 years ago • 3 comments

Hi! :wave: Thanks for making this. I was so surprised when I started playing around with Unity again that there wasn't an opinionated formatter like prettier for C# that would work from the command line, so I was pleased to find this.

I see that there's a .csharpierignore file, but between Unity and Rider there's a lot of directories to ignore. It would be nice if we could specify ignoring .gitignore also, either by default or with configuration.

Currently working around it by symbolic linking .csharpierignore to .gitignore.

bbugh avatar Mar 10 '22 13:03 bbugh

I am curious what you are ignoring with .gitignore that you also want to exclude from formatting. CSharpier currently has logic that attempts to ignore any known generated code files. For example files that start with TemporaryGeneratedFile_ or that have extensions such as .generated.cs or .designer.cs. If possible, I'd prefer to modify that logic to exclude any known cases that it is missing.

belav avatar Mar 10 '22 22:03 belav

I couldn't find any way to get csharpier to verbosely print what files it's loading, but without .csharpierignore:

dotnet csharpier . --check
Total time:                      20383ms
Total files:                      2293
files that were not formatted:       0

with ln -s .gitignore .csharpierignore, it correctly detects only 7 files:

Total time:                        486ms
Total files:                         7
files that were not formatted:       0

Some examples of the extra files, none have any consistent pattern:

obj/Debug/.NETFramework,Version=v4.7.1.AssemblyAttributes.cs
Library/PackageCache/[email protected]/Editor/AssemblyInfo.cs
Library/PackageCache/[email protected]/Editor/TexturePlatformSettingsView.cs

The .gitignore files for directories:

/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/
/[Mm]emoryCaptures/
/[Rr]ecordings/
/[Aa]ssets/AssetStoreTools*
/[Aa]ssets/Plugins/Editor/JetBrains*
.vs/
.gradle/
ExportedObj/
.consulo/
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
/[Aa]ssets/[Ss]treamingAssets/aa/*
out/
.idea_modules/

Some of these can have .cs files, some can't, so it's easier to ignore them all.

bbugh avatar Mar 11 '22 03:03 bbugh

I created #632 because getting a list of what csharpier is doing seems like a useful thing.

From what I know of Unity, and from glancing at a few public repos, it seems like all of the code that you would want formatted lives in ./Assets. So if none of the ignored folders there include .cs files, then maybe running csharpier against just that folder is an easy fix.

I've had some time to noodle on a few possible approaches to using a .gitignore as the .csharpierignore.

If csharpier uses a .gitignore it finds instead of looking for a .csharpierignore, it could end up formatting code that was previously ignored. If there is a ./.csharpierignore, a ./src/.gitignore, and a file at ./src/FileIgnoredByCSharpierIgnore.cs, then the ./src/.gitignore would be picked up and used and the file that was previously ignored would now get formatted. You could add it to the .gitignore and still leave it in the repo, but that feels a bit weird.

If you can specify an ignore file with the cli, you could use --ignore-file .gitignore, but then you lose the ability to have ignores at different levels. You also run into a similar problem as above, where you may be adding files to the .gitignore even though they are being committed.

Another option would be to implement an import feature. The .csharpierignore can then import the .gitignore. Although getting exclusions to work seems like it would be a bit messy. https://stackoverflow.com/questions/5533050/gitignore-exclude-folder-but-include-specific-subfolder This also feels like the most work, and introduces a whole new concept to ignore files.

So my hope is that dotnet csharpier ./Assets formats exactly what you need. Otherwise it seems like --ignore-file .gitignore is a decent option. Prettier has something similar (which I missed when I first went looking at if they do anything similar), with a proposal to support passing multiple paths, which would take care of the problem with wanting csharpier to ignore some files, but not have git ignore them.

belav avatar Mar 12 '22 16:03 belav