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

Seeing "warning: file found to be present in multiple build targets" when file is actually *not present* in multiple targets #11248

Closed
nasadorian opened this issue Oct 17, 2022 · 4 comments · Fixed by #11299
Assignees
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-diagnostics Area: Error and warning messages generated by Cargo itself. C-enhancement Category: enhancement E-easy Experience: Easy

Comments

@nasadorian
Copy link

nasadorian commented Oct 17, 2022

Problem

I've seen numerous issues related to this warning, however I believe I've found a bug with it. I have two binaries listed in my [[bin]] table, each with a different main file. When one of the files is in the tests directory, the warning surfaces. However when both files are under src, there is no warning. Cargo seems to be complaining about a non-problematic usage of the binary table. Please see example in repro section.

Steps

Configuration 1: Warning occurs when test_server.rs lives in tests directory.

[package]
name = "example"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "example"
path = "src/server.rs"

[[bin]]
name = "example-for-test"
path = "tests/test_server.rs"
├── Cargo.lock
├── Cargo.toml
├── src
 │  └── server.rs
└── tests
    └── test_server.rs
$ cargo check
warning: file found to be present in multiple build targets: /private/tmp/example/tests/test_server.rs
    Checking example v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s

Configuration 2: Warning not raised when test_server.rs is placed in src.

[package]
name = "example"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "example"
path = "src/server.rs"

[[bin]]
name = "example-for-test"
path = "src/test_server.rs"

[dependencies]
.
├── Cargo.lock
├── Cargo.toml
├── src
 │  ├── server.rs
 │  └── test_server.rs
└── tests
$ cargo check
    Checking example v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.63.0 (fd9c4297c 2022-07-01)
@nasadorian nasadorian added the C-bug Category: bug label Oct 17, 2022
@weihanglo
Copy link
Member

Hmm… It should be intentional. The error was emitted from here, and there is a test validating this behaviour.

Rust source files under tests/ would be promoted as a Cargo Target if detected by target auto discovery, which is documented here. You can disable it by configured [package] section, such as

[package]
# ...
autobins = false
autoexamples = false
autotests = false
autobenches = false

The warning contains no further action to suppress or resolve it. It could be enhanced by:

  • telling which kind of target it is being promoted and gets conflicts.
  • referring to the doc about the auto discovery and how to disable it (if it is the cause).

It is hard to decide the right amount of a warning message. We may not want to cram all info inside one warning, but I think at least we need to provide an action item for users to proceed.

@weihanglo weihanglo added A-diagnostics Area: Error and warning messages generated by Cargo itself. A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) C-enhancement Category: enhancement E-easy Experience: Easy E-help-wanted and removed C-bug Category: bug E-help-wanted labels Oct 17, 2022
@Rustin170506
Copy link
Member

@rustbot claim

@Rustin170506
Copy link
Member

Rustin170506 commented Oct 26, 2022

  • telling which kind of target it is being promoted and gets conflicts.

@weihanglo I think not only the type but also the name is needed. I have another question: Do we need to print all conflict targets? Because the current implementation only returns one error if we meet a conflict.

  • referring to the doc about the auto discovery and how to disable it (if it is the cause).

It seems difficult to implement this because Target does not have the information about auto discovery.

@weihanglo
Copy link
Member

Do we need to print all conflict targets?

Probably? The list shouldn't be too long in normal cases, I guess? You can play with the message and choose one in the balance between informative and conciseness.

It seems difficult to implement this because Target does not have the information about auto discovery.

Hmm… I see. Thank you for the investigation. I feel like Cargo can still provide possible approaches for user to suppress the warning. If it is not too complicated to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-diagnostics Area: Error and warning messages generated by Cargo itself. C-enhancement Category: enhancement E-easy Experience: Easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants