fix: apply remappings to resolved relative import paths#353
Merged
Conversation
This fixes foundry-rs/foundry#12667 where `forge verify-contract` fails when remappings target files that are imported via relative paths within library dependencies. When a relative import (e.g., `./LibMem/LibMem.sol`) is resolved to an absolute path that matches a remapping pattern, the remapping should be applied to redirect to the correct source file. Previously, relative imports were normalized without checking remappings, causing the standard JSON input to include the wrong source file path. This broke contract verification on block explorers like Etherscan. The fix adds `apply_remapping_to_path()` which checks if a resolved absolute path matches any remapping and returns the remapped path if the target file exists. Added a test case that reproduces the exact scenario from the issue. Amp-Thread-ID: https://ampcode.com/threads/T-019bdef4-0444-75eb-bcf4-8fd194a24a0a Co-authored-by: Amp <amp@ampcode.com>
1ab8e71 to
64caf73
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Ref foundry-rs/foundry#12667 where
forge verify-contractfails when remappings target files that are imported via relative paths within library dependencies.Problem
When a library file (e.g.,
lib/ens-contracts/contracts/utils/BytesUtils.sol) imports another file via a relative path (e.g.,./LibMem/LibMem.sol), the import resolves tolib/ens-contracts/contracts/utils/LibMem/LibMem.sol.If a project has a remapping like:
The project expects
src/common/utils/LibMem.solto be used instead. This works forforge buildandforge testbecause the remappings are passed to solc, which applies them during compilation.However,
forge verify-contract --show-standard-json-inputwas including the wrong source file (lib/ens-contracts/contracts/utils/LibMem/LibMem.sol) instead of the remapped file (src/common/utils/LibMem.sol), causing verification to fail.Solution
Added
apply_remapping_to_path()which checks if a resolved absolute path matches any remapping pattern. When resolving relative imports, we now apply this check after path normalization.The function only uses the remapped path if the target file actually exists, ensuring backward compatibility.
Testing
Added a test case that reproduces the exact scenario from the issue:
./LibMem/LibMem.sol(relative import)lib/external/utils/LibMem/tosrc/utils/src/utils/LibMem.solAll existing tests pass.