Skip to content
Merged
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
8 changes: 5 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
43 changes: 42 additions & 1 deletion src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))?;

Expand Down Expand Up @@ -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<features::SupportTable>) {
let Some(support) = support else { return };
let uses = features::used(request.schemas, &features::Options::of(request));
let mut ignored: BTreeMap<features::Feature, Vec<&features::FeatureUse>> = 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);
}
}
48 changes: 46 additions & 2 deletions src/commands/targets.rs
Original file line number Diff line number Diff line change
@@ -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-<lang>` 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<Column> = 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(())
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
26 changes: 26 additions & 0 deletions tests/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'"));
}
38 changes: 38 additions & 0 deletions tests/cli/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading