Skip to content

Commit 48c2882

Browse files
authored
Merge pull request #179 from mkatychev/feat/display-dependency-impl
feat(core,logging): attach location/dependency where `wasm_pkg_core::wit::resolve_dependencies` failed
2 parents 4e1ea31 + c968e01 commit 48c2882

File tree

8 files changed

+81
-44
lines changed

8 files changed

+81
-44
lines changed

crates/wasm-pkg-client/src/caching/file.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ pub struct FileCache {
2020
root: PathBuf,
2121
}
2222

23+
impl std::fmt::Display for FileCache {
24+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25+
write!(f, "{}", self.root.display())
26+
}
27+
}
28+
2329
impl FileCache {
2430
/// Creates a new file cache that stores data in the given directory.
2531
pub async fn new(root: impl AsRef<Path>) -> anyhow::Result<Self> {

crates/wasm-pkg-client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl Client {
225225
self.config
226226
.namespace_registry(package.namespace())
227227
.and_then(|meta| {
228-
// If the overriden registry matches the registry we are trying to resolve, we
228+
// If the overridden registry matches the registry we are trying to resolve, we
229229
// should use the metadata, otherwise we'll need to fetch the metadata from the
230230
// registry
231231
match (meta, is_override) {

crates/wasm-pkg-common/src/config.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ pub enum RegistryMapping {
4242
Custom(CustomConfig),
4343
}
4444

45+
impl RegistryMapping {
46+
/// returns the inner [`Registry`] object for both mapping variants
47+
fn registry(&self) -> &Registry {
48+
match self {
49+
RegistryMapping::Registry(reg) => reg,
50+
RegistryMapping::Custom(custom) => &custom.registry,
51+
}
52+
}
53+
}
54+
4555
/// Custom registry configuration
4656
#[derive(Debug, Clone, Serialize, Deserialize)]
4757
pub struct CustomConfig {
@@ -181,27 +191,21 @@ impl Config {
181191
/// - The default registry
182192
/// - Hard-coded fallbacks for certain well-known namespaces
183193
pub fn resolve_registry(&self, package: &PackageRef) -> Option<&Registry> {
184-
if let Some(RegistryMapping::Registry(reg)) = self.package_registry_overrides.get(package) {
185-
Some(reg)
186-
} else if let Some(RegistryMapping::Custom(custom)) =
187-
self.package_registry_overrides.get(package)
194+
let namespace = package.namespace();
195+
// look in `self.package_registry_overrides `
196+
// then in `self.namespace_registries`
197+
if let Some(reg) = self
198+
.package_registry_overrides
199+
.get(package)
200+
.or_else(|| self.namespace_registries.get(namespace))
201+
.map(|pkg| pkg.registry())
188202
{
189-
Some(&custom.registry)
190-
} else if let Some(RegistryMapping::Registry(reg)) =
191-
self.namespace_registries.get(package.namespace())
192-
{
193-
Some(reg)
194-
} else if let Some(RegistryMapping::Custom(custom)) =
195-
self.namespace_registries.get(package.namespace())
196-
{
197-
Some(&custom.registry)
203+
return Some(reg);
198204
} else if let Some(reg) = self.default_registry.as_ref() {
199-
Some(reg)
200-
} else if let Some(reg) = self.fallback_namespace_registries.get(package.namespace()) {
201-
Some(reg)
202-
} else {
203-
None
205+
return Some(reg);
204206
}
207+
208+
self.fallback_namespace_registries.get(namespace)
205209
}
206210

207211
/// Returns the default registry.

crates/wasm-pkg-core/src/lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl AsRef<File> for Locker {
364364
}
365365
}
366366

367-
// NOTE(thomastaylor312): These lock file primitives from here on down are mostly copyed wholesale
367+
// NOTE(thomastaylor312): These lock file primitives from here on down are mostly copied wholesale
368368
// from the lock file implementation of cargo-component with some minor modifications to make them
369369
// work with tokio
370370

crates/wasm-pkg-core/src/resolver.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ pub enum Dependency {
3838
Local(PathBuf),
3939
}
4040

41+
impl std::fmt::Display for Dependency {
42+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43+
match self {
44+
Dependency::Package(RegistryPackage {
45+
name,
46+
version,
47+
registry,
48+
}) => {
49+
let registry = registry.as_deref().unwrap_or("_");
50+
let name = name.as_ref().map(|n| n.to_string());
51+
52+
write!(
53+
f,
54+
"{{registry=\"{registry}\" package=\"{}@{version}\"}}",
55+
name.as_deref().unwrap_or("_:_"),
56+
)
57+
}
58+
Dependency::Local(path_buf) => write!(f, "{}", path_buf.display()),
59+
}
60+
}
61+
}
62+
4163
impl FromStr for Dependency {
4264
type Err = anyhow::Error;
4365

@@ -413,7 +435,7 @@ impl<'a> DependencyResolver<'a> {
413435
if !force_override
414436
&& (self.resolutions.contains_key(name) || self.dependencies.contains_key(name))
415437
{
416-
tracing::debug!(%name, "dependency already exists and override is not set, ignoring");
438+
tracing::debug!(%name, %dependency, "dependency already exists and override is not set, ignoring");
417439
return Ok(());
418440
}
419441
self.dependencies.insert(
@@ -432,7 +454,7 @@ impl<'a> DependencyResolver<'a> {
432454
});
433455

434456
// This is a bit of a hack, but if there are multiple local dependencies that are
435-
// nested and overriden, getting the packages from the local package treats _all_
457+
// nested and overridden, getting the packages from the local package treats _all_
436458
// deps as registry deps. So if we're handling a local path and the dependencies
437459
// have a registry package already, override it. Otherwise follow normal overrides.
438460
// We should definitely fix this and change where we resolve these things

crates/wasm-pkg-core/src/wit.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,16 @@ pub async fn resolve_dependencies(
179179
let mut resolver = DependencyResolver::new_with_client(client, lock_file)?;
180180
// add deps from config first in case they're local deps and then add deps from the directory
181181
if let Some(overrides) = config.overrides.as_ref() {
182-
for (pkg, ovride) in overrides.iter() {
182+
for (pkg, ovr) in overrides.iter() {
183183
let pkg: PackageRef = pkg.parse().context("Unable to parse as a package ref")?;
184-
let dep = match (ovride.path.as_ref(), ovride.version.as_ref()) {
185-
(Some(path), None) => {
186-
let path = tokio::fs::canonicalize(path).await?;
187-
Dependency::Local(path)
188-
}
189-
(Some(path), Some(_)) => {
190-
tracing::warn!("Ignoring version override for local package");
191-
let path = tokio::fs::canonicalize(path).await?;
184+
let dep = match (ovr.path.as_ref(), ovr.version.as_ref()) {
185+
(Some(path), v) => {
186+
if v.is_some() {
187+
tracing::warn!("Ignoring version override for local package");
188+
}
189+
let path = tokio::fs::canonicalize(path)
190+
.await
191+
.with_context(|| format!("resolving local dependency {}", path.display()))?;
192192
Dependency::Local(path)
193193
}
194194
(None, Some(version)) => Dependency::Package(RegistryPackage {
@@ -201,10 +201,12 @@ pub async fn resolve_dependencies(
201201
continue;
202202
}
203203
};
204+
205+
tracing::debug!(dependency = %dep);
204206
resolver
205207
.add_dependency(&pkg, &dep)
206208
.await
207-
.context("Unable to add dependency")?;
209+
.with_context(|| format!("unable to add dependency {dep}"))?;
208210
}
209211
}
210212
let (_name, packages) = get_packages(path)?;

crates/wkg/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl Common {
6666
} else {
6767
FileCache::global_cache_path().context("unable to find cache directory")?
6868
};
69+
6970
FileCache::new(dir).await
7071
}
7172

@@ -76,6 +77,8 @@ impl Common {
7677
let config = self.load_config().await?;
7778
let cache = self.load_cache().await?;
7879
let client = Client::new(config);
80+
81+
tracing::debug!(filecache_dir = %cache);
7982
Ok(CachingClient::new(Some(client), cache))
8083
}
8184
}

crates/wkg/tests/fixtures/wasi-http/wit/types.wit

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ interface types {
205205
/// list with the same key.
206206
entries: func() -> list<tuple<field-key,field-value>>;
207207

208-
/// Make a deep copy of the Fields. Equivelant in behavior to calling the
208+
/// Make a deep copy of the Fields. Equivalent in behavior to calling the
209209
/// `fields` constructor on the return value of `entries`. The resulting
210210
/// `fields` is mutable.
211211
clone: func() -> fields;
@@ -310,7 +310,7 @@ interface types {
310310
/// `delete` operations will fail with `header-error.immutable`.
311311
///
312312
/// This headers resource is a child: it must be dropped before the parent
313-
/// `outgoing-request` is dropped, or its ownership is transfered to
313+
/// `outgoing-request` is dropped, or its ownership is transferred to
314314
/// another component by e.g. `outgoing-handler.handle`.
315315
headers: func() -> headers;
316316
}
@@ -426,19 +426,19 @@ interface types {
426426
finish: static func(this: incoming-body) -> future-trailers;
427427
}
428428

429-
/// Represents a future which may eventaully return trailers, or an error.
429+
/// Represents a future which may eventually return trailers, or an error.
430430
///
431431
/// In the case that the incoming HTTP Request or Response did not have any
432432
/// trailers, this future will resolve to the empty set of trailers once the
433433
/// complete Request or Response body has been received.
434434
resource future-trailers {
435435

436436
/// Returns a pollable which becomes ready when either the trailers have
437-
/// been received, or an error has occured. When this pollable is ready,
437+
/// been received, or an error has occurred. When this pollable is ready,
438438
/// the `get` method will return `some`.
439439
subscribe: func() -> pollable;
440440

441-
/// Returns the contents of the trailers, or an error which occured,
441+
/// Returns the contents of the trailers, or an error which occurred,
442442
/// once the future is ready.
443443
///
444444
/// The outer `option` represents future readiness. Users can wait on this
@@ -450,7 +450,7 @@ interface types {
450450
///
451451
/// The inner `result` represents that either the HTTP Request or Response
452452
/// body, as well as any trailers, were received successfully, or that an
453-
/// error occured receiving them. The optional `trailers` indicates whether
453+
/// error occurred receiving them. The optional `trailers` indicates whether
454454
/// or not trailers were present in the body.
455455
///
456456
/// When some `trailers` are returned by this method, the `trailers`
@@ -483,7 +483,7 @@ interface types {
483483
/// `delete` operations will fail with `header-error.immutable`.
484484
///
485485
/// This headers resource is a child: it must be dropped before the parent
486-
/// `outgoing-request` is dropped, or its ownership is transfered to
486+
/// `outgoing-request` is dropped, or its ownership is transferred to
487487
/// another component by e.g. `outgoing-handler.handle`.
488488
headers: func() -> headers;
489489

@@ -507,7 +507,7 @@ interface types {
507507
///
508508
/// If the user code drops this resource, as opposed to calling the static
509509
/// method `finish`, the implementation should treat the body as incomplete,
510-
/// and that an error has occured. The implementation should propogate this
510+
/// and that an error has occurred. The implementation should propagate this
511511
/// error to the HTTP protocol by whatever means it has available,
512512
/// including: corrupting the body on the wire, aborting the associated
513513
/// Request, or sending a late status code for the Response.
@@ -539,14 +539,14 @@ interface types {
539539
) -> result<_, error-code>;
540540
}
541541

542-
/// Represents a future which may eventaully return an incoming HTTP
542+
/// Represents a future which may eventually return an incoming HTTP
543543
/// Response, or an error.
544544
///
545545
/// This resource is returned by the `wasi:http/outgoing-handler` interface to
546546
/// provide the HTTP Response corresponding to the sent Request.
547547
resource future-incoming-response {
548548
/// Returns a pollable which becomes ready when either the Response has
549-
/// been received, or an error has occured. When this pollable is ready,
549+
/// been received, or an error has occurred. When this pollable is ready,
550550
/// the `get` method will return `some`.
551551
subscribe: func() -> pollable;
552552

@@ -560,8 +560,8 @@ interface types {
560560
/// is `some`, and error on subsequent calls.
561561
///
562562
/// The inner `result` represents that either the incoming HTTP Response
563-
/// status and headers have recieved successfully, or that an error
564-
/// occured. Errors may also occur while consuming the response body,
563+
/// status and headers have received successfully, or that an error
564+
/// occurred. Errors may also occur while consuming the response body,
565565
/// but those will be reported by the `incoming-body` and its
566566
/// `output-stream` child.
567567
get: func() -> option<result<result<incoming-response, error-code>>>;

0 commit comments

Comments
 (0)