feat(forge): specify compilation profile in vm.getCode#13191
Merged
Conversation
Add support for specifying a compilation profile in artifact path parsing for
vm.getCode, vm.getDeployedCode, and vm.deployCode cheatcodes.
This allows users with multiple compilation profiles in foundry.toml to select
specific artifact versions. For example:
- `vm.getCode("Contract.sol:v1")`
- `vm.getCode("Contract.sol:ContractName:optimized")`
The feature distinguishes between semver versions (like 0.8.23) and profile
names (like v1, optimized) by attempting to parse the suffix as a version
first.
Closes #12821
- Combine duplicate if branches for version detection - Remove needless ref borrow in profile comparison
- Refactor parsing logic into reusable parse_artifact_path function - Add 11 unit tests covering all artifact path formats: - file only, file+contract, file+contract+version, file+contract+profile - file+version, contract only, contract+version, contract+profile - Various profile name patterns (v1, v2, paris, optimized, etc.) - Add Solidity integration test for profile-based getCode
…file
Add comprehensive tests showing that vm.getCode correctly retrieves
different bytecode for different compilation profiles:
- Create testdata/multi-profile/ with Counter contract compiled using
'default', 'optimized' (10000 runs), and 'unoptimized' (no optimizer) profiles
- Add tests verifying:
- Different profiles produce different bytecode
- vm.getCode('Contract:profile') works correctly
- vm.getCode('path/to/Contract.sol:Contract:profile') works correctly
- vm.deployCode with profile works
- vm.getDeployedCode with profile works
- Non-existent profiles fail appropriately
- Remove invalid 'profiles' field from compilation_restrictions - Use assertTrue instead of assertNotEq (not available in DSTest) - Simplify tests to verify profile parsing works with default profile - Test that non-existent profiles fail appropriately
The multi-version tests use a contract named Counter, causing conflicts when my multi-profile tests also used Counter.
The additional_compiler_profiles cause all contracts to be compiled with multiple profiles, which breaks the existing multi-version tests that rely on version-based disambiguation. The unit tests for artifact path parsing adequately test the profile feature at the parsing level. End-to-end testing of profile-based selection requires a more complex test setup.
Add test demonstrating vm.getCode with profile selection using the existing
paris compilation profile. Tests:
- vm.getCode("path:Contract:paris") format
- vm.getCode("Contract:paris") format
- vm.getDeployedCode and vm.deployCode with profile
- Error when using non-existent profile
- Error when requesting wrong profile for a contract
This test uses the existing paris profile which is already scoped to
paris/** files, avoiding conflicts with other multi-version tests.
Amp-Thread-ID: https://ampcode.com/threads/T-019bea01-185e-7079-990e-f33585663be2
Co-authored-by: Amp <amp@ampcode.com>
Fix the artifact disambiguation logic to not filter by running_artifact's
version/profile when user has explicitly specified those criteria. This
allows adding global additional_compiler_profiles without breaking
existing tests.
Changes:
- Fix disambiguation: only use running_artifact's version for filtering
when user did NOT specify a version in the artifact path
- Same fix for profile filtering
- Add 'optimized' and 'unoptimized' profiles to testdata/foundry.toml
- Add GetCodeMultiProfile.t.sol demonstrating that different profiles
produce different bytecode and can be selected via vm.getCode
The test verifies:
- vm.getCode("Contract:optimized") returns different bytecode than
vm.getCode("Contract:unoptimized")
- vm.deployCode and vm.getDeployedCode work with profile selection
- Non-existent profiles fail appropriately
Amp-Thread-ID: https://ampcode.com/threads/T-019bea01-185e-7079-990e-f33585663be2
Co-authored-by: Amp <amp@ampcode.com>
assertNotEq doesn't have an overload for bytes32, use require with != instead
Changes: - Remove GetCodeMultiProfile.t.sol test that expected artifacts compiled with 'optimized' and 'unoptimized' profiles without corresponding compilation_restrictions - Remove unused 'optimized' and 'unoptimized' profiles from foundry.toml (the paris profile remains as it has proper restrictions) - Update multi-version tests to use full paths (multi-version/Counter.sol) to avoid ambiguity with other Counter contracts in testdata that would cause 'multiple matching artifacts found' error Amp-Thread-ID: https://ampcode.com/threads/T-019bea8a-abde-7172-9b50-efb0569a8222 Co-authored-by: Amp <amp@ampcode.com>
…e-support # Conflicts: # crates/cheatcodes/src/fs.rs
mablr
reviewed
Jun 10, 2026
6bd1467 to
b06d8a7
Compare
mablr
approved these changes
Jun 10, 2026
grandizzy
approved these changes
Jun 10, 2026
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
Add support for specifying a compilation profile in artifact path parsing for
vm.getCode,vm.getDeployedCode, andvm.deployCodecheatcodes.Motivation
This allows users with multiple compilation profiles in
foundry.tomlto select specific artifact versions. For example, if you have:You can now use:
vm.getCode("Contract.sol:v1")vm.getCode("Contract.sol:v2")vm.getCode("Contract.sol:ContractName:optimized")Implementation
The feature distinguishes between semver versions (like
0.8.23) and profile names (likev1,optimized) by:0.8.23)The profile filter is applied during artifact lookup to select the correct compiled artifact.
Supported Formats
path/to/contract.sol:ContractName:profilepath/to/contract.sol:profileContractName:profileCloses #12821