-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathlib.rs
More file actions
1328 lines (1174 loc) · 48.1 KB
/
lib.rs
File metadata and controls
1328 lines (1174 loc) · 48.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![deny(missing_docs)]
//! rattler-build library.
pub mod build;
pub mod cache;
pub mod conda_build_config;
pub mod console_utils;
pub mod metadata;
mod normalized_key;
pub mod opt;
pub mod package_test;
pub mod packaging;
pub mod recipe;
pub mod render;
pub mod script;
pub mod selectors;
pub mod source;
pub mod system_tools;
pub mod tool_configuration;
#[cfg(feature = "tui")]
pub mod tui;
pub mod types;
pub mod used_variables;
pub mod utils;
pub mod variant_config;
mod variant_render;
mod consts;
mod env_vars;
pub mod hash;
mod linux;
mod macos;
mod post_process;
pub mod rebuild;
#[cfg(feature = "recipe-generation")]
pub mod recipe_generator;
mod unix;
mod windows;
mod package_cache_reporter;
pub mod source_code;
use std::{
collections::{BTreeMap, HashMap},
path::{Path, PathBuf},
process::Command,
str::FromStr,
sync::{Arc, Mutex},
};
use build::{WorkingDirectoryBehavior, run_build, skip_existing};
use console_utils::LoggingOutputHandler;
use dialoguer::Confirm;
use dunce::canonicalize;
use fs_err as fs;
use futures::FutureExt;
use miette::{Context, IntoDiagnostic};
pub use normalized_key::NormalizedKey;
use opt::*;
use package_test::TestConfiguration;
use petgraph::{algo::toposort, graph::DiGraph, visit::DfsPostOrder};
use rattler_conda_types::{
MatchSpec, NamedChannelOrUrl, PackageName, Platform, compression_level::CompressionLevel,
package::ArchiveType,
};
use rattler_config::config::build::PackageFormatAndCompression;
use rattler_solve::SolveStrategy;
use rattler_virtual_packages::VirtualPackageOverrides;
use recipe::parser::{Dependency, TestType, find_outputs_from_src};
use recipe::variable::Variable;
use render::resolved_dependencies::RunExportsDownload;
use selectors::SelectorConfig;
use source::patch::apply_patch_custom;
use source_code::Source;
use system_tools::SystemTools;
use tool_configuration::{Configuration, ContinueOnFailure, SkipExisting, TestStrategy};
use types::Directories;
use types::{
BuildConfiguration, BuildSummary, PackageIdentifier, PackagingSettings,
build_reindexed_channels,
};
use variant_config::VariantConfig;
use crate::metadata::{Debug, Output, PlatformWithVirtualPackages};
/// Returns the recipe path.
pub fn get_recipe_path(path: &Path) -> miette::Result<PathBuf> {
let recipe_path = canonicalize(path);
if let Err(e) = &recipe_path {
match e.kind() {
std::io::ErrorKind::NotFound => {
return Err(miette::miette!(
"The file {} could not be found.",
path.to_string_lossy()
));
}
std::io::ErrorKind::PermissionDenied => {
return Err(miette::miette!(
"Permission denied when trying to access the file {}.",
path.to_string_lossy()
));
}
_ => {
return Err(miette::miette!(
"An unknown error occurred while trying to access the file {}: {:?}",
path.to_string_lossy(),
e
));
}
}
}
let mut recipe_path = recipe_path.into_diagnostic()?;
// If the recipe_path is a directory, look for "recipe.yaml" in the directory.
if recipe_path.is_dir() {
let recipe_yaml_path = recipe_path.join("recipe.yaml");
if recipe_yaml_path.exists() && recipe_yaml_path.is_file() {
recipe_path = recipe_yaml_path;
} else {
return Err(miette::miette!(
"'recipe.yaml' not found in the directory {}",
path.to_string_lossy()
));
}
}
Ok(recipe_path)
}
/// Returns the tool configuration.
pub fn get_tool_config(
build_data: &BuildData,
fancy_log_handler: &Option<LoggingOutputHandler>,
) -> miette::Result<Configuration> {
let client = tool_configuration::reqwest_client_from_auth_storage(
build_data.common.auth_file.clone(),
build_data.common.s3_config.clone(),
build_data.common.mirror_config.clone(),
build_data.common.allow_insecure_host.clone(),
)
.into_diagnostic()?;
let configuration_builder = Configuration::builder()
.with_keep_build(build_data.keep_build)
.with_compression_threads(build_data.compression_threads)
.with_reqwest_client(client)
.with_test_strategy(build_data.test)
.with_skip_existing(build_data.skip_existing)
.with_continue_on_failure(build_data.continue_on_failure)
.with_noarch_build_platform(build_data.noarch_build_platform)
.with_channel_priority(build_data.common.channel_priority)
.with_allow_insecure_host(build_data.common.allow_insecure_host.clone())
.with_error_prefix_in_binary(build_data.error_prefix_in_binary)
.with_allow_symlinks_on_windows(build_data.allow_symlinks_on_windows)
.with_zstd_repodata_enabled(build_data.common.use_zstd)
.with_bz2_repodata_enabled(build_data.common.use_bz2)
.with_sharded_repodata_enabled(build_data.common.use_sharded)
.with_jlap_enabled(build_data.common.use_jlap);
let configuration_builder = if let Some(fancy_log_handler) = fancy_log_handler {
configuration_builder.with_logging_output_handler(fancy_log_handler.clone())
} else {
configuration_builder
};
Ok(configuration_builder.finish())
}
/// Returns the output for the build.
pub async fn get_build_output(
build_data: &BuildData,
recipe_path: &Path,
tool_config: &Configuration,
) -> miette::Result<Vec<Output>> {
let mut output_dir = build_data.common.output_dir.clone();
if output_dir.exists() {
output_dir = canonicalize(&output_dir).into_diagnostic()?;
}
if build_data.target_platform == Platform::NoArch
|| build_data.build_platform == Platform::NoArch
{
return Err(miette::miette!(
"target-platform / build-platform cannot be `noarch` - that should be defined in the recipe"
));
}
tracing::debug!(
"Platforms: build: {}, host: {}, target: {}",
build_data.build_platform,
build_data.host_platform,
build_data.target_platform
);
let selector_config = SelectorConfig {
// We ignore noarch here
target_platform: build_data.target_platform,
host_platform: build_data.host_platform,
hash: None,
build_platform: build_data.build_platform,
variant: BTreeMap::new(),
experimental: build_data.common.experimental,
// allow undefined while finding the variants
allow_undefined: true,
recipe_path: Some(recipe_path.to_path_buf()),
};
let span = tracing::info_span!("Finding outputs from recipe");
let enter = span.enter();
// First find all outputs from the recipe
let named_source = Source::from_path(recipe_path).into_diagnostic()?;
let outputs = find_outputs_from_src(named_source.clone())?;
// Check if there is a `variants.yaml` or `conda_build_config.yaml` file next to
// the recipe that we should potentially use.
let mut detected_variant_config = None;
// find either variants_config_file or conda_build_config_file automatically
for file in [
consts::VARIANTS_CONFIG_FILE,
consts::CONDA_BUILD_CONFIG_FILE,
] {
if let Some(variant_path) = recipe_path.parent().map(|parent| parent.join(file)) {
if variant_path.is_file() {
if !build_data.ignore_recipe_variants {
let mut configs = build_data.variant_config.clone();
configs.push(variant_path);
detected_variant_config = Some(configs);
} else {
tracing::debug!(
"Ignoring variants from {} because \"--ignore-recipe-variants\" was specified",
variant_path.display()
);
}
break;
}
};
}
// If `-m foo.yaml` is passed as variant config, we should use that instead of
// the auto-detected one. For that reason we add them to the end of the list.
let mut variant_configs = detected_variant_config.unwrap_or_default();
variant_configs.extend(build_data.variant_config.clone());
let mut variant_config = VariantConfig::from_files(&variant_configs, &selector_config)?;
// Apply variant overrides from command line
for (key, values) in &build_data.variant_overrides {
let normalized_key = NormalizedKey::from(key.as_str());
let variables: Vec<Variable> = values.iter().map(|v| Variable::from_string(v)).collect();
variant_config.variants.insert(normalized_key, variables);
}
let outputs_and_variants =
variant_config.find_variants(&outputs, named_source, &selector_config)?;
tracing::info!("Found {} variants\n", outputs_and_variants.len());
for discovered_output in &outputs_and_variants {
let skipped = if discovered_output.recipe.build().skip() {
console::style(" (skipped)").red().to_string()
} else {
"".to_string()
};
tracing::info!(
"\nBuild variant: {}-{}-{}{}",
discovered_output.name,
discovered_output.version,
discovered_output.build_string,
skipped
);
let mut table = comfy_table::Table::new();
table
.load_preset(comfy_table::presets::UTF8_FULL_CONDENSED)
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS)
.set_header(["Variant", "Version"]);
for (key, value) in discovered_output.used_vars.iter() {
table.add_row([key.normalize(), format!("{:?}", value)]);
}
tracing::info!("\n{}\n", table);
}
drop(enter);
let mut subpackages = BTreeMap::new();
let mut outputs = Vec::new();
let global_build_name = outputs_and_variants
.first()
.map(|o| o.name.clone())
.unwrap_or_default();
for discovered_output in outputs_and_variants {
let recipe = &discovered_output.recipe;
if recipe.build().skip() {
continue;
}
subpackages.insert(
recipe.package().name().clone(),
PackageIdentifier {
name: recipe.package().name().clone(),
version: recipe.package().version().clone(),
build_string: discovered_output.build_string.clone(),
},
);
let build_name = if recipe.cache.is_some() {
global_build_name.clone()
} else {
recipe.package().name().as_normalized().to_string()
};
let variant_channels = if let Some(channel_sources) = discovered_output
.used_vars
.get(&NormalizedKey("channel_sources".to_string()))
{
Some(
channel_sources
.to_string()
.split(',')
.map(str::trim)
.map(|s| NamedChannelOrUrl::from_str(s).into_diagnostic())
.collect::<miette::Result<Vec<_>>>()?,
)
} else {
None
};
// priorities
// 1. channel_sources from variant file
// 2. channels from args
// 3. channels from pixi_config
// 4. conda-forge as fallback
if variant_channels.is_some() && build_data.channels.is_some() {
return Err(miette::miette!(
"channel_sources and channels cannot both be set at the same time"
));
}
let channels = variant_channels.unwrap_or_else(|| {
build_data
.channels
.clone()
.unwrap_or(vec![NamedChannelOrUrl::Name("conda-forge".to_string())])
});
let channels = channels
.into_iter()
.map(|c| c.into_base_url(&tool_config.channel_config))
.collect::<Result<Vec<_>, _>>()
.into_diagnostic()?;
let timestamp = chrono::Utc::now();
let virtual_package_override = VirtualPackageOverrides::from_env();
let output = Output {
recipe: recipe.clone(),
build_configuration: BuildConfiguration {
target_platform: discovered_output.target_platform,
host_platform: PlatformWithVirtualPackages::detect_for_platform(
build_data.host_platform,
&virtual_package_override,
)
.into_diagnostic()?,
build_platform: PlatformWithVirtualPackages::detect_for_platform(
build_data.build_platform,
&virtual_package_override,
)
.into_diagnostic()?,
hash: discovered_output.hash.clone(),
variant: discovered_output.used_vars.clone(),
directories: Directories::setup(
&build_name,
recipe_path,
&output_dir,
build_data.no_build_id,
×tamp,
recipe.build().merge_build_and_host_envs(),
)
.into_diagnostic()?,
channels,
channel_priority: tool_config.channel_priority,
solve_strategy: SolveStrategy::Highest,
timestamp,
subpackages: subpackages.clone(),
packaging_settings: PackagingSettings::from_args(
build_data.package_format.archive_type,
build_data.package_format.compression_level,
),
store_recipe: !build_data.no_include_recipe,
force_colors: build_data.color_build_log && console::colors_enabled(),
sandbox_config: build_data.sandbox_configuration.clone(),
debug: build_data.debug,
exclude_newer: build_data.exclude_newer,
},
finalized_dependencies: None,
finalized_sources: None,
finalized_cache_dependencies: None,
finalized_cache_sources: None,
system_tools: SystemTools::new(),
build_summary: Arc::new(Mutex::new(BuildSummary::default())),
extra_meta: Some(
build_data
.extra_meta
.clone()
.unwrap_or_default()
.into_iter()
.collect(),
),
};
outputs.push(output);
}
Ok(outputs)
}
fn can_test(output: &Output, all_output_names: &[&PackageName], done_outputs: &[Output]) -> bool {
let check_if_matches = |spec: &MatchSpec, output: &Output| -> bool {
if spec.name.as_ref() != Some(output.name()) {
return false;
}
if let Some(version_spec) = &spec.version {
if !version_spec.matches(output.recipe.package().version()) {
return false;
}
}
if let Some(build_string_spec) = &spec.build {
if !build_string_spec.matches(&output.build_string()) {
return false;
}
}
true
};
// Check if any run dependencies are not built yet
if let Some(ref deps) = output.finalized_dependencies {
for dep in &deps.run.depends {
if all_output_names
.iter()
.any(|o| Some(*o) == dep.spec().name.as_ref())
{
// this dependency might not be built yet
if !done_outputs.iter().any(|o| check_if_matches(dep.spec(), o)) {
return false;
}
}
}
}
// Also check that for all script tests
for test in output.recipe.tests() {
if let TestType::Command(command) = test {
for dep in command
.requirements
.build
.iter()
.chain(command.requirements.run.iter())
{
let dep_spec: MatchSpec = dep.parse().expect("Could not parse MatchSpec");
if all_output_names
.iter()
.any(|o| Some(*o) == dep_spec.name.as_ref())
{
// this dependency might not be built yet
if !done_outputs.iter().any(|o| check_if_matches(&dep_spec, o)) {
return false;
}
}
}
}
}
true
}
/// Runs build.
pub async fn run_build_from_args(
build_output: Vec<Output>,
tool_configuration: Configuration,
) -> miette::Result<()> {
let mut outputs = Vec::new();
let mut test_queue = Vec::new();
let outputs_to_build = skip_existing(build_output, &tool_configuration).await?;
let all_output_names = outputs_to_build
.iter()
.map(|o| o.name())
.collect::<Vec<_>>();
for (index, output) in outputs_to_build.iter().enumerate() {
let (output, archive) = match run_build(
output.clone(),
&tool_configuration,
WorkingDirectoryBehavior::Cleanup,
)
.boxed_local()
.await
{
Ok((output, archive)) => {
output.record_build_end();
(output, archive)
}
Err(e) => {
if tool_configuration.continue_on_failure == ContinueOnFailure::Yes {
tracing::error!("Build failed for {}: {}", output.identifier(), e);
output.record_warning(&format!("Build failed: {}", e));
continue;
}
return Err(e);
}
};
outputs.push(output.clone());
// We can now run the tests for the output. However, we need to check if
// all dependencies that are needed for the test are already built.
// Decide whether the tests should be skipped or not
let (skip_test, skip_test_reason) = match tool_configuration.test_strategy {
TestStrategy::Skip => (true, "the argument --test=skip was set".to_string()),
TestStrategy::Native => {
// Skip if `host_platform != build_platform` and `target_platform != noarch`
if output.build_configuration.target_platform != Platform::NoArch
&& output.build_configuration.host_platform.platform
!= output.build_configuration.build_platform.platform
{
let reason = format!(
"the argument --test=native was set and the build is a cross-compilation (target_platform={}, build_platform={}, host_platform={})",
output.build_configuration.target_platform,
output.build_configuration.build_platform.platform,
output.build_configuration.host_platform.platform
);
(true, reason)
} else {
(false, "".to_string())
}
}
TestStrategy::NativeAndEmulated => (false, "".to_string()),
};
if skip_test {
tracing::info!("Skipping tests because {}", skip_test_reason);
build_reindexed_channels(&output.build_configuration, &tool_configuration)
.await
.into_diagnostic()
.context("failed to reindex output channel")?;
} else {
test_queue.push((output, archive));
let is_last_iteration = index == outputs_to_build.len() - 1;
let to_test = if is_last_iteration {
// On last iteration, test everything in the queue
std::mem::take(&mut test_queue)
} else {
// Update the test queue with the tests that we can't run yet
let (to_test, new_test_queue) = test_queue
.into_iter()
.partition(|(output, _)| can_test(output, &all_output_names, &outputs));
test_queue = new_test_queue;
to_test
};
for (output, archive) in &to_test {
match package_test::run_test(
archive,
&TestConfiguration {
test_prefix: output
.build_configuration
.directories
.output_dir
.join("test"),
target_platform: Some(output.build_configuration.target_platform),
host_platform: Some(output.build_configuration.host_platform.clone()),
current_platform: output.build_configuration.build_platform.clone(),
keep_test_prefix: tool_configuration.no_clean,
channels: build_reindexed_channels(
&output.build_configuration,
&tool_configuration,
)
.await
.into_diagnostic()
.context("failed to reindex output channel")?,
channel_priority: tool_configuration.channel_priority,
solve_strategy: SolveStrategy::Highest,
tool_configuration: tool_configuration.clone(),
test_index: None,
output_dir: output.build_configuration.directories.output_dir.clone(),
debug: output.build_configuration.debug,
exclude_newer: output.build_configuration.exclude_newer,
},
None,
)
.await
{
Ok(_) => {}
Err(e) => {
// move the package file to the failed directory
let failed_dir = output
.build_configuration
.directories
.output_dir
.join("broken");
fs::create_dir_all(&failed_dir).into_diagnostic()?;
fs::rename(archive, failed_dir.join(archive.file_name().unwrap()))
.into_diagnostic()?;
if tool_configuration.continue_on_failure == ContinueOnFailure::Yes {
tracing::error!("Test failed for {}: {}", output.identifier(), e);
output.record_warning(&format!("Test failed: {}", e));
} else {
return Err(miette::miette!("Test failed: {}", e));
}
}
}
}
}
}
let span = tracing::info_span!("Build summary");
let _enter = span.enter();
for output in outputs {
// print summaries for each output
let _ = output.log_build_summary().map_err(|e| {
tracing::error!("Error writing build summary: {}", e);
e
});
}
Ok(())
}
/// Check if the noarch builds should be skipped because the noarch platform has
/// been set
pub async fn skip_noarch(
mut outputs: Vec<Output>,
tool_configuration: &tool_configuration::Configuration,
) -> miette::Result<Vec<Output>> {
if let Some(noarch_build_platform) = tool_configuration.noarch_build_platform {
outputs.retain(|output| {
// Skip the build if:
// - target_platform is "noarch"
// and
// - build_platform != noarch_build_platform
let should_skip = output.build_configuration.target_platform == Platform::NoArch
&& output.build_configuration.build_platform.platform != noarch_build_platform;
if should_skip {
// The identifier should always be set at this point
tracing::info!(
"Skipping build because noarch_build_platform is set to {} for {}",
noarch_build_platform,
output.identifier()
);
}
!should_skip
});
}
Ok(outputs)
}
/// Runs test.
pub async fn run_test(
test_data: TestData,
fancy_log_handler: Option<LoggingOutputHandler>,
) -> miette::Result<()> {
let package_file = canonicalize(test_data.package_file).into_diagnostic()?;
let mut tool_config_builder = Configuration::builder();
// Determine virtual packages of the system. These packages define the
// capabilities of the system. Some packages depend on these virtual
// packages to indicate compatibility with the hardware of the system.
let current_platform = if let Some(fancy_log_handler) = fancy_log_handler {
tool_config_builder =
tool_config_builder.with_logging_output_handler(fancy_log_handler.clone());
fancy_log_handler
.wrap_in_progress("determining virtual packages", move || {
PlatformWithVirtualPackages::detect(&VirtualPackageOverrides::from_env())
})
.into_diagnostic()?
} else {
PlatformWithVirtualPackages::detect(&VirtualPackageOverrides::from_env())
.into_diagnostic()?
};
let tool_config = tool_config_builder
.with_keep_build(true)
.with_compression_threads(test_data.compression_threads)
.with_reqwest_client(
tool_configuration::reqwest_client_from_auth_storage(
test_data.common.auth_file,
test_data.common.s3_config,
test_data.common.mirror_config,
test_data.common.allow_insecure_host.clone(),
)
.into_diagnostic()?,
)
.with_channel_priority(test_data.common.channel_priority)
.finish();
let channels = test_data
.channels
.unwrap_or(vec![NamedChannelOrUrl::Name("conda-forge".to_string())]);
let channels = channels
.into_iter()
.map(|c| c.into_base_url(&tool_config.channel_config))
.collect::<Result<Vec<_>, _>>()
.into_diagnostic()?;
let tempdir = tempfile::tempdir().into_diagnostic()?;
let test_options = TestConfiguration {
test_prefix: tempdir.path().to_path_buf(),
target_platform: None,
host_platform: None,
current_platform,
keep_test_prefix: false,
test_index: test_data.test_index,
channels,
channel_priority: tool_config.channel_priority,
solve_strategy: SolveStrategy::Highest,
tool_configuration: tool_config,
output_dir: test_data.common.output_dir,
debug: test_data.debug,
exclude_newer: None,
};
let package_name = package_file
.file_name()
.ok_or_else(|| miette::miette!("Could not get file name from package file"))?
.to_string_lossy()
.to_string();
let span = tracing::info_span!("Running tests for", package = %package_name);
let _enter = span.enter();
package_test::run_test(&package_file, &test_options, None)
.await
.into_diagnostic()?;
Ok(())
}
/// Rebuild.
pub async fn rebuild(
rebuild_data: RebuildData,
fancy_log_handler: LoggingOutputHandler,
) -> miette::Result<()> {
let reqwest_client = tool_configuration::reqwest_client_from_auth_storage(
rebuild_data.common.auth_file,
rebuild_data.common.s3_config.clone(),
rebuild_data.common.mirror_config.clone(),
rebuild_data.common.allow_insecure_host.clone(),
)
.into_diagnostic()?;
// Check if the input is a URL or local path
let (_temp_dir_guard, package_path) = match rebuild_data.package_file {
PackageSource::Url(ref url) => {
// Download the package to a temporary location
tracing::info!("Downloading package from {}", url);
let response = reqwest_client
.get_client()
.get(url.as_str())
.send()
.await
.into_diagnostic()?;
if !response.status().is_success() {
miette::bail!("Failed to download package: HTTP {}", response.status());
}
// Extract filename from URL or use a default
let Some(filename) = url
.path_segments()
.and_then(|mut segments| segments.next_back())
.map(|s| s.to_string())
else {
miette::bail!("Failed to extract filename from URL: {}", url);
};
let temp_dir = tempfile::tempdir().into_diagnostic()?;
let package_path = temp_dir.path().join(filename);
let bytes = response.bytes().await.into_diagnostic()?;
fs::write(&package_path, &bytes).into_diagnostic()?;
tracing::info!("Downloaded package to: {:?}", package_path);
// Keep the temp directory alive for the duration
(Some(temp_dir), package_path)
}
PackageSource::Path(ref path) => {
// Use the local path directly
(None, path.clone())
}
};
// Calculate SHA256 of the original package
let original_sha = rattler_digest::compute_file_digest::<rattler_digest::Sha256>(&package_path)
.into_diagnostic()?;
tracing::info!("Original package SHA256: {:x}", original_sha);
tracing::info!("Rebuilding \"{}\"", package_path.display());
// we extract the recipe folder from the package file (info/recipe/*)
// and then run the rendered recipe with the same arguments as the original
// build
let temp_folder = tempfile::tempdir().into_diagnostic()?;
rebuild::extract_recipe(&package_path, temp_folder.path()).into_diagnostic()?;
let temp_dir = temp_folder.keep();
tracing::info!("Extracted recipe to: {:?}", temp_dir);
let rendered_recipe =
fs::read_to_string(temp_dir.join("rendered_recipe.yaml")).into_diagnostic()?;
let mut output: Output = serde_yaml::from_str(&rendered_recipe).into_diagnostic()?;
// set recipe dir to the temp folder
output.build_configuration.directories.recipe_dir = temp_dir;
// Use a temporary directory for the build output to avoid overwriting the original
let temp_output_dir = tempfile::tempdir().into_diagnostic()?;
let temp_output_path = temp_output_dir.path().to_path_buf();
fs::create_dir_all(&temp_output_path).into_diagnostic()?;
output.build_configuration.directories.output_dir = temp_output_path.clone();
let tool_config = Configuration::builder()
.with_logging_output_handler(fancy_log_handler)
.with_keep_build(true)
.with_compression_threads(rebuild_data.compression_threads)
.with_reqwest_client(reqwest_client)
.with_test_strategy(rebuild_data.test)
.finish();
output
.build_configuration
.directories
.recreate_directories()
.into_diagnostic()?;
let (rebuilt_output, temp_rebuilt_path) =
run_build(output, &tool_config, WorkingDirectoryBehavior::Cleanup).await?;
// Generate timestamp for the rebuilt package
let timestamp = chrono::Utc::now().format("%Y%m%d-%H%M%S");
// Create final output directory
let final_output_dir = rebuild_data.common.output_dir.clone();
fs::create_dir_all(&final_output_dir).into_diagnostic()?;
// Insert timestamp before the extension
let new_filename = format!(
"{}-{}-{}-rebuilt-{timestamp}{}",
rebuilt_output.name().as_normalized(),
rebuilt_output.version(),
rebuilt_output.build_string(),
rebuilt_output
.build_configuration
.packaging_settings
.archive_type
.extension()
);
let rebuilt_path = final_output_dir.join(&new_filename);
// Move the rebuilt package to final location with new name
fs::rename(&temp_rebuilt_path, &rebuilt_path).into_diagnostic()?;
// Now we can drop the temp directory
drop(temp_output_dir);
// Calculate SHA256 of the rebuilt package
let rebuilt_sha = rattler_digest::compute_file_digest::<rattler_digest::Sha256>(&rebuilt_path)
.into_diagnostic()?;
tracing::info!("Rebuilt package SHA256: {:x}", rebuilt_sha);
tracing::info!("Rebuilt package saved to: \"{:?}\"", rebuilt_path);
// Compare the SHA hashes
if original_sha == rebuilt_sha {
tracing::info!(
"✅ Rebuild successful! SHA256 hashes match. Packages are bit-for-bit identical!"
);
} else {
tracing::warn!("❌ Rebuild produced different output! SHA256 hashes do not match.");
tracing::info!("❌ Rebuild produced different output!");
tracing::info!(" Original SHA256: {:x}", original_sha);
tracing::info!(" Rebuilt SHA256: {:x}", rebuilt_sha);
tracing::info!(" Rebuilt package: {}", rebuilt_path.display());
// Check if diffoscope is available
let diffoscope_available = Command::new("diffoscope").arg("--version").output().is_ok();
if diffoscope_available {
let confirmation = Confirm::new()
.with_prompt("Do you want to run diffoscope?")
.interact()
.unwrap();
if confirmation {
let mut command = Command::new("diffoscope");
command
.arg(&package_path)
.arg(&rebuilt_path)
.arg("--text-color")
.arg("always");
tracing::info!("Running diffoscope: {:?}", command);
let output = command.output().into_diagnostic()?;
tracing::info!("{}", String::from_utf8_lossy(&output.stdout));
if !output.stderr.is_empty() {
tracing::info!("{}", String::from_utf8_lossy(&output.stderr));
}
}
} else {
tracing::info!("\nHint: Install diffoscope to see detailed differences:");
tracing::info!(" pixi global install diffoscope");
}
}
Ok(())
}
/// Sort the build outputs (recipes) topologically based on their dependencies.
pub fn sort_build_outputs_topologically(
outputs: &mut Vec<Output>,
up_to: Option<&str>,
) -> miette::Result<()> {
let mut graph = DiGraph::<usize, ()>::new();
let mut name_to_index = HashMap::new();
// Index outputs by their produced names for quick lookup
for (idx, output) in outputs.iter().enumerate() {
let idx = graph.add_node(idx);
name_to_index.insert(output.name().clone(), idx);
}
// Add edges based on dependencies
for output in outputs.iter() {
let output_idx = *name_to_index
.get(output.name())
.expect("We just inserted it");
for dep in output.recipe.requirements().run_build_host() {
let dep_name = match dep {
Dependency::Spec(spec) => spec
.name
.clone()
.expect("MatchSpec should always have a name"),
Dependency::PinSubpackage(pin) => pin.pin_value().name.clone(),
Dependency::PinCompatible(pin) => pin.pin_value().name.clone(),
};
if let Some(&dep_idx) = name_to_index.get(&dep_name) {
// do not point to self (circular dependency) - this can happen with
// pin_subpackage in run_exports, for example.
if output_idx == dep_idx {
continue;
}
graph.add_edge(output_idx, dep_idx, ());
}
}
}
let sorted_indices = if let Some(up_to) = up_to {
// Find the node index for the "up-to" package
let up_to_index = name_to_index.get(up_to).copied().ok_or_else(|| {
miette::miette!("The package '{}' was not found in the outputs", up_to)
})?;
// Perform a DFS post-order traversal from the "up-to" node to find all
// dependencies
let mut dfs = DfsPostOrder::new(&graph, up_to_index);
let mut sorted_indices = Vec::new();
while let Some(nx) = dfs.next(&graph) {
sorted_indices.push(nx);
}
sorted_indices
} else {
// Perform topological sort
let mut sorted_indices = toposort(&graph, None).map_err(|cycle| {
let node = cycle.node_id();
let name = outputs[node.index()].name();
miette::miette!("Cycle detected in dependencies: {}", name.as_source())
})?;
sorted_indices.reverse();
sorted_indices
};