From 6b6b9ffcc56cffa92e111c0c90100a7207440f69 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Tue, 13 Oct 2026 02:44:59 +0800 Subject: [PATCH 1/2] feat: link std in `lib` mode, name the mode, and add `generate --dry-run` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **`lib` links `comline_std` instead of inlining std.** The machinery was already there - `external_std` filters std's schemas out and adds the dependency - it was just off for everyone. Now the default follows the mode, because the two modes differ in whether there is anywhere to put a dependency: `lib` writes the package manifest, so the generator adds `comline_std` to it and every crate links one shared std; `code` emits loose files with no manifest, so all a generator can do is leave a comment naming the line to add by hand, and inlining stays the honest default there. A `lib` crate carried its own private copy of std before this, drifting from everyone else's. On a schema using `std::http::Request`, generation drops from 7 files to 4. It stays a default, not a rule: `external_std = false` on the target or in `[generate]` still inlines std in `lib` mode. `--no-external-std` joins `--external-std` for the same reason - the default is no longer a fixed `false`, so either direction can be the one a run needs to override. **The target line names the mode.** `language: rust, version: 1.70.0, mode: lib`. The header stays as it was: mode is a per-target decision and would be a lie up there the moment two targets disagreed. **`generate --dry-run`** prints the tree each target would write and writes nothing. The generators are pure and `write_file` is the only side effect in this command, so a dry run is the real run with the write suppressed - what it prints is what *would* be written, not a prediction of it. That matters most for `lib`, where `out` often points outside the project entirely: • Generating code - dry run, nothing written ⚙️ language: rust, version: 1.70.0, mode: lib ../libdemo-rs/ ├─ Cargo.toml └─ src/ ├─ greeting.rs ├─ lib.rs └─ types.rs ✓ Would write 4 file(s) **`comline new` scaffolds `src/greeting.ids`.** `main` is a reserved schema name as of core#91, so the scaffold names itself after what it declares, like the repo's own `examples/` do. Five test fixtures that used the name move to `api.ids` for the same reason. Three places still claimed `code` was the only implemented mode - the `generate` long help, the `--mode` flag help and the scaffolded `comline.toml`'s own comment. `lib` has worked since G2a; only `dylib` is unbuilt. --- Cargo.lock | 10 +- Cargo.toml | 8 +- src/cli.rs | 20 ++- src/commands/generate.rs | 49 +++++-- src/commands/new.rs | 7 +- src/gen_config.rs | 84 +++++++++++- src/main.rs | 11 +- src/ui.rs | 105 ++++++++++++++- tests/cli/check.rs | 6 +- tests/cli/clean.rs | 4 +- tests/cli/diff.rs | 2 +- tests/cli/generate.rs | 122 +++++++++++++++--- tests/cli/new.rs | 2 +- tests/cli/reset.rs | 2 +- .../simple_project/src/{main.ids => api.ids} | 0 15 files changed, 373 insertions(+), 59 deletions(-) rename tests/fixtures/simple_project/src/{main.ids => api.ids} (100%) diff --git a/Cargo.lock b/Cargo.lock index 4449cda..2ae8c4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -397,7 +397,7 @@ dependencies = [ [[package]] name = "comline-codegen" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=52803df6436f49aec5f2266a94c89d4ced4b229c#52803df6436f49aec5f2266a94c89d4ced4b229c" +source = "git+https://github.com/ComlineProject/generation?rev=69a3e37ae788a91c5f72dd047d6196aa4c95fdef#69a3e37ae788a91c5f72dd047d6196aa4c95fdef" dependencies = [ "comline-core", "eyre", @@ -406,7 +406,7 @@ dependencies = [ [[package]] name = "comline-codegen-rust" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-rust?rev=eb8396a337e0efe452cea32cdb0e6a50dd56f2d5#eb8396a337e0efe452cea32cdb0e6a50dd56f2d5" +source = "git+https://github.com/ComlineProject/comline-rust?rev=c95ce654458c262f96fe9ba2e17459d7ff483c2c#c95ce654458c262f96fe9ba2e17459d7ff483c2c" dependencies = [ "comline-codegen", "comline-core", @@ -416,7 +416,7 @@ dependencies = [ [[package]] name = "comline-codegen-typescript" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-typescript?rev=d8a1df17b83752314a34d9c0b7817e4feb16820e#d8a1df17b83752314a34d9c0b7817e4feb16820e" +source = "git+https://github.com/ComlineProject/comline-typescript?rev=bfa088c7ca898aca0e8fa74e77d40e2fbb45a0a6#bfa088c7ca898aca0e8fa74e77d40e2fbb45a0a6" dependencies = [ "comline-codegen", "comline-core", @@ -426,7 +426,7 @@ dependencies = [ [[package]] name = "comline-core" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=5c1f3474b462ed912f4f2f5a518b87a5f6576aaa#5c1f3474b462ed912f4f2f5a518b87a5f6576aaa" +source = "git+https://github.com/ComlineProject/core?rev=016e38603152444f9b51fddfd46b1e6b1d73f1d8#016e38603152444f9b51fddfd46b1e6b1d73f1d8" dependencies = [ "ariadne", "blake3", @@ -455,7 +455,7 @@ dependencies = [ [[package]] name = "comline-core-stdlib" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=5c1f3474b462ed912f4f2f5a518b87a5f6576aaa#5c1f3474b462ed912f4f2f5a518b87a5f6576aaa" +source = "git+https://github.com/ComlineProject/core?rev=016e38603152444f9b51fddfd46b1e6b1d73f1d8#016e38603152444f9b51fddfd46b1e6b1d73f1d8" [[package]] name = "concolor" diff --git a/Cargo.toml b/Cargo.toml index 305d20c..a7680b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ toml = "0.8" # 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 = "5c1f3474b462ed912f4f2f5a518b87a5f6576aaa", features = ["deps"] } +comline-core = { git = "https://github.com/ComlineProject/core", rev = "016e38603152444f9b51fddfd46b1e6b1d73f1d8", features = ["deps"] } # All three pinned to revs that agree on one `comline-codegen` - bumped # together: generation#40's branch-tip commit (GenRequest.external_std - # comline-rust/comline-typescript's own codegen crates consume @@ -37,9 +37,9 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "5c1f3474 # they do, since Cargo only unifies a git dependency across byte-identical # revs), then comline-rust#30 and comline-typescript#25 (both branch-tip, # pending those PRs' merge). -comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "52803df6436f49aec5f2266a94c89d4ced4b229c" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "eb8396a337e0efe452cea32cdb0e6a50dd56f2d5", optional = true } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "d8a1df17b83752314a34d9c0b7817e4feb16820e", optional = true } +comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "69a3e37ae788a91c5f72dd047d6196aa4c95fdef" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "c95ce654458c262f96fe9ba2e17459d7ff483c2c", optional = true } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "bfa088c7ca898aca0e8fa74e77d40e2fbb45a0a6", 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 e7470a3..8c0e7c0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -82,7 +82,9 @@ lists none — from `code_generation.languages` in `config.idp`. Output location and layout live in `comline.toml`'s `[generate]` table. `--out` / `--layout` / \ `--mode` override one target and need `--target` when several are configured. \ Defaults: out `generated`, layout `{{language}}/{{namespace}}.{{ext}}`, mode \ -`code` (the only one implemented). +`code` (`lib` emits a buildable package instead; `dylib` is not built yet). + +`--dry-run` prints the tree each target would write and writes nothing. Examples: comline generate @@ -101,15 +103,23 @@ Examples: #[arg(short, long, value_name = "TEMPLATE")] layout: Option, - /// Emit form for the selected target: `code` (only one supported today), `lib`, `dylib` + /// Emit form for the selected target: `code` or `lib` (`dylib` is not built yet) #[arg(short, long, value_name = "MODE")] mode: Option, - /// Reference `comline_std` as its own crate instead of inlining it - /// (Rust only; overrides `comline.toml`'s `[generate] external_std`) - #[arg(long)] + /// Reference `comline_std` as its own package instead of inlining it + /// (Rust only; the default in `lib` mode, off in `code` mode) + #[arg(long, conflicts_with = "no_external_std")] external_std: bool, + /// Inline `std` into the generated output instead of referencing it + #[arg(long)] + no_external_std: bool, + + /// Print what would be written, without writing anything + #[arg(long)] + dry_run: bool, + /// Regenerate automatically when a schema or `config.idp` changes #[arg(short, long)] watch: bool, diff --git a/src/commands/generate.rs b/src/commands/generate.rs index fb3f0e6..9b5f3c8 100644 --- a/src/commands/generate.rs +++ b/src/commands/generate.rs @@ -9,7 +9,7 @@ //! `compile_package` — no `.comline/` write, no version bump. `"all"` or an //! explicit list reads committed versions from the CAS chain instead. -use std::path::Path; +use std::path::{Path, PathBuf}; use comline_codegen::{GenRequest, Mode, PackageMeta, Registry}; use comline_core::package::build::{self, cas::ObjectStore}; @@ -36,13 +36,13 @@ pub(crate) fn generator_registry() -> Registry { registry } -pub fn run(work_dir: &Path, overrides: &Overrides, watching: bool) -> Result<()> { +pub fn run(work_dir: &Path, overrides: &Overrides, dry_run: bool, watching: bool) -> Result<()> { ensure_project(work_dir)?; if watching { - watch::run(work_dir, "generate", || generate_once(work_dir, overrides)) + watch::run(work_dir, "generate", || generate_once(work_dir, overrides, dry_run)) } else { - generate_once(work_dir, overrides) + generate_once(work_dir, overrides, dry_run) } } @@ -52,8 +52,12 @@ struct GenVersion { schemas: Vec<(String, Vec)>, // (namespace path, frozen units) } -fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> { - ui::step(format!("Generating code{}", ui::at_path(work_dir))); +fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool) -> Result<()> { + ui::step(format!( + "Generating code{}{}", + ui::at_path(work_dir), + if dry_run { " - dry run, nothing written" } else { "" }, + )); let cfg = ComlineToml::load(work_dir)?; @@ -169,7 +173,10 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> { } let multi = versions.len() > 1; - let label = format!("language: {}, version: {}", t.language, t.lang_version); + let label = format!( + "language: {}, version: {}, mode: {}", + t.language, t.lang_version, t.mode, + ); ui::detail(if ui::plain() { label } else { @@ -190,6 +197,7 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> { gv.package_version.clone() }, }, + lang_version: t.lang_version.clone(), default_framing: t.default_framing.clone(), external_std: t.external_std, }; @@ -201,6 +209,13 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> { let files = generator(&request).map_err(|e| miette!("`{}` generator: {e}", t.language))?; + // A dry run lists destinations instead of writing them, then + // renders them as one tree per target. Everything above has + // already happened for real - the generators are pure, and + // `write_file` is the only side effect in this command - so the + // tree is what *would* be written rather than a guess at it. + let mut planned: Vec = Vec::new(); + match mode { // One file per schema, placed by `layout` - whichever schemas // the generator actually produced a file for (it may @@ -211,6 +226,11 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> { let namespace = f.path.with_extension("").to_string_lossy().into_owned(); let dest = t.dest_for(&namespace, ext, &spec_version, &gv.package_version)?; + if dry_run { + planned.push(dest); + written += 1; + continue; + } write_file(&dest, &f.contents)?; let src_name = format!("{namespace}.{SCHEMA_EXTENSION}"); @@ -235,6 +255,11 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> { }; for f in &files { let dest = root.join(&f.path); + if dry_run { + planned.push(dest); + written += 1; + continue; + } write_file(&dest, &f.contents)?; let shown = dest.strip_prefix(work_dir).unwrap_or(&dest).display(); ui::detail(format!(" {} {} {shown}", f.path.display(), ui::arrow())); @@ -242,10 +267,18 @@ fn generate_once(work_dir: &Path, overrides: &Overrides) -> Result<()> { } } } + + if dry_run { + ui::tree(work_dir, &planned); + } } } - ui::success(format!("Generated {written} file(s)")); + ui::success(if dry_run { + format!("Would write {written} file(s)") + } else { + format!("Generated {written} file(s)") + }); Ok(()) } diff --git a/src/commands/new.rs b/src/commands/new.rs index df2b1d4..06bb35d 100644 --- a/src/commands/new.rs +++ b/src/commands/new.rs @@ -7,7 +7,7 @@ use miette::{IntoDiagnostic, Result, WrapErr}; use crate::ui; -const MAIN_IDS: &str = "\ +const GREETING_IDS: &str = "\ /// The language a greeting is written in. enum Language { English @@ -38,7 +38,8 @@ const COMLINE_TOML: &str = "\ # [generate] # out = \"generated\" # output root # layout = \"{{language}}/{{namespace}}.{{ext}}\" # path under the root -# mode = \"code\" # code | lib | dylib (only `code` today) +# mode = \"code\" # code (loose source) | lib (a buildable package); dylib not built yet +# external_std = false # reference `comline_std` rather than inlining std; defaults on in `lib` # package_versions = \"latest\" # latest | all | [\"0.3.0\", \"0.4.0\"] # default_framing = \"jsonrpc\" # wire framing for protocols with no `@framing` (else datagram) # @@ -69,7 +70,7 @@ pub fn run(work_dir: &Path, name: &str, git: bool) -> Result<()> { write(&root.join("config.idp"), &config_idp(&package))?; write(&root.join("comline.toml"), COMLINE_TOML)?; - write(&root.join("src/main.ids"), MAIN_IDS)?; + write(&root.join("src/greeting.ids"), GREETING_IDS)?; write(&root.join(".gitignore"), GITIGNORE)?; if git { diff --git a/src/gen_config.rs b/src/gen_config.rs index a48b93a..6846500 100644 --- a/src/gen_config.rs +++ b/src/gen_config.rs @@ -22,11 +22,21 @@ pub const FILE_NAME: &str = "comline.toml"; pub const DEFAULT_OUT: &str = "generated"; /// Default on-disk layout under a target's root. pub const DEFAULT_LAYOUT: &str = "{{language}}/{{namespace}}.{{ext}}"; -/// Default emit form. Only `"code"` is implemented today. +/// Default emit form. `"code"` and `"lib"` are implemented; `"dylib"` is not. pub const DEFAULT_MODE: &str = "code"; -/// Default: `std` stays inlined into the generated output, unchanged from -/// before this existed. -pub const DEFAULT_EXTERNAL_STD: bool = false; + +/// Whether `std` is referenced as its own package rather than inlined, when +/// nothing says either way. +/// +/// It follows the mode, because the two modes differ in whether there is +/// anywhere to put the dependency. `lib` writes the package manifest, so the +/// generator adds `comline_std` to it and the crate links one shared std. +/// `code` emits loose source files with no manifest to add anything to - all +/// a generator can do there is leave a comment naming the dependency line to +/// write by hand, so inlining stays the honest default. +fn default_external_std(mode: &str) -> bool { + mode == "lib" +} /// Which package versions to generate bindings for. /// @@ -165,8 +175,11 @@ pub struct Overrides<'a> { pub out: Option<&'a str>, pub layout: Option<&'a str>, pub mode: Option<&'a str>, - /// `--external-std` is presence-only (no `--no-external-std`, since the - /// default is already off) - `Some(true)` when passed, `None` otherwise. + /// `Some(true)` from `--external-std`, `Some(false)` from + /// `--no-external-std`, `None` when neither was passed. Both spellings + /// exist because the default is no longer a fixed `false` - it follows + /// the mode (see [`default_external_std`]), so either direction can be + /// the one a given run needs to override. pub external_std: Option, } @@ -344,7 +357,7 @@ pub fn resolve( env_external_std, t.external_std, cfg.generate.external_std, - DEFAULT_EXTERNAL_STD, + default_external_std(&mode), ); let lang_version = t @@ -466,6 +479,63 @@ mod tests { assert_eq!(resolved[0].default_framing.as_deref(), Some("datagram")); } + /// `lib` writes the package manifest, so the generator can add + /// `comline_std` to it; `code` emits loose files with nowhere to put a + /// dependency, so inlining stays the honest default there. + #[test] + fn external_std_follows_the_mode() { + for (mode, expected) in [("code", false), ("lib", true)] { + let cfg = parse(&format!( + "[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n\ + mode = \"{mode}\"\n", + )); + let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap(); + assert_eq!(resolved[0].external_std, expected, "mode {mode}"); + } + } + + /// Following the mode is a *default*, not a rule: a crate that wants a + /// self-contained copy of std can still say so. + #[test] + fn an_explicit_false_still_inlines_std_in_lib_mode() { + let cfg = parse( + "[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n\ + mode = \"lib\"\nexternal_std = false\n", + ); + let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap(); + assert!(!resolved[0].external_std); + } + + #[test] + fn the_section_default_applies_to_a_code_target_too() { + let cfg = parse( + "[generate]\nexternal_std = true\n\n\ + [[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n", + ); + let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap(); + assert!(resolved[0].external_std, "`code`, but asked for explicitly"); + } + + /// Both flag spellings exist because the default is no longer a fixed + /// `false` - either direction can be the one a run needs to override. + #[test] + fn either_flag_spelling_overrides_the_mode_default() { + let cfg = parse( + "[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n\ + mode = \"lib\"\n", + ); + let off = Overrides { external_std: Some(false), ..Default::default() }; + let resolved = resolve(&cfg, &[], Path::new("/x"), &off).unwrap(); + assert!(!resolved[0].external_std, "--no-external-std"); + + let cfg = parse( + "[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n", + ); + let on = Overrides { external_std: Some(true), ..Default::default() }; + let resolved = resolve(&cfg, &[], Path::new("/x"), &on).unwrap(); + assert!(resolved[0].external_std, "--external-std"); + } + #[test] fn default_framing_absent_is_none() { let cfg = parse("[generate]\n"); diff --git a/src/main.rs b/src/main.rs index c965d35..7aef5df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,6 +122,8 @@ fn run(cli: Cli) -> Result<()> { layout, mode, external_std, + no_external_std, + dry_run, watch, } => { let overrides = gen_config::Overrides { @@ -129,9 +131,14 @@ fn run(cli: Cli) -> Result<()> { out: out.as_deref().map(|p| p.to_str().unwrap_or_default()), layout: layout.as_deref(), mode: mode.as_deref(), - external_std: external_std.then_some(true), + // Two presence-only flags rather than one `Option`: + // clap rejects passing both (`conflicts_with`), so at most + // one is ever set. + external_std: external_std + .then_some(true) + .or(no_external_std.then_some(false)), }; - commands::generate::run(&work_dir, &overrides, watch) + commands::generate::run(&work_dir, &overrides, dry_run, watch) } Commands::Diff { old, new } => commands::diff::run(&work_dir, &old, &new), Commands::Clean { dry_run } => commands::clean::run(&work_dir, dry_run), diff --git a/src/ui.rs b/src/ui.rs index 54f7edb..4d7f085 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -9,7 +9,7 @@ //! stderr is not a terminal or `NO_COLOR` is set. use std::borrow::Cow; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; @@ -199,3 +199,106 @@ pub fn spinner(msg: impl Into>) -> ProgressBar { pub fn finish_spinner(pb: ProgressBar) { pb.finish_and_clear(); } + +/// Print `paths` as a directory tree, rooted at their deepest common +/// ancestor — what `generate --dry-run` shows in place of the file-by-file +/// mapping a real run prints. +/// +/// A tree rather than a flat list because `lib` mode's output is a package: +/// its shape (a manifest beside a `src/` of modules) is most of what there +/// is to check before letting it write, often to a directory outside the +/// project. Paths are shown relative to `base` when they live under it, +/// absolute otherwise — the same rule [`at_path`] follows. +pub fn tree(base: &Path, paths: &[PathBuf]) { + if quiet() || paths.is_empty() { + return; + } + + let root = common_root(paths); + let shown_root = root.strip_prefix(base).unwrap_or(&root); + let mut label = if shown_root.as_os_str().is_empty() { + ".".to_string() + } else { + shown_root.display().to_string() + }; + label.push('/'); + detail(format!(" {label}")); + + // One entry per line of the tree, as the `/`-separated segments below + // `root`. Sorted so directories group and the output is stable. + let mut rows: Vec> = paths + .iter() + .filter_map(|p| p.strip_prefix(&root).ok()) + .map(|rel| { + rel.components() + .map(|c| c.as_os_str().to_string_lossy().into_owned()) + .collect() + }) + .collect(); + rows.sort(); + rows.dedup(); + + print_level(&rows, &[], " "); +} + +/// The deepest directory containing every path in `paths`. +fn common_root(paths: &[PathBuf]) -> PathBuf { + let mut components: Option> = None; + for path in paths { + let parent: Vec = path + .parent() + .unwrap_or(Path::new("")) + .components() + .map(|c| c.as_os_str().to_os_string()) + .collect(); + components = Some(match components { + None => parent, + Some(common) => common + .into_iter() + .zip(parent) + .take_while(|(a, b)| a == b) + .map(|(a, _)| a) + .collect(), + }); + } + components.unwrap_or_default().into_iter().collect() +} + +/// Render the children of `prefix` (a path of segments already drawn), +/// recursing a level at a time. `indent` is the running left margin, which +/// grows by either a pipe or blank depending on whether the parent was the +/// last of its siblings. +fn print_level(rows: &[Vec], prefix: &[String], indent: &str) { + let mut children: Vec<&String> = rows + .iter() + .filter(|r| r.len() > prefix.len() && r.starts_with(prefix)) + .map(|r| &r[prefix.len()]) + .collect(); + children.dedup(); + + let (tee, elbow, pipe) = if plain() { + ("|- ", "\\- ", "| ") + } else { + ("├─ ", "└─ ", "│ ") + }; + + for (i, name) in children.iter().enumerate() { + let last = i + 1 == children.len(); + let mut path = prefix.to_vec(); + path.push((*name).clone()); + + // A name with nothing below it is a file; anything else is a + // directory, and gets the trailing slash that says so. + let is_dir = rows.iter().any(|r| r.len() > path.len() && r.starts_with(&path)); + let branch = if last { elbow } else { tee }; + detail(format!( + "{indent}{branch}{name}{}", + if is_dir { "/" } else { "" }, + )); + + if is_dir { + let deeper = format!("{indent}{}", if last { " " } else { pipe }); + print_level(rows, &path, &deeper); + } + } +} diff --git a/tests/cli/check.rs b/tests/cli/check.rs index a9fea70..9011db0 100644 --- a/tests/cli/check.rs +++ b/tests/cli/check.rs @@ -41,7 +41,7 @@ fn without_config_fails_with_precondition_code() { fn reports_a_broken_schema() { let temp = tempfile::tempdir().unwrap(); let project = fixture_project(temp.path()); - fs::write(project.join("src/main.ids"), "struct Broken {").unwrap(); + fs::write(project.join("src/api.ids"), "struct Broken {").unwrap(); comline_cmd() .current_dir(&project) @@ -57,7 +57,7 @@ fn a_broken_schema_reports_a_source_snippet_not_a_debug_dump() { let temp = tempfile::tempdir().unwrap(); let project = fixture_project(temp.path()); fs::write( - project.join("src/main.ids"), + project.join("src/api.ids"), "struct Greeting {\n / name: string\n}\n", ) .unwrap(); @@ -70,7 +70,7 @@ fn a_broken_schema_reports_a_source_snippet_not_a_debug_dump() { .code(1) .stderr(predicate::str::contains("ParseError {").not()) .stderr(predicate::str::contains("FailedNode(").not()) - .stderr(predicate::str::contains("main.ids")) + .stderr(predicate::str::contains("api.ids")) .stderr(predicate::str::contains("unexpected token")) .stderr(predicate::str::is_match(r"\d+:\d+").unwrap()); } diff --git a/tests/cli/clean.rs b/tests/cli/clean.rs index 9e29c1f..e5a4ee4 100644 --- a/tests/cli/clean.rs +++ b/tests/cli/clean.rs @@ -19,7 +19,7 @@ fn removes_generated_code_but_keeps_the_store() { .args(["generate", "--target", "rust"]) .assert() .success(); - assert!(project.join("generated/rust/main.rs").exists()); + assert!(project.join("generated/rust/api.rs").exists()); assert!(project.join(".comline").is_dir()); comline_cmd() @@ -54,7 +54,7 @@ fn dry_run_lists_without_deleting() { .success() .stderr(predicate::str::contains("would remove")); - assert!(project.join("generated/rust/main.rs").exists()); + assert!(project.join("generated/rust/api.rs").exists()); } #[test] diff --git a/tests/cli/diff.rs b/tests/cli/diff.rs index 69c2a98..2fef3a7 100644 --- a/tests/cli/diff.rs +++ b/tests/cli/diff.rs @@ -18,7 +18,7 @@ fn reports_changes_between_two_versions() { .success(); // add a struct, then rebuild to bump the version - let schema = project.join("src/main.ids"); + let schema = project.join("src/api.ids"); let mut text = fs::read_to_string(&schema).unwrap(); text.push_str("\nstruct Added {\n x: string\n}\n"); fs::write(&schema, text).unwrap(); diff --git a/tests/cli/generate.rs b/tests/cli/generate.rs index 6d7a7f9..5f5c932 100644 --- a/tests/cli/generate.rs +++ b/tests/cli/generate.rs @@ -18,12 +18,12 @@ fn writes_one_file_per_schema_under_the_default_layout() { .success() .stderr(predicate::str::contains("language: rust, version:")) .stderr(predicate::str::contains( - "main.ids → generated/rust/main.rs", + "api.ids → generated/rust/api.rs", )) .stderr(predicate::str::contains("Generated")); // default layout: generated/{language}/{namespace}.{ext} - assert!(project.join("generated/rust/main.rs").exists()); + assert!(project.join("generated/rust/api.rs").exists()); assert!(project.join("generated/rust/other.rs").exists()); // and it does NOT freeze a version assert!(!project.join(".comline").exists()); @@ -48,7 +48,7 @@ fn honours_out_flag_and_a_flat_layout() { .assert() .success(); - assert!(project.join("bindings/main.rs").exists()); + assert!(project.join("bindings/api.rs").exists()); assert!(project.join("bindings/other.rs").exists()); assert!(!project.join("generated").exists()); } @@ -65,7 +65,7 @@ fn reads_out_from_comline_toml() { .assert() .success(); - assert!(project.join("gen/rust/main.rs").exists()); + assert!(project.join("gen/rust/api.rs").exists()); } #[test] @@ -81,7 +81,7 @@ fn env_out_overrides_the_file() { .assert() .success(); - assert!(project.join("envgen/rust/main.rs").exists()); + assert!(project.join("envgen/rust/api.rs").exists()); assert!(!project.join("gen").exists()); } @@ -97,7 +97,7 @@ fn out_flag_beats_env_out() { .assert() .success(); - assert!(project.join("flaggen/rust/main.rs").exists()); + assert!(project.join("flaggen/rust/api.rs").exists()); assert!(!project.join("envgen").exists()); } @@ -121,7 +121,7 @@ fn env_out_applies_in_a_multi_target_config() { .assert() .success(); - assert!(project.join("envgen/rust/main.rs").exists()); + assert!(project.join("envgen/rust/api.rs").exists()); } #[test] @@ -135,7 +135,7 @@ fn plain_uses_ascii_arrow() { .assert() .success() .stderr(predicate::str::contains( - "main.ids -> generated/rust/main.rs", + "api.ids -> generated/rust/api.rs", )) .stderr(predicate::str::contains('⚙').not()); } @@ -236,13 +236,13 @@ fn lib_mode_emits_a_crate() { assert!(cargo.contains("serde = { version = \"1\"")); let lib = fs::read_to_string(crate_dir.join("src/lib.rs")).unwrap(); - assert!(lib.contains("pub mod main;")); + assert!(lib.contains("pub mod api;")); assert!(lib.contains("pub mod other;")); - assert!(crate_dir.join("src/main.rs").exists()); + assert!(crate_dir.join("src/api.rs").exists()); assert!(crate_dir.join("src/other.rs").exists()); // no bare files at the target root - assert!(!crate_dir.join("main.rs").exists()); + assert!(!crate_dir.join("api.rs").exists()); } #[test] @@ -317,7 +317,7 @@ fn built_twice(project: &std::path::Path) { .arg("build") .assert() .success(); - let schema = project.join("src/main.ids"); + let schema = project.join("src/api.ids"); let mut text = fs::read_to_string(&schema).unwrap(); text.push_str("\nstruct Added {\n x: string\n}\n"); fs::write(&schema, text).unwrap(); @@ -349,7 +349,7 @@ fn latest_stamps_the_last_committed_version() { .assert() .success(); - assert!(project.join("generated/rust/0.0.1/main.rs").exists()); + assert!(project.join("generated/rust/0.0.1/api.rs").exists()); } #[test] @@ -372,8 +372,8 @@ fn package_versions_all_generates_every_committed_version() { .stderr(predicate::str::contains("[0.0.1]")) .stderr(predicate::str::contains("[0.1.0]")); - assert!(project.join("generated/rust/0.0.1/main.rs").exists()); - assert!(project.join("generated/rust/0.1.0/main.rs").exists()); + assert!(project.join("generated/rust/0.0.1/api.rs").exists()); + assert!(project.join("generated/rust/0.1.0/api.rs").exists()); } #[test] @@ -394,7 +394,7 @@ fn package_versions_list_generates_only_the_named_versions() { .assert() .success(); - assert!(project.join("generated/rust/0.0.1/main.rs").exists()); + assert!(project.join("generated/rust/0.0.1/api.rs").exists()); assert!(!project.join("generated/rust/0.1.0").exists()); } @@ -556,3 +556,93 @@ fn env_external_std_beats_comline_toml() { let src = fs::read_to_string(project.join("generated/rust/uses_std.rs")).unwrap(); assert!(src.contains("comline_std::http::Request"), "got: {src}"); } + +/// Which mode a target is being generated as was invisible — the only way +/// to tell `code` from `lib` was to look at what landed. +#[test] +fn the_target_line_names_the_mode() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + + comline_cmd() + .current_dir(&project) + .args(["generate", "--target", "rust"]) + .assert() + .success() + .stderr(predicate::str::contains("language: rust, version: 1.70.0, mode: code")); + + comline_cmd() + .current_dir(&project) + .args(["generate", "--target", "rust", "--mode", "lib"]) + .assert() + .success() + .stderr(predicate::str::contains("mode: lib")); +} + +/// The generators are pure and `write_file` is the only side effect, so a +/// dry run is the real run with the write suppressed — what it prints is +/// what would be written, not a prediction of it. +#[test] +fn dry_run_prints_the_tree_and_writes_nothing() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + + comline_cmd() + .current_dir(&project) + .args(["generate", "--target", "rust", "--mode", "lib", "--dry-run"]) + .assert() + .success() + .stderr(predicate::str::contains("dry run, nothing written")) + .stderr(predicate::str::contains("Cargo.toml")) + .stderr(predicate::str::contains("src/")) + .stderr(predicate::str::contains("api.rs")) + .stderr(predicate::str::contains("Would write 4 file(s)")); + + assert!(!project.join("generated").exists(), "a dry run must write nothing"); +} + +/// `lib` mode writes the package manifest, so it can add `comline_std` to +/// it — every `lib` crate used to carry its own private copy of std +/// instead, drifting from everyone else's. +#[test] +fn lib_mode_links_comline_std_instead_of_inlining_it() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + add_std_reference(&project); + + comline_cmd() + .current_dir(&project) + .args(["generate", "--target", "rust", "--mode", "lib"]) + .assert() + .success(); + + let crate_dir = project.join("generated/rust"); + let cargo = fs::read_to_string(crate_dir.join("Cargo.toml")).unwrap(); + assert!(cargo.contains("comline_std = { git"), "{cargo}"); + assert!(cargo.contains("rust-version = \"1.70.0\""), "{cargo}"); + assert!(!crate_dir.join("src/std.rs").exists(), "std should not be inlined"); + assert!(!crate_dir.join("src/std").exists(), "std should not be inlined"); +} + +/// `code` mode has no manifest to add a dependency to, so it keeps +/// inlining — and `--no-external-std` puts `lib` back to that too. +#[test] +fn code_mode_still_inlines_std_and_the_flag_overrides_lib() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + add_std_reference(&project); + + comline_cmd() + .current_dir(&project) + .args(["generate", "--target", "rust"]) + .assert() + .success(); + assert!(project.join("generated/rust/std/http.rs").exists(), "code mode inlines std"); + + comline_cmd() + .current_dir(&project) + .args(["generate", "--target", "rust", "--mode", "lib", "--no-external-std"]) + .assert() + .success(); + assert!(project.join("generated/rust/src/std/http.rs").exists(), "--no-external-std inlines"); +} diff --git a/tests/cli/new.rs b/tests/cli/new.rs index 6643f83..12de2a8 100644 --- a/tests/cli/new.rs +++ b/tests/cli/new.rs @@ -20,7 +20,7 @@ fn scaffolds_a_buildable_project() { let root = temp.path().join("my_api"); assert!(root.join("config.idp").exists()); assert!(root.join("comline.toml").exists()); - assert!(root.join("src/main.ids").exists()); + assert!(root.join("src/greeting.ids").exists()); assert!(root.join(".gitignore").exists()); let config = fs::read_to_string(root.join("config.idp")).unwrap(); diff --git a/tests/cli/reset.rs b/tests/cli/reset.rs index a2f2786..38c403b 100644 --- a/tests/cli/reset.rs +++ b/tests/cli/reset.rs @@ -20,7 +20,7 @@ fn force_removes_the_store_and_generated_code() { .assert() .success(); assert!(project.join(".comline").is_dir()); - assert!(project.join("generated/rust/main.rs").exists()); + assert!(project.join("generated/rust/api.rs").exists()); comline_cmd() .current_dir(&project) diff --git a/tests/fixtures/simple_project/src/main.ids b/tests/fixtures/simple_project/src/api.ids similarity index 100% rename from tests/fixtures/simple_project/src/main.ids rename to tests/fixtures/simple_project/src/api.ids From b91d48d13aff34cfd0121e762b3e55de41130bb2 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Tue, 13 Oct 2026 02:53:06 +0800 Subject: [PATCH 2/2] chore: point at the merged generator revs Including comline-rust#38's STD_REV repoint, so a `lib`-mode crate this CLI generates links the current comline_std rather than the one the previous pin named. `comline-codegen` stays on the rev the generator crates themselves pin - cli has no [patch] to unify two of them. --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ae8c4f..c146e24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -406,7 +406,7 @@ dependencies = [ [[package]] name = "comline-codegen-rust" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-rust?rev=c95ce654458c262f96fe9ba2e17459d7ff483c2c#c95ce654458c262f96fe9ba2e17459d7ff483c2c" +source = "git+https://github.com/ComlineProject/comline-rust?rev=5b1ba2b099cc295c6768935623f4c084f134e6b8#5b1ba2b099cc295c6768935623f4c084f134e6b8" dependencies = [ "comline-codegen", "comline-core", @@ -416,7 +416,7 @@ dependencies = [ [[package]] name = "comline-codegen-typescript" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-typescript?rev=bfa088c7ca898aca0e8fa74e77d40e2fbb45a0a6#bfa088c7ca898aca0e8fa74e77d40e2fbb45a0a6" +source = "git+https://github.com/ComlineProject/comline-typescript?rev=c484b764b4341d8a8529eb52613e4ce2fb6c35d5#c484b764b4341d8a8529eb52613e4ce2fb6c35d5" dependencies = [ "comline-codegen", "comline-core", diff --git a/Cargo.toml b/Cargo.toml index a7680b4..80cff59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,8 +38,8 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "016e3860 # revs), then comline-rust#30 and comline-typescript#25 (both branch-tip, # pending those PRs' merge). comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "69a3e37ae788a91c5f72dd047d6196aa4c95fdef" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "c95ce654458c262f96fe9ba2e17459d7ff483c2c", optional = true } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "bfa088c7ca898aca0e8fa74e77d40e2fbb45a0a6", optional = true } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "5b1ba2b099cc295c6768935623f4c084f134e6b8", optional = true } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "c484b764b4341d8a8529eb52613e4ce2fb6c35d5", optional = true } [features] # Which language generators are compiled in. Drop one to shed its whole