Skip to content

Commit

Permalink
and for rust commands
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Feb 10, 2025
1 parent 85d725a commit 43c4549
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/dfx/src/lib/builders/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ impl CanisterBuilder for RustBuilder {
"Executing: cargo build --target wasm32-unknown-unknown --release -p {} --locked",
package
);
let output = cargo.output().context("Failed to run 'cargo build'. You might need to run `cargo update` (or a similar command like `cargo vendor`) if you have updated `Cargo.toml`, because `dfx build` uses the --locked flag with Cargo.")?;
let mut output = Err(anyhow!("uninitialized"));
env.with_suspend_all_spinners(Box::new(|| {
output = cargo.output().context("Failed to run 'cargo build'. You might need to run `cargo update` (or a similar command like `cargo vendor`) if you have updated `Cargo.toml`, because `dfx build` uses the --locked flag with Cargo.");
}));
let output = output?;

if !output.status.success() {
bail!("Failed to compile the rust package: {}", package);
Expand Down
29 changes: 19 additions & 10 deletions src/dfx/src/lib/models/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,12 @@ impl CanisterPool {
}

#[context("Failed step_prebuild_all.")]
fn step_prebuild_all(&self, log: &Logger, build_config: &BuildConfig) -> DfxResult<()> {
fn step_prebuild_all(
&self,
env: &dyn Environment,
log: &Logger,
build_config: &BuildConfig,
) -> DfxResult<()> {
// moc expects all .did files of dependencies to be in <output_idl_path> with name <canister id>.did.
// Because some canisters don't get built these .did files have to be copied over manually.
for canister in self.canisters.iter().filter(|c| {
Expand Down Expand Up @@ -684,7 +689,7 @@ impl CanisterPool {
.iter()
.any(|can| can.info.should_cargo_audit())
{
self.run_cargo_audit()?;
self.run_cargo_audit(env)?;
} else {
trace!(
self.logger,
Expand Down Expand Up @@ -756,7 +761,7 @@ impl CanisterPool {
log: &Logger,
build_config: &BuildConfig,
) -> DfxResult<Vec<Result<&BuildOutput, BuildError>>> {
self.step_prebuild_all(log, build_config)
self.step_prebuild_all(env, log, build_config)
.map_err(|e| DfxError::new(BuildError::PreBuildAllStepFailed(Box::new(e))))?;

let canisters_to_build = self.canisters_to_build(build_config);
Expand Down Expand Up @@ -861,7 +866,7 @@ impl CanisterPool {
}

/// If `cargo-audit` is installed this runs `cargo audit` and displays any vulnerable dependencies.
fn run_cargo_audit(&self) -> DfxResult {
fn run_cargo_audit(&self, env: &dyn Environment) -> DfxResult {
let location = Command::new("cargo")
.args(["locate-project", "--message-format=plain", "--workspace"])
.output()
Expand Down Expand Up @@ -896,12 +901,16 @@ impl CanisterPool {
self.logger,
"Checking for vulnerabilities in rust canisters."
);
let out = Command::new("cargo")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("audit")
.output()
.context("Failed to run 'cargo audit'.")?;
let mut out = Err(anyhow!("uninitialized"));
env.with_suspend_all_spinners(Box::new(|| {
out = Command::new("cargo")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("audit")
.output()
.context("Failed to run 'cargo audit'.");
}));
let out = out?;
if out.status.success() {
info!(self.logger, "Audit found no vulnerabilities.")
} else {
Expand Down

0 comments on commit 43c4549

Please sign in to comment.