VSCode for Foundry

Hey there! If you’re into blockchain development, particularly with Solidity, and love using Foundry, you’re in the right place. Today, I’m going to walk you through setting up Visual Studio Code (VSCode) to work seamlessly with Solidity and Foundry. Let’s dive in!

Install Extension

First things first, to get Solidity support in VSCode, you need to install the VSCode Solidity extension. This extension is a must-have for any Solidity developer using VSCode.

Remappings

Remappings are crucial in ensuring that your project’s dependencies are correctly resolved. Here’s how to set them up:

  • Using remappings.txt: Place your remappings in a remappings.txt file. This is especially useful if you’re transitioning from foundry.toml.
  • From foundry.toml: Already have remappings in foundry.toml? Just copy them over to remappings.txt.
  • Autogenerated Remappings: If you rely on Foundry’s autogenerated remappings, simply run forge remappings > remappings.txt to create your remappings.txt file.

Configure Dependencies

To help the Solidity extension find your project’s dependencies, add these settings to your .vscode/settings.json:

JSON
{
    "solidity.packageDefaultDependenciesContractsDirectory": "src",
    "solidity.packageDefaultDependenciesDirectory": "lib"
}

Here, src is where your source code lives, and lib is your dependency directory.

Formatter

Good code formatting is non-negotiable. To enable Foundry’s built-in formatter and automatically format your code on save, add these settings to your .vscode/settings.json:

JSON
{
    "editor.formatOnSave": true,
    "[solidity]": {
        "editor.defaultFormatter": "JuanBlanco.solidity"
    },
    "solidity.formatter": "forge"
}

For more on configuring the formatter, check out the Formatter reference.

solc Version

Consistency in compiler versions between your IDE and Foundry is key. Specify your Solidity compiler version in .vscode/settings.json:

JSON
{
    "solidity.compileUsingRemoteVersion": "v0.8.23"
}

And align Foundry with this version in foundry.toml:

TOML
solc = "0.8.23"

And there you have it! A straightforward guide to setting up your VSCode for a smooth Solidity development experience with Foundry. Remember, a well-set-up environment can make your coding experience much more enjoyable and efficient. Happy coding!

Topics