Skip to content

fix: apply remappings to resolved relative import paths#353

Merged
grandizzy merged 1 commit intomainfrom
fix/verify-remappings-12667
Jan 21, 2026
Merged

fix: apply remappings to resolved relative import paths#353
grandizzy merged 1 commit intomainfrom
fix/verify-remappings-12667

Conversation

@gakonst
Copy link
Member

@gakonst gakonst commented Jan 21, 2026

Summary

Ref foundry-rs/foundry#12667 where forge verify-contract fails 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 to lib/ens-contracts/contracts/utils/LibMem/LibMem.sol.

If a project has a remapping like:

lib/ens-contracts/contracts/utils/LibMem/=src/common/utils/

The project expects src/common/utils/LibMem.sol to be used instead. This works for forge build and forge test because the remappings are passed to solc, which applies them during compilation.

However, forge verify-contract --show-standard-json-input was 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:

  • A library file imports ./LibMem/LibMem.sol (relative import)
  • A remapping redirects lib/external/utils/LibMem/ to src/utils/
  • The test verifies the resolved path is src/utils/LibMem.sol

All existing tests pass.

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>
@gakonst gakonst force-pushed the fix/verify-remappings-12667 branch from 1ab8e71 to 64caf73 Compare January 21, 2026 05:35
@grandizzy grandizzy marked this pull request as ready for review January 21, 2026 05:42
Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

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

Makes sense!

@grandizzy grandizzy merged commit 04efacd into main Jan 21, 2026
18 checks passed
@grandizzy grandizzy deleted the fix/verify-remappings-12667 branch January 21, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants