From 1eb61203b725684fd2c7e25ac7e2d53eef10c64c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 8 Mar 2021 21:13:15 +0300 Subject: Make `code generation` just work Contributors don't need to learn about `cargo xtask codegen` if `cargo test` just does the right thing. --- crates/rust-analyzer/src/config.rs | 4 ++-- crates/test_utils/src/lib.rs | 5 ++++- docs/dev/architecture.md | 3 +-- xtask/src/codegen.rs | 43 ++++++++++++++++++-------------------- xtask/src/flags.rs | 10 --------- xtask/src/main.rs | 1 - xtask/src/release.rs | 5 ++--- xtask/src/tidy.rs | 21 ++++++++++++------- 8 files changed, 42 insertions(+), 50 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index bceca2b0f..078c83f75 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -864,7 +864,7 @@ mod tests { use super::*; #[test] - fn ensure_schema_in_package_json() { + fn generate_package_json_config() { let s = Config::json_schema(); let schema = format!("{:#}", s); let mut schema = schema @@ -895,7 +895,7 @@ mod tests { } #[test] - fn schema_in_sync_with_docs() { + fn generate_config_documentation() { let docs_path = project_root().join("docs/user/generated_config.adoc"); let current = fs::read_to_string(&docs_path).unwrap(); let expected = ConfigData::manual(); diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 097a54139..dd582c77c 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -18,7 +18,7 @@ use std::{ }; use profile::StopWatch; -use stdx::lines_with_ends; +use stdx::{is_ci, lines_with_ends}; use text_size::{TextRange, TextSize}; pub use dissimilar::diff as __diff; @@ -376,6 +376,9 @@ pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> { "\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n", display_path.display() ); + if is_ci() { + eprintln!("\n NOTE: run `cargo test` locally and commit the updated files\n"); + } if let Some(parent) = file.parent() { let _ = std::fs::create_dir_all(parent); } diff --git a/docs/dev/architecture.md b/docs/dev/architecture.md index ead12616e..0a3fd4285 100644 --- a/docs/dev/architecture.md +++ b/docs/dev/architecture.md @@ -308,9 +308,8 @@ This sections talks about the things which are everywhere and nowhere in particu ### Code generation Some of the components of this repository are generated through automatic processes. -`cargo xtask codegen` runs all generation tasks. +Generated code is updated automatically on `cargo test`. Generated code is generally committed to the git repository. -There are tests to check that the generated code is fresh. In particular, we generate: diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index e3f9ccada..7cdd8f251 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -7,9 +7,9 @@ mod gen_syntax; mod gen_parser_tests; +mod gen_lint_completions; mod gen_assists_docs; mod gen_feature_docs; -mod gen_lint_completions; mod gen_diagnostic_docs; use std::{ @@ -18,38 +18,35 @@ use std::{ }; use xshell::{cmd, pushenv, read_file, write_file}; -use crate::{ensure_rustfmt, flags, project_root, Result}; +use crate::{ensure_rustfmt, project_root, Result}; pub(crate) use self::{ - gen_assists_docs::{generate_assists_docs, generate_assists_tests}, - gen_diagnostic_docs::generate_diagnostic_docs, - gen_feature_docs::generate_feature_docs, - gen_lint_completions::generate_lint_completions, - gen_parser_tests::generate_parser_tests, - gen_syntax::generate_syntax, + gen_assists_docs::generate_assists_tests, gen_lint_completions::generate_lint_completions, + gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax, }; +pub(crate) fn docs() -> Result<()> { + // We don't commit docs to the repo, so we can just overwrite them. + gen_assists_docs::generate_assists_docs(Mode::Overwrite)?; + gen_feature_docs::generate_feature_docs(Mode::Overwrite)?; + gen_diagnostic_docs::generate_diagnostic_docs(Mode::Overwrite)?; + Ok(()) +} + +#[allow(unused)] +fn used() { + generate_parser_tests(Mode::Overwrite); + generate_assists_tests(Mode::Overwrite); + generate_syntax(Mode::Overwrite); + generate_lint_completions(Mode::Overwrite); +} + #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(crate) enum Mode { Overwrite, Ensure, } -impl flags::Codegen { - pub(crate) fn run(self) -> Result<()> { - if self.features { - generate_lint_completions(Mode::Overwrite)?; - } - generate_syntax(Mode::Overwrite)?; - generate_parser_tests(Mode::Overwrite)?; - generate_assists_tests(Mode::Overwrite)?; - generate_assists_docs(Mode::Overwrite)?; - generate_feature_docs(Mode::Overwrite)?; - generate_diagnostic_docs(Mode::Overwrite)?; - Ok(()) - } -} - /// A helper to update file on disk if it has changed. /// With verify = false, fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { diff --git a/xtask/src/flags.rs b/xtask/src/flags.rs index b39d937ca..48d1ad45e 100644 --- a/xtask/src/flags.rs +++ b/xtask/src/flags.rs @@ -27,10 +27,6 @@ xflags::xflags! { optional --jemalloc } - cmd codegen { - optional --features - } - cmd lint {} cmd fuzz-tests {} cmd pre-cache {} @@ -67,7 +63,6 @@ pub struct Xtask { pub enum XtaskCmd { Help(Help), Install(Install), - Codegen(Codegen), Lint(Lint), FuzzTests(FuzzTests), PreCache(PreCache), @@ -92,11 +87,6 @@ pub struct Install { pub jemalloc: bool, } -#[derive(Debug)] -pub struct Codegen { - pub features: bool, -} - #[derive(Debug)] pub struct Lint; diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 25fd32f92..c2dda928e 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -40,7 +40,6 @@ fn main() -> Result<()> { return Ok(()); } flags::XtaskCmd::Install(cmd) => cmd.run(), - flags::XtaskCmd::Codegen(cmd) => cmd.run(), flags::XtaskCmd::Lint(_) => run_clippy(), flags::XtaskCmd::FuzzTests(_) => run_fuzzer(), flags::XtaskCmd::PreCache(cmd) => cmd.run(), diff --git a/xtask/src/release.rs b/xtask/src/release.rs index d8d86fd63..dde5d14ee 100644 --- a/xtask/src/release.rs +++ b/xtask/src/release.rs @@ -2,7 +2,7 @@ use std::fmt::Write; use xshell::{cmd, cp, pushd, read_dir, write_file}; -use crate::{codegen, date_iso, flags, is_release_tag, project_root, Mode, Result}; +use crate::{codegen, date_iso, flags, is_release_tag, project_root, Result}; impl flags::Release { pub(crate) fn run(self) -> Result<()> { @@ -12,8 +12,7 @@ impl flags::Release { cmd!("git reset --hard tags/nightly").run()?; cmd!("git push").run()?; } - codegen::generate_assists_docs(Mode::Overwrite)?; - codegen::generate_feature_docs(Mode::Overwrite)?; + codegen::docs()?; let website_root = project_root().join("../rust-analyzer.github.io"); let changelog_dir = website_root.join("./thisweek/_posts"); diff --git a/xtask/src/tidy.rs b/xtask/src/tidy.rs index 3818b9e0f..03b4c0092 100644 --- a/xtask/src/tidy.rs +++ b/xtask/src/tidy.rs @@ -12,31 +12,36 @@ use crate::{ }; #[test] -fn generated_grammar_is_fresh() { +fn generate_grammar() { codegen::generate_syntax(Mode::Ensure).unwrap() } #[test] -fn generated_tests_are_fresh() { +fn generate_parser_tests() { codegen::generate_parser_tests(Mode::Ensure).unwrap() } #[test] -fn generated_assists_are_fresh() { +fn generate_assists_tests() { codegen::generate_assists_tests(Mode::Ensure).unwrap(); } +/// This clones rustc repo, and so is not worth to keep up-to-date. We update +/// manually by un-ignoring the test from time to time. +#[test] +#[ignore] +fn generate_lint_completions() { + codegen::generate_lint_completions(Mode::Overwrite).unwrap() +} + #[test] fn check_code_formatting() { run_rustfmt(Mode::Ensure).unwrap() } #[test] -fn smoke_test_docs_generation() { - // We don't commit docs to the repo, so we can just overwrite in tests. - codegen::generate_assists_docs(Mode::Overwrite).unwrap(); - codegen::generate_feature_docs(Mode::Overwrite).unwrap(); - codegen::generate_diagnostic_docs(Mode::Overwrite).unwrap(); +fn smoke_test_generate_documentation() { + codegen::docs().unwrap() } #[test] -- cgit v1.2.3