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

cargo init - check .gitignore before appending /target #12306

Closed
Mirch opened this issue Jun 23, 2023 · 3 comments
Closed

cargo init - check .gitignore before appending /target #12306

Mirch opened this issue Jun 23, 2023 · 3 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.

Comments

@Mirch
Copy link

Mirch commented Jun 23, 2023

Problem

When running cargo init in an existing git repository, cargo adds a /target line to .gitignore, regardless of whether that line is already present.

Proposed Solution

After checking if the .gitignore file exists, scan the lines to search for a /target, target/ or */taget line.

Notes

cargo -vV output:

cargo 1.68.0 (115f34552 2023-02-26)
release: 1.68.0
commit-hash: 115f34552518a2f9b96d740192addbac1271e7e6
commit-date: 2023-02-26
host: aarch64-apple-darwin
libgit2: 1.5.0 (sys:0.16.0 vendored)
libcurl: 7.79.1 (sys:0.4.59+curl-7.86.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
os: Mac OS 12.5.1 [64-bit]
@Mirch Mirch added C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage. labels Jun 23, 2023
@weihanglo
Copy link
Member

What version did you use having that issue? Having an output of cargo -vV attached here would be nice for us to track the issue.

The feature exists from 1.33 via #6521 so far I can tell. The function format_existing does the job.

There is also a snapshot test to verify the behavior. The input is:

And output be like:

/target
**/some.file
# Added by cargo
#
# already existing elements were commented out
#/target
/Cargo.lock

So it looks like the issue shouldn't happen. I wonder if the entry of your /target have some trailing spaces?

@weihanglo weihanglo added S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request. and removed S-triage Status: This issue is waiting on initial triage. labels Jun 26, 2023
@Mirch
Copy link
Author

Mirch commented Jun 26, 2023

@weihanglo I added the cargo -vV output to the issue.

I also tried to reproduce the problem and found the following:

If /target is present, the line added by cargo init is indeed commented out. However, it looks like target/ or *target/ are not correctly identified.

The following .gitignore file is what I'm left with after creating a new repository from Github (with the Rust template for .gitignore), cloning the repo and then running cargo init:

# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb


# Added by cargo

/target

@weihanglo
Copy link
Member

Thanks for the reply. I think your reproduction is correct.

However, cargo init at this moment doesn't add an item if there is an exact-matched one. For /target, target/ and *target/, each of them has a slightly different meaning. I don't feel like Cargo should be “smarter” to ignore all of them when one exists.

@Mirch Mirch closed this as completed Jun 27, 2023
@Mirch Mirch closed this as not planned Won't fix, can't repro, duplicate, stale Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-needs-info Status: Needs more info, such as a reproduction or more background for a feature request.
Projects
None yet
Development

No branches or pull requests

2 participants