X Tutup
The Wayback Machine - https://web.archive.org/web/20210210090104/https://github.com/github/gitignore/pull/3629
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Rust.gitignore rules for build artifacts #3629

Open
wants to merge 2 commits into
base: master
from

Conversation

@clmay
Copy link

@clmay clmay commented Jan 27, 2021

Reasons for making this change:

The changes made in #3436 are incorrect and should be reversed. Cargo, the de facto build tool for Rust, always outputs build / compilation artifacts to target/ in the root of the crate (project), by default (see the relevant portion of the Cargo book). We should target Cargo's defaults and allow other users to override them if they have customized the configuration of their builds.

The correct gitignore rule for excluding the target directory is

/target

since we want to ignore the top-level directory (only), as well as all of its children. Since the debug directory is itself always a child of target under the default Cargo configuration, excluding only target is sufficient for also excluding the debug directory that the previous committer added to the rules. Several other commenters in #3436 also pointed this out, but after the changes were already merged.

Importantly, the rules used in #3436 are incorrect, because they can exclude directories/files in src, e.g.:

  • src/target/some_file.rs or
  • src/components/debug/

which are actual source files for the project. Scoping the exclusion specifically to the top-level target directory generated by Cargo is a safer default.


tl;dr: We always know (absent custom configuration) that the target directory at the project root will contain all of Cargo's build and compilation artifacts, and that /target in the .gitignore is the sole safe default that will exclude them from git tracking correctly and safely; the same cannot be said for the current .gitignore rules, which could very well ignore actual source code in a project (and in a way that could be easy to miss / very unexpected).

Aside: the above rules will always be adequate for a "vanilla" Cargo project without custom config and should be considered the community defaults. Changes for customization or third-party tooling probably shouldn't be added here.


Links to documentation supporting these rule changes:

https://doc.rust-lang.org/cargo/guide/build-cache.html

@clmay clmay changed the title Fix Rust.gitignore rule for `target` directory Fix Rust.gitignore rules for build artifacts Jan 27, 2021
@clmay
Copy link
Author

@clmay clmay commented Jan 29, 2021

@shiftkey I don't seem to be able to add a reviewer, could you have a look at this when you've time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant
X Tutup