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
11 changes: 6 additions & 5 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter", "fmt"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
toml = "0.8"
# The error type of `comline-core`'s `build_with` check, which `build` passes.
eyre = "0.6.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
# crate's opt-in: cross-package `dependencies = {...}` resolution (core#6) —
# needs real I/O (git fetch, filesystem), so it's off by default for
# `comline-core`'s wasm32 consumers (the playground editor, the language
# server); the CLI is the one consumer that actually runs `comline build`.
comline-core = { git = "https://github.com/ComlineProject/core", rev = "eb5802f2d8ab12fcf8f9b63afba10d71dcbbc98d", features = ["deps"] }
comline-core = { git = "https://github.com/ComlineProject/core", rev = "a3f9eef50c939b4cd5e7563f96d67c8db4012747", features = ["deps"] }
# All three pinned to revs that agree on one `comline-codegen`, and bumped
# together: comline-rust and comline-typescript's codegen crates consume
# `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 = "dbb8530034cd2a8e6d390737b091e8bc2ca7029e" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "dbe579776327928a3b31c8a3fef1bbfea16c42ac", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "c9b868084ef7a13618944e72ecdf16f91223982a", optional = true }
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "bbfadb25cd34f02974c69cc2f102e667a089f684" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "577bdb2910db8b3aedb6061f5436816836a08942", optional = true }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "c72d14efa93135f892c04c6546f690a8e65b6cc1", optional = true }

[features]
# Which language generators are compiled in. Drop one to shed its whole
Expand Down
4 changes: 2 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ specification_version = 1

code_generation = {
languages = {
rust#1.70.0 = {}
rust#1.85.0 = {}
}
}
```
Expand Down Expand Up @@ -305,7 +305,7 @@ comline -p ./schemas generate --target rust

```
• Generating code (in schemas/)
⚙️ language: rust, version: 1.70.0
⚙️ language: rust, version: 1.85.0
main.ids → generated/rust/main.rs
other.ids → generated/rust/other.rs
✓ Generated 2 file(s)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ mod tests {

#[test]
fn a_manifest_without_dependencies_gets_a_block_at_the_end() {
let manifest = format!("{HEAD}\ncode_generation = {{\n languages = {{\n rust#1.70.0 = {{}}\n }}\n}}\n");
let manifest = format!("{HEAD}\ncode_generation = {{\n languages = {{\n rust#1.85.0 = {{}}\n }}\n}}\n");
assert_eq!(
with_dependency(&manifest, "shared", &fields()),
format!(
Expand Down
21 changes: 19 additions & 2 deletions src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
//! `comline build` — compile, validate and freeze a new immutable version.
//!
//! Before freezing, every target declared under `code_generation.languages`
//! is proven against the generators this binary was built with
//! ([`lang_targets::check_declared`]): a declaration is a promise consumers
//! rely on.

use std::path::Path;

use comline_core::package::build::{self, VersionBump};
use miette::Result;

use crate::changes;
use crate::commands::generate::generator_registry;
use crate::commands::{core_err, ensure_project};
use crate::lang_targets;
use crate::ui;
use crate::watch;

Expand All @@ -23,11 +30,21 @@ pub fn run(work_dir: &Path, watching: bool) -> Result<()> {
fn build_once(work_dir: &Path) -> Result<()> {
ui::step(format!("Building project{}", ui::at_path(work_dir)));
let spinner = ui::spinner("compiling schemas");
let result = build::build(work_dir);
let registry = generator_registry();
let mut target_warnings = Vec::new();
let result = build::build_with(work_dir, |context| {
target_warnings = lang_targets::check_declared(context, &registry)?;
Ok(())
});
ui::finish_spinner(spinner);

let result = result.map_err(|e| core_err("build failed", e))?;
for warning in result.context.rendered_warnings() {
for warning in result
.context
.rendered_warnings()
.into_iter()
.chain(target_warnings)
{
ui::warn(warning);
}

Expand Down
66 changes: 31 additions & 35 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use comline_codegen::{features, GenRequest, Mode, PackageMeta, Registry};
use comline_codegen::{features, GenRequest, Mode, PackageMeta, Registry, Resolved, Unresolved};
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 All @@ -21,6 +21,7 @@ use miette::{miette, IntoDiagnostic, Result, WrapErr};

use crate::commands::{core_err, ensure_project};
use crate::gen_config::{self, ComlineToml, DeclaredLang, Overrides, VersionSpec};
use crate::lang_targets::{self, TargetError};
use crate::{history, ui, watch};

/// The generator registry, composed from the per-language `comline-codegen-*`
Expand Down Expand Up @@ -87,19 +88,8 @@ fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool, list: bo
return Ok(());
};

// Languages the congregation declared: `rust#1.70.0` -> ("rust", "1.70.0").
let declared: Vec<DeclaredLang> = config_units
.iter()
.filter_map(|u| match u {
ConfigUnit::CodeGeneration(g) => {
g.name.split_once('#').map(|(lang, ver)| DeclaredLang {
language: lang.to_owned(),
lang_version: ver.to_owned(),
})
}
_ => None,
})
.collect();
// Languages the congregation declared: `rust#1.85.0` -> ("rust", "1.85.0").
let declared: Vec<DeclaredLang> = lang_targets::declared(config_units);

let spec_version = config_units
.iter()
Expand Down Expand Up @@ -165,18 +155,29 @@ fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool, list: bo
// for one named by a `[[generate.target]]`, so it says what to do
// about it: `only` is how a consumer whose binary can't produce a
// language says so on purpose.
let Some((generator, ext)) = registry.find(&t.language, &t.lang_version) else {
return Err(miette!(
"no generator for `{}` (version `{}`)\n \
help: this build of comline has no {} generator. Install one that \
does, or narrow the set in {}:\n \
[generate]\n only = [...]",
t.language,
t.lang_version,
t.language,
gen_config::FILE_NAME,
));
};
let resolved: Resolved =
match lang_targets::resolve(&registry, &context, &t.language, &t.lang_version) {
Ok(resolved) => resolved,
Err(TargetError::Unresolved(Unresolved::UnknownLanguage { .. })) => {
return Err(miette!(
"no generator for `{}` (version `{}`)\n \
help: this build of comline has no {} generator. Install one that \
does, or narrow the set in {}:\n \
[generate]\n only = [...]",
t.language,
t.lang_version,
t.language,
gen_config::FILE_NAME,
))
}
Err(e) => return Err(miette!("target `{}`: {e}", t.language)),
};
match lang_targets::unverified(&context, &t.language, &resolved) {
Ok(Some(warning)) => ui::warn(warning),
Ok(None) => {}
Err(error) => return Err(miette!("target `{}`: {error}", t.language)),
}
let (generator, ext) = (resolved.generator.generate, resolved.generator.ext);

let versions = expand_versions(&t.versions, work_dir, &live_schemas)?;
if mode == Mode::Lib && versions.len() > 1 {
Expand Down Expand Up @@ -229,11 +230,7 @@ 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),
);
warn_ignored(&t.language, &request, &resolved);
let files =
generator(&request).map_err(|e| miette!("`{}` generator: {e}", t.language))?;

Expand Down Expand Up @@ -402,17 +399,16 @@ fn gen_version_from_commit(
/// 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 };
fn warn_ignored(language: &str, request: &GenRequest, resolved: &Resolved) {
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 {
if resolved.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 s = resolved.support(feature);
let mut message = format!("`{language}` ignores {}", feature.title());
if let Some(note) = s.note {
message.push_str(&format!(": {note}"));
Expand Down
19 changes: 18 additions & 1 deletion src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::process::Command;

use miette::{IntoDiagnostic, Result, WrapErr};

use crate::commands::generate::generator_registry;
use crate::ui;

const GREETING_IDS: &str = "\
Expand Down Expand Up @@ -108,15 +109,31 @@ fn package_ident(name: &str) -> String {
ident
}

/// The language a new project declares: Rust at its generator's floor, the
/// oldest toolchain it generates for. That's the widest promise, and a
/// consumer can raise a version but never lower it, so a higher one would
/// shut older toolchains out for good. A binary built without Rust
/// scaffolds the first language it has.
fn scaffold_language() -> String {
let targets = generator_registry().targets();
targets
.iter()
.find(|t| t.name == "rust")
.or(targets.first())
.map(|t| format!("{}#{}", t.name, t.version))
.unwrap_or_else(|| "rust#1.85.0".to_string())
}

fn config_idp(name: &str) -> String {
let language = scaffold_language();
format!(
"\
congregation {name}
specification_version = 1

code_generation = {{
languages = {{
rust#1.70.0 = {{}}
{language} = {{}}
}}
}}
"
Expand Down
43 changes: 34 additions & 9 deletions src/commands/targets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! `comline targets` — print every `language#version` this build of
//! `comline` can generate, to stdout; with `--features`, what each of them
//! supports.
//! `comline` can generate, to stdout: each generator's floor, then the
//! versions it handles and has verified. With `--features`, what each of them
//! supports, and from which version.
//!
//! Reflects how this binary was compiled (which `gen-<lang>` Cargo features
//! were on), not any particular project — ignores `--path` entirely, same as
Expand All @@ -10,6 +11,7 @@ use std::collections::BTreeMap;
use std::io::Write as _;

use comline_codegen::features::{self, Column, Feature};
use comline_codegen::versions::Versions;
use miette::{IntoDiagnostic, Result};

use crate::commands::generate::generator_registry;
Expand All @@ -23,11 +25,11 @@ pub fn run(with_features: bool, json: bool) -> Result<()> {
.targets()
.into_iter()
.filter_map(|t| {
let support = registry.support(t.name, t.version)?;
let resolved = registry.resolve(t.name, t.version).ok()?;
Some(Column {
generator: t.name,
version: t.version,
support,
versions: t.versions,
support: resolved.generator.support,
})
})
.collect();
Expand All @@ -42,16 +44,25 @@ pub fn run(with_features: bool, json: bool) -> Result<()> {
}

for t in registry.targets() {
writeln!(out, "{}#{}", t.name, t.version).into_diagnostic()?;
let Some(support) = registry
.support(t.name, t.version)
// `name#floor` first: the token `config.idp` takes, so anything
// reading the first word keeps working. Then the range.
writeln!(out, "{}#{} {}", t.name, t.version, range(&t.versions)).into_diagnostic()?;
let Some(resolved) = registry
.resolve(t.name, t.version)
.ok()
.filter(|_| with_features)
else {
continue;
};
for &feature in Feature::ALL {
let s = features::support_for(support, feature);
let s = features::support_for(resolved.generator.support, feature);
let mut line = format!(" {:<11} {}", s.state.id(), feature.id());
if let Some(since) = s.since {
line.push_str(&format!(
" from {} (below it, unsupported: {})",
since.version, since.why
));
}
if let Some(note) = s.note {
line.push_str(&format!(" - {note}"));
}
Expand All @@ -63,3 +74,17 @@ pub fn run(with_features: bool, json: bool) -> Result<()> {
}
Ok(())
}

/// The versions a generator handles, as a reader wants them: from its floor
/// on, where its output changes, and how far it's verified.
fn range(versions: &Versions) -> String {
let mut s = format!("{} or later", versions.floor);
if !versions.changes.is_empty() {
s.push_str(&format!(", changes at {}", versions.changes.join(", ")));
}
s.push_str(&format!("; verified up to {}", versions.verified_up_to));
if versions.forward_compatible {
s.push_str(", and newer toolchains build its output");
}
format!("({s})")
}
Loading
Loading