wit_parser/resolve/fs.rs
1//! Filesystem operations for [`Resolve`].
2
3use alloc::format;
4use std::path::Path;
5use std::vec::Vec;
6
7use anyhow::{Context, Result, bail};
8
9use super::{PackageSources, Resolve};
10use crate::UnresolvedPackageGroup;
11
12/// All the sources used during resolving a directory or path.
13#[derive(Clone, Debug)]
14pub struct PackageSourceMap {
15 inner: PackageSources,
16}
17
18impl PackageSourceMap {
19 fn from_single_source(package_id: super::PackageId, source: &Path) -> Result<Self> {
20 let path_str = source
21 .to_str()
22 .ok_or_else(|| anyhow::anyhow!("path is not valid utf-8: {:?}", source))?;
23 Ok(Self {
24 inner: PackageSources::from_single_source(package_id, path_str),
25 })
26 }
27
28 fn from_inner(inner: PackageSources) -> Self {
29 Self { inner }
30 }
31
32 /// All unique source paths.
33 pub fn paths(&self) -> impl Iterator<Item = &Path> {
34 self.inner.source_names().map(Path::new)
35 }
36
37 /// Source paths for package
38 pub fn package_paths(&self, id: super::PackageId) -> Option<impl Iterator<Item = &Path>> {
39 self.inner
40 .package_source_names(id)
41 .map(|iter| iter.map(Path::new))
42 }
43}
44
45enum ParsedFile {
46 #[cfg(feature = "decoding")]
47 Package(super::PackageId),
48 Unresolved(UnresolvedPackageGroup),
49}
50
51impl Resolve {
52 /// Parse WIT packages from the input `path`.
53 ///
54 /// The input `path` can be one of:
55 ///
56 /// * A directory containing a WIT package with an optional `deps` directory
57 /// for local dependencies. In this case `deps` is parsed first and then
58 /// the parent `path` is parsed and returned.
59 /// * A single standalone WIT file.
60 /// * A wasm-encoded WIT package as a single file in either the text or
61 /// binary format.
62 ///
63 /// More information can also be found at [`Resolve::push_dir`] and
64 /// [`Resolve::push_file`].
65 pub fn push_path(
66 &mut self,
67 path: impl AsRef<Path>,
68 ) -> Result<(super::PackageId, PackageSourceMap)> {
69 self._push_path(path.as_ref())
70 }
71
72 fn _push_path(&mut self, path: &Path) -> Result<(super::PackageId, PackageSourceMap)> {
73 if path.is_dir() {
74 self.push_dir(path).with_context(|| {
75 format!(
76 "failed to resolve directory while parsing WIT for path [{}]",
77 path.display()
78 )
79 })
80 } else {
81 let id = self.push_file(path)?;
82 Ok((id, PackageSourceMap::from_single_source(id, path)?))
83 }
84 }
85
86 /// Parses the filesystem directory at `path` as a WIT package and returns
87 /// a fully resolved [`super::PackageId`] list as a result.
88 ///
89 /// The directory itself is parsed with [`UnresolvedPackageGroup::parse_dir`]
90 /// and then all packages found are inserted into this `Resolve`. The `path`
91 /// specified may have a `deps` subdirectory which is probed automatically
92 /// for any other WIT dependencies.
93 ///
94 /// The `deps` folder may contain:
95 ///
96 /// * `$path/deps/my-package/*.wit` - a directory that may contain multiple
97 /// WIT files. This is parsed with [`UnresolvedPackageGroup::parse_dir`]
98 /// and then inserted into this [`Resolve`]. Note that cannot recursively
99 /// contain a `deps` directory.
100 /// * `$path/deps/my-package.wit` - a single-file WIT package. This is
101 /// parsed with [`Resolve::push_file`] and then added to `self` for
102 /// name resolution.
103 /// * `$path/deps/my-package.{wasm,wat}` - a wasm-encoded WIT package either
104 /// in the text for binary format.
105 ///
106 /// In all cases entries in the `deps` folder are added to `self` first
107 /// before adding files found in `path` itself. All WIT packages found are
108 /// candidates for name-based resolution that other packages may use.
109 ///
110 /// This function returns a tuple of two values. The first value is a
111 /// [`super::PackageId`], which represents the main WIT package found within
112 /// `path`. This argument is useful for passing to [`Resolve::select_world`]
113 /// for choosing something to bindgen with.
114 ///
115 /// The second value returned is a [`PackageSourceMap`], which contains all the sources
116 /// that were parsed during resolving. This can be useful for:
117 /// * build systems that want to rebuild bindings whenever one of the files changed
118 /// * or other tools, which want to identify the sources for the resolved packages
119 pub fn push_dir(
120 &mut self,
121 path: impl AsRef<Path>,
122 ) -> Result<(super::PackageId, PackageSourceMap)> {
123 self._push_dir(path.as_ref())
124 }
125
126 fn _push_dir(&mut self, path: &Path) -> Result<(super::PackageId, PackageSourceMap)> {
127 let top_pkg = UnresolvedPackageGroup::parse_dir(path)
128 .with_context(|| format!("failed to parse package: {}", path.display()))?;
129 let deps = path.join("deps");
130 let deps = self
131 .parse_deps_dir(&deps)
132 .with_context(|| format!("failed to parse dependency directory: {}", deps.display()))?;
133
134 let (pkg_id, inner) = self.sort_unresolved_packages(top_pkg, deps)?;
135 Ok((pkg_id, PackageSourceMap::from_inner(inner)))
136 }
137
138 fn parse_deps_dir(&mut self, path: &Path) -> Result<Vec<UnresolvedPackageGroup>> {
139 let mut ret = Vec::new();
140 if !path.exists() {
141 return Ok(ret);
142 }
143 let mut entries = path
144 .read_dir()
145 .and_then(|i| i.collect::<std::io::Result<Vec<_>>>())
146 .context("failed to read directory")?;
147 entries.sort_by_key(|e| e.file_name());
148 for dep in entries {
149 let path = dep.path();
150 let pkg = if dep.file_type()?.is_dir() || path.metadata()?.is_dir() {
151 // If this entry is a directory or a symlink point to a
152 // directory then always parse it as an `UnresolvedPackage`
153 // since it's intentional to not support recursive `deps`
154 // directories.
155 UnresolvedPackageGroup::parse_dir(&path)
156 .with_context(|| format!("failed to parse package: {}", path.display()))?
157 } else {
158 // If this entry is a file then we may want to ignore it but
159 // this may also be a standalone WIT file or a `*.wasm` or
160 // `*.wat` encoded package.
161 let filename = dep.file_name();
162 match Path::new(&filename).extension().and_then(|s| s.to_str()) {
163 Some("wit") | Some("wat") | Some("wasm") => match self._push_file(&path)? {
164 #[cfg(feature = "decoding")]
165 ParsedFile::Package(_) => continue,
166 ParsedFile::Unresolved(pkg) => pkg,
167 },
168
169 // Other files in deps dir are ignored for now to avoid
170 // accidentally including things like `.DS_Store` files in
171 // the call below to `parse_dir`.
172 _ => continue,
173 }
174 };
175 ret.push(pkg);
176 }
177 Ok(ret)
178 }
179
180 /// Parses the contents of `path` from the filesystem and pushes the result
181 /// into this `Resolve`.
182 ///
183 /// The `path` referenced here can be one of:
184 ///
185 /// * A WIT file. Note that in this case this single WIT file will be the
186 /// entire package and any dependencies it has must already be in `self`.
187 /// * A WIT package encoded as WebAssembly, either in text or binary form.
188 /// In this the package and all of its dependencies are automatically
189 /// inserted into `self`.
190 ///
191 /// In both situations the `PackageId`s of the resulting resolved packages
192 /// are returned from this method. The return value is mostly useful in
193 /// conjunction with [`Resolve::select_world`].
194 pub fn push_file(&mut self, path: impl AsRef<Path>) -> Result<super::PackageId> {
195 match self._push_file(path.as_ref())? {
196 #[cfg(feature = "decoding")]
197 ParsedFile::Package(id) => Ok(id),
198 ParsedFile::Unresolved(pkg) => self.push_group(pkg),
199 }
200 }
201
202 fn _push_file(&mut self, path: &Path) -> Result<ParsedFile> {
203 let contents = std::fs::read(path)
204 .with_context(|| format!("failed to read path for WIT [{}]", path.display()))?;
205
206 // If decoding is enabled at compile time then try to see if this is a
207 // wasm file.
208 #[cfg(feature = "decoding")]
209 {
210 use crate::decoding::{DecodedWasm, decode};
211
212 #[cfg(feature = "wat")]
213 let is_wasm = wat::Detect::from_bytes(&contents).is_wasm();
214 #[cfg(not(feature = "wat"))]
215 let is_wasm = wasmparser::Parser::is_component(&contents);
216
217 if is_wasm {
218 #[cfg(feature = "wat")]
219 let contents = wat::parse_bytes(&contents).map_err(|mut e| {
220 e.set_path(path);
221 e
222 })?;
223
224 match decode(&contents)? {
225 DecodedWasm::Component(..) => {
226 bail!("found an actual component instead of an encoded WIT package in wasm")
227 }
228 DecodedWasm::WitPackage(resolve, pkg) => {
229 let remap = self.merge(resolve)?;
230 return Ok(ParsedFile::Package(remap.packages[pkg.index()]));
231 }
232 }
233 }
234 }
235
236 // If this wasn't a wasm file then assume it's a WIT file.
237 let text = match core::str::from_utf8(&contents) {
238 Ok(s) => s,
239 Err(_) => bail!("input file is not valid utf-8 [{}]", path.display()),
240 };
241 let pkgs = UnresolvedPackageGroup::parse(path, text)?;
242 Ok(ParsedFile::Unresolved(pkgs))
243 }
244
245 /// Convenience method for combining [`UnresolvedPackageGroup::parse`] and
246 /// [`Resolve::push_group`].
247 ///
248 /// The `path` provided is used for error messages but otherwise is not
249 /// read. This method does not touch the filesystem. The `contents` provided
250 /// are the contents of a WIT package.
251 pub fn push_str(&mut self, path: impl AsRef<Path>, contents: &str) -> Result<super::PackageId> {
252 let path = path
253 .as_ref()
254 .to_str()
255 .ok_or_else(|| anyhow::anyhow!("path is not valid utf-8: {:?}", path.as_ref()))?;
256 self.push_source(path, contents)
257 }
258}