Skip to content

Commit 1eb6120

Browse files
committed
Make code generation just work
Contributors don't need to learn about `cargo xtask codegen` if `cargo test` just does the right thing.
1 parent abb6b8f commit 1eb6120

File tree

8 files changed

+42
-50
lines changed

8 files changed

+42
-50
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ mod tests {
864864
use super::*;
865865

866866
#[test]
867-
fn ensure_schema_in_package_json() {
867+
fn generate_package_json_config() {
868868
let s = Config::json_schema();
869869
let schema = format!("{:#}", s);
870870
let mut schema = schema
@@ -895,7 +895,7 @@ mod tests {
895895
}
896896

897897
#[test]
898-
fn schema_in_sync_with_docs() {
898+
fn generate_config_documentation() {
899899
let docs_path = project_root().join("docs/user/generated_config.adoc");
900900
let current = fs::read_to_string(&docs_path).unwrap();
901901
let expected = ConfigData::manual();

crates/test_utils/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::{
1818
};
1919

2020
use profile::StopWatch;
21-
use stdx::lines_with_ends;
21+
use stdx::{is_ci, lines_with_ends};
2222
use text_size::{TextRange, TextSize};
2323

2424
pub use dissimilar::diff as __diff;
@@ -376,6 +376,9 @@ pub fn try_ensure_file_contents(file: &Path, contents: &str) -> Result<(), ()> {
376376
"\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n",
377377
display_path.display()
378378
);
379+
if is_ci() {
380+
eprintln!("\n NOTE: run `cargo test` locally and commit the updated files\n");
381+
}
379382
if let Some(parent) = file.parent() {
380383
let _ = std::fs::create_dir_all(parent);
381384
}

docs/dev/architecture.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,8 @@ This sections talks about the things which are everywhere and nowhere in particu
308308
### Code generation
309309

310310
Some of the components of this repository are generated through automatic processes.
311-
`cargo xtask codegen` runs all generation tasks.
311+
Generated code is updated automatically on `cargo test`.
312312
Generated code is generally committed to the git repository.
313-
There are tests to check that the generated code is fresh.
314313

315314
In particular, we generate:
316315

xtask/src/codegen.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
88
mod gen_syntax;
99
mod gen_parser_tests;
10+
mod gen_lint_completions;
1011
mod gen_assists_docs;
1112
mod gen_feature_docs;
12-
mod gen_lint_completions;
1313
mod gen_diagnostic_docs;
1414

1515
use std::{
@@ -18,38 +18,35 @@ use std::{
1818
};
1919
use xshell::{cmd, pushenv, read_file, write_file};
2020

21-
use crate::{ensure_rustfmt, flags, project_root, Result};
21+
use crate::{ensure_rustfmt, project_root, Result};
2222

2323
pub(crate) use self::{
24-
gen_assists_docs::{generate_assists_docs, generate_assists_tests},
25-
gen_diagnostic_docs::generate_diagnostic_docs,
26-
gen_feature_docs::generate_feature_docs,
27-
gen_lint_completions::generate_lint_completions,
28-
gen_parser_tests::generate_parser_tests,
29-
gen_syntax::generate_syntax,
24+
gen_assists_docs::generate_assists_tests, gen_lint_completions::generate_lint_completions,
25+
gen_parser_tests::generate_parser_tests, gen_syntax::generate_syntax,
3026
};
3127

28+
pub(crate) fn docs() -> Result<()> {
29+
// We don't commit docs to the repo, so we can just overwrite them.
30+
gen_assists_docs::generate_assists_docs(Mode::Overwrite)?;
31+
gen_feature_docs::generate_feature_docs(Mode::Overwrite)?;
32+
gen_diagnostic_docs::generate_diagnostic_docs(Mode::Overwrite)?;
33+
Ok(())
34+
}
35+
36+
#[allow(unused)]
37+
fn used() {
38+
generate_parser_tests(Mode::Overwrite);
39+
generate_assists_tests(Mode::Overwrite);
40+
generate_syntax(Mode::Overwrite);
41+
generate_lint_completions(Mode::Overwrite);
42+
}
43+
3244
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
3345
pub(crate) enum Mode {
3446
Overwrite,
3547
Ensure,
3648
}
3749

38-
impl flags::Codegen {
39-
pub(crate) fn run(self) -> Result<()> {
40-
if self.features {
41-
generate_lint_completions(Mode::Overwrite)?;
42-
}
43-
generate_syntax(Mode::Overwrite)?;
44-
generate_parser_tests(Mode::Overwrite)?;
45-
generate_assists_tests(Mode::Overwrite)?;
46-
generate_assists_docs(Mode::Overwrite)?;
47-
generate_feature_docs(Mode::Overwrite)?;
48-
generate_diagnostic_docs(Mode::Overwrite)?;
49-
Ok(())
50-
}
51-
}
52-
5350
/// A helper to update file on disk if it has changed.
5451
/// With verify = false,
5552
fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> {

xtask/src/flags.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ xflags::xflags! {
2727
optional --jemalloc
2828
}
2929

30-
cmd codegen {
31-
optional --features
32-
}
33-
3430
cmd lint {}
3531
cmd fuzz-tests {}
3632
cmd pre-cache {}
@@ -67,7 +63,6 @@ pub struct Xtask {
6763
pub enum XtaskCmd {
6864
Help(Help),
6965
Install(Install),
70-
Codegen(Codegen),
7166
Lint(Lint),
7267
FuzzTests(FuzzTests),
7368
PreCache(PreCache),
@@ -92,11 +87,6 @@ pub struct Install {
9287
pub jemalloc: bool,
9388
}
9489

95-
#[derive(Debug)]
96-
pub struct Codegen {
97-
pub features: bool,
98-
}
99-
10090
#[derive(Debug)]
10191
pub struct Lint;
10292

xtask/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ fn main() -> Result<()> {
4040
return Ok(());
4141
}
4242
flags::XtaskCmd::Install(cmd) => cmd.run(),
43-
flags::XtaskCmd::Codegen(cmd) => cmd.run(),
4443
flags::XtaskCmd::Lint(_) => run_clippy(),
4544
flags::XtaskCmd::FuzzTests(_) => run_fuzzer(),
4645
flags::XtaskCmd::PreCache(cmd) => cmd.run(),

xtask/src/release.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::Write;
22

33
use xshell::{cmd, cp, pushd, read_dir, write_file};
44

5-
use crate::{codegen, date_iso, flags, is_release_tag, project_root, Mode, Result};
5+
use crate::{codegen, date_iso, flags, is_release_tag, project_root, Result};
66

77
impl flags::Release {
88
pub(crate) fn run(self) -> Result<()> {
@@ -12,8 +12,7 @@ impl flags::Release {
1212
cmd!("git reset --hard tags/nightly").run()?;
1313
cmd!("git push").run()?;
1414
}
15-
codegen::generate_assists_docs(Mode::Overwrite)?;
16-
codegen::generate_feature_docs(Mode::Overwrite)?;
15+
codegen::docs()?;
1716

1817
let website_root = project_root().join("../rust-analyzer.github.io");
1918
let changelog_dir = website_root.join("./thisweek/_posts");

xtask/src/tidy.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,36 @@ use crate::{
1212
};
1313

1414
#[test]
15-
fn generated_grammar_is_fresh() {
15+
fn generate_grammar() {
1616
codegen::generate_syntax(Mode::Ensure).unwrap()
1717
}
1818

1919
#[test]
20-
fn generated_tests_are_fresh() {
20+
fn generate_parser_tests() {
2121
codegen::generate_parser_tests(Mode::Ensure).unwrap()
2222
}
2323

2424
#[test]
25-
fn generated_assists_are_fresh() {
25+
fn generate_assists_tests() {
2626
codegen::generate_assists_tests(Mode::Ensure).unwrap();
2727
}
2828

29+
/// This clones rustc repo, and so is not worth to keep up-to-date. We update
30+
/// manually by un-ignoring the test from time to time.
31+
#[test]
32+
#[ignore]
33+
fn generate_lint_completions() {
34+
codegen::generate_lint_completions(Mode::Overwrite).unwrap()
35+
}
36+
2937
#[test]
3038
fn check_code_formatting() {
3139
run_rustfmt(Mode::Ensure).unwrap()
3240
}
3341

3442
#[test]
35-
fn smoke_test_docs_generation() {
36-
// We don't commit docs to the repo, so we can just overwrite in tests.
37-
codegen::generate_assists_docs(Mode::Overwrite).unwrap();
38-
codegen::generate_feature_docs(Mode::Overwrite).unwrap();
39-
codegen::generate_diagnostic_docs(Mode::Overwrite).unwrap();
43+
fn smoke_test_generate_documentation() {
44+
codegen::docs().unwrap()
4045
}
4146

4247
#[test]

0 commit comments

Comments
 (0)