cargo-hack 0.6.44

Cargo subcommand to provide various options useful for testing and continuous integration.
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
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Refs:
// - https://doc.rust-lang.org/nightly/cargo/commands/cargo-metadata.html#output-format
// - https://github.com/rust-lang/cargo/blob/0.47.0/src/cargo/ops/cargo_output_metadata.rs#L56-L63
// - https://github.com/rust-lang/cargo/blob/0.47.0/src/cargo/core/package.rs#L57-L80
// - https://github.com/oli-obk/cargo_metadata

use std::{
    collections::{BTreeMap, HashMap},
    ffi::OsStr,
    ops,
    path::{Path, PathBuf},
};

use anyhow::{Context as _, Result, format_err};
use cargo_config2::Config;
use serde_json::{Map, Value};

use crate::{cargo, cli::Args, fs, process::ProcessBuilder, restore, term};

type Object = Map<String, Value>;
type ParseResult<T> = Result<T, &'static str>;

/// An opaque unique identifier for referring to the package.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub(crate) struct PackageId {
    index: usize,
}

pub(crate) struct Metadata {
    pub(crate) cargo_version: u32,
    /// List of all packages in the workspace and all feature-enabled dependencies.
    //
    /// This doesn't contain dependencies if cargo-metadata is run with --no-deps.
    pub(crate) packages: Box<[Package]>,
    /// List of members of the workspace.
    pub(crate) workspace_members: Box<[PackageId]>,
    /// The resolved dependency graph for the entire workspace.
    pub(crate) resolve: Resolve,
    /// The absolute path to the root of the workspace.
    pub(crate) workspace_root: PathBuf,
}

impl Metadata {
    pub(crate) fn new(
        manifest_path: Option<&str>,
        cargo: &OsStr,
        mut cargo_version: u32,
        args: &Args,
        restore: &mut restore::Manager,
    ) -> Result<Self> {
        let stable_cargo_version =
            cargo::version(cmd!("rustup", "run", "stable", "cargo")).map_or(0, |v| v.minor);

        let config;
        let include_deps_features = if args.include_deps_features {
            config = Config::load()?;
            let targets = config.build_target_for_cli(&args.target)?;
            let host = config.host_triple()?;
            Some((targets, host))
        } else {
            None
        };

        let mut cmd;
        let append_metadata_args = |cmd: &mut ProcessBuilder<'_>| {
            cmd.arg("metadata");
            cmd.arg("--format-version=1");
            if let Some(manifest_path) = manifest_path {
                cmd.arg("--manifest-path");
                cmd.arg(manifest_path);
            }
            if let Some((targets, host)) = &include_deps_features {
                if targets.is_empty() {
                    cmd.arg("--filter-platform");
                    cmd.arg(host);
                } else {
                    for target in targets {
                        cmd.arg("--filter-platform");
                        cmd.arg(target);
                    }
                }
                // features-related flags are unneeded when --no-deps is used.
                // TODO:
                // cmd.arg("--all-features");
            } else {
                cmd.arg("--no-deps");
            }
        };
        let json = if stable_cargo_version > cargo_version {
            cmd = cmd!(cargo, "metadata", "--format-version=1", "--no-deps");
            if let Some(manifest_path) = manifest_path {
                cmd.arg("--manifest-path");
                cmd.arg(manifest_path);
            }
            let no_deps_raw = cmd.read()?;
            let no_deps: Object = serde_json::from_str(&no_deps_raw)
                .with_context(|| format!("failed to parse output from {cmd}"))?;
            let lockfile =
                Path::new(no_deps["workspace_root"].as_str().unwrap()).join("Cargo.lock");
            if !lockfile.exists() {
                let mut cmd = cmd!(cargo, "generate-lockfile");
                if let Some(manifest_path) = manifest_path {
                    cmd.arg("--manifest-path");
                    cmd.arg(manifest_path);
                }
                cmd.run_with_output()?;
            }
            let guard = term::verbose::scoped(false);
            restore.register_always(fs::read(&lockfile)?, lockfile);
            // Try with stable cargo because if workspace member has
            // a dependency that requires newer cargo features, `cargo metadata`
            // with older cargo may fail.
            cmd = cmd!("rustup", "run", "stable", "cargo");
            append_metadata_args(&mut cmd);
            let json = cmd.read();
            restore.restore_last()?;
            drop(guard);
            match json {
                Ok(json) => {
                    cargo_version = stable_cargo_version;
                    json
                }
                Err(_e) => {
                    if include_deps_features.is_some() {
                        // If failed, try again with the version of cargo we will actually use.
                        cmd = cmd!(cargo);
                        append_metadata_args(&mut cmd);
                        cmd.read()?
                    } else {
                        no_deps_raw
                    }
                }
            }
        } else {
            cmd = cmd!(cargo);
            append_metadata_args(&mut cmd);
            cmd.read()?
        };

