From 3497701c536a826ab27b0a8efd3875e5446cf14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6thberg?= Date: Tue, 14 May 2024 10:49:55 +0200 Subject: [PATCH] Always resolve package_id from metadata when finding bootloader and partition table (#632) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The package ID format was officially documented as an opaque identifier and the only thing we were allowed to do with them is look them up in the cargo metadata. Rust 1.77 switched the package ID field to use the publicly documented "Package ID Specification" format instead, which means that the old logic no longer works. Signed-off-by: Johannes Löthberg --- CHANGELOG.md | 1 + cargo-espflash/src/main.rs | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f5315849..bfb52cf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed help text for size parameter of read-flash subcommand +- [cargo-espflash]: Always resolve package_id from metadata when finding bootloader and partition table (#632) ### Changed diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index 10a30e78..10a3e770 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -4,7 +4,7 @@ use std::{ process::{exit, Command, ExitStatus, Stdio}, }; -use cargo_metadata::Message; +use cargo_metadata::{Message, MetadataCommand}; use clap::{Args, CommandFactory, Parser, Subcommand}; use espflash::{ cli::{ @@ -349,6 +349,12 @@ fn build( .or_else(|| cargo_config.target()) .ok_or_else(|| NoTargetError::new(Some(chip)))?; + let mut metadata_cmd = MetadataCommand::new(); + if let Some(features) = &build_options.features { + metadata_cmd.features(cargo_metadata::CargoOpt::SomeFeatures(features.clone())); + } + let metadata = metadata_cmd.exec().into_diagnostic()?; + if !chip.into_target().supports_build_target(target) { return Err(UnsupportedTargetError::new(target, chip).into()); } @@ -440,9 +446,19 @@ fn build( for message in messages { match message.into_diagnostic()? { - Message::BuildScriptExecuted(script) - if script.package_id.repr.starts_with("esp-idf-sys") => - { + Message::BuildScriptExecuted(script) => { + // We can't use the `Index` implementation on `Metadata` because `-Zbuild-std` + // pulls in dependencies not listed in the metadata which then causes the `Index` + // implementation to panic. + let Some(package) = metadata.packages.iter().find(|p| p.id == script.package_id) + else { + continue; + }; + + if package.name != "esp-idf-sys" { + continue; + } + // If the `esp-idf-sys` package is being used, attempt to use the bootloader and // partition table compiled by `embuild` instead. let build_path = PathBuf::from(script.out_dir).join("build");