|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use miette::Diagnostic; |
| 4 | +use pixi_record::{DevSourceRecord, PinnedSourceSpec}; |
| 5 | +use pixi_spec::{BinarySpec, PixiSpec, SourceAnchor}; |
| 6 | +use pixi_spec_containers::DependencyMap; |
| 7 | +use rattler_conda_types::PackageName; |
| 8 | +use thiserror::Error; |
| 9 | +use tracing::instrument; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + BuildBackendMetadataError, BuildBackendMetadataSpec, CommandDispatcher, CommandDispatcherError, |
| 13 | + CommandDispatcherErrorResultExt, build::source_metadata_cache::MetadataKind, |
| 14 | +}; |
| 15 | + |
| 16 | +/// A specification for retrieving development source metadata. |
| 17 | +/// |
| 18 | +/// This queries the build backend for all outputs from a source and creates |
| 19 | +/// DevSourceRecords for each one. |
| 20 | +#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize)] |
| 21 | +pub struct DevSourceMetadataSpec { |
| 22 | + /// The development source specification |
| 23 | + pub package_name: PackageName, |
| 24 | + |
| 25 | + /// Information about the build backend to request the information from |
| 26 | + pub backend_metadata: BuildBackendMetadataSpec, |
| 27 | +} |
| 28 | + |
| 29 | +/// The result of querying development source metadata. |
| 30 | +#[derive(Debug, Clone)] |
| 31 | +pub struct DevSourceMetadata { |
| 32 | + /// Information about the source checkout that was used |
| 33 | + pub source: PinnedSourceSpec, |
| 34 | + |
| 35 | + /// All the dev source records for outputs from this source |
| 36 | + pub records: Vec<DevSourceRecord>, |
| 37 | +} |
| 38 | + |
| 39 | +/// An error that can occur while retrieving dev source metadata. |
| 40 | +#[derive(Debug, Error, Diagnostic)] |
| 41 | +pub enum DevSourceMetadataError { |
| 42 | + #[error(transparent)] |
| 43 | + #[diagnostic(transparent)] |
| 44 | + BuildBackendMetadata(#[from] BuildBackendMetadataError), |
| 45 | + |
| 46 | + #[error( |
| 47 | + "the build backend does not support the `conda/outputs` procedure, which is required for development sources" |
| 48 | + )] |
| 49 | + UnsupportedProtocol, |
| 50 | +} |
| 51 | + |
| 52 | +impl DevSourceMetadataSpec { |
| 53 | + /// Retrieves development source metadata by querying the build backend. |
| 54 | + /// |
| 55 | + /// This method: |
| 56 | + /// 1. Gets metadata from the build backend |
| 57 | + /// 2. Creates a DevSourceRecord for each output |
| 58 | + /// 3. Combines build/host/run dependencies for each output |
| 59 | + #[instrument( |
| 60 | + skip_all, |
| 61 | + name = "dev-source-metadata", |
| 62 | + fields( |
| 63 | + source = %self.backend_metadata.source, |
| 64 | + platform = %self.backend_metadata.build_environment.host_platform, |
| 65 | + ) |
| 66 | + )] |
| 67 | + pub(crate) async fn request( |
| 68 | + self, |
| 69 | + command_dispatcher: CommandDispatcher, |
| 70 | + ) -> Result<DevSourceMetadata, CommandDispatcherError<DevSourceMetadataError>> { |
| 71 | + // Get the metadata from the build backend |
| 72 | + let build_backend_metadata = command_dispatcher |
| 73 | + .build_backend_metadata(self.backend_metadata.clone()) |
| 74 | + .await |
| 75 | + .map_err_with(DevSourceMetadataError::BuildBackendMetadata)?; |
| 76 | + |
| 77 | + // We only support the Outputs protocol for dev sources |
| 78 | + let outputs = match &build_backend_metadata.metadata.metadata { |
| 79 | + MetadataKind::Outputs { outputs } => outputs, |
| 80 | + MetadataKind::GetMetadata { .. } => { |
| 81 | + return Err(CommandDispatcherError::Failed( |
| 82 | + DevSourceMetadataError::UnsupportedProtocol, |
| 83 | + )); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + // Create a SourceAnchor for resolving relative paths in dependencies |
| 88 | + let source_anchor = SourceAnchor::from(pixi_spec::SourceSpec::from( |
| 89 | + build_backend_metadata.source.clone(), |
| 90 | + )); |
| 91 | + |
| 92 | + // Create a DevSourceRecord for each output |
| 93 | + let mut records = Vec::new(); |
| 94 | + for output in outputs { |
| 95 | + if output.metadata.name != self.package_name { |
| 96 | + continue; |
| 97 | + } |
| 98 | + let record = Self::create_dev_source_record( |
| 99 | + output, |
| 100 | + &build_backend_metadata.source, |
| 101 | + &build_backend_metadata.metadata.input_hash, |
| 102 | + &self.backend_metadata.variants, |
| 103 | + &source_anchor, |
| 104 | + )?; |
| 105 | + records.push(record); |
| 106 | + } |
| 107 | + |
| 108 | + Ok(DevSourceMetadata { |
| 109 | + source: build_backend_metadata.source.clone(), |
| 110 | + records, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + /// Creates a DevSourceRecord from a CondaOutput. |
| 115 | + /// |
| 116 | + /// This combines all dependencies (build, host, run) into a single map |
| 117 | + /// and resolves relative source paths. |
| 118 | + fn create_dev_source_record( |
| 119 | + output: &pixi_build_types::procedures::conda_outputs::CondaOutput, |
| 120 | + source: &PinnedSourceSpec, |
| 121 | + input_hash: &Option<pixi_record::InputHash>, |
| 122 | + variants: &Option<BTreeMap<String, Vec<String>>>, |
| 123 | + source_anchor: &SourceAnchor, |
| 124 | + ) -> Result<DevSourceRecord, CommandDispatcherError<DevSourceMetadataError>> { |
| 125 | + // Combine all dependencies into a single map |
| 126 | + let mut all_dependencies = DependencyMap::default(); |
| 127 | + let mut all_constraints = DependencyMap::default(); |
| 128 | + |
| 129 | + // Helper to process dependencies and resolve paths |
| 130 | + let process_deps = |
| 131 | + |deps: Option< |
| 132 | + &pixi_build_types::procedures::conda_outputs::CondaOutputDependencies, |
| 133 | + >, |
| 134 | + dependencies: &mut DependencyMap<PackageName, PixiSpec>, |
| 135 | + constraints: &mut DependencyMap<PackageName, BinarySpec>| { |
| 136 | + if let Some(deps) = deps { |
| 137 | + // Process depends |
| 138 | + for depend in &deps.depends { |
| 139 | + let name = PackageName::new_unchecked(&depend.name); |
| 140 | + let spec = |
| 141 | + crate::build::conversion::from_package_spec_v1(depend.spec.clone()); |
| 142 | + |
| 143 | + // Resolve relative paths for source dependencies |
| 144 | + let resolved_spec = match spec.into_source_or_binary() { |
| 145 | + itertools::Either::Left(source_spec) => { |
| 146 | + PixiSpec::from(source_anchor.resolve(source_spec)) |
| 147 | + } |
| 148 | + itertools::Either::Right(binary_spec) => PixiSpec::from(binary_spec), |
| 149 | + }; |
| 150 | + dependencies.insert(name, resolved_spec); |
| 151 | + } |
| 152 | + |
| 153 | + // Process constraints |
| 154 | + for constraint in &deps.constraints { |
| 155 | + let name = PackageName::new_unchecked(&constraint.name); |
| 156 | + let spec = |
| 157 | + crate::build::conversion::from_binary_spec_v1(constraint.spec.clone()); |
| 158 | + constraints.insert(name, spec); |
| 159 | + } |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + // Process all dependency types |
| 164 | + process_deps( |
| 165 | + output.build_dependencies.as_ref(), |
| 166 | + &mut all_dependencies, |
| 167 | + &mut all_constraints, |
| 168 | + ); |
| 169 | + process_deps( |
| 170 | + output.host_dependencies.as_ref(), |
| 171 | + &mut all_dependencies, |
| 172 | + &mut all_constraints, |
| 173 | + ); |
| 174 | + process_deps( |
| 175 | + Some(&output.run_dependencies), |
| 176 | + &mut all_dependencies, |
| 177 | + &mut all_constraints, |
| 178 | + ); |
| 179 | + |
| 180 | + // Extract variant values (not the lists of possible values) |
| 181 | + // For now, we'll take the first value of each variant |
| 182 | + // TODO: This needs to be properly handled based on the actual variant selection |
| 183 | + let variant_values = variants |
| 184 | + .as_ref() |
| 185 | + .map(|v| { |
| 186 | + v.iter() |
| 187 | + .filter_map(|(k, values)| { |
| 188 | + values.first().map(|first| (k.clone(), first.clone())) |
| 189 | + }) |
| 190 | + .collect() |
| 191 | + }) |
| 192 | + .unwrap_or_default(); |
| 193 | + |
| 194 | + Ok(DevSourceRecord { |
| 195 | + name: output.metadata.name.clone(), |
| 196 | + source: source.clone(), |
| 197 | + input_hash: input_hash.clone(), |
| 198 | + variants: variant_values, |
| 199 | + dependencies: all_dependencies, |
| 200 | + constraints: all_constraints, |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
0 commit comments