Skip to content

Commit 5a88fb1

Browse files
committed
tidy: add auto: prefix to --extra-checks syntax
currently this just uses a very simple extension-based heirustic.
1 parent 969987c commit 5a88fb1

File tree

9 files changed

+67
-20
lines changed

9 files changed

+67
-20
lines changed

src/bootstrap/src/core/config/flags.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ pub enum Subcommand {
383383
bless: bool,
384384
#[arg(long)]
385385
/// comma-separated list of other files types to check (accepts py, py:lint,
386-
/// py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck, spellcheck:fix)
386+
/// py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck)
387+
///
388+
/// Any argument can be prefixed with "auto:" to only run if
389+
/// relevant files are modified (eg. "auto:py").
387390
extra_checks: Option<String>,
388391
#[arg(long)]
389392
/// rerun tests even if the inputs are unchanged

src/etc/completions/x.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ complete -c x -n "__fish_x_using_subcommand doc" -l skip-std-check-if-no-downloa
293293
complete -c x -n "__fish_x_using_subcommand doc" -s h -l help -d 'Print help (see more with \'--help\')'
294294
complete -c x -n "__fish_x_using_subcommand test" -l test-args -d 'extra arguments to be passed for the test tool being used (e.g. libtest, compiletest or rustdoc)' -r
295295
complete -c x -n "__fish_x_using_subcommand test" -l compiletest-rustc-args -d 'extra options to pass the compiler when running compiletest tests' -r
296-
complete -c x -n "__fish_x_using_subcommand test" -l extra-checks -d 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck, spellcheck:fix)' -r
296+
complete -c x -n "__fish_x_using_subcommand test" -l extra-checks -d 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck)' -r
297297
complete -c x -n "__fish_x_using_subcommand test" -l compare-mode -d 'mode describing what file the actual ui output will be compared to' -r
298298
complete -c x -n "__fish_x_using_subcommand test" -l pass -d 'force {check,build,run}-pass tests to this mode' -r
299299
complete -c x -n "__fish_x_using_subcommand test" -l run -d 'whether to execute run-* tests' -r

src/etc/completions/x.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ Register-ArgumentCompleter -Native -CommandName 'x' -ScriptBlock {
339339
'x;test' {
340340
[CompletionResult]::new('--test-args', '--test-args', [CompletionResultType]::ParameterName, 'extra arguments to be passed for the test tool being used (e.g. libtest, compiletest or rustdoc)')
341341
[CompletionResult]::new('--compiletest-rustc-args', '--compiletest-rustc-args', [CompletionResultType]::ParameterName, 'extra options to pass the compiler when running compiletest tests')
342-
[CompletionResult]::new('--extra-checks', '--extra-checks', [CompletionResultType]::ParameterName, 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck, spellcheck:fix)')
342+
[CompletionResult]::new('--extra-checks', '--extra-checks', [CompletionResultType]::ParameterName, 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck)')
343343
[CompletionResult]::new('--compare-mode', '--compare-mode', [CompletionResultType]::ParameterName, 'mode describing what file the actual ui output will be compared to')
344344
[CompletionResult]::new('--pass', '--pass', [CompletionResultType]::ParameterName, 'force {check,build,run}-pass tests to this mode')
345345
[CompletionResult]::new('--run', '--run', [CompletionResultType]::ParameterName, 'whether to execute run-* tests')

src/etc/completions/x.py.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ complete -c x.py -n "__fish_x.py_using_subcommand doc" -l skip-std-check-if-no-d
293293
complete -c x.py -n "__fish_x.py_using_subcommand doc" -s h -l help -d 'Print help (see more with \'--help\')'
294294
complete -c x.py -n "__fish_x.py_using_subcommand test" -l test-args -d 'extra arguments to be passed for the test tool being used (e.g. libtest, compiletest or rustdoc)' -r
295295
complete -c x.py -n "__fish_x.py_using_subcommand test" -l compiletest-rustc-args -d 'extra options to pass the compiler when running compiletest tests' -r
296-
complete -c x.py -n "__fish_x.py_using_subcommand test" -l extra-checks -d 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck, spellcheck:fix)' -r
296+
complete -c x.py -n "__fish_x.py_using_subcommand test" -l extra-checks -d 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck)' -r
297297
complete -c x.py -n "__fish_x.py_using_subcommand test" -l compare-mode -d 'mode describing what file the actual ui output will be compared to' -r
298298
complete -c x.py -n "__fish_x.py_using_subcommand test" -l pass -d 'force {check,build,run}-pass tests to this mode' -r
299299
complete -c x.py -n "__fish_x.py_using_subcommand test" -l run -d 'whether to execute run-* tests' -r

