Hardhat plugin that adds Hardhat support to Foundry projects for seamless Ethereum development workflow integration
npx @tessl/cli install tessl/npm-nomicfoundation--hardhat-foundry@1.2.0The @nomicfoundation/hardhat-foundry plugin enables seamless integration between Hardhat and Foundry development environments for Ethereum smart contract projects. It automatically configures Hardhat to use Foundry's contracts directory structure and dependency management system, allowing developers to leverage both tools' strengths within a single project.
npm install @nomicfoundation/hardhat-foundryimport "@nomicfoundation/hardhat-foundry";For accessing utility functions:
import {
getForgeConfig,
parseRemappings,
getRemappings,
installDependency,
HardhatFoundryError
} from "@nomicfoundation/hardhat-foundry/dist/src/foundry";The plugin works automatically once imported in your Hardhat configuration:
// hardhat.config.ts
import "@nomicfoundation/hardhat-foundry";
export default {
solidity: "0.8.19",
// Other Hardhat config...
};For projects without Foundry setup, initialize it:
npx hardhat init-foundryThe plugin consists of several key components:
foundry.toml is presentinit-foundry task for project setupThe plugin automatically extends Hardhat configuration to integrate with Foundry projects when a foundry.toml file is present.
Configuration Extension (Automatic)
When imported, the plugin automatically:
foundry.toml presence and warns if missingforge config --jsonsrc configurationInitialize Foundry setup in existing Hardhat projects.
/**
* Hardhat task that initializes Foundry setup in current project
* Creates foundry.toml file and installs forge-std dependency
* Usage: npx hardhat init-foundry
* @task init-foundry
*/The init-foundry task:
foundry.toml file with appropriate configurationfoundry-rs/forge-std dependency automaticallyGenerated foundry.toml structure:
[profile.default]
src = 'contracts'
out = 'out'
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'Retrieve Foundry project configuration data.
/**
* Gets the current Foundry configuration by running `forge config --json`
* @returns Foundry configuration object containing src, cache_path, and other config properties
*/
function getForgeConfig(): {
src: string;
cache_path: string;
out: string;
libs: string[];
test: string;
remappings: string[];
[key: string]: any;
};Usage Example:
import { getForgeConfig } from "@nomicfoundation/hardhat-foundry/dist/src/foundry";
const config = getForgeConfig();
console.log(config.src); // e.g., "contracts"
console.log(config.cache_path); // e.g., "cache_forge"Parse and retrieve Foundry remappings for dependency resolution.
/**
* Parses Foundry remappings from text format into a mapping object
* @param remappingsTxt - Raw remappings text from `forge remappings`
* @returns Object mapping remapping prefixes to their target paths
*/
function parseRemappings(remappingsTxt: string): Record<string, string>;
/**
* Gets Foundry remappings by running `forge remappings` (cached)
* @returns Promise resolving to remappings object
*/
function getRemappings(): Promise<Record<string, string>>;Usage Examples:
import { parseRemappings, getRemappings } from "@nomicfoundation/hardhat-foundry/dist/src/foundry";
// Parse remappings manually
const remappings = parseRemappings("@openzeppelin/=lib/openzeppelin-contracts/\n@forge-std/=lib/forge-std/src/");
console.log(remappings);
// { "@openzeppelin/": "lib/openzeppelin-contracts/", "@forge-std/": "lib/forge-std/src/" }
// Get remappings automatically (cached)
const currentRemappings = await getRemappings();
console.log(currentRemappings);Remapping Format Rules:
prefix=target:) are not allowed=) are not allowedInstall Foundry dependencies using forge install.
/**
* Installs a Foundry dependency using `forge install`
* @param dependency - Dependency identifier (e.g., "foundry-rs/forge-std")
* @returns Promise that resolves when installation completes
*/
function installDependency(dependency: string): Promise<void>;Usage Example:
import { installDependency } from "@nomicfoundation/hardhat-foundry/dist/src/foundry";
// Install forge standard library
await installDependency("foundry-rs/forge-std");
// Install OpenZeppelin contracts
await installDependency("OpenZeppelin/openzeppelin-contracts");The function automatically:
--no-commit flag is supportedForgeInstallError if installation failsComprehensive error handling for Foundry-related operations.
/**
* Main error class for hardhat-foundry plugin operations
*/
class HardhatFoundryError extends NomicLabsHardhatPluginError {
constructor(message: string, parent?: Error);
}
/**
* Specialized error class for forge install command failures
*/
class ForgeInstallError extends HardhatFoundryError {
constructor(dependency: string, parent: Error);
}Common Error Scenarios:
forge command is not available (exit code 127)foundry.toml file has syntax errors (exit code 134)src or cache_path keys are missingforge install fails for dependencies (throws ForgeInstallError)Usage Example:
import { HardhatFoundryError, getForgeConfig } from "@nomicfoundation/hardhat-foundry/dist/src/foundry";
try {
const config = getForgeConfig();
} catch (error) {
if (error instanceof HardhatFoundryError) {
console.error("Foundry integration error:", error.message);
}
}The plugin integrates with Hardhat's internal compilation system through task overrides:
Remapping Integration: The plugin overrides TASK_COMPILE_GET_REMAPPINGS to provide Foundry remappings during compilation.
Version Compatibility: The plugin overrides TASK_COMPILE_TRANSFORM_IMPORT_NAME to ensure compatibility with Hardhat >= 2.17.2.
These integrations are automatic and don't require direct interaction from users.
// Internal type for remapping storage
type Remappings = Record<string, string>;
// Error classes
class HardhatFoundryError extends NomicLabsHardhatPluginError {
constructor(message: string, parent?: Error);
}
// Specialized error class for forge install command failures
class ForgeInstallError extends HardhatFoundryError {
constructor(dependency: string, parent: Error);
}Runtime Dependencies:
picocolors: ^1.1.0 - For colored console outputPeer Dependencies:
hardhat: ^2.26.0 - Required Hardhat frameworkNode.js Built-ins:
fs: File system operationspath: Path manipulationchild_process: Command executionutil: Utility functions