cargo: cargo does not see `Cargo.toml` if `.gitignore` contains `/*`

Problem

If .gitignore is written in the style of “ignore everything except this”, then cargo package --list for some reason does not see anything. It seems to me that everything was fine on previous versions, I spent an hour investigating, did I miss something?

cargo version: 1.80.1 (376290515 2024-07-16) git version 2.39.2 (Apple Git-143) OS: mac

Cargo.toml:

[package]
name = "gitignore-bug"
version = "0.1.0"
edition = "2021"
license = "MIT"
description = "to reproduce bug"

[dependencies]

.gitignore:

/target
/*
!.gitignore
!Cargo.toml
!src

cargo package --list

warning: manifest has no documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
warning: no `Cargo.toml` file found when packaging `gitignore-bug` (note the case of the file name).
.cargo_vcs_info.json
Cargo.lock

Steps

cargo init
echo '\n/*\n\n!.gitignore\n!Cargo.toml\n!src' >> .gitignore
git add . && git commit -m "."
cargo package --list

Possible Solution(s)

No response

Notes

cargo build is working, cargo publish is not working

Version

cargo 1.80.1 (376290515 2024-07-16)
release: 1.80.1
commit-hash: 37629051518c3df9ac2c1744589362a02ecafa99
commit-date: 2024-07-16
host: aarch64-apple-darwin
libgit2: 1.7.2 (sys:0.18.3 vendored)
libcurl: 8.7.1 (sys:0.4.72+curl-8.6.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 13.6.8 [64-bit]

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

I was also unable to reproduce the issue with v1.80.1 and v1.82.0.

My guess is that this gitoxide commit is the fix that was first introduced in gix v0.64 - maybe older cargo releases with prior versions of gix suffer from the issue, i.e. directory traversal would allow to skip the traversal root when everything was excluded, with only includes to go by.

I don’t even have a question for @cospectrum as it’s clear by now that we can’t reproduce the issue, which probably means that some global git configuration is affecting this unexpectedly. It’s actually kind of impossible right now to disable this effect without recompiling, unfortunately.

cargo/testing ( master) +5 -1 [$!⇡1] took 6s
❯ cargo package --list
warning: manifest has no description.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
.cargo_vcs_info.json
.gitignore
Cargo.lock
Cargo.toml
Cargo.toml.orig
src/main.rs

cargo/testing ( master) +5 -1 [$!⇡1]
❯ ../target/debug/cargo package --list
warning: manifest has no description.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
.cargo_vcs_info.json
.gitignore
Cargo.lock
Cargo.toml
Cargo.toml.orig
src/main.rs

cargo/testing ( master) +5 -1 [$!⇡1]
❯ cargo --version
cargo 1.80.1 (376290515 2024-07-16)

cargo/testing ( master) +5 -1 [$!⇡1]
❯ ../target/debug/cargo --version
cargo 1.82.0 (9a170d718 2024-08-15)

Apologies for getting back to this with such a delay, but there is good news: It looks like the latest master already has a fix.

In the example output below, ../testing was initialized with the reproducer from this issue.

❯ git rev-parse @
88957a1b7f65b233118dcd4f59df2763043ef9dd

cargo ( master) [$] via 🐍
❯ cargo run -- package --manifest-path ../testing/Cargo.toml  -l
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
     Running `target/debug/cargo package --manifest-path ../testing/Cargo.toml -l`
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
.cargo_vcs_info.json
.gitignore
Cargo.lock
Cargo.toml
Cargo.toml.orig
src/main.rs

The fix is part of gix v0.64, which comes with gix-dir v0.6.0 that is very likely the crate the contains the actual fix.

As a lower boundary, when compiling an older version (88957a1b7f65b233118dcd4f59df2763043ef9dd ~100), the issue can be reproduced in the test setup used here:

cargo (6d68563) [$] via 🐍
❯ cargo run -- package --manifest-path ../testing/Cargo.toml  -l
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.28s
     Running `target/debug/cargo package --manifest-path ../testing/Cargo.toml -l`
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
warning: no `Cargo.toml` file found when packaging `testing` (note the case of the file name).
.cargo_vcs_info.json
Cargo.lock

Thus, no action is needed unless a backport is desired. Then I’d have to try and port the fix to gix-dir v0.5.x and Cargo would need another patch release after updating to it.

That’s great! Actually, I noticed that previously cargo init didn’t initialize a repository in the newly created cargo project as I invoked it from within the Cargo repository.

Once that is fixed, I can reproduce the issue.

❯ cargo package -l
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
warning: no `Cargo.toml` file found when packaging `testing` (note the case of the file name).
.cargo_vcs_info.json
Cargo.lock

On it now.