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

Panic-less code and user prompt for toolchain installation #30

Merged
merged 2 commits into from
Dec 21, 2024

Conversation

tombh
Copy link
Contributor

@tombh tombh commented Dec 21, 2024

There's now prompts for when Rust toolchain assets need to be installed. Surprisingly responding to a prompt with a single keypress requires a whole crate, namely crossterm.

Also all the code is now "panic-less", so no unwrap(), panic(), etc. Everything goes through ? thanks to the anyhow crate.

Fixes #14

@tombh tombh changed the title Panic less code and user prompt for toolchain installation Panic-less code and user prompt for toolchain installation Dec 21, 2024
Copy link
Collaborator

@schell schell left a comment

Choose a reason for hiding this comment

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

Just a quick fix about main logging the error chain and producing a backtrace for better debugging.

crates/cargo-gpu/src/main.rs Show resolved Hide resolved
reason = "CRAB GOOD. CRAB IMPORTANT."
)]
{
print!("🦀 ");
Copy link
Collaborator

Choose a reason for hiding this comment

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

😜

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can totally change this BTW. I just used Ferris to as an example of what we could do.

}
}

fn main() -> anyhow::Result<()> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In general I think it's fine for a cli program to panic when it hits an unrecoverable error. There are benefits to this behavior like knowing exactly where in the program the failure occurred since the line number is included in the print statement and a backtrace is produced.
But wrapping all possible errors in Result can be nice for testing and is a generally good practice.
So - my only concern here is that if main returns the Result, we have no way of logging the error (and the errors further up the chain of errors) or the backtrace and instead we rely on whatever Rust's default behavior is - which I think is likely just panicking with the text of the top-level error, erasing the backtrace.

So I think here we should probably not return Result and instead if let Err(msg) = result { ... } making sure to use all of anyhow's machinery to print the relevant information.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good points, I agree. I've refactored so that main() wraps and captures the error from a run() function, then writes the error message to stderr and logs the backtrace when not a release build.

Also add some user output for the major steps of installing toolchains,
compiling `spirv-builder-cli` and compiling the actual shader.

Surprisingly there's no way in the standard library to get a single
keypress from the user! There's ways to get a line, therefore a keypress
then a newline, but that's not so conventional for responding to a "y/n"
prompt.

Fixes Rust-GPU#14
Copy link
Collaborator

@schell schell left a comment

Choose a reason for hiding this comment

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

Awesome work! 🙇

@schell schell merged commit c34e883 into Rust-GPU:main Dec 21, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Install step should get confirmation from the user about installing the toolchain and components
2 participants