Skip to content

Conversation

@adamrk
Copy link
Contributor

@adamrk adamrk commented Feb 12, 2020

Addresses #3085

@adamrk adamrk marked this pull request as ready for review February 12, 2020 09:33
@adamrk adamrk requested a review from matklad February 12, 2020 09:33
@adamrk adamrk changed the title Add error context to failures in ra_project_model using anyhow crate Add error context to failures in ra_project_model using anyhow crate Feb 12, 2020
meta.current_dir(parent);
}
let meta = meta.exec().map_err(|e| format!("cargo metadata failed: {}", e))?;
let meta = meta.exec().map_err(|e| anyhow!("cargo metadata failed: {}", e))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest to check that we get a good error message here. I am concerned because we got a report #3118 where the error message was not very helpful... I also believe meta.exec().context("cargo metadata failed") would give us a bit better error message, though not sure...

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, I think this should use .context

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran the function with a path which wasn't pointing to a Cargo.toml file and neither setup records he path that was used in the error message. Here's what I got with your change:

cargo metadata failed

Caused by:
    Error during execution of `cargo metadata`: error: the manifest-path must be a path to a Cargo.toml file

Maybe it would make sense to add a new method on MetadataCommand which returns the full command called and include that in the error message?

I can't tell from that bug report exactly what info would have clarified things.

Copy link
Contributor

@matklad matklad Feb 12, 2020

Choose a reason for hiding this comment

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

I think something like this should work:

meta.exec()
  .with_context(|| format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display()))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

serde = { version = "1.0.89", features = ["derive"] }
serde_json = "1.0.39"

anyhow = "1.0.26"
Copy link
Member

Choose a reason for hiding this comment

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

Missing trailing newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

match (|| -> Result<_> {
match (|| -> ::std::result::Result<String, Box<dyn Error + Send + Sync + 'static>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably wants to use anyhow::Error rather than Box<dyn Error> as well.

Copy link
Contributor

@Veetaha Veetaha Feb 12, 2020

Choose a reason for hiding this comment

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

@matklad , maybe anyhow::Result<String>, anyhow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

.args(&["--print", "sysroot"])
.output()
.map_err(|e| format!("rustc --print sysroot failed: {}", e))?;
.map_err(|e| anyhow!("rustc --print sysroot failed: {}", e))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Here .context would be more appropriate I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Err("failed to locate sysroot")?;
let exit_message = match rustc_output.status.code() {
Some(code) => {
anyhow!("failed to locate sysroot: rustc --print sysroot exited with code {}", code)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can use bail! macro, rather than an Err(...)? pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@matklad
Copy link
Contributor

matklad commented Feb 12, 2020

Looks like CI is failing due to -D warnings?

@adamrk adamrk force-pushed the use-anyhow-in-ra_project_model branch from 5dc8045 to d908c9a Compare February 12, 2020 12:56
@adamrk
Copy link
Contributor Author

adamrk commented Feb 12, 2020

Looks like CI is failing due to -D warnings?

My mistake - just mixed up changes addressing separate comments from the review.

let cargo_toml = PathBuf::from("/");
let result = CargoWorkspace::from_cargo_metadata(&cargo_toml, &CargoFeatures::default());
println!("error: {:?}", result)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This test doesn't assert anything and probably should be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops - forgot to take it out.

@matklad
Copy link
Contributor

matklad commented Feb 13, 2020

bors r+

@matklad matklad merged commit 6f2cab1 into rust-lang:master Feb 13, 2020
@adamrk adamrk deleted the use-anyhow-in-ra_project_model branch February 17, 2020 11:34
cjhopman pushed a commit to cjhopman/rust-analyzer that referenced this pull request Apr 10, 2020
…ate (rust-lang#3119)

Add error context to failures in ra_project_model using anyhow crate
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.

4 participants