| 199 | /// Create metadata about the root package in the Cargo manifest, without any dependencies. |
| 200 | #[tracing::instrument(target = "cargo_lambda")] |
| 201 | pub fn load_metadata<P: AsRef<Path> + Debug>( |
| 202 | manifest_path: P, |
| 203 | target_dir: Option<&Path>, |
| 204 | ) -> Result<CargoMetadata, MetadataError> { |
| 205 | trace!("loading Cargo metadata"); |
| 206 | let mut metadata_cmd = cargo_metadata::MetadataCommand::new(); |
| 207 | metadata_cmd |
| 208 | .no_deps() |
| 209 | .verbose(enabled!(target: "cargo_lambda", Level::TRACE)); |
| 210 | |
| 211 | // If target-dir was explicitly specified, pass it to cargo metadata |
| 212 | if let Some(dir) = target_dir { |
| 213 | metadata_cmd.other_options(vec![format!("--target-dir={}", dir.display())]); |
| 214 | } |
| 215 | |
| 216 | // try to split manifest path and assign current_dir to enable parsing a project-specific |
| 217 | // cargo config |
| 218 | let manifest_ref = manifest_path.as_ref(); |
| 219 | |
| 220 | match (manifest_ref.parent(), manifest_ref.file_name()) { |
| 221 | (Some(project), Some(manifest)) if is_project_metadata_ok(project) => { |
| 222 | metadata_cmd.current_dir(project); |
| 223 | metadata_cmd.manifest_path(manifest); |
| 224 | } |
| 225 | _ => { |
| 226 | // fall back to using the manifest_path without changing the dir |
| 227 | // this means there will not be any project-specific config parsing |
| 228 | metadata_cmd.manifest_path(manifest_ref); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | trace!(metadata = ?metadata_cmd, "loading cargo metadata"); |
| 233 | let meta = metadata_cmd |
| 234 | .exec() |
| 235 | .map_err(MetadataError::FailedCmdExecution)?; |
| 236 | trace!(metadata = ?meta, "loaded cargo metadata"); |
| 237 | Ok(meta) |
| 238 | } |
| 239 | |
| 240 | /// Load the main binary in the project. |
| 241 | /// It returns an error if the project includes from than one binary. |