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-rustdoc: error: too many file operands when experimental feature -w is passed without parameters #132718

Open
NovaliX-Dev opened this issue Nov 7, 2024 · 3 comments
Labels
A-CLI Area: Command-line interface (CLI) to the compiler A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. requires-nightly This issue requires a nightly compiler in some way. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@NovaliX-Dev
Copy link

When i try to run this command : cargo +nightly rustdoc -- -Z unstable-options -w

Rustdoc returns this error :

error: too many file operands

Which confuses me. I mean, i didn't passed any file format, so how can there be too many of them ?

We should have a clearer error, something like this :

error: no output format given after `-w`
@NovaliX-Dev NovaliX-Dev added the C-bug Category: This is a bug. label Nov 7, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 7, 2024
@fmease fmease added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. A-CLI Area: Command-line interface (CLI) to the compiler requires-nightly This issue requires a nightly compiler in some way. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 7, 2024
@fmease
Copy link
Member

fmease commented Nov 7, 2024

While cargo rustdoc comes from a different repository (namely https://github.com/rust-lang/cargo), the rustdoc binary does bear the blame here:

So, assuming the package contains a library crate, Cargo executes the following command (manually adjusted for legibility):

rustdoc --edition=2021 --crate-type lib --crate-name "$CRATE_NAME" src/lib.rs \
    -o "$CARGO_TARGET_DIR/doc" --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values())' \
    --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=192 \
    -C metadata=$DATA -L "dependency=$CARGO_TARGET_DIR/debug/deps" \
    -Z unstable-options -w --crate-version "$CRATE_VERSION"

Now, as you can see, Cargo passes extra arguments after -w which should've been fine but here's the kicker: For some reason, rustdoc/getopts tries to parse the argument for -w greedily or verbatim. Meaning that it considers the string --crate-version to be the argument for -w (!) instead of its own separate flag (and -w's arg to be none or empty)1. As a result of a that, the crate version "$CRATE_VERSION" (e.g., 0.1.0) gets interpreted as a stray file operand (top-level argument). Hence the error too many file operands (it sees src/lib.rs and "$CRATE_VERSION").

If possible (getopts is quite primitive), the fix would be not to parse the argument of -w so greedily. This should lead to the error changing to error: Argument to option 'w' missing.

Footnotes

  1. Cf: rustdoc f.rs -Zunstable-options -w --verbose leads to error: unknown output format `--verbose` .

@fmease fmease changed the title [rustdoc] error : too many file operands when experimental feature -w is passed without parameters cargo-rustdoc: error: too many file operands when experimental feature -w is passed without parameters Nov 7, 2024
@fmease
Copy link
Member

fmease commented Nov 7, 2024

For some reason, rustdoc/getopts tries to parse the argument for -w greedily or verbatim

I've just noticed this seems to affect all "argument-taking" flags, e.g. --crate-version --verbose literally sets the crate version to --verbose. Oh no, getopts >:(

@fmease
Copy link
Member

fmease commented Nov 7, 2024

Switching getopt's parsing style from FloatingFrees to StopAtFirstFree is not what we want.

Ah, I think we want to update all references of .optopt(…) to .optflagopt(…) and (I guess) manually reject .opt_str("…") == None, though one needs to double-check what other consequences that would have and if it would be a breaking change (if it leads to fewer or more program invocations getting accepted). Hmm, that doesn't work either lol >.< It would lead to -w getting accepted as if it wasn't passed at all (None). Oh, this is annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-CLI Area: Command-line interface (CLI) to the compiler A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. requires-nightly This issue requires a nightly compiler in some way. 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

3 participants