Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// The CLI specific code lives in the cli module and sub-modules.
#[macro_use]
pub mod log;
mod ci;
pub mod common;
mod docs;
pub mod errors;
Expand Down
50 changes: 50 additions & 0 deletions src/cli/ci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::{fmt, io::Write};

use anyhow::Result;
use clap::{ValueEnum, builder::PossibleValue};

use crate::{config::Cfg, utils::ExitCode};

pub(crate) fn problem_matcher(flavor: Flavor, cfg: &Cfg<'_>) -> Result<ExitCode> {
print_str(flavor.problem_matcher(), cfg)
}

fn print_str(s: &str, cfg: &Cfg<'_>) -> Result<ExitCode> {
let stdout = cfg.process.stdout();
write!(stdout.lock(), "{s}")?;
Ok(ExitCode::SUCCESS)
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) enum Flavor {
Github,
}

impl Flavor {
fn problem_matcher(&self) -> &'static str {
match self {
Self::Github => include_str!("ci/matcher/github.json"),
}
}
}

impl ValueEnum for Flavor {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Github]
}

fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
Some(match self {
Self::Github => PossibleValue::new("github"),
})
}
}

impl fmt::Display for Flavor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.to_possible_value() {
Some(v) => write!(f, "{}", v.get_name()),
None => unreachable!(),
}
}
}
44 changes: 44 additions & 0 deletions src/cli/ci/matcher/github.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{

@rami3l rami3l Sep 1, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some mock tests to ensure that the matchers are more or less correct here.

r7kamura/rust-problem-matchers doesn't have a test suite but I'm looking at something like what GitHub does in setup-go where you basically write a small interpreter and verify at least the regex declarations are correct.
#5011 (comment)

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some mock tests

How do we ensure these still work as Cargo evolves?

"problemMatcher": [
{
"owner": "rust-compiler",
"pattern": [
{
"regexp": "^(?:\\x1B\\[[0-9;]*[a-zA-Z])*(warning|warn|error)(\\[(\\S*)\\])?(?:\\x1B\\[[0-9;]*[a-zA-Z])*: (.*?)(?:\\x1B\\[[0-9;]*[a-zA-Z])*$",
"severity": 1,
"message": 4,
"code": 3
},
{
"regexp": "^(?:\\x1B\\[[0-9;]*[a-zA-Z])*\\s+(?:\\x1B\\[[0-9;]*[a-zA-Z])*-->\\s(?:\\x1B\\[[0-9;]*[a-zA-Z])*(\\S+):(\\d+):(\\d+)(?:\\x1B\\[[0-9;]*[a-zA-Z])*$",
"file": 1,
"line": 2,
"column": 3
}
]
},
{
"owner": "rust-formatter",
"pattern": [
{
"regexp": "^(Diff in (\\S+)) at line (\\d+):",
"message": 1,
"file": 2,
"line": 3
}
]
},
{
"owner": "rust-panic",
"pattern": [
{
"regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+):(\\d+)$",
"message": 1,
"file": 2,
"line": 3,
"column": 4
}
]
}
]
}
28 changes: 27 additions & 1 deletion src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use tracing_subscriber::{EnvFilter, Registry, reload::Handle};

use crate::{
cli::{
ci,
common::{self, PackageUpdate, update_console_filter},
docs,
errors::CliError,
Expand Down Expand Up @@ -304,6 +305,13 @@ enum RustupSubcmd {
#[arg(default_value = "rustup")]
command: CompletionCommand,
},

/// Generate CI-specific configurations
#[command(hide = true)]
Ci {
#[command(subcommand)]
subcmd: CiSubcmd,
},
}

fn update_toolchain_value_parser(s: &str) -> Result<PartialToolchainDesc> {
Expand All @@ -326,6 +334,7 @@ impl RustupSubcmd {
// These subcommands don't require the active toolchain, so auto-installing it should be
// disabled to avoid surprises.
Self::Check { .. }
| Self::Ci { .. }
| Self::Completions { .. }
| Self::Component { .. }
| Self::Default { .. }
Expand All @@ -347,7 +356,10 @@ impl RustupSubcmd {
fn should_warn_empty_setup(&self) -> bool {
match self {
// These subcommands are not about toolchains, so the hint would be noise.
Self::Completions { .. } | Self::DumpTestament | Self::Self_ { .. } => false,
Self::Ci { .. }
| Self::Completions { .. }
| Self::DumpTestament
| Self::Self_ { .. } => false,

// For all other subcommands, the hint may be useful if rustup is still unusable after
// the command has completed.
Expand Down Expand Up @@ -674,6 +686,17 @@ enum SetSubcmd {
},
}

#[derive(Debug, Subcommand)]
#[command(arg_required_else_help = true, subcommand_required = true)]
enum CiSubcmd {
/// Show the example problem matcher for the given CI flavor
#[command(alias = "matcher")]
ProblemMatcher {
#[arg(value_enum)]
flavor: ci::Flavor,
},
}

#[tracing::instrument(level = "trace", fields(args = format!("{:?}", process.args_os().collect::<Vec<_>>())), skip(process, console_filter))]
pub async fn main(
current_dir: PathBuf,
Expand Down Expand Up @@ -869,6 +892,9 @@ pub async fn main(
RustupSubcmd::Completions { shell, command } => {
output_completion_script(shell, command, process)
}
RustupSubcmd::Ci { subcmd } => match subcmd {
CiSubcmd::ProblemMatcher { flavor } => ci::problem_matcher(flavor, cfg),
},
}?;

if should_warn && cfg.list_toolchains()?.is_empty() && cfg.get_default()?.is_none() {
Expand Down
5 changes: 5 additions & 0 deletions tests/suite/cli_rustup_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ fn rustup_upgrade_cmd_help_flag() {
test_help("rustup_upgrade_cmd_help_flag", &["upgrade", "--help"]);
}

#[test]
fn rustup_ci_cmd_help_flag() {
test_help("rustup_ci_cmd_help_flag", &["ci", "--help"]);
}

#[test]
fn rustup_which_cmd_help_flag() {
test_help("rustup_which_cmd_help_flag", &["which", "--help"]);
Expand Down
47 changes: 47 additions & 0 deletions tests/suite/cli_rustup_ui/rustup_ci_cmd_help_flag.stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading