bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.21k stars 381 forks source link

feat: add support for .gitignore #1908

Open jbedard opened 2 months ago

jbedard commented 2 months ago

What type of PR is this?

Feature

What package or component does this PR mostly affect?

all

What does this PR do? Why is it needed?

Allow users to opt-in to respecting the gitignore when collecting files for gazelle extensions to consume.

While bazel can access git-ignored files many repositories manually exclude git-ignored content by not including it in source file lists or globs in BUILDs or by duplicating the git-ignore entries as # gazelle:exclude.

Ideally users would put all bazel ignored content in .bazelignore but without glob support that is often not done.

This should remain opt-in for repositories wanting it but by default bazel can access git-ignored content so gazelle should as well.

Which issues(s) does this PR fix?

Fixes #1166

Other notes for review

jbedard commented 2 months ago

@fmeum can you confirm if adding this to the gazelle core is something we'd like to do? If so are there preferences for which library to use vs manual?

tyler-french commented 2 months ago

How is this different from using 'gazelle:exclude' directives?

jbedard commented 2 months ago

People won't want to copy+paste gitignore files into BUILD files...?

jbedard commented 2 months ago

Functionally gitignore also works a bit different with things like ! operators etc. That is one reason to use a library such as go-git instead of doing it ourselves but we could also potentially do it ourselves but don't support !...

fmeum commented 2 months ago

I kind of like this as it reduces the amount of Gazelle-specific configuration.

I don't know which library is best, but we should definitely use one rather than roll our own.

jbedard commented 2 months ago

I don't know which library is best, but we should definitely use one rather than roll our own.

I agree we should use a real library and not try to re-implement gitignore logic, otherwise we'll just end up with a never-ending stream "it doesn't work like git" bugs.

The go-git library is what I've used in all the aspect gazelle extensions and I've never had any issues so I think that's a good place to start. If we add appropriate tests then we should be able to change the implementation later.

jbedard commented 2 months ago

FWIW I've also tried other libraries which all had issues in the aspect gazelle extensions:

Those both also seem to be unmaintained.

Others I have not tried seem to also be unmaintained and many look too simple or slow:

DavidZbarsky-at commented 2 months ago

This looks great to me, @fmeum wdyt?

fmeum commented 1 month ago

Based on https://github.com/aspect-build/aspect-cli/issues/755, what do you expect to be the use case for this if we can't enable it by default due to performance issues? It's surprising to me that .gitignore processing should be costly - do you know what the hotspots are in the current implementation?

jbedard commented 1 month ago

I was suggesting it should be disabled by default because that is the ideal use case for both "correctness" and performance (although performance is only really a concern in very large projects globbing on hundreds of thousands of files).

However enabling it by default is probably a better initial experience for most projects so I'm fine with that as well.

Longer explanation, imo...

The ideal use case is when bazelignore specifies everything bazel should ignore, and gitignore specifies everything git should ignore. Those are not always 1-1... sometimes bazel might need to process things that are git ignored, very often things such as IDE config are in git but should be ignored by bazel etc.

In reality the more common use case is projects use gitignore and then (partially) duplicate that in glob([exclude]) + # gazelle:exclude, and bazelignore is almost unused. This is done either due to lack of awareness of bazelignore or the fact that bazelignore does not support globs so it is too much of a hassle or sometimes not even possible.

This feature would remove the need for many of those glob([exclude]) + # gazelle:exclude which I think makes this feature worth it for most projects. I essentially agree with everything in this comment: https://github.com/bazel-contrib/bazel-gazelle/issues/1166#issuecomment-1024595950