1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! File resolver.
//!
//! Modified from [`solang`](https://github.com/hyperledger/solang/blob/0f032dcec2c6e96797fd66fa0175a02be0aba71c/src/file_resolver.rs).
use super::SourceFile;
use crate::{Session, SourceMap};
use itertools::Itertools;
use normalize_path::NormalizePath;
use solar_config::ImportRemapping;
use solar_data_structures::smallvec::SmallVec;
use std::{
borrow::Cow,
io,
path::{Path, PathBuf},
sync::{Arc, OnceLock},
};
/// An error that occurred while resolving a path.
#[derive(Debug, thiserror::Error)]
pub enum ResolveError {
#[error("couldn't read stdin: {0}")]
ReadStdin(#[source] io::Error),
#[error("couldn't read {0}: {1}")]
ReadFile(PathBuf, #[source] io::Error),
#[error("file {0} not found")]
NotFound(PathBuf),
#[error("multiple files match {}: {}", .0.display(), .1.iter().map(|f| f.name.display()).format(", "))]
MultipleMatches(PathBuf, Vec<Arc<SourceFile>>),
}
/// Performs file resolution by applying import paths and mappings.
#[derive(derive_more::Debug)]
pub struct FileResolver<'a> {
#[debug(skip)]
source_map: &'a SourceMap,
/// Include paths.
include_paths: Vec<PathBuf>,
/// Import remappings.
remappings: Vec<ImportRemapping>,
/// Custom current directory.
custom_current_dir: Option<PathBuf>,
/// [`std::env::current_dir`] cache. Unused if the current directory is set manually.
env_current_dir: OnceLock<Option<PathBuf>>,
}
impl<'a> FileResolver<'a> {
/// Creates a new file resolver.
pub fn new(source_map: &'a SourceMap) -> Self {
Self {
source_map,
include_paths: Vec::new(),
remappings: Vec::new(),
custom_current_dir: None,
env_current_dir: OnceLock::new(),
}
}
/// Configures the file resolver from a session.
pub fn configure_from_sess(&mut self, sess: &Session) {
self.add_include_paths(sess.opts.include_paths.iter().cloned());
self.add_import_remappings(sess.opts.import_remappings.iter().cloned());
'b: {
if let Some(base_path) = &sess.opts.base_path {
let base_path = if base_path.is_absolute() {
base_path.as_path()
} else {
&if let Ok(path) = self.canonicalize_unchecked(base_path) {
path
} else {
break 'b;
}
};
self.set_current_dir(base_path);
}
}
}
/// Clears the internal state.
pub fn clear(&mut self) {
self.include_paths.clear();
self.remappings.clear();
self.custom_current_dir = None;
self.env_current_dir.take();
}
/// Sets the current directory.
///
/// # Panics
///
/// Panics if `current_dir` is not an absolute path.
#[track_caller]
#[doc(alias = "set_base_path")]
pub fn set_current_dir(&mut self, current_dir: &Path) {
if !current_dir.is_absolute() {
panic!("current_dir must be an absolute path");
}
self.custom_current_dir = Some(current_dir.to_path_buf());
}
/// Adds include paths.
pub fn add_include_paths(&mut self, paths: impl IntoIterator<Item = PathBuf>) {
self.include_paths.extend(paths);
}
/// Adds an include path.
pub fn add_include_path(&mut self, path: PathBuf) {
self.include_paths.push(path)
}
/// Adds import remappings.
pub fn add_import_remappings(&mut self, remappings: impl IntoIterator<Item = ImportRemapping>) {
self.remappings.extend(remappings);
}
/// Adds an import remapping.
pub fn add_import_remapping(&mut self, remapping: ImportRemapping) {
self.remappings.push(remapping);
}
/// Returns the source map.
pub fn source_map(&self) -> &'a SourceMap {
self.source_map
}
/// Returns the current directory, or `.` if it could not be resolved.
#[doc(alias = "base_path")]
pub fn current_dir(&self) -> &Path {
self.try_current_dir().unwrap_or(Path::new("."))
}
/// Returns the current directory, if resolved successfully.
#[doc(alias = "try_base_path")]
pub fn try_current_dir(&self) -> Option<&Path> {
self.custom_current_dir.as_deref().or_else(|| self.env_current_dir())
}
fn env_current_dir(&self) -> Option<&Path> {
self.env_current_dir
.get_or_init(|| {
std::env::current_dir()
.inspect_err(|e| debug!("failed to get current_dir: {e}"))
.ok()
})
.as_deref()
}
/// Canonicalizes a path using [`Self::current_dir`].
pub fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
self.canonicalize_unchecked(&self.make_absolute(path))
}
fn canonicalize_unchecked(&self, path: &Path) -> io::Result<PathBuf> {
self.source_map.file_loader().canonicalize_path(path)
}
/// Normalizes a path removing unnecessary components.
///
/// Does not perform I/O.
pub fn normalize<'b>(&self, path: &'b Path) -> Cow<'b, Path> {
// NOTE: checking `is_normalized` will not produce the correct result since it won't
// consider `./` segments. See its documentation.
Cow::Owned(path.normalize())
}
/// Makes the path absolute by joining it with the current directory.
///
/// Does not perform I/O.
pub fn make_absolute<'b>(&self, path: &'b Path) -> Cow<'b, Path> {
if path.is_absolute() {
Cow::Borrowed(path)
} else if let Some(current_dir) = self.try_current_dir() {
Cow::Owned(current_dir.join(path))
} else {
Cow::Borrowed(path)
}
}
/// Resolves an import path.
///
/// `parent` is the path of the file that contains the import, if any.
#[instrument(level = "debug", skip_all, fields(path = %path.display()))]
pub fn resolve_file(
&self,
path: &Path,
parent: Option<&Path>,
) -> Result<Arc<SourceFile>, ResolveError> {
// https://docs.soliditylang.org/en/latest/path-resolution.html
// Only when the path starts with ./ or ../ are relative paths considered; this means
// that `import "b.sol";` will check the import paths for b.sol, while `import "./b.sol";`
// will only check the path relative to the current file.
//
// `parent.is_none()` only happens when resolving imports from a custom/stdin file, or when
// manually resolving a file, like from CLI arguments. In these cases, the file is
// considered to be in the current directory.
// Technically, this behavior allows the latter, the manual case, to also be resolved using
// remappings, which is not the case in solc, but this simplifies the implementation.
let is_relative = path.starts_with("./") || path.starts_with("../");
if (is_relative && parent.is_some()) || parent.is_none() {
let try_path = if let Some(base) = parent.filter(|_| is_relative).and_then(Path::parent)
{
&base.join(path)
} else {
path
};
if let Some(file) = self.try_file(try_path)? {
return Ok(file);
}
// See above.
if is_relative {
return Err(ResolveError::NotFound(path.into()));
}
}
let original_path = path;
let path = &*self.remap_path(path, parent);
let mut candidates = SmallVec::<[_; 1]>::new();
// Quick deduplication when include paths are duplicated.
let mut push_candidate = |file: Arc<SourceFile>| {
if !candidates.iter().any(|f| Arc::ptr_eq(f, &file)) {
candidates.push(file);
}
};
// If there are no include paths, then try the file directly. See
// https://docs.soliditylang.org/en/latest/path-resolution.html#base-path-and-include-paths
// "By default the base path is empty, which leaves the source unit name unchanged."
if self.include_paths.is_empty() || path.is_absolute() {
if let Some(file) = self.try_file(path)? {
push_candidate(file);
}
} else {
// Try all the include paths.
let base_path = self.try_current_dir().into_iter();
for include_path in base_path.chain(self.include_paths.iter().map(|p| p.as_path())) {
let path = include_path.join(path);
if let Some(file) = self.try_file(&path)? {
push_candidate(file);
}
}
}
match candidates.len() {
0 => Err(ResolveError::NotFound(original_path.into())),
1 => Ok(candidates.pop().unwrap()),
_ => Err(ResolveError::MultipleMatches(original_path.into(), candidates.into_vec())),
}
}
/// Applies the import path mappings to `path`.
// Reference: <https://github.com/argotorg/solidity/blob/e202d30db8e7e4211ee973237ecbe485048aae97/libsolidity/interface/ImportRemapper.cpp#L32>
pub fn remap_path<'b>(&self, path: &'b Path, parent: Option<&Path>) -> Cow<'b, Path> {
let remapped = self.remap_path_(path, parent);
if remapped != path {
trace!(remapped=%remapped.display());
}
remapped
}
fn remap_path_<'b>(&self, path: &'b Path, parent: Option<&Path>) -> Cow<'b, Path> {
let _context = &*parent.map(|p| p.to_string_lossy()).unwrap_or_default();
let mut longest_prefix = 0;
let mut longest_context = 0;
let mut best_match_target = None;
let mut unprefixed_path = path;
for ImportRemapping { context, prefix, path: target } in &self.remappings {
let context = &*sanitize_path(context);
let prefix = &*sanitize_path(prefix);
// Skip if current context is closer.
if context.len() < longest_context {
continue;
}
// Skip if current context is not a prefix of the context.
if !_context.starts_with(context) {
continue;
}
// Skip if we already have a closer prefix match.
if prefix.len() < longest_prefix && context.len() == longest_context {
continue;
}
// Skip if the prefix does not match.
let Ok(up) = path.strip_prefix(prefix) else {
continue;
};
longest_context = context.len();
longest_prefix = prefix.len();
best_match_target = Some(sanitize_path(target));
unprefixed_path = up;
}
if let Some(best_match_target) = best_match_target {
let mut out = PathBuf::from(&*best_match_target);
out.push(unprefixed_path);
Cow::Owned(out)
} else {
Cow::Borrowed(unprefixed_path)
}
}
/// Loads stdin into the source map.
pub fn load_stdin(&self) -> Result<Arc<SourceFile>, ResolveError> {
self.source_map().load_stdin().map_err(ResolveError::ReadStdin)
}
/// Loads `path` into the source map. Returns `None` if the file doesn't exist.
#[instrument(level = "debug", skip_all, fields(path = %path.display()))]
pub fn try_file(&self, path: &Path) -> Result<Option<Arc<SourceFile>>, ResolveError> {
// Normalize unnecessary components.
let rpath = &*self.normalize(path);
if let Some(file) = self.source_map().get_file(rpath) {
trace!("loaded from cache 1");
return Ok(Some(file));
}
// Make the path absolute with the current directory.
let apath = &*self.make_absolute(rpath);
if apath != rpath
&& let Some(file) = self.source_map().get_file(apath)
{
trace!("loaded from cache 2");
return Ok(Some(file));
}
// Canonicalize, checking symlinks and if it exists.
if let Ok(path) = self.canonicalize_unchecked(apath) {
return self
.source_map()
// Store the file with `apath` as the name instead of `path`.
// In case of symlinks we want to reference the symlink path, not the target path.
.load_file_with_name(apath.to_path_buf().into(), &path)
.map(Some)
.map_err(|e| ResolveError::ReadFile(path, e));
}
trace!("not found");
Ok(None)
}
}
fn sanitize_path(s: &str) -> impl std::ops::Deref<Target = str> + '_ {
// TODO: Equivalent of: `boost::filesystem::path(_path).generic_string()`
s
}