Skip to content

Cargo publish excludes files called target #12790

@KarelPeeters

Description

@KarelPeeters

Problem

When publishing or packaging a crate that contains a file called target (different from the typical root target folder), the file is not included. This happens even though the target file is not ignored by either .gitignore or .git/info/exclude. It's also not a glob evaluation issue, this is still an issue if .gitignore is completely empty.

This also only happens if the target file is currently tracked in git. This is not that important by itself, but it might help figuring out what exactly causes the bug.

Steps

I've made a repository to reproduce this bug: https://github.com/KarelPeeters/cargo_bug_package_target

The steps to follow:

> cargo package --list --allow-dirty
.gitignore
Cargo.lock
Cargo.toml
Cargo.toml.orig
README.md
Readme.md
data/not_target
src/main.rs

note that data/target is missing, this is the main bug.

  • ensure that target is not "tracked" any more by moving or deleting .git entirely
> mv .git .not_git
  • check the list of files that will be packaged again
> cargo package --list --allow-dirty
Cargo.lock
Cargo.toml
Cargo.toml.orig
README.md
Readme.md
data\not_target
data\target
src\main.rs

Note that data/target is not missing anymore, this shows that the bug only happens for tracked files.

Possible Solution(s)

I assume this is caused by some bad interaction in build_ar_list between the VCSInfo stuff and the hardcoded check for a filename of "target" here: https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/cargo_package.rs#L934

I'm not sure what much of the context there is trying to do, so I don't know what the right fix is.

One issue to keep in mind is that multiple crates that share a repository while not being a workspace can have multiple target folders. So a simple check like "only ignore the root target directory" is also not good enough. There should be existing infrastructure to figure out what the real target directory is though, since cargo clean (like many other subcommands) needs that.

Notes

I noticed this when trying to vendor some cuda headers and associated files, which is neccesary to get docs.rs to build one of my crates correctly.

One of these files happens to be called target.

Here it is in my github repository: https://github.com/KarelPeeters/Kyanite/tree/1eb40f2dde70d3432f218bfbaa5c7d37eebbd394/kn-cuda-sys/doc_headers/cuda_include/nv
But the same file is missing on docs.rs and crates.io: https://docs.rs/crate/kn-cuda-sys/0.4.1/source/doc_headers/cuda_include/nv/

Version

cargo 1.71.0 (cfd3bbd8f 2023-06-08)
release: 1.71.0
commit-hash: cfd3bbd8fe4fd92074dfad04b7eb9a923646839f
commit-date: 2023-06-08
host: x86_64-pc-windows-msvc
libgit2: 1.6.4 (sys:0.17.1 vendored)
libcurl: 8.0.1-DEV (sys:0.4.61+curl-8.0.1 vendored ssl:Schannel)
os: Windows 10.0.22000 (Windows 11 Core) [64-bit]

Metadata

Metadata

Assignees

Labels

C-bugCategory: bugCommand-packageCommand-publishE-easyExperience: EasyS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions