Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat: support remapping contexts #2509

Merged
merged 13 commits into from
Jul 15, 2023

Conversation

onbjerg
Copy link
Collaborator

@onbjerg onbjerg commented Jul 14, 2023

Motivation

solc supports remapping contexts to limit the scope of remappings to subsets of your project, e.g. libraries you import.

We want this for foundry-rs/foundry#1855

Solution

In foundry-rs/foundry#1855 we ideally would want to auto-detect the contexts as well, i.e. limit remappings from libraries to the library roots.

This PR adds support for parsing and serializing remapping contexts, and limits import resolution based on the remapping contexts.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@onbjerg onbjerg marked this pull request as ready for review July 15, 2023 00:57
Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, just doc nits

@@ -325,23 +325,37 @@ impl ProjectPathsConfig {
/// duplicate `contracts` segment:
/// `@openzeppelin/contracts/contracts/token/ERC20/IERC20.sol` we check for this edge case
/// here so that both styles work out of the box.
pub fn resolve_library_import(&self, import: &Path) -> Option<PathBuf> {
pub fn resolve_library_import(&self, cwd: &Path, import: &Path) -> Option<PathBuf> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a cwd now?

Comment on lines +337 to +338
if let Some(ctx) = r.context.as_ref() {
cwd.starts_with(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah because of the context?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep we want to limit only to that path bc otherwise we end up polluting other files' remappings in the deps

Comment on lines +35 to +37
/// - A `prefix`: the path that's used in your smart contract, i.e.
/// `@openzeppelin/contracts-ethereum-package`
/// - A `target`: the absolute path of the downloaded contracts on your computer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also copy the context docs from the solc docs here?

Copy link
Contributor

@Evalir Evalir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sick! my only worry is windows support (as always with these path prs)

#[cfg(windows)]
let mut s = String::new();
if let Some(context) = self.context.as_ref() {
#[cfg(target_os = "windows")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have tests for windows style paths but did you get to test this on a windows box? don't have one available atm :/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested in CI

);
assert_eq!(remapping.to_string(), "context:oz=a/b/c/d/".to_string());

let remapping = "context:foo=C:/bar/src/";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@@ -287,7 +345,7 @@ impl fmt::Display for RelativeRemapping {
{
format!("{}={}", self.name, self.path.original().display())
}
};
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace (target_os = "windows") with just (windows)

RelativeRemappingPathBuf::with_root(root.as_ref(), c)
.path
.to_string_lossy()
.to_string()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.to_string()
.into_owned()

s.push_str(context);
}
s.push(':');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace (target_os = "windows") with just (windows)

Copy link
Owner

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to get this released ASAP because it's blocking a new foundry feature and given people only have nits we can do these in follow ups

@@ -56,7 +56,7 @@ coins-ledger = { version = "0.8.3", default-features = false, optional = true }
semver = { workspace = true, optional = true }

# trezor
trezor-client = { version = "0.1", default-features = false, features = [
trezor-client = { version = "=0.1.0", default-features = false, features = [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this should fix the proto issue?

Comment on lines +337 to +338
if let Some(ctx) = r.context.as_ref() {
cwd.starts_with(ctx)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep we want to limit only to that path bc otherwise we end up polluting other files' remappings in the deps

@gakonst gakonst merged commit 8082aa8 into gakonst:master Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants