Skip to content
Open
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
1 change: 1 addition & 0 deletions .changepacks/changepack_log_collector_round30.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"crates/vespera/Cargo.toml":"Patch","crates/vespera_core/Cargo.toml":"Patch","crates/vespera_macro/Cargo.toml":"Patch","crates/vespera_inprocess/Cargo.toml":"Patch","crates/vespera_jni/Cargo.toml":"Patch","libs/vespera-bridge/build.gradle.kts":"Patch","libs/vespera-bridge-gradle-plugin/build.gradle.kts":"Patch"},"note":"Route collector: cfg-disjoint twin mod declarations (#[cfg(unix)] mod m; beside #[cfg(not(unix))] mod m;) mount their routes under any(all(..), all(..)) instead of an always-false AND; an ungated #[path] module loads only its target in every build, so a test-only child of it stays out of the OpenAPI document; #[path] children are resolved as rustc resolves them (beside a non-mod.rs target); ungated mod and #[path] declarations of every declaring file count, not only files with cfg and test text, and a declaration counts only when its declaring file is itself loaded in every build.","date":"2026-11-19T00:00:00.000Z"}
58 changes: 50 additions & 8 deletions crates/vespera_macro/src/collector/module_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,46 @@ pub(super) fn module_cfg_predicates(folder_path: &Path, segments: &[String]) ->
let mut parent = folder_path.to_path_buf();
let mut predicates = Vec::new();
for segment in segments {
for declarer in [parent.join("mod.rs"), sibling_rs_file(&parent)] {
for module in declared_mods(&declarer)
.iter()
.filter(|module| &module.segment == segment)
{
predicates.extend(module.cfgs.iter().cloned());
}
}
let declarations: Vec<Vec<String>> = [parent.join("mod.rs"), sibling_rs_file(&parent)]
.iter()
.flat_map(|declarer| {
declared_mods(declarer)
.iter()
.filter(|module| &module.segment == segment)
.map(|module| module.cfgs.clone())
.collect::<Vec<_>>()
})
.collect();
predicates.extend(declared_cfg(&declarations));
parent.push(segment);
}
predicates
}

/// The predicates under which the `mod` declarations of one module load it
/// (each declaration's `#[cfg]` predicates), in the form the generator ANDs
/// them. An ungated declaration loads the module in every build, so nothing
/// gates it. One declaration gives its predicates. Cfg-disjoint twins are
/// ORed (only one of them is compiled per build), so they give one
/// `any(all(..), ..)`: `#[cfg(unix)] mod m;` beside `#[cfg(not(unix))] mod m;`
/// is `any(all(unix), all(not(unix)))`, not `unix` AND `not(unix)`.
fn declared_cfg(declarations: &[Vec<String>]) -> Vec<String> {
if declarations.iter().any(Vec::is_empty) {
return Vec::new();
}
match declarations {
[] => Vec::new(),
[single] => single.clone(),
many => {
let twins: Vec<String> = many
.iter()
.map(|cfgs| format!("all({})", cfgs.join(", ")))
.collect();
vec![format!("any({})", twins.join(", "))]
}
}
}

/// The Rust module path of a route file, as its `mod` declarations name the
/// modules: a `#[path]` module is named by its declaration (`mod admin` with
/// `#[path = "admin_impl.rs"]` is `routes::admin`, though the file is
Expand Down Expand Up @@ -383,6 +410,21 @@ mod tests {
#[case::path_mod_rs_bare("#[cfg(unix)] #[path = \"mod.rs\"] mod admin;", &["admin"], &["unix"])]
#[case::path_without_stem("#[cfg(unix)] #[path = \"\"] mod admin;", &["admin"], &["unix"])]
#[case::non_literal_path("#[cfg(unix)] #[path = concat!(\"a\", \".rs\")] mod admin;", &["admin"], &["unix"])]
#[case::undeclared_module("mod other;", &["admin"], &[])]
// Cfg-disjoint twins load the module under either predicate (round-30 M1):
// ANDing them would never load it.
#[case::cfg_twins(
"#[cfg(unix)] pub mod admin;\n#[cfg(not(unix))] pub mod admin;",
&["admin"],
&["any(all(unix), all(not (unix)))"]
)]
#[case::multi_cfg_twins(
"#[cfg(unix)] #[cfg(test)] mod admin; #[cfg(windows)] mod admin;",
&["admin"],
&["any(all(unix, test), all(windows))"]
)]
// An ungated twin loads the module in every build: nothing gates it.
#[case::ungated_twin("#[cfg(unix)] pub mod admin;\npub mod admin;", &["admin"], &[])]
fn module_declaration_cfg(
#[case] mod_rs: &str,
#[case] segments: &[&str],
Expand Down
Loading
Loading