Cargo: Change the `/target` line in .gitignore generated by cargo new to `/**/target`

Created on 1 Nov 2018  路  7Comments  路  Source: rust-lang/cargo

Describe the problem you are trying to solve

Ignore all target directories within a directory at infinite depth with cargo new. This is useful as projects grow, without manually needing to add target directories to .gitignore, or developers figuring out how to fix many files being added in a target directory with pull requests. At the moment the generated file for .gitignore with cargo new hi is as shown:

[jray@jr-dl r]$ cargo new hi
     Created binary (application) `hi` project
[jray@jr-dl r]$ cd hi
[jray@jr-dl hi]$ cat .gitignore
/target
**/*.rs.b

Describe the solution you'd like

Change the /target line above to /**/target, similar to https://github.com/github/gitignore/pull/2866.

Notes

Similar to https://github.com/rust-lang/cargo/issues/2565.

C-feature-request

Most helpful comment

Having a submodule src/target/mod.rs doesn't seem like an entirely uncommon module name. An alternative would be to have cargo new create a sub-.gitignore inside new crates folders when run in an existing git repo (maybe even detecting if you have a workspace and only adding it if not).

All 7 comments

It's intentional that it doesn't end in / so that it can be a symlink, so we probably don't want to undo that.

Fair enough.

Having a submodule src/target/mod.rs doesn't seem like an entirely uncommon module name. An alternative would be to have cargo new create a sub-.gitignore inside new crates folders when run in an existing git repo (maybe even detecting if you have a workspace and only adding it if not).

Alternatively in such cases you can then modify .gitignore to then exclude such folders.

There was a really nice comment over here

Ultimately I think the rule that we want is

target/
!/**/src/**/target/

This allows for the workspace case without any issue.

Perhaps

!/**/src/**/target/

to allow target to be a symlink (as discussed above).

Was this page helpful?
0 / 5 - 0 ratings