Skip to content

Commit

Permalink
Remove tame-index (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle committed Aug 23, 2023
1 parent 097594a commit 33d4997
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-22.04]
features: ["--features targets", "--features prefer-index", null]
features: ["--features targets", null]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ rust-version = "1.65.0"

[features]
default = []
# Uses the index as the source of truth for what features are available for a crate
prefer-index = ["dep:tame-index"]
# Adds support for filtering target specific dependencies
targets = ["cfg-expr/targets"]

Expand All @@ -29,8 +27,6 @@ targets = ["cfg-expr/targets"]
cargo_metadata = "0.17"
# Used to parse and evaluate cfg() expressions for dependencies
cfg-expr = "0.15"
# Allows inspection of the cargo registry index(ices)
tame-index = { version = "0.4", optional = true, default-features = false }
# Used to create and traverse graph structures
petgraph = "0.6"
# Used for checking version requirements
Expand All @@ -44,3 +40,5 @@ insta = "1.21"
similar-asserts = "1.1"
# Used to deserialize test files into metadata we can load
serde_json = "1.0"
# index metadata retrieval
tame-index = "0.5"
26 changes: 5 additions & 21 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub(crate) mod features;

#[cfg(feature = "prefer-index")]
pub mod index;

use crate::{DepKind, Edge, Error, Kid, Krates};
Expand Down Expand Up @@ -312,8 +311,7 @@ pub struct Builder {
exclude: Vec<crate::PkgSpec>,
ignore_kinds: u32,
workspace: bool,
#[cfg(feature = "prefer-index")]
crates_io_index: Option<tame_index::index::ComboIndexCache>,
crates_io_index: Option<index::BuildIndexCache>,
}