src/etc/completions/x.py.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
339339
'x.py;test' {
340340
[CompletionResult]::new('--test-args', '--test-args', [CompletionResultType]::ParameterName, 'extra arguments to be passed for the test tool being used (e.g. libtest, compiletest or rustdoc)')
341341
[CompletionResult]::new('--compiletest-rustc-args', '--compiletest-rustc-args', [CompletionResultType]::ParameterName, 'extra options to pass the compiler when running compiletest tests')
342-
[CompletionResult]::new('--extra-checks', '--extra-checks', [CompletionResultType]::ParameterName, 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck, spellcheck:fix)')
342+
[CompletionResult]::new('--extra-checks', '--extra-checks', [CompletionResultType]::ParameterName, 'comma-separated list of other files types to check (accepts py, py:lint, py:fmt, shell, shell:lint, cpp, cpp:fmt, spellcheck)')
343343
[CompletionResult]::new('--compare-mode', '--compare-mode', [CompletionResultType]::ParameterName, 'mode describing what file the actual ui output will be compared to')
344344
[CompletionResult]::new('--pass', '--pass', [CompletionResultType]::ParameterName, 'force {check,build,run}-pass tests to this mode')
345345
[CompletionResult]::new('--run', '--run', [CompletionResultType]::ParameterName, 'whether to execute run-* tests')

src/etc/completions/x.py.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ _arguments "${_arguments_options[@]}" : \
338338
_arguments "${_arguments_options[@]}" : \
339339
'*--test-args=[extra arguments to be passed for the test tool being used (e.g. libtest, compiletest or rustdoc)]:ARGS:_default' \
340340
'*--compiletest-rustc-args=[extra options to pass the compiler when running compiletest tests]:ARGS:_default' \
341-
'--extra-checks=[comma-separated list of other files types to check (accepts py, py\:lint, py\:fmt, shell, shell\:lint, cpp, cpp\:fmt, spellcheck, spellcheck\:fix)]:EXTRA_CHECKS:_default' \
341+
'--extra-checks=[comma-separated list of other files types to check (accepts py, py\:lint, py\:fmt, shell, shell\:lint, cpp, cpp\:fmt, spellcheck)]:EXTRA_CHECKS:_default' \
342342
'--compare-mode=[mode describing what file the actual ui output will be compared to]:COMPARE MODE:_default' \
343343
'--pass=[force {check,build,run}-pass tests to this mode]:check | build | run:_default' \
344344
'--run=[whether to execute run-* tests]:auto | always | never:_default' \

src/etc/completions/x.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ _arguments "${_arguments_options[@]}" : \
338338
_arguments "${_arguments_options[@]}" : \
339339
'*--test-args=[extra arguments to be passed for the test tool being used (e.g. libtest, compiletest or rustdoc)]:ARGS:_default' \
340340
'*--compiletest-rustc-args=[extra options to pass the compiler when running compiletest tests]:ARGS:_default' \
341-
'--extra-checks=[comma-separated list of other files types to check (accepts py, py\:lint, py\:fmt, shell, shell\:lint, cpp, cpp\:fmt, spellcheck, spellcheck\:fix)]:EXTRA_CHECKS:_default' \
341+
'--extra-checks=[comma-separated list of other files types to check (accepts py, py\:lint, py\:fmt, shell, shell\:lint, cpp, cpp\:fmt, spellcheck)]:EXTRA_CHECKS:_default' \
342342
'--compare-mode=[mode describing what file the actual ui output will be compared to]:COMPARE MODE:_default' \
343343
'--pass=[force {check,build,run}-pass tests to this mode]:check | build | run:_default' \
344344
'--run=[whether to execute run-* tests]:auto | always | never:_default' \

src/tools/tidy/src/ext_tool_checks.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use std::process::Command;
2323
use std::str::FromStr;
2424
use std::{fmt, fs, io};
2525

26+
use crate::CiInfo;
27+
2628
const MIN_PY_REV: (u32, u32) = (3, 9);
2729
const MIN_PY_REV_STR: &str = "≥3.9";
2830

@@ -37,22 +39,26 @@ const RUFF_CONFIG_PATH: &[&str] = &["src", "tools", "tidy", "config", "ruff.toml
3739
const RUFF_CACHE_PATH: &[&str] = &["cache", "ruff_cache"];
3840
const PIP_REQ_PATH: &[&str] = &["src", "tools", "tidy", "config", "requirements.txt"];
3941

42+
const SPELLCHECK_DIRS: &[&str] = &["compiler", "library", "src/bootstrap", "src/librustdoc"];
43+
4044
pub fn check(
4145
root_path: &Path,
4246
outdir: &Path,
47+
ci_info: &CiInfo,
4348
bless: bool,
4449
extra_checks: Option<&str>,
4550
pos_args: &[String],
4651
bad: &mut bool,
4752
) {
48-
if let Err(e) = check_impl(root_path, outdir, bless, extra_checks, pos_args) {
53+
if let Err(e) = check_impl(root_path, outdir, ci_info, bless, extra_checks, pos_args) {
4954
tidy_error!(bad, "{e}");
5055
}
5156
}
5257

5358
fn check_impl(
5459
root_path: &Path,
5560
outdir: &Path,
61+
ci_info: &CiInfo,
5662
bless: bool,
5763
extra_checks: Option<&str>,
5864
pos_args: &[String],
@@ -73,7 +79,13 @@ fn check_impl(
7379
(ExtraCheckArg::from_str(s), s)
7480
})
7581
.filter_map(|(res, src)| match res {
76-
Ok(x) => Some(x),
82+
Ok(arg) => {
83+
if arg.is_inactive_auto(ci_info) {
84+
None
85+
} else {
86+
Some(arg)
87+
}
88+
}
7789
Err(err) => {
7890
eprintln!("warning: bad extra check argument {src:?}: {err:?}");
7991
None
@@ -249,14 +261,9 @@ fn check_impl(
249261
if spellcheck {
250262
let config_path = root_path.join("typos.toml");
251263
// sync target files with .github/workflows/spellcheck.yml
252-
let mut args = vec![
253-
"-c",
254-
config_path.as_os_str().to_str().unwrap(),
255-
"./compiler",
256-
"./library",
257-
"./src/bootstrap",
258-
"./src/librustdoc",
259-
];
264+
let mut args = vec!["-c", config_path.as_os_str().to_str().unwrap()];
265+
266+
args.extend_from_slice(SPELLCHECK_DIRS);
260267

261268
if bless {
262269
eprintln!("spellcheck files and fix");
@@ -663,9 +670,12 @@ enum ExtraCheckParseError {
663670
TooManyParts,
664671
/// Tried to parse the empty string
665672
Empty,
673+
/// `auto` specified without lang part.
674+
AutoRequiresLang,
666675
}
667676

668677
struct ExtraCheckArg {
678+
auto: bool,
669679
lang: ExtraCheckLang,
670680
/// None = run all extra checks for the given lang
671681
kind: Option<ExtraCheckKind>,
@@ -675,21 +685,47 @@ impl ExtraCheckArg {
675685
fn matches(&self, lang: ExtraCheckLang, kind: ExtraCheckKind) -> bool {
676686
self.lang == lang && self.kind.map(|k| k == kind).unwrap_or(true)
677687
}
688+
689+
/// Returns `true` if this is an auto arg and the relevant files are not modified.
690+
fn is_inactive_auto(&self, ci_info: &CiInfo) -> bool {
691+
if !self.auto {
692+
return false;
693+
}
694+
let ext = match self.lang {
695+
ExtraCheckLang::Py => ".py",
696+
ExtraCheckLang::Cpp => ".cpp",
697+
ExtraCheckLang::Shell => ".sh",
698+
ExtraCheckLang::Spellcheck => {
699+
return !crate::files_modified(ci_info, |s| {
700+
SPELLCHECK_DIRS.iter().any(|dir| Path::new(s).starts_with(dir))
701+
});
702+
}
703+
};
704+
!crate::files_modified(ci_info, |s| s.ends_with(ext))
705+
}
678706
}
679707

680708
impl FromStr for ExtraCheckArg {
681709
type Err = ExtraCheckParseError;
682710

683711
fn from_str(s: &str) -> Result<Self, Self::Err> {
712+
let mut auto = false;
684713
let mut parts = s.split(':');
685-
let Some(first) = parts.next() else {
714+
let Some(mut first) = parts.next() else {
686715
return Err(ExtraCheckParseError::Empty);
687716
};
717+
if first == "auto" {
718+
let Some(part) = parts.next() else {
719+
return Err(ExtraCheckParseError::AutoRequiresLang);
720+
};
721+
auto = true;
722+
first = part;
723+
}
688724
let second = parts.next();
689725
if parts.next().is_some() {
690726
return Err(ExtraCheckParseError::TooManyParts);
691727
}
692-
Ok(Self { lang: first.parse()?, kind: second.map(|s| s.parse()).transpose()? })
728+
Ok(Self { auto, lang: first.parse()?, kind: second.map(|s| s.parse()).transpose()? })
693729
}
694730
}
695731

src/tools/tidy/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,15 @@ fn main() {
173173
};
174174
check!(unstable_book, &src_path, collected);
175175

176-
check!(ext_tool_checks, &root_path, &output_directory, bless, extra_checks, pos_args);
176+
check!(
177+
ext_tool_checks,
178+
&root_path,
179+
&output_directory,
180+
&ci_info,
181+
bless,
182+
extra_checks,
183+
pos_args
184+
);
177185
});
178186

179187
if bad.load(Ordering::Relaxed) {

0 commit comments

Comments
 (0)