Visual Studio Code Setup for Redis

VS Code meets Redis

Recently, I have been working on Redis OSS and I sometimes write the code on my personal system (Macbook Air 13" 2017) which is sufficient enough to develop systems code. I’ve been using IntelliJ since my internship days and I’m big fan of Jetbrains product. However, IntelliJ Ultimate/CLion comes with a big price tag (649€ for the first year) if you’re developing software on your own. So, I wanted to try something which is free and still convenient to use. So, I tried setting up VS Code which is also open source editor by Microsoft. And I was blown away by the ease and maturity of extensions for VS Code. I would like to share my setup below. There are three key areas I’m going to cover, Extensions, Tasks and Launch.

In the tasks.json file, you could setup instructions to run certain task like building, testing, packaging, etc.

I’ve three tasks for now.

  • Clean — make clean
  • Compile — make
  • Build — Clean & Compile (You can create a lineage of tasks. Ammazing!)
  • writeRedisPID — Grep forredis-server and put the process id to a temporary file. (Will see below the purpose of this.)
{
"version": "2.0.0",
"tasks": [
{
"label": "Clean",
"type": "shell",
"command": "make",
"args": [
"clean"
],
"problemMatcher": []
},
{
"label": "Compile",
"type": "shell",
"command": "make",
"problemMatcher": []
},
{
"label": "Build",
"dependsOn": [
"Clean",
"Compile"
]
},
{
"label": "writeRedisPID",
"type": "shell",
"command": "ps -ef | grep 'redis-server' | grep -v grep | awk '{print $2}' > /tmp/redis.pid"
}
]
}

In the launch.json file, you can define how to run/debug your application. I’ve two different configurations.

  1. Run — . In this case, it’s Redis, so after compiling we get theredis-serverinside the src directory. We can also define the preLaunchTask, which we already discussed above. And I’ve provided Build as my preLaunchTask. So, the redis gets compiled and then the process gets started.
  2. Attach — This is handy to attach to an existing running process and debug it. Generally I work on a single Redis process and here I use the preLaunchTask writeRedisPID to store the running redis process id in the temporary file and provide that as input in the launch configuration "processId":"${input:redisPID}
{
"version": "0.2.0",
"configurations": [
{
"name": "Redis Server Run",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/src/redis-server",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "Build"
},
{
"name": "Redis Server Attach",
"type": "cppdbg",
"request": "attach",
"processId": "${input:redisPID}",
"program": "${workspaceFolder}/src/redis-server",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "writeRedisPID",
"subProcess": true,
"justMyCode": true,
"redirectOutput": true,
}
],
"inputs": [
{
"id": "redisPID",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "/tmp/redis.pid"
}
}
]
}

You can also copy the below content and use it directly if you’re also working on redis or change the program field according to your application.

I hope this will be helpful for people who would like to start with systems programming and want a simple and easy setup. I’ve also shared some of my learnings. Feel free to check them out.

Let me know in the comments if you are using any other plugins/extensions for better productivity.

Have a good day. Ciao!

Software Developer/Open Source Contributor