impl Builder {
Expand Down Expand Up @@ -503,22 +501,10 @@ impl Builder {
/// You can force the usage of the git registry (if not overridden in the
/// local environment) by specifying `Some("1.69.0")` or lower semver as the
/// sparse registry was not made the default until `1.70.0`
#[cfg(feature = "prefer-index")]
pub fn with_crates_io_index(
mut self,
config_root: Option<tame_index::PathBuf>,
cargo_home: Option<tame_index::PathBuf>,
cargo_version: Option<&str>,
) -> Result<Self, Error> {
let crates_io =
tame_index::IndexUrl::crates_io(config_root, cargo_home.as_deref(), cargo_version)?;

let index = tame_index::index::ComboIndexCache::new(
tame_index::IndexLocation::new(crates_io).with_root(cargo_home),
)?;

self.crates_io_index = Some(index);
Ok(self)
#[inline]
pub fn with_crates_io_index(&mut self, index_cache_build: index::BuildIndexCache) -> &mut Self {
self.crates_io_index = Some(index_cache_build);
self
}

/// Builds a [`Krates`] graph using metadata that be retrieved via the
Expand Down Expand Up @@ -625,7 +611,6 @@ impl Builder {

// Load all the cache entries from disk for all the possible _unique_
// crates in the graph so that we don't need to access disk again
#[cfg(feature = "prefer-index")]
let index = self.crates_io_index.map(|index| {
index::CachingIndex::new(index, packages.iter().map(|pkg| pkg.name.clone()).collect())
});
Expand Down Expand Up @@ -847,7 +832,6 @@ impl Builder {
}
};

#[cfg(feature = "prefer-index")]
if let Some(index) = &index {
index::fix_features(index, krate);
}
Expand Down
42 changes: 8 additions & 34 deletions src/builder/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::{BTreeMap, BTreeSet};
use tame_index::index::ComboIndexCache;

pub type BuildIndexCache =
Box<dyn FnOnce(BTreeSet<String>) -> BTreeMap<String, Option<IndexKrate>>>;

pub type FeaturesMap = BTreeMap<String, Vec<String>>;

Expand All @@ -19,40 +21,12 @@ pub struct CachingIndex {
}

impl CachingIndex {
/// Creates a caching index around the specified index
pub fn new(inner: ComboIndexCache, krates: BTreeSet<String>) -> Self {
let mut cache = BTreeMap::new();
for name in krates {
let read = || -> Option<IndexKrate> {
let name = name.as_str().try_into().ok()?;
let krate = inner.cached_krate(name).ok()??;
let versions = krate
.versions
.into_iter()
.filter_map(|kv| {
// The index (currently) can have both features, and
// features2, the features method gives us an iterator
// over both
kv.version.parse::<semver::Version>().ok().map(|version| {
IndexKrateVersion {
version,
features: kv
.features()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
}
})
})
.collect();

Some(IndexKrate { versions })
};

let krate = read();
cache.insert(name, krate);
/// Creates a caching index for information provided by the user
#[inline]
pub fn new(build: BuildIndexCache, krates: BTreeSet<String>) -> Self {
Self {
cache: build(krates),
}

Self { cache }
}

fn index_krate_features(&self, name: &str, version: &semver::Version) -> Option<&FeaturesMap> {
Expand Down
14 changes: 0 additions & 14 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ pub enum Error {
/// Due to how the graph was built, all possible root nodes were actually
/// filtered out, leaving an empty graph
NoRootKrates,
/// An error occurred trying to open or read the crates.io index
#[cfg(feature = "prefer-index")]
Index(tame_index::Error),
}

impl fmt::Display for Error {
Expand All @@ -25,8 +22,6 @@ impl fmt::Display for Error {
Self::Metadata(err) => write!(f, "{err}"),
Self::InvalidPkgSpec(err) => write!(f, "package spec was invalid: {err}"),
Self::NoRootKrates => f.write_str("no root crates available"),
#[cfg(feature = "prefer-index")]
Self::Index(err) => write!(f, "{err}"),
}
}
}
Expand All @@ -35,8 +30,6 @@ impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Metadata(err) => Some(err),
#[cfg(feature = "with-crates-index")]
Self::CratesIndex(err) => Some(err),
_ => None,
}
}
Expand All @@ -47,10 +40,3 @@ impl From<CMErr> for Error {
Error::Metadata(e)
}
}

#[cfg(feature = "prefer-index")]
impl From<tame_index::Error> for Error {
fn from(e: tame_index::Error) -> Self {
Error::Index(e)
}
}
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ mod builder;
mod errors;
mod pkgspec;

#[cfg(feature = "prefer-index")]
pub use builder::index;
pub use builder::{
features::{Feature, ParsedFeature},
Builder, Cmd, LockOptions, NoneFilter, OnFilter, Scope, Target,
index, Builder, Cmd, LockOptions, NoneFilter, OnFilter, Scope, Target,
};
pub use errors::Error;
pub use pkgspec::PkgSpec;
Expand Down
51 changes: 41 additions & 10 deletions tests/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ fn handles_cyclic_features() {
}

/// Tests validating <https://github.com/EmbarkStudios/krates/issues/46>
#[cfg(feature = "prefer-index")]
mod prefer_index {
fn confirm_index_snapshot(builder: krates::Builder) {
let mut cmd = krates::Cmd::new();
Expand All @@ -250,16 +249,48 @@ mod prefer_index {
#[test]
fn uses_sparse_index() {
let mut b = krates::Builder::new();
b = b.with_crates_io_index(None, None, None).unwrap();
confirm_index_snapshot(b);
}

/// Validates we can use the sparse index to fix features
#[test]
#[ignore = "incredibly slow if git index is missing/outdated"]
fn uses_git_index() {
let mut b = krates::Builder::new();
b = b.with_crates_io_index(None, None, Some("1.69.0")).unwrap();
let cache_index = |krates: std::collections::BTreeSet<String>| {
let index = tame_index::index::ComboIndexCache::new(tame_index::IndexLocation::new(
tame_index::IndexUrl::CratesIoSparse,
))
.unwrap();

let mut cache = std::collections::BTreeMap::new();
for name in krates {
let read = || -> Option<krates::index::IndexKrate> {
let name = name.as_str().try_into().ok()?;
let krate = index.cached_krate(name).ok()??;
let versions = krate
.versions
.into_iter()
.filter_map(|kv| {
// The index (currently) can have both features, and
// features2, the features method gives us an iterator
// over both
kv.version.parse::<semver::Version>().ok().map(|version| {
krates::index::IndexKrateVersion {
version,
features: kv
.features()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
}
})
})
.collect();

Some(krates::index::IndexKrate { versions })
};

let krate = read();
cache.insert(name, krate);
}

cache
};

b.with_crates_io_index(Box::new(cache_index));
confirm_index_snapshot(b);
}
}

0 comments on commit 33d4997

Please sign in to comment.