diff --git a/Cargo.lock b/Cargo.lock index eca8a94..f4254c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -387,6 +387,7 @@ dependencies = [ "notify-debouncer-mini", "predicates", "serde", + "serde_json", "tempfile", "thiserror 1.0.69", "toml", @@ -397,16 +398,17 @@ dependencies = [ [[package]] name = "comline-codegen" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=39d378b01cbbc66282689f0b763b77c063dfe38a#39d378b01cbbc66282689f0b763b77c063dfe38a" +source = "git+https://github.com/ComlineProject/generation?rev=269ec60590db1c6599499e82b53c41e56bcf2b32#269ec60590db1c6599499e82b53c41e56bcf2b32" dependencies = [ "comline-core", "eyre", + "serde_json", ] [[package]] name = "comline-codegen-rust" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-rust?rev=37d537edc79d842b6e364ed06adc95b2e5db729a#37d537edc79d842b6e364ed06adc95b2e5db729a" +source = "git+https://github.com/ComlineProject/comline-rust?rev=9fe85575008e35c0f5c58c62da472db9b0703832#9fe85575008e35c0f5c58c62da472db9b0703832" dependencies = [ "comline-codegen", "comline-core", @@ -416,7 +418,7 @@ dependencies = [ [[package]] name = "comline-codegen-typescript" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-typescript?rev=cfca91460686502171e3ccce418d555c8b3513b6#cfca91460686502171e3ccce418d555c8b3513b6" +source = "git+https://github.com/ComlineProject/comline-typescript?rev=5d7805f4bd441dd97a91d347939c7823f42af4a9#5d7805f4bd441dd97a91d347939c7823f42af4a9" dependencies = [ "comline-codegen", "comline-core", diff --git a/Cargo.toml b/Cargo.toml index fba19ae..093b832 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ notify-debouncer-mini = "0.4" tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter", "fmt"] } serde = { version = "1", features = ["derive"] } +serde_json = "1.0" toml = "0.8" # git rev (not crates.io) so this and the `comline-codegen*` crates share one # `comline-core` in the tree. Local dev: see .cargo/config. `deps` is this @@ -35,9 +36,9 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "09830151 # `comline-codegen`'s types, so the rev below must be byte-identical to the # one *they* pin - Cargo only unifies a git dependency across identical rev # strings, and two copies of `GenRequest` are two incompatible types. -comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "39d378b01cbbc66282689f0b763b77c063dfe38a" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "37d537edc79d842b6e364ed06adc95b2e5db729a", optional = true } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "cfca91460686502171e3ccce418d555c8b3513b6", optional = true } +comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "269ec60590db1c6599499e82b53c41e56bcf2b32" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "9fe85575008e35c0f5c58c62da472db9b0703832", optional = true } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "5d7805f4bd441dd97a91d347939c7823f42af4a9", optional = true } [features] # Which language generators are compiled in. Drop one to shed its whole diff --git a/src/cli.rs b/src/cli.rs index 7cf0f9c..8a80c50 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -262,8 +262,22 @@ Print one `language#version` per line — the exact token syntax `config.idp`'s `comline` was compiled with. Ignores `--path`: this reflects the build, not \ any particular project. +With `--features`, also what each generator implements, feature by feature: \ +supported, partial, ignored (generated without it) or unsupported (refused), \ +each claim proven by the generators' conformance suite. `--json` prints the \ +same as JSON. + Examples: comline targets - comline targets | grep '^rust#'")] - Targets, + comline targets | grep '^rust#' + comline targets --features + comline targets --features --json")] + Targets { + /// Also list what each generator supports, feature by feature + #[arg(long)] + features: bool, + /// With `--features`: print it as JSON + #[arg(long, requires = "features")] + json: bool, + }, } diff --git a/src/commands/generate.rs b/src/commands/generate.rs index 8e044f1..4793a70 100644 --- a/src/commands/generate.rs +++ b/src/commands/generate.rs @@ -9,9 +9,10 @@ //! `compile_package` — no `.comline/` write, no version bump. `"all"` or an //! explicit list reads committed versions from the CAS chain instead. +use std::collections::BTreeMap; use std::path::{Path, PathBuf}; -use comline_codegen::{GenRequest, Mode, PackageMeta, Registry}; +use comline_codegen::{features, GenRequest, Mode, PackageMeta, Registry}; use comline_core::package::build::{self, cas::ObjectStore}; use comline_core::package::config::ir::frozen::FrozenUnit as ConfigUnit; use comline_core::schema::idl::constants::SCHEMA_EXTENSION; @@ -228,6 +229,11 @@ fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool, list: bo // schema and a parent of deeper schemas), not source *text* - // they carry no span to render a snippet against, so there's // nothing for the multi-line branch there to do. + warn_ignored( + &t.language, + &request, + registry.support(&t.language, &t.lang_version), + ); let files = generator(&request).map_err(|e| miette!("`{}` generator: {e}", t.language))?; @@ -382,3 +388,38 @@ fn gen_version_from_commit( schemas, }) } + +/// A warning per feature `request` uses that this generator ignores - it +/// generates, but without the feature (a TypeScript target with +/// `@validators` gets the types, not the checks). An unsupported feature +/// needs nothing here: the generator refuses it, naming every use. +fn warn_ignored(language: &str, request: &GenRequest, support: Option) { + let Some(support) = support else { return }; + let uses = features::used(request.schemas, &features::Options::of(request)); + let mut ignored: BTreeMap> = BTreeMap::new(); + for u in &uses { + if features::support_for(support, u.feature).state == features::State::Ignored { + ignored.entry(u.feature).or_default().push(u); + } + } + for (feature, uses) in ignored { + let s = features::support_for(support, feature); + let mut message = format!("`{language}` ignores {}", feature.title()); + if let Some(note) = s.note { + message.push_str(&format!(": {note}")); + } + if let Some(tracking) = s.tracking { + message.push_str(&format!(" ({tracking})")); + } + let first = uses[0]; + let place = match first.namespace.is_empty() { + true => first.owner.clone(), + false => format!("schema '{}', {}", first.namespace, first.owner), + }; + message.push_str(&format!(" - used by {place}")); + if uses.len() > 1 { + message.push_str(&format!(" and {} more", uses.len() - 1)); + } + ui::warn(message); + } +} diff --git a/src/commands/targets.rs b/src/commands/targets.rs index ab5a319..e6e6a90 100644 --- a/src/commands/targets.rs +++ b/src/commands/targets.rs @@ -1,21 +1,65 @@ //! `comline targets` — print every `language#version` this build of -//! `comline` can generate, to stdout. +//! `comline` can generate, to stdout; with `--features`, what each of them +//! supports. //! //! Reflects how this binary was compiled (which `gen-` Cargo features //! were on), not any particular project — ignores `--path` entirely, same as //! `completions`. +use std::collections::BTreeMap; use std::io::Write as _; +use comline_codegen::features::{self, Column, Feature}; use miette::{IntoDiagnostic, Result}; use crate::commands::generate::generator_registry; -pub fn run() -> Result<()> { +pub fn run(with_features: bool, json: bool) -> Result<()> { let registry = generator_registry(); let mut out = std::io::stdout().lock(); + + if json { + let columns: Vec = registry + .targets() + .into_iter() + .filter_map(|t| { + let support = registry.support(t.name, t.version)?; + Some(Column { + generator: t.name, + version: t.version, + support, + }) + }) + .collect(); + let report = features::json(&columns, &BTreeMap::new()); + writeln!( + out, + "{}", + serde_json::to_string_pretty(&report).into_diagnostic()? + ) + .into_diagnostic()?; + return Ok(()); + } + for t in registry.targets() { writeln!(out, "{}#{}", t.name, t.version).into_diagnostic()?; + let Some(support) = registry + .support(t.name, t.version) + .filter(|_| with_features) + else { + continue; + }; + for &feature in Feature::ALL { + let s = features::support_for(support, feature); + let mut line = format!(" {:<11} {}", s.state.id(), feature.id()); + if let Some(note) = s.note { + line.push_str(&format!(" - {note}")); + } + if let Some(tracking) = s.tracking { + line.push_str(&format!(" ({tracking})")); + } + writeln!(out, "{line}").into_diagnostic()?; + } } Ok(()) } diff --git a/src/main.rs b/src/main.rs index ba3fc96..7048053 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,6 +159,6 @@ fn run(cli: Cli) -> Result<()> { commands::add::run(&work_dir, &name, source, !no_hash) } Commands::Completions { shell } => commands::completions::run(shell), - Commands::Targets => commands::targets::run(), + Commands::Targets { features, json } => commands::targets::run(features, json), } } diff --git a/tests/cli/generate.rs b/tests/cli/generate.rs index eb34a6b..c450441 100644 --- a/tests/cli/generate.rs +++ b/tests/cli/generate.rs @@ -749,3 +749,29 @@ fn code_mode_still_inlines_std_and_the_flag_overrides_lib() { "--no-external-std inlines" ); } + +/// A generator that ignores a feature the schema uses still generates, and +/// says so: TypeScript gets `@validators`' types, not the checks. +#[test] +fn an_ignored_feature_is_a_warning() { + let temp = tempfile::tempdir().unwrap(); + let project = copy_fixture("validated_project", temp.path()); + let config = project.join("config.idp"); + let idp = std::fs::read_to_string(&config).unwrap(); + std::fs::write( + &config, + idp.replace( + "rust#1.70.0 = {}", + "rust#1.70.0 = {}\n typescript#5.0 = {}", + ), + ) + .unwrap(); + + comline_cmd() + .current_dir(&project) + .args(["generate", "--target", "typescript"]) + .assert() + .success() + .stderr(predicate::str::contains("`typescript` ignores validators: the types are generated; the checks aren't run yet (core#96)")) + .stderr(predicate::str::contains("used by schema 'accounts', type alias 'Handle'")); +} diff --git a/tests/cli/global.rs b/tests/cli/global.rs index 61eaab9..7705aae 100644 --- a/tests/cli/global.rs +++ b/tests/cli/global.rs @@ -48,6 +48,44 @@ fn targets_are_written_to_stdout_as_language_hash_version() { .stderr(predicate::str::is_empty()); } +#[test] +fn targets_features_lists_each_generators_support() { + comline_cmd() + .args(["targets", "--features"]) + .assert() + .success() + .stdout(predicate::str::contains("rust#")) + .stdout(predicate::str::contains(" supported structs")) + .stdout(predicate::str::contains(" unsupported unions - ")); +} + +#[test] +fn targets_features_json_is_the_report() { + let output = comline_cmd() + .args(["targets", "--features", "--json"]) + .output() + .unwrap(); + assert!(output.status.success()); + let report: serde_json::Value = serde_json::from_slice(&output.stdout).expect("JSON"); + assert!(report["features"] + .as_array() + .unwrap() + .iter() + .any(|f| f["id"] == "unions")); + let rust = report["generators"] + .as_array() + .unwrap() + .iter() + .find(|g| g["name"] == "rust") + .unwrap(); + assert_eq!(rust["support"]["unions"]["state"], "unsupported"); +} + +#[test] +fn targets_json_needs_features() { + comline_cmd().args(["targets", "--json"]).assert().failure(); +} + #[test] fn targets_ignores_path_and_works_outside_any_project() { let temp = tempfile::tempdir().unwrap();