-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
resolve.rs
593 lines (529 loc) · 18.7 KB
/
resolve.rs
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
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet},
iter, mem,
path::Path,
str::FromStr,
sync::Arc,
};
use binstalk_fetchers::FETCHER_GH_CRATE_META;
use binstalk_types::{
cargo_toml_binstall::Strategy,
crate_info::{CrateSource, SourceType},
};
use compact_str::{CompactString, ToCompactString};
use itertools::Itertools;
use leon::Template;
use maybe_owned::MaybeOwned;
use semver::{Version, VersionReq};
use tokio::{task::spawn_blocking, time::timeout};
use tracing::{debug, error, info, instrument, warn};
use url::Url;
use crate::{
bins,
errors::{BinstallError, VersionParseError},
fetchers::{Data, Fetcher, TargetData},
helpers::{
cargo_toml::Manifest, cargo_toml_workspace::load_manifest_from_workspace,
download::ExtractedFiles, remote::Client, target_triple::TargetTriple,
tasks::AutoAbortJoinHandle,
},
manifests::cargo_toml_binstall::{Meta, PkgMeta, PkgOverride},
ops::{CargoTomlFetchOverride, Options},
};
mod crate_name;
#[doc(inline)]
pub use crate_name::CrateName;
mod version_ext;
#[doc(inline)]
pub use version_ext::VersionReqExt;
mod resolution;
#[doc(inline)]
pub use resolution::{Resolution, ResolutionFetch, ResolutionSource};
#[instrument(skip_all)]
pub async fn resolve(
opts: Arc<Options>,
crate_name: CrateName,
curr_version: Option<Version>,
) -> Result<Resolution, BinstallError> {
let crate_name_name = crate_name.name.clone();
let resolution = resolve_inner(opts, crate_name, curr_version)
.await
.map_err(|err| err.crate_context(crate_name_name))?;
Ok(resolution)
}
async fn resolve_inner(
opts: Arc<Options>,
crate_name: CrateName,
curr_version: Option<Version>,
) -> Result<Resolution, BinstallError> {
info!("Resolving package: '{}'", crate_name);
let version_req = match (&crate_name.version_req, &opts.version_req) {
(Some(version), None) => MaybeOwned::Borrowed(version),
(None, Some(version)) => MaybeOwned::Borrowed(version),
(Some(_), Some(_)) => Err(BinstallError::SuperfluousVersionOption)?,
(None, None) => MaybeOwned::Owned(VersionReq::STAR),
};
let version_req_str = version_req.to_compact_string();
let Some(package_info) = PackageInfo::resolve(
&opts,
crate_name.name,
curr_version,
&version_req,
opts.client.clone(),
)
.await?
else {
return Ok(Resolution::AlreadyUpToDate);
};
let desired_targets = opts
.desired_targets
.get()
.await
.iter()
.map(|target| {
debug!("Building metadata for target: {target}");
let meta = package_info.meta.merge_overrides(
iter::once(&opts.cli_overrides).chain(package_info.overrides.get(target)),
);
debug!("Found metadata: {meta:?}");
Ok(Arc::new(TargetData {
target: target.clone(),
meta,
target_related_info: TargetTriple::from_str(target)?,
}))
})
.collect::<Result<Vec<_>, BinstallError>>()?;
let resolvers = &opts.resolvers;
let binary_name = match package_info.binaries.as_slice() {
[bin] if bin.name != package_info.name => Some(CompactString::from(bin.name.as_str())),
_ => None,
};
let mut handles: Vec<Arc<dyn Fetcher>> = Vec::with_capacity(
desired_targets.len() * resolvers.len()
+ if binary_name.is_some() {
desired_targets.len()
} else {
0
},
);
let gh_api_client = opts.gh_api_client.get().await?;
let mut handles_fn =
|data: Arc<Data>, filter_fetcher_by_name_predicate: fn(&'static str) -> bool| {
handles.extend(
resolvers
.iter()
.cartesian_product(&desired_targets)
.filter_map(|(f, target_data)| {
let fetcher = f(
opts.client.clone(),
gh_api_client.clone(),
data.clone(),
target_data.clone(),
opts.signature_policy,
);
if let Some(disabled_strategies) =
target_data.meta.disabled_strategies.as_deref()
{
if disabled_strategies.contains(&fetcher.strategy()) {
return None;
}
}
filter_fetcher_by_name_predicate(fetcher.fetcher_name()).then_some(fetcher)
}),
)
};
handles_fn(
Arc::new(Data::new(
package_info.name.clone(),
package_info.version_str.clone(),
package_info.repo.clone(),
)),
|_| true,
);
if let Some(binary_name) = binary_name {
handles_fn(
Arc::new(Data::new(
binary_name,
package_info.version_str.clone(),
package_info.repo.clone(),
)),
|name| name == FETCHER_GH_CRATE_META,
);
}
if !opts.disable_telemetry {
for fetcher in &handles {
fetcher.clone().report_to_upstream();
}
}
for fetcher in handles {
match timeout(
opts.maximum_resolution_timeout,
AutoAbortJoinHandle::new(fetcher.clone().find()).flattened_join(),
)
.await
{
Ok(ret) => match ret {
Ok(true) => {
// Generate temporary binary path
let bin_path = opts.temp_dir.join(format!(
"bin-{}-{}-{}",
package_info.name,
fetcher.target(),
fetcher.fetcher_name()
));
match download_extract_and_verify(
fetcher.as_ref(),
&bin_path,
&package_info,
&opts.install_path,
opts.no_symlinks,
)
.await
{
Ok(bin_files) => {
if !bin_files.is_empty() {
return Ok(Resolution::Fetch(Box::new(ResolutionFetch {
fetcher,
new_version: package_info.version,
name: package_info.name,
version_req: version_req_str,
source: package_info.source,
bin_files,
})));
} else {
warn!(
"Error when checking binaries provided by fetcher {}: \
The fetcher does not provide any optional binary",
fetcher.source_name(),
);
}
}
Err(err) => {
if let BinstallError::UserAbort = err {
return Err(err);
}
warn!(
"Error while downloading and extracting from fetcher {}: {}",
fetcher.source_name(),
err
);
}
}
}
Ok(false) => (),
Err(err) => {
warn!(
"Error while checking fetcher {}: {}",
fetcher.source_name(),
err
);
}
},
Err(err) => {
warn!(
"Timeout reached while checking fetcher {}: {}",
fetcher.source_name(),
err
);
}
}
}
if !opts.cargo_install_fallback {
return Err(BinstallError::NoFallbackToCargoInstall);
}
let meta = package_info
.meta
.merge_overrides(iter::once(&opts.cli_overrides));
let target_meta = desired_targets
.first()
.map(|target_data| &target_data.meta)
.unwrap_or(&meta);
if let Some(disabled_strategies) = target_meta.disabled_strategies.as_deref() {
if disabled_strategies.contains(&Strategy::Compile) {
return Err(BinstallError::NoFallbackToCargoInstall);
}
}
Ok(Resolution::InstallFromSource(ResolutionSource {
name: package_info.name,
version: package_info.version_str,
}))
}
/// * `fetcher` - `fetcher.find()` must have returned `Ok(true)`.
///
/// Can return empty Vec if all `BinFile` is optional and does not exist
/// in the archive downloaded.
async fn download_extract_and_verify(
fetcher: &dyn Fetcher,
bin_path: &Path,
package_info: &PackageInfo,
install_path: &Path,
no_symlinks: bool,
) -> Result<Vec<bins::BinFile>, BinstallError> {
// Download and extract it.
// If that fails, then ignore this fetcher.
let extracted_files = fetcher.fetch_and_extract(bin_path).await?;
debug!("extracted_files = {extracted_files:#?}");
// Build final metadata
let meta = fetcher.target_meta();
// Verify that all non-optional bin_files exist
let bin_files = collect_bin_files(
fetcher,
package_info,
meta,
bin_path,
install_path,
no_symlinks,
&extracted_files,
)?;
let name = &package_info.name;
package_info
.binaries
.iter()
.zip(bin_files)
.filter_map(|(bin, bin_file)| {
match bin_file.check_source_exists(&mut |p| extracted_files.has_file(p)) {
Ok(()) => Some(Ok(bin_file)),
// This binary is optional
Err(err) => {
let required_features = &bin.required_features;
let bin_name = bin.name.as_str();
if required_features.is_empty() {
error!(
"When resolving {name} bin {bin_name} is not found. \
This binary is not optional so it must be included in the archive, please contact with \
upstream to fix this issue."
);
// This bin is not optional, error
Some(Err(err))
} else {
// Optional, print a warning and continue.
let features = required_features.iter().format(",");
warn!(
"When resolving {name} bin {bin_name} is not found. \
But since it requires features {features}, this bin is ignored."
);
None
}
}
}
})
.collect::<Result<Vec<bins::BinFile>, bins::Error>>()
.map_err(BinstallError::from)
}
fn collect_bin_files(
fetcher: &dyn Fetcher,
package_info: &PackageInfo,
meta: PkgMeta,
bin_path: &Path,
install_path: &Path,
no_symlinks: bool,
extracted_files: &ExtractedFiles,
) -> Result<Vec<bins::BinFile>, BinstallError> {
// List files to be installed
// based on those found via Cargo.toml
let bin_data = bins::Data {
name: &package_info.name,
target: fetcher.target(),
version: &package_info.version_str,
repo: package_info.repo.as_deref(),
meta,
bin_path,
install_path,
target_related_info: &fetcher.target_data().target_related_info,
};
let bin_dir = bin_data
.meta
.bin_dir
.as_deref()
.map(Cow::Borrowed)
.unwrap_or_else(|| {
bins::infer_bin_dir_template(&bin_data, &mut |p| extracted_files.get_dir(p).is_some())
});
let template = Template::parse(&bin_dir)?;
// Create bin_files
let bin_files = package_info
.binaries
.iter()
.map(|bin| bins::BinFile::new(&bin_data, bin.name.as_str(), &template, no_symlinks))
.collect::<Result<Vec<_>, bins::Error>>()?;
let mut source_set = BTreeSet::new();
for bin in &bin_files {
if !source_set.insert(&bin.source) {
return Err(BinstallError::DuplicateSourceFilePath {
path: bin.source.clone(),
});
}
}
Ok(bin_files)
}
struct PackageInfo {
meta: PkgMeta,
binaries: Vec<Bin>,
name: CompactString,
version_str: CompactString,
source: CrateSource,
version: Version,
repo: Option<String>,
overrides: BTreeMap<String, PkgOverride>,
}
struct Bin {
name: String,
required_features: Vec<String>,
}
impl PackageInfo {
/// Return `None` if already up-to-date.
async fn resolve(
opts: &Options,
name: CompactString,
curr_version: Option<Version>,
version_req: &VersionReq,
client: Client,
) -> Result<Option<Self>, BinstallError> {
use CargoTomlFetchOverride::*;
// Fetch crate via crates.io, git, or use a local manifest path
let (manifest, source) = match opts.cargo_toml_fetch_override.as_ref() {
Some(Path(manifest_path)) => (
spawn_blocking({
let manifest_path = manifest_path.clone();
let name = name.clone();
move || load_manifest_path(manifest_path, &name)
})
.await??,
CrateSource {
source_type: SourceType::Path,
url: MaybeOwned::Owned(Url::parse(&format!(
"file://{}",
manifest_path.display()
))?),
},
),
#[cfg(feature = "git")]
Some(Git(git_url)) => {
use crate::helpers::git::{GitCancellationToken, Repository as GitRepository};
let cancellation_token = GitCancellationToken::default();
// Cancel git operation if the future is cancelled (dropped).
let cancel_on_drop = cancellation_token.clone().cancel_on_drop();
let (ret, commit_hash) = spawn_blocking({
let git_url = git_url.clone();
let name = name.clone();
move || {
let dir = tempfile::TempDir::new()?;
let repo = GitRepository::shallow_clone(
git_url,
dir.as_ref(),
Some(cancellation_token),
)?;
Ok::<_, BinstallError>((
load_manifest_from_workspace(dir.as_ref(), &name)
.map_err(BinstallError::from)?,
repo.get_head_commit_hash()?,
))
}
})
.await??;
// Git operation done, disarm it
cancel_on_drop.disarm();
(
ret,
CrateSource {
source_type: SourceType::Git,
url: MaybeOwned::Owned(Url::parse(&format!("{git_url}#{commit_hash}"))?),
},
)
}
None => (
Box::pin(
opts.registry
.fetch_crate_matched(client, &name, version_req),
)
.await?,
{
let registry = format!("{}", opts.registry);
if registry == "https://index.crates.io/" {
CrateSource::cratesio_registry()
} else {
CrateSource {
source_type: SourceType::Registry,
url: MaybeOwned::Owned(Url::parse(®istry)?),
}
}
},
),
};
let Some(mut package) = manifest.package else {
return Err(BinstallError::CargoTomlMissingPackage(name));
};
let new_version_str = package.version().to_compact_string();
let new_version = match Version::parse(&new_version_str) {
Ok(new_version) => new_version,
Err(err) => {
return Err(Box::new(VersionParseError {
v: new_version_str,
err,
})
.into())
}
};
if let Some(curr_version) = curr_version {
if new_version == curr_version {
info!(
"{} v{curr_version} is already installed, use --force to override",
name
);
return Ok(None);
}
}
let (mut meta, binaries): (_, Vec<Bin>) = (
package
.metadata
.take()
.and_then(|m| m.binstall)
.unwrap_or_default(),
manifest
.bin
.into_iter()
.filter_map(|p| {
p.name.map(|name| Bin {
name,
required_features: p.required_features,
})
})
.collect(),
);
// Check binaries
if binaries.is_empty() {
Err(BinstallError::UnspecifiedBinaries)
} else {
Ok(Some(Self {
overrides: mem::take(&mut meta.overrides),
meta,
binaries,
name,
source,
version_str: new_version_str,
version: new_version,
repo: package.repository().map(ToString::to_string),
}))
}
}
}
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
///
/// This is a blocking function.
pub fn load_manifest_path<P: AsRef<Path>, N: AsRef<str>>(
manifest_path: P,
name: N,
) -> Result<Manifest<Meta>, BinstallError> {
fn inner(manifest_path: &Path, crate_name: &str) -> Result<Manifest<Meta>, BinstallError> {
debug!(
"Reading crate {crate_name} manifest at local path: {}",
manifest_path.display()
);
// Load and parse manifest (this checks file system for binary output names)
let manifest = load_manifest_from_workspace(manifest_path, crate_name)?;
// Return metadata
Ok(manifest)
}
inner(manifest_path.as_ref(), name.as_ref())
}