astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
33.25k stars 1.11k forks source link

It would be nice if using `--respect-gitignore` outside of a git repo spat out an error #12644

Open jfly opened 3 months ago

jfly commented 3 months ago

keywords: respect-gitignore

In https://github.com/astral-sh/ruff/issues/5930, ruff tried to start respecting .gitignore files even when outside of a git repo. Unfortunately, we had to revert that.

I'm not asking to reconsider that revert: I understand that it had to happen. However, I do think it would be useful if --respect-gitignore would error out when run outside of a git repo. I personally find the current behavior quite confusing, and lost some time today trying to understand why ruff was disrespecting my .gitignore even though I asked it to respect it!

I'd be happy to send in a PR implementing this if you're open to it.

Thanks for the great tool!

charliermarsh commented 3 months ago

Seems reasonable... Might be hard to know when we should raise this exactly. E.g., is it only when it's provided on the command-line? What if it comes from a configuration file? (I think true is also the default so need to differentiate between "provided" and "not provided".)

A warning could also be reasonable here.

MichaReiser commented 3 months ago

I think there's some more complication because respect_gitignore should rather be named respect_ignore because it doesn't just toggle .gitignore, it also toggles whether ruff respects .ignore, git ignore in parent directories, git global, git exclude, and ignores hidden files.

https://github.com/astral-sh/ruff/blob/ebe5b06c95cc958d460d7cfc520e540017d9492f/crates/ruff_server/src/session/index/ruff_settings.rs#L135-L137

standard_filters

To me it's also unclear when such a warning would be shown? Is it when ruff doesn't find any gitignore or only if there is no gitignore in the root directory? Limiting it to the former seems overly restrictive but the latter has the downside that ruff always has to walk the tree eagerly even when only linting a subset of the files.