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

Rustdoc: Test fails on "unresolved import" though test doesn't use any crate #103775

Closed
mirao opened this issue Oct 30, 2022 · 8 comments
Closed
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@mirao
Copy link

mirao commented Oct 30, 2022

I tried this code (src/main.rs):

use regex::Regex;

/// ```
/// assert!(true)
/// ```
fn main() {
    println!("Hello, world!");
    let regex = Regex::new("^Hello").unwrap();
    println!("{}", regex);
}

with Cargo.toml

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

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
regex = "1.6.0"

I run rustdoc --test src/main.rs

I expected to see this happen:

running 1 test
test src/main.rs - main (line 1) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.20s

Instead, this happened:

error[E0432]: unresolved import `regex`
 --> src/main.rs:1:5
  |
1 | use regex::Regex;
  |     ^^^^^ maybe a missing crate `regex`?
  |
  = help: consider adding `extern crate regex` to use the `regex` crate

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.

Note that cargo run works well

$ cargo run
   Compiling hello_world v0.1.0 (/home/mirao/workspace/rust/hello_world)
    Finished dev [unoptimized + debuginfo] target(s) in 1.34s
     Running `target/debug/hello_world`
Hello, world!
^Hello

Meta

rustc --version --verbose:

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6

rustdoc --version

rustdoc 1.64.0 (a55dd71d5 2022-09-19)

I'm running Ubuntu 22.04

@mirao mirao added the C-bug Category: This is a bug. label Oct 30, 2022
@mirao mirao changed the title Rustdoc: Test fails on "maybe a missing crate" though test doesn't use any crate Rustdoc: Test fails on "unresolved import" though test doesn't use any crate Oct 30, 2022
@mirao
Copy link
Author

mirao commented Oct 30, 2022

If I use qualified name for Regex in main() without using import, then test passes:

/// ```
/// assert!(true)
/// ```
fn main() {
    println!("Hello, world!");
    let regex = regex::Regex::new("^Hello").unwrap();
    println!("{}", regex);
}

$ rustdoc --test src/main.rs

running 1 test
test src/main.rs - main (line 1) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.21s

@fmease
Copy link
Member

fmease commented Oct 30, 2022

You are invoking rustdoc on src/main.rs which knows nothing about Cargo's package manifest Cargo.toml. You should instead be using cargo test or cargo test --bins to run tests.

The rustdoc binary is the low-level documentation generator invoked by the high-level cargo test & cargo doc commands (which can resolve your dependencies like regex in this case) similar to the low-level rustc binary which is invoked by cargo build. You will almost never need to invoke rustc / rustdoc directly.

@rustbot label invalid

@rustbot
Copy link
Collaborator

rustbot commented Oct 30, 2022

Error: Label invalid can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@fmease
Copy link
Member

fmease commented Oct 31, 2022

Regarding your comment:

If I use qualified name for Regex in main() without using import, then test passes:

This is due to an unrelated reason (what I said about rustdoc --test vs. cargo test still applies): rustdoc doesn't perform full semantic analysis as far as I know and assumes a thing or two about the validity of your program.
E.g. the following file also gets successfully documented:

fn main() {
    i_do_not_exist;
}

@fmease
Copy link
Member

fmease commented Oct 31, 2022

Thanks for your report! However, none of this is a bug. The issues you are seeing a due to the incorrect use of the provided Rust tools. The issue with rustdoc accepting several semantically malformed programs is well-known and a won't-fix.

@mirao
Copy link
Author

mirao commented Oct 31, 2022

You should instead be using cargo test or cargo test --bins to run tests.

But it doesn't run docs tests in my case: #50784
Nevertheless I understand that rustdoc is a low level tool and ideally cargo test should be used. It makes sense.

@estebank estebank added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-testsuite Area: The testsuite used to check the correctness of rustc T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Oct 31, 2022
@fmease
Copy link
Member

fmease commented Nov 1, 2022

Ah, I see. That's bothersome :(

So essentially, this issue then becomes a duplicate of #50784, right?

@mirao
Copy link
Author

mirao commented Nov 2, 2022

Duplicate of #50784

@mirao mirao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants