diff --git a/core/src/compat.rs b/core/src/compat.rs index 8bde4ab..1720412 100644 --- a/core/src/compat.rs +++ b/core/src/compat.rs @@ -647,6 +647,22 @@ struct ProtocolDecl<'a> { functions: Vec<(&'a str, &'a [FrozenUnit])>, } +/// The names a version's compiled schemas are split off by, as +/// dependencies' rather than the package's own: its `dependencies`, and +/// `std`, which is built in and never vendored (no `dep_std` entry) yet +/// compiled into every build, so it's compared by reach like the rest. +pub fn dependency_names<'a>(dependencies: impl IntoIterator) -> BTreeSet<&'a str> { + dependencies.into_iter().chain(std::iter::once("std")).collect() +} + +/// A build tree's vendored dependencies: its `dep_` entries. +pub fn vendored(tree: &Tree) -> impl Iterator { + tree.entries + .iter() + .filter(|e| e.mode == EntryMode::Tree) + .filter_map(|e| e.name.strip_prefix("dep_")) +} + /// A stored version: its own schemas, its dependencies' (vendored), and /// its configs, its own and its dependencies' (what a version stored before /// core#251 doesn't hold of them is unseen, see [`StoredConfigs`]). @@ -660,12 +676,7 @@ impl Loaded { fn of(store: &ObjectStore, tree: &Tree) -> Result { use crate::schema::ir::frozen::cas::blob::load_schema_from_tree; - let dependency_names: BTreeSet<&str> = tree - .entries - .iter() - .filter(|e| e.mode == EntryMode::Tree) - .filter_map(|e| e.name.strip_prefix("dep_")) - .collect(); + let dependency_names = dependency_names(vendored(tree)); let mut own = vec![]; let mut dependencies: BTreeMap = BTreeMap::new(); for entry in &tree.entries { @@ -802,8 +813,7 @@ pub fn facts_of_context(context: &ProjectContext, version: &str) -> PackageFacts .iter() .filter_map(|s| s.borrow().frozen_schema.borrow().clone()) .collect(); - let dependency_names: BTreeSet<&str> = - context.dependency_configs.iter().map(|(n, _)| n.as_str()).collect(); + let dependency_names = dependency_names(context.dependency_configs.iter().map(|(n, _)| n.as_str())); let (mut mine, mut theirs) = (vec![], vec![]); for units in own { let first = schema_namespace(&units).and_then(|ns| ns.split_once("::").map(|(f, _)| f.to_string())); diff --git a/core/src/package/build/cas/build.rs b/core/src/package/build/cas/build.rs index f6533fa..4d7a778 100644 --- a/core/src/package/build/cas/build.rs +++ b/core/src/package/build/cas/build.rs @@ -195,14 +195,9 @@ pub fn compare(store: &ObjectStore, old: &Tree, new: &Tree) -> Result = old - .entries - .iter() - .chain(&new.entries) - .filter(|e| e.mode == EntryMode::Tree) - .filter_map(|e| e.name.strip_prefix("dep_")) - .collect(); + // and compared by reach below, `std`'s too. + use crate::compat::{dependency_names, vendored}; + let dependency_names = dependency_names(vendored(old).chain(vendored(new))); type Schemas = Vec>; let split = |schemas: Schemas| -> (Schemas, BTreeMap) { let mut own = vec![]; diff --git a/core/tests/cas/mod.rs b/core/tests/cas/mod.rs index 842264e..74536af 100644 --- a/core/tests/cas/mod.rs +++ b/core/tests/cas/mod.rs @@ -15,6 +15,7 @@ pub mod dependency_vendoring_tests; #[cfg(feature = "deps")] pub mod dependency_configs_tests; pub mod release_tests; +pub mod std_reach_tests; pub mod compat_tests; pub mod reconcile_tests; pub mod store_never_wiped_tests; diff --git a/core/tests/cas/std_reach_tests.rs b/core/tests/cas/std_reach_tests.rs new file mode 100644 index 0000000..86e45c1 --- /dev/null +++ b/core/tests/cas/std_reach_tests.rs @@ -0,0 +1,55 @@ +// `std` is built in and never vendored, so a build has no `dep_std` +// entry, yet its compiled schemas are in every build. It's compared by +// reach, like a vendored dependency: what the package doesn't reach of it +// is neither the package's change nor a version bump. + +use comline_core::package::build::cas::objects::{EntryMode, Tree}; +use comline_core::package::build::cas::ObjectStore; +use comline_core::package::build::VersionBump; +use tempfile::TempDir; + +/// A built version's root tree holding `schemas`, each as a `schema_` +/// entry, the way a build writes them. +fn tree_of(store: &ObjectStore, schemas: &[Vec]) -> Tree { + use comline_core::schema::ir::frozen::cas::blob::build_tree_from_schema; + let mut root = Tree::new(); + for (i, schema) in schemas.iter().enumerate() { + let tree = build_tree_from_schema(schema, store).unwrap(); + let hash = store.write(&tree.to_bytes().unwrap()).unwrap(); + root.add_entry(EntryMode::Tree, format!("schema_{i}"), hash); + } + root +} + +#[test] +fn std_is_compared_by_reach_like_a_dependency() { + // `std` is built in, never vendored (no `dep_std` entry), yet its + // compiled schemas are in every build. A declaration `std` gains that + // the package doesn't reach is neither the package's own feature nor a + // version bump. + use comline_core::package::build::cas::build::compare; + use comline_core::schema::ir::frozen::unit::FrozenUnit; + + let unit = |name: &str| FrozenUnit::Struct { + docstring: None, + parameters: vec![], + name: name.to_string(), + fields: vec![], + span: (0, 0), + }; + let own = vec![FrozenUnit::Namespace("api".into()), unit("Holder")]; + let std_before = vec![FrozenUnit::Namespace("std::extra".into()), unit("Existing")]; + let std_after = vec![FrozenUnit::Namespace("std::extra".into()), unit("Existing"), unit("Added")]; + + let dir = TempDir::new().unwrap(); + let store = ObjectStore::new(dir.path()); + store.init().unwrap(); + let old = tree_of(&store, &[own.clone(), std_before]); + let new = tree_of(&store, &[own, std_after]); + + let comparison = compare(&store, &old, &new).unwrap(); + assert!(comparison.schema_changes.is_empty(), "{:?}", comparison.schema_changes); + assert!(comparison.dependency_changes.is_empty(), "{:?}", comparison.dependency_changes); + // Trees with no stored config count it unseen, at most a patch. + assert!(comparison.bump < VersionBump::Minor, "{:?}", comparison.bump); +}