Skip to content

Commit af35904

Browse files
committed
tidy: skip submodules if not present for non-CI environments
Signed-off-by: onur-ozkan <[email protected]>
1 parent dd50e18 commit af35904

File tree

7 files changed

+63
-28
lines changed

7 files changed

+63
-28
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -5697,6 +5697,7 @@ dependencies = [
56975697
name = "tidy"
56985698
version = "0.1.0"
56995699
dependencies = [
5700+
"build_helper",
57005701
"cargo_metadata 0.15.4",
57015702
"ignore",
57025703
"miropt-test-tools",

src/bootstrap/src/core/build_steps/dist.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,8 @@ impl Step for PlainSourceTarball {
10141014
// perhaps it should be removed in favor of making `dist` perform the `vendor` step?
10151015

10161016
// Ensure we have all submodules from src and other directories checked out.
1017-
for submodule in builder.get_all_submodules() {
1017+
for submodule in build_helper::util::parse_gitmodules(&builder.src.join(".gitmodules"))
1018+
{
10181019
builder.update_submodule(Path::new(submodule));
10191020
}
10201021

src/bootstrap/src/core/builder.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ use std::collections::BTreeSet;
44
use std::env;
55
use std::ffi::{OsStr, OsString};
66
use std::fmt::{Debug, Write};
7-
use std::fs::{self, File};
7+
use std::fs;
88
use std::hash::Hash;
9-
use std::io::{BufRead, BufReader};
109
use std::ops::Deref;
1110
use std::path::{Path, PathBuf};
1211
use std::process::Command;
13-
use std::sync::OnceLock;
1412
use std::time::{Duration, Instant};
1513

1614
use crate::core::build_steps::tool::{self, SourceType};
@@ -577,7 +575,8 @@ impl<'a> ShouldRun<'a> {
577575
///
578576
/// [`path`]: ShouldRun::path
579577
pub fn paths(mut self, paths: &[&str]) -> Self {
580-
let submodules_paths = self.builder.get_all_submodules();
578+
let gitmodules = self.builder.src.join(".gitmodules");
579+
let submodules_paths = build_helper::util::parse_gitmodules(&gitmodules);
581580

582581
self.paths.insert(PathSet::Set(
583582
paths
@@ -2238,28 +2237,6 @@ impl<'a> Builder<'a> {
22382237
out
22392238
}
22402239

2241-
/// Return paths of all submodules.
2242-
pub fn get_all_submodules(&self) -> &[String] {
2243-
static SUBMODULES_PATHS: OnceLock<Vec<String>> = OnceLock::new();
2244-
2245-
let init_submodules_paths = |src: &PathBuf| {
2246-
let file = File::open(src.join(".gitmodules")).unwrap();
2247-
2248-
let mut submodules_paths = vec![];
2249-
for line in BufReader::new(file).lines().map_while(Result::ok) {
2250-
let line = line.trim();
2251-
if line.starts_with("path") {
2252-
let actual_path = line.split(' ').last().expect("Couldn't get value of path");
2253-
submodules_paths.push(actual_path.to_owned());
2254-
}
2255-
}
2256-
2257-
submodules_paths
2258-
};
2259-
2260-
SUBMODULES_PATHS.get_or_init(|| init_submodules_paths(&self.src))
2261-
}
2262-
22632240
/// Ensure that a given step is built *only if it's supposed to be built by default*, returning
22642241
/// its output. This will cache the step, so it's safe (and good!) to call this as often as
22652242
/// needed to ensure that all dependencies are build.

src/tools/build_helper/src/util.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
use std::fs::File;
2+
use std::io::{BufRead, BufReader};
3+
use std::path::PathBuf;
14
use std::process::Command;
5+
use std::sync::OnceLock;
26

37
/// Invokes `build_helper::util::detail_exit` with `cfg!(test)`
48
///
@@ -45,3 +49,25 @@ pub fn try_run(cmd: &mut Command, print_cmd_on_fail: bool) -> Result<(), ()> {
4549
Ok(())
4650
}
4751
}
52+
53+
/// Returns submodule paths from the given gitmodules file.
54+
pub fn parse_gitmodules(gitmodules: &PathBuf) -> &[String] {
55+
static SUBMODULES_PATHS: OnceLock<Vec<String>> = OnceLock::new();
56+
57+
let init_submodules_paths = || {
58+
let file = File::open(gitmodules).unwrap();
59+
60+
let mut submodules_paths = vec![];
61+
for line in BufReader::new(file).lines().map_while(Result::ok) {
62+
let line = line.trim();
63+
if line.starts_with("path") {
64+
let actual_path = line.split(' ').last().expect("Couldn't get value of path");
65+
submodules_paths.push(actual_path.to_owned());
66+
}
67+
}
68+
69+
submodules_paths
70+
};
71+
72+
SUBMODULES_PATHS.get_or_init(|| init_submodules_paths())
73+
}

src/tools/tidy/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55
autobins = false
66

77
[dependencies]
8+
build_helper = { path = "../build_helper" }
89
cargo_metadata = "0.15"
910
regex = "1"
1011
miropt-test-tools = { path = "../miropt-test-tools" }

src/tools/tidy/src/deps.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Checks the licenses of third-party dependencies.
22
3+
use build_helper::ci::CiEnv;
34
use cargo_metadata::{Metadata, Package, PackageId};
45
use std::collections::HashSet;
6+
use std::fs::read_dir;
57
use std::path::Path;
68

79
/// These are licenses that are allowed for all crates, including the runtime,
@@ -515,6 +517,19 @@ pub fn check(root: &Path, cargo: &Path, bad: &mut bool) {
515517
let mut checked_runtime_licenses = false;
516518

517519
for &(workspace, exceptions, permitted_deps) in WORKSPACES {
520+
let gitmodules = root.join(".gitmodules");
521+
let submodules = build_helper::util::parse_gitmodules(&gitmodules);
522+
// Skip if it's a submodule, not in a CI environment, and not initialized.
523+
//
524+
// This prevents enforcing developers to fetch submodules for tidy.
525+
if submodules.contains(&workspace.into())
526+
&& !CiEnv::is_ci()
527+
// If the directory is empty, we can consider it as an uninitialized submodule.
528+
&& read_dir(root.join(workspace)).unwrap().next().is_none()
529+
{
530+
continue;
531+
}
532+
518533
if !root.join(workspace).join("Cargo.lock").exists() {
519534
tidy_error!(bad, "the `{workspace}` workspace doesn't have a Cargo.lock");
520535
continue;

src/tools/tidy/src/extdeps.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Check for external package sources. Allow only vendorable packages.
22
3-
use std::fs;
3+
use build_helper::ci::CiEnv;
4+
use std::fs::{self, read_dir};
45
use std::path::Path;
56

67
/// List of allowed sources for packages.
@@ -14,6 +15,19 @@ const ALLOWED_SOURCES: &[&str] = &[
1415
/// workspace `Cargo.toml`.
1516
pub fn check(root: &Path, bad: &mut bool) {
1617
for &(workspace, _, _) in crate::deps::WORKSPACES {
18+
let gitmodules = root.join(".gitmodules");
19+
let submodules = build_helper::util::parse_gitmodules(&gitmodules);
20+
// Skip if it's a submodule, not in a CI environment, and not initialized.
21+
//
22+
// This prevents enforcing developers to fetch submodules for tidy.
23+
if submodules.contains(&workspace.into())
24+
&& !CiEnv::is_ci()
25+
// If the directory is empty, we can consider it as an uninitialized submodule.
26+
&& read_dir(root.join(workspace)).unwrap().next().is_none()
27+
{
28+
continue;
29+
}
30+
1731
// FIXME check other workspaces too
1832
// `Cargo.lock` of rust.
1933
let path = root.join(workspace).join("Cargo.lock");

0 commit comments

Comments
 (0)