Skip to content

Commit 1e4af59

Browse files
committed
Allow loading from single wit file
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
1 parent 812d78e commit 1e4af59

1 file changed

Lines changed: 37 additions & 41 deletions

File tree

  • crates/wac-resolver/src

crates/wac-resolver/src/fs.rs

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -79,50 +79,46 @@ impl FileSystemPackageResolver {
7979
// First check to see if a directory exists.
8080
// If so, then treat it as a textual WIT package.
8181
#[cfg(feature = "wit")]
82-
if path.is_dir() {
83-
log::debug!(
84-
"loading WIT package from directory `{path}`",
85-
path = path.display()
86-
);
87-
88-
let mut resolve = wit_parser::Resolve::new();
89-
let (pkg, _) =
90-
resolve
91-
.push_dir(&path)
92-
.map_err(|e| Error::PackageResolutionFailure {
93-
name: key.name.to_string(),
94-
span: *span,
95-
source: e,
96-
})?;
97-
98-
packages.insert(
99-
*key,
100-
Arc::new(
101-
wit_component::encode(Some(true), &resolve, pkg)
102-
.with_context(|| {
103-
format!(
104-
"failed to encode WIT package from directory `{path}`",
105-
path = path.display()
106-
)
107-
})
108-
.map_err(|e| Error::PackageResolutionFailure {
109-
name: key.name.to_string(),
110-
span: *span,
111-
source: e,
112-
})?,
113-
),
114-
);
115-
116-
continue;
117-
} else if path.extension().and_then(std::ffi::OsStr::to_str) == Some("wit") {
118-
return Err(Error::PackageResolutionFailure {
82+
{
83+
let pkg_res_failure = |e| Error::PackageResolutionFailure {
11984
name: key.name.to_string(),
12085
span: *span,
121-
source: anyhow!(
122-
"WIT packages must be directories, not files: `{path}`",
86+
source: e,
87+
};
88+
let mut resolve = wit_parser::Resolve::new();
89+
let pkg = if path.is_dir() {
90+
log::debug!(
91+
"loading WIT package from directory `{path}`",
12392
path = path.display()
124-
),
125-
});
93+
);
94+
95+
let (pkg, _) = resolve.push_dir(&path).map_err(pkg_res_failure)?;
96+
Some(pkg)
97+
} else if path.extension().and_then(std::ffi::OsStr::to_str) == Some("wit") {
98+
let unresolved = wit_parser::UnresolvedPackage::parse_file(&path)
99+
.map_err(pkg_res_failure)?;
100+
let pkg = resolve.push(unresolved).map_err(pkg_res_failure)?;
101+
Some(pkg)
102+
} else {
103+
None
104+
};
105+
if let Some(pkg) = pkg {
106+
packages.insert(
107+
*key,
108+
Arc::new(
109+
wit_component::encode(Some(true), &resolve, pkg)
110+
.with_context(|| {
111+
format!(
112+
"failed to encode WIT package from `{path}`",
113+
path = path.display()
114+
)
115+
})
116+
.map_err(pkg_res_failure)?,
117+
),
118+
);
119+
120+
continue;
121+
}
126122
}
127123

128124
if !path.is_file() {

0 commit comments

Comments
 (0)