Skip to content

Commit

Permalink
Merge #1248
Browse files Browse the repository at this point in the history
1248: Inherit stderr when running `cargo metadata` r=messense a=messense

Use an extension trait to achieve the same functionality so we don't need to wait for upstream.

Fixes #1243 

Co-authored-by: messense <messense@icloud.com>
  • Loading branch information
bors[bot] and messense committed Nov 4, 2022
2 parents a3e9650 + 00d78e8 commit fe3546d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -52,6 +52,7 @@ mod develop;
mod metadata;
mod module_writer;
mod new_project;
mod polyfill;
mod project_layout;
mod pyproject_toml;
mod python_interpreter;
Expand Down
26 changes: 26 additions & 0 deletions src/polyfill.rs
@@ -0,0 +1,26 @@
use cargo_metadata::{Metadata, MetadataCommand};
use std::process::Stdio;

pub trait MetadataCommandExt {
/// Runs configured `cargo metadata` and returns parsed `Metadata`.
/// Inherits stderr from parent process.
fn exec_inherit_stderr(&self) -> Result<Metadata, cargo_metadata::Error>;
}

impl MetadataCommandExt for MetadataCommand {
fn exec_inherit_stderr(&self) -> Result<Metadata, cargo_metadata::Error> {
let mut command = self.cargo_command();
command.stderr(Stdio::inherit());
let output = command.output()?;
if !output.status.success() {
return Err(cargo_metadata::Error::CargoMetadata {
stderr: String::from_utf8(output.stderr)?,
});
}
let stdout = std::str::from_utf8(&output.stdout)?
.lines()
.find(|line| line.starts_with('{'))
.ok_or(cargo_metadata::Error::NoJson)?;
Self::parse(stdout)
}
}
3 changes: 2 additions & 1 deletion src/project_layout.rs
@@ -1,4 +1,5 @@
use crate::build_options::{extract_cargo_metadata_args, CargoOptions};
use crate::polyfill::MetadataCommandExt;
use crate::{CargoToml, Metadata21, PyProjectToml};
use anyhow::{bail, format_err, Context, Result};
use cargo_metadata::{Metadata, MetadataCommand};
Expand Down Expand Up @@ -277,7 +278,7 @@ impl ProjectResolver {
let result = MetadataCommand::new()
.manifest_path(manifest_path)
.other_options(cargo_metadata_extra_args)
.exec();
.exec_inherit_stderr();

let cargo_metadata = match result {
Ok(cargo_metadata) => cargo_metadata,
Expand Down
3 changes: 2 additions & 1 deletion src/source_distribution.rs
@@ -1,4 +1,5 @@
use crate::module_writer::{add_data, ModuleWriter};
use crate::polyfill::MetadataCommandExt;
use crate::{BuildContext, PyProjectToml, SDistWriter};
use anyhow::{bail, Context, Result};
use cargo_metadata::{Metadata, MetadataCommand};
Expand Down Expand Up @@ -442,7 +443,7 @@ pub fn source_distribution(
.manifest_path(path_dep)
// We don't need to resolve the dependency graph
.no_deps()
.exec()
.exec_inherit_stderr()
.with_context(|| {
format!(
"Cargo metadata failed for {} at '{}'",
Expand Down
6 changes: 1 addition & 5 deletions tests/common/errors.rs
Expand Up @@ -85,11 +85,7 @@ pub fn locked_doesnt_build_without_cargo_lock() -> Result<()> {
.source()
.ok_or_else(|| format_err!("{}", err))?
.to_string();
// Sometimes the first message is "waiting for file lock on package cache",
// so we can only check if the lock file is in the message somewhere
if !err_string.starts_with("`cargo metadata` exited with an error:")
|| !err_string.contains("lock file")
{
if !err_string.starts_with("`cargo metadata` exited with an error:") {
bail!("{:?}", err_string);
}
} else {
Expand Down

0 comments on commit fe3546d

Please sign in to comment.