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

Display valid crate types in error message for --crate-type flag #134720

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

malezjaa
Copy link

@malezjaa malezjaa commented Dec 24, 2024

This PR improves the error message for the --crate-type flag. When an invalid crate type is provided, the compiler will now show a list of valid options.

Before

image

After

image

I based the implementation on OutputType::shorthands_display

Closes #70183

@rustbot
Copy link
Collaborator

rustbot commented Dec 24, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BoxyUwU (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 24, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Dec 24, 2024

I seem to recall not all the targets support all the crate types. For example, does all of the wasm targets support dylib crate type?

Example: https://github.com/rust-lang/rust/pull/130599/files#diff-ec897fdc27fe33a590c42303d7adda80b03a9af4860440e41a235d7081aa2165

Though I suppose the user will get another follow-up error anyway 😄

@malezjaa
Copy link
Author

malezjaa commented Dec 24, 2024

You're right 🙂 This error is just to show the valid values for --crate-type.

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an ui test for this diagnostics?

@malezjaa
Copy link
Author

Hi, I added the tests. I'm not sure if I did it the right way, though.

@malezjaa malezjaa requested a review from jieyouxu December 24, 2024 17:34
@malezjaa malezjaa force-pushed the feat/crate-type-valid-values branch from 980ca14 to 62353f8 Compare December 24, 2024 19:27
src/etc/pre-push.sh Outdated Show resolved Hide resolved
@clubby789
Copy link
Contributor

The UI test looks good. You'll should squash all your changes into one commit

@malezjaa malezjaa force-pushed the feat/crate-type-valid-values branch from a60dbd4 to 6315245 Compare December 24, 2024 20:39
@malezjaa
Copy link
Author

Done 👍

@jieyouxu jieyouxu assigned jieyouxu and unassigned BoxyUwU Dec 25, 2024
Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks like a nice improvement, only some tiny stylistic nits, then LGTM.

compiler/rustc_session/src/config.rs Outdated Show resolved Hide resolved
compiler/rustc_session/src/config.rs Outdated Show resolved Hide resolved
@jieyouxu jieyouxu added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 25, 2024
@jieyouxu
Copy link
Member

Thanks, I'll r+ after PR CI is green.

@jieyouxu jieyouxu added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 25, 2024
@rust-log-analyzer

This comment has been minimized.

@jieyouxu
Copy link
Member

(You might need to rebless the test)

@jieyouxu
Copy link
Member

Also, can you squash the commits into one? Thanks.

@malezjaa malezjaa force-pushed the feat/crate-type-valid-values branch from 9a2ef0a to b56ec2f Compare December 25, 2024 12:41
@jieyouxu
Copy link
Member

Cool.
@bors r+ rollup

@bors
Copy link
Contributor

bors commented Dec 25, 2024

📌 Commit b56ec2f has been approved by jieyouxu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 25, 2024
@jieyouxu
Copy link
Member

@rustbot blocked (on rust-lang/cargo#14990 merging and also for a cargo submodule bump in this repo which contains that PR)

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 29, 2024
github-merge-queue bot pushed a commit to rust-lang/cargo that referenced this pull request Dec 29, 2024
### What does this PR try to resolve?

This PR relaxes the `bad_crate_type` test to have it only match the
prefix of the unknown crate type error message emitted by rustc.

This is so that the cargo test isn't sensitive to (future) suggestions
for known crate types that rustc may emit to help the user.

### How should we test and review this PR?

This test should already be run as part of cargo CI. (This is definitely
run as part of rust-lang/rust CI, lol.)

### Additional information

rust-lang/rust side PR that's trying to add a suffix to the bad crate
type error message to list all valid `--crate-type` values:
rust-lang/rust#134720.

Without relaxing this test, the rust-lang/rust side PR [fails
with](rust-lang/rust#134720 (comment)):

<details>
<summary>rust-lang/rust CI fail message</summary>

(Ignore the missing colon after `unknown crate type`)

```
---- bad_config::bad_crate_type stdout ----
running `/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/cargo build -v`
thread 'bad_config::bad_crate_type' panicked at src/tools/cargo/tests/testsuite/bad_config.rs:434:10:

---- expected: tests/testsuite/bad_config.rs:424:27
++++ actual:   stderr
   1    1 | [ERROR] failed to run `rustc` to learn about crate-type bad_type information
   2    2 |
   3    3 | Caused by:
   4    4 |   process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --crate-type bad_type` ([EXIT_STATUS]: 1)
   5    5 |   --- stderr
   6      -   [ERROR] unknown crate type: `bad_type`
Error:      6 +   [ERROR] unknown crate type `bad_type`, expected one of: `bin`, `cdylib`, `dylib`, `lib`, `proc-macro`, `rlib`
   7    7 |
```
</details>

Discussed at
https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Reblessing.20a.20cargo.20test.
@malezjaa malezjaa force-pushed the feat/crate-type-valid-values branch from c211549 to e20e87d Compare December 29, 2024 21:11
@malezjaa
Copy link
Author

Okay done. Sorry that it took so long. Had to rebuild it 2 times and test.

@jieyouxu
Copy link
Member

There's no rush at all, we can't merge this before the cargo submodule bump which contains my cargo-side PR to merge back into this repo anyway lol.

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this LGTM now, we just have to wait for the cargo submodule bump before the cargo test in this repo can pass.

compiler/rustc_session/src/config.rs Outdated Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@malezjaa
Copy link
Author

Oh yeah. I ran x.py test tidy. Not x.py fmt. Didn't know that. Sorry!

@malezjaa malezjaa force-pushed the feat/crate-type-valid-values branch from e20e87d to 0d5087d Compare December 29, 2024 22:12
@weihanglo weihanglo mentioned this pull request Dec 31, 2024
@malezjaa
Copy link
Author

malezjaa commented Jan 6, 2025

Hey @jieyouxu sorry for pinging, but I think this pr can be merged.

@jieyouxu jieyouxu added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Jan 6, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Jan 6, 2025

Thanks for the reminder, I missed the cargo bump.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 6, 2025

📌 Commit 0d5087d has been approved by jieyouxu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 6, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Jan 7, 2025
…ues, r=jieyouxu

Display valid crate types in error message for --crate-type flag

This PR improves the error message for the --crate-type flag. When an invalid crate type is provided, the compiler will now show a list of valid options.

### Before
![image](https://github.com/user-attachments/assets/4922e4e5-eeca-40cd-ac1c-1c6319a81aee)

### After
![image](https://github.com/user-attachments/assets/67ea1f35-aa41-4e4f-8691-47c273d0cff9)

I based the implementation on `OutputType::shorthands_display`

Closes rust-lang#70183
@jhpratt
Copy link
Member

jhpratt commented Jan 7, 2025

@bors r-

presumptive (partial) cause of #135181 (comment)

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 7, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Jan 7, 2025

Ah right, I added a new test, may have to rebless it

@jieyouxu jieyouxu closed this Jan 7, 2025
@jieyouxu jieyouxu reopened this Jan 7, 2025
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#22 exporting to docker image format
#22 sending tarball 27.0s done
#22 DONE 33.0s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---

failures:

---- [ui] tests/ui/invalid-compile-flags/crate-type-flag.rs#empty_crate_type stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/invalid-compile-flags/crate-type-flag.empty_crate_type/crate-type-flag.empty_crate_type.stderr"

- error: unknown crate type: ``
- error: unknown crate type: ``
+ error: unknown crate type: ``, expected one of: lib, rlib, staticlib, dylib, cdylib, bin, proc-macro
3 


The actual stderr differed from the expected stderr.
The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args invalid-compile-flags/crate-type-flag.rs`

error in revision `empty_crate_type`: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/invalid-compile-flags/crate-type-flag.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--cfg" "empty_crate_type" "--check-cfg" "cfg(test,FALSE,lib,rlib,staticlib,dylib,cdylib,bin,proc_dash_macro,multivalue,multivalue_combined,proc_underscore_macro,empty_crate_type,unknown)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/invalid-compile-flags/crate-type-flag.empty_crate_type" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--crate-type="
--- stderr -------------------------------
--- stderr -------------------------------
error: unknown crate type: ``, expected one of: lib, rlib, staticlib, dylib, cdylib, bin, proc-macro


---- [ui] tests/ui/invalid-compile-flags/crate-type-flag.rs#proc_underscore_macro stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/invalid-compile-flags/crate-type-flag.proc_underscore_macro/crate-type-flag.proc_underscore_macro.stderr"
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/invalid-compile-flags/crate-type-flag.proc_underscore_macro/crate-type-flag.proc_underscore_macro.stderr"
diff of stderr:

- error: unknown crate type: `proc_macro`
+ error: unknown crate type: `proc_macro`, expected one of: lib, rlib, staticlib, dylib, cdylib, bin, proc-macro
3 


The actual stderr differed from the expected stderr.
The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args invalid-compile-flags/crate-type-flag.rs`

error in revision `proc_underscore_macro`: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/invalid-compile-flags/crate-type-flag.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--cfg" "proc_underscore_macro" "--check-cfg" "cfg(test,FALSE,lib,rlib,staticlib,dylib,cdylib,bin,proc_dash_macro,multivalue,multivalue_combined,proc_underscore_macro,empty_crate_type,unknown)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/invalid-compile-flags/crate-type-flag.proc_underscore_macro" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--crate-type=proc_macro"
--- stderr -------------------------------
--- stderr -------------------------------
error: unknown crate type: `proc_macro`, expected one of: lib, rlib, staticlib, dylib, cdylib, bin, proc-macro


---- [ui] tests/ui/invalid-compile-flags/crate-type-flag.rs#unknown stdout ----
---- [ui] tests/ui/invalid-compile-flags/crate-type-flag.rs#unknown stdout ----
Saved the actual stderr to "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/invalid-compile-flags/crate-type-flag.unknown/crate-type-flag.unknown.stderr"

- error: unknown crate type: `🤡`
- error: unknown crate type: `🤡`
+ error: unknown crate type: `🤡`, expected one of: lib, rlib, staticlib, dylib, cdylib, bin, proc-macro
3 


The actual stderr differed from the expected stderr.
The actual stderr differed from the expected stderr.
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args invalid-compile-flags/crate-type-flag.rs`

error in revision `unknown`: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/invalid-compile-flags/crate-type-flag.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--cfg" "unknown" "--check-cfg" "cfg(test,FALSE,lib,rlib,staticlib,dylib,cdylib,bin,proc_dash_macro,multivalue,multivalue_combined,proc_underscore_macro,empty_crate_type,unknown)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/invalid-compile-flags/crate-type-flag.unknown" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "--crate-type=\xf0\x9f\xa4\xa1"
--- stderr -------------------------------
--- stderr -------------------------------
error: unknown crate type: `🤡`, expected one of: lib, rlib, staticlib, dylib, cdylib, bin, proc-macro



failures:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustc --crate-type flag should display valid values
9 participants