tidy/
extdeps.rs

1//! Check for external package sources. Allow only vendorable packages.
2
3use std::fs;
4use std::path::Path;
5
6use crate::deps::WorkspaceInfo;
7
8/// List of allowed sources for packages.
9const ALLOWED_SOURCES: &[&str] = &[
10    r#""registry+https://github.com/rust-lang/crates.io-index""#,
11    // This is `rust_team_data` used by `site` in src/tools/rustc-perf,
12    r#""git+https://github.com/rust-lang/team#a5260e76d3aa894c64c56e6ddc8545b9a98043ec""#,
13];
14
15/// Checks for external package sources. `root` is the path to the directory that contains the
16/// workspace `Cargo.toml`.
17pub fn check(root: &Path, bad: &mut bool) {
18    for &WorkspaceInfo { path, submodules, .. } in crate::deps::WORKSPACES {
19        if crate::deps::has_missing_submodule(root, submodules) {
20            continue;
21        }
22
23        // FIXME check other workspaces too
24        // `Cargo.lock` of rust.
25        let lockfile = root.join(path).join("Cargo.lock");
26
27        if !lockfile.exists() {
28            tidy_error!(bad, "the `{path}` workspace doesn't have a Cargo.lock");
29            continue;
30        }
31
32        // Open and read the whole file.
33        let cargo_lock = t!(fs::read_to_string(&lockfile));
34
35        // Process each line.
36        for line in cargo_lock.lines() {
37            // Consider only source entries.
38            if !line.starts_with("source = ") {
39                continue;
40            }
41
42            // Extract source value.
43            let source = line.split_once('=').unwrap().1.trim();
44
45            // Ensure source is allowed.
46            if !ALLOWED_SOURCES.contains(&source) {
47                tidy_error!(bad, "invalid source: {}", source);
48            }
49        }
50    }
51}