tidy/
extdeps.rs

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