        let map = serde_json::from_str(&json)
            .with_context(|| format!("failed to parse output from {cmd}"))?;
        Self::from_obj(map, cargo_version)
            .map_err(|s| format_err!("failed to parse `{s}` field from metadata"))
    }

    fn from_obj(mut map: Object, cargo_version: u32) -> ParseResult<Self> {
        let raw_packages = map.remove_array("packages")?;
        let mut packages = Vec::with_capacity(raw_packages.len());
        let mut pkg_id_map = HashMap::with_capacity(raw_packages.len());
        for (i, pkg) in raw_packages.into_iter().enumerate() {
            let (id, pkg) = Package::from_value(pkg, cargo_version)?;
            pkg_id_map.insert(id, i);
            packages.push(pkg);
        }
        let workspace_members = map
            .remove_array("workspace_members")?
            .into_iter()
            .map(|v| -> ParseResult<_> {
                let id: String = into_string(v).ok_or("workspace_members")?;
                Ok(PackageId { index: pkg_id_map[&id] })
            })
            .collect::<Result<_, _>>()?;
        let resolve = match map.remove_nullable("resolve", into_object)? {
            Some(resolve) => Resolve::from_obj(resolve, &pkg_id_map, cargo_version)?,
            None => Resolve { nodes: HashMap::default() },
        };
        Ok(Self {
            cargo_version,
            packages: packages.into_boxed_slice(),
            workspace_members,
            resolve,
            workspace_root: map.remove_string("workspace_root")?,
        })
    }
}

impl ops::Index<PackageId> for Metadata {
    type Output = Package;
    #[inline]
    fn index(&self, index: PackageId) -> &Self::Output {
        &self.packages[index.index]
    }
}

/// The resolved dependency graph for the entire workspace.
pub(crate) struct Resolve {
    /// Nodes in a dependency graph.
    ///
    /// This is always empty if cargo-metadata is run with --no-deps.
    pub(crate) nodes: HashMap<PackageId, Node>,
}

impl Resolve {
    fn from_obj(
        mut map: Object,
        pkg_id_map: &HashMap<String, usize>,
        cargo_version: u32,
    ) -> ParseResult<Self> {
        let nodes = map
            .remove_array("nodes")?
            .into_iter()
            .map(|v| -> ParseResult<_> {
                let (id, node) = Node::from_value(v, pkg_id_map, cargo_version)?;
                Ok((PackageId { index: pkg_id_map[&id] }, node))
            })
            .collect::<Result<_, _>>()?;
        Ok(Self { nodes })
    }
}

/// A node in a dependency graph.
pub(crate) struct Node {
    /// The dependencies of this package.
    ///
    /// This is always empty if running with a version of Cargo older than 1.30.
    pub(crate) deps: Box<[NodeDep]>,
}

impl Node {
    fn from_value(
        mut value: Value,
        pkg_id_map: &HashMap<String, usize>,
        cargo_version: u32,
    ) -> ParseResult<(String, Self)> {
        let map = value.as_object_mut().ok_or("nodes")?;

        let id = map.remove_string("id")?;
        Ok((id, Self {
            // This field was added in Rust 1.30.
            deps: if cargo_version >= 30 {
                map.remove_array("deps")?
                    .into_iter()
                    .map(|v| NodeDep::from_value(v, pkg_id_map, cargo_version))
                    .collect::<Result<_, _>>()?
            } else {
                Box::default()
            },
        }))
    }
}

/// A dependency in a node.
pub(crate) struct NodeDep {
    /// The Package ID of the dependency.
    pub(crate) pkg: PackageId,
    /// The kinds of dependencies.
    ///
    /// This is always empty if running with a version of Cargo older than 1.41.
    pub(crate) dep_kinds: Box<[DepKindInfo]>,
}

impl NodeDep {
    fn from_value(
        mut value: Value,
        pkg_id_map: &HashMap<String, usize>,
        cargo_version: u32,
    ) -> ParseResult<Self> {
        let map = value.as_object_mut().ok_or("deps")?;

        let id: String = map.remove_string("pkg")?;
        Ok(Self {
            pkg: PackageId { index: pkg_id_map[&id] },
            // This field was added in Rust 1.41.
            dep_kinds: if cargo_version >= 41 {
                map.remove_array("dep_kinds")?
                    .into_iter()
                    .map(DepKindInfo::from_value)
                    .collect::<Result<_, _>>()?
            } else {
                Box::default()
            },
        })
    }
}

/// Information about a dependency kind.
pub(crate) struct DepKindInfo {
    /// The kind of dependency.
    pub(crate) kind: Option<Box<str>>,
    /// The target platform for the dependency.
    /// This is `None` if it is not a target dependency.
    pub(crate) target: Option<Box<str>>,
}

impl DepKindInfo {
    fn from_value(mut value: Value) -> ParseResult<Self> {
        let map = value.as_object_mut().ok_or("dep_kinds")?;

        Ok(Self {
            kind: map.remove_nullable("kind", into_string)?,
            target: map.remove_nullable("target", into_string)?,
        })
    }
}

pub(crate) struct Package {
    /// The name of the package.
    pub(crate) name: Box<str>,
    // /// The version of the package.
    // pub(crate) version: Box<str>,
    /// List of dependencies of this particular package.
    pub(crate) dependencies: Box<[Dependency]>,
    /// Features provided by the crate, mapped to the features required by that feature.
    pub(crate) features: BTreeMap<Box<str>, Box<[Box<str>]>>,
    /// Absolute path to this package's manifest.
    pub(crate) manifest_path: Box<Path>,
    /// List of registries to which this package may be published.
    ///
    /// This is always `true` if running with a version of Cargo older than 1.39.
    pub(crate) publish: bool,
    /// The minimum supported Rust version of this package.
    ///
    /// This is always `None` if running with a version of Cargo older than 1.58.
    pub(crate) rust_version: Option<Box<str>>,
}

impl Package {
    fn from_value(mut value: Value, cargo_version: u32) -> ParseResult<(String, Self)> {
        let map = value.as_object_mut().ok_or("packages")?;

        let id = map.remove_string("id")?;
        Ok((id, Self {
            name: map.remove_string("name")?,
            // version: map.remove_string("version")?,
            dependencies: map
                .remove_array("dependencies")?
                .into_iter()
                .map(Dependency::from_value)
                .collect::<Result<_, _>>()?,
            features: map
                .remove_object("features")?
                .into_iter()
                .map(|(k, v)| {
                    into_array(v)
                        .and_then(|v| {
                            v.into_iter().map(into_string::<Box<str>>).collect::<Option<_>>()
                        })
                        .map(|v| (k.into_boxed_str(), v))
                })
                .collect::<Option<_>>()
                .ok_or("features")?,
            manifest_path: map.remove_string::<PathBuf>("manifest_path")?.into_boxed_path(),
            // This field was added in Rust 1.39.
            publish: if cargo_version >= 39 {
                // Publishing is unrestricted if null, and forbidden if an empty array.
                map.remove_nullable("publish", into_array)?.is_none_or(|a| !a.is_empty())
            } else {
                true
            },
            // This field was added in Rust 1.58.
            rust_version: if cargo_version >= 58 {
                map.remove_nullable("rust_version", into_string)?
            } else {
                None
            },
        }))
    }

    pub(crate) fn optional_deps(&self) -> impl Iterator<Item = &str> + '_ {
        self.dependencies.iter().filter_map(Dependency::as_feature)
    }
}

/// A dependency of the main crate.
pub(crate) struct Dependency {
    /// The name of the dependency.
    pub(crate) name: Box<str>,
    // /// The version requirement for the dependency.
    // pub(crate) req: Box<str>,
    /// Whether or not this is an optional dependency.
    pub(crate) optional: bool,
    // TODO: support this
    // /// The target platform for the dependency.
    // /// This is `None` if it is not a target dependency.
    // pub(crate) target: Option<Box<str>>,
    /// If the dependency is renamed, this is the new name for the dependency
    /// as a string.
    /// This is `None` if it is not renamed.
    ///
    /// This is always `None` if running with a version of Cargo older than 1.26.
    pub(crate) rename: Option<Box<str>>,
}

impl Dependency {
    fn from_value(mut value: Value) -> ParseResult<Self> {
        let map = value.as_object_mut().ok_or("dependencies")?;

        Ok(Self {
            name: map.remove_string("name")?,
            // req: map.remove_string("req")?,
            optional: map.get("optional").and_then(Value::as_bool).ok_or("optional")?,
            // This field was added in Rust 1.26.
            rename: map.remove_nullable("rename", into_string)?,
        })
    }

    pub(crate) fn as_feature(&self) -> Option<&str> {
        if self.optional { Some(self.rename.as_ref().unwrap_or(&self.name)) } else { None }
    }
}

#[allow(clippy::option_option)]
fn allow_null<T>(value: Value, f: impl FnOnce(Value) -> Option<T>) -> Option<Option<T>> {
    if value.is_null() { Some(None) } else { f(value).map(Some) }
}

fn into_string<S: From<String>>(value: Value) -> Option<S> {
    if let Value::String(string) = value { Some(string.into()) } else { None }
}
fn into_array(value: Value) -> Option<Vec<Value>> {
    if let Value::Array(array) = value { Some(array) } else { None }
}
fn into_object(value: Value) -> Option<Object> {
    if let Value::Object(object) = value { Some(object) } else { None }
}

trait ObjectExt {
    fn remove_string<S: From<String>>(&mut self, key: &'static str) -> ParseResult<S>;
    fn remove_array(&mut self, key: &'static str) -> ParseResult<Vec<Value>>;
    fn remove_object(&mut self, key: &'static str) -> ParseResult<Object>;
    fn remove_nullable<T>(
        &mut self,
        key: &'static str,
        f: impl FnOnce(Value) -> Option<T>,
    ) -> ParseResult<Option<T>>;
}

impl ObjectExt for Object {
    fn remove_string<S: From<String>>(&mut self, key: &'static str) -> ParseResult<S> {
        self.remove(key).and_then(into_string).ok_or(key)
    }
    fn remove_array(&mut self, key: &'static str) -> ParseResult<Vec<Value>> {
        self.remove(key).and_then(into_array).ok_or(key)
    }
    fn remove_object(&mut self, key: &'static str) -> ParseResult<Object> {
        self.remove(key).and_then(into_object).ok_or(key)
    }
    fn remove_nullable<T>(
        &mut self,
        key: &'static str,
        f: impl FnOnce(Value) -> Option<T>,
    ) -> ParseResult<Option<T>> {
        self.remove(key).and_then(|v| allow_null(v, f)).ok_or(key)
    }
}