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

fix: use cargo build for compilation when emulating cargo run #9

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

Conversation

adrienthebo
Copy link

Given the following binary generated by cargo new --bin:

// src/main.rs
fn main() {
    println!("Hello, world!");
}

Running cargo debug run panics due to invalid JSON; cargo-debug assumes that the supplied subcommand is either test or build but does not validate this, and then assumes that the supplied subcommand only emits JSON formatted manifest information on stdout. The above example that writes to stdout breaks that assumption, causing a panic.

 cargo debug run
   Compiling example v0.1.0 (/Users/user/personal/projects/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/example`
thread 'main' panicked at src/main.rs:131:23:
Invalid cargo JSON message: Error("expected value", line: 3, column: 1)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This commit resolves this by adding the following:

  1. proper adaptation for cargo-debug run similar to the special-casing for cargo-debug test
  2. checks that will warn users if the subcommand isn't known (and may fail)
  3. error handling when an invalid cargo metadata message is received (replacing a panic with a graceful exit)

This commit also lays a bit of ground work to allow for wrapping of cargo-debug run --bin by adding some preliminary support for the run subcommand.

Given the following binary generated by `cargo new --bin`:

```
// src/main.rs
fn main() {
    println!("Hello, world!");
}
```

Running `cargo debug run` panics due to invalid JSON; cargo-debug assumes that
the supplied subcommand is either `test` or `build` but does not validate this,
and then assumes that the supplied subcommand only emits JSON formatted manifest
information on stdout. The above example that writes to stdout breaks that assumption,
causing a panic.

```
 cargo debug run
   Compiling example v0.1.0 (/Users/user/personal/projects/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/example`
thread 'main' panicked at src/main.rs:131:23:
Invalid cargo JSON message: Error("expected value", line: 3, column: 1)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

This commit resolves this by adding the following:

1. proper adaptation for `cargo-debug run` similar to the special-casing for `cargo-debug test`
2. checks that will warn users if the subcommand isn't known (and may fail)
3. error handling when an invalid cargo metadata message is received (replacing a panic with a graceful exit)
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.

1 participant