Easier debugging with VSCode and fpm

Been using fpm for a while now and I really like the features and overall structure of it. However, I wonder how people are integrating fpm with the debug tools in VSCode. Since fpm builds the executables in a non-deterministic path, and the launch.json configuration needs to point to an executable, the procedure is cumbersome. After some research into fpm --runner argument, I’ve made the following combination of tasks to debug a file in my test/ folder, with the default folder organization for a fpm project:

tasks.json:

{
    "version": "2.0.0",
    "tasks": [

        {
            "label": "Make debug directory",
            "type": "shell",
            "command": "if [ ! -d \"./debug\" ]; then mkdir debug; fi",
        },
        
        {
            "label": "Build target file",
            "type": "shell",
            "command": "fpm test --target ${fileBasenameNoExtension} --runner 'cp -t debug'",
            "dependsOn": "Make debug directory"
        }
    ]
}

launch.json:

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "(gdb) Fortran",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/debug/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true,
          },
        ],
        "preLaunchTask": "Build target file",
      },
    ],
  }

With this set of tasks I can press F5 and it builds the target file, copies it to a debug folder in my project root (creating it if it doesn’t exist), and runs it through VSCode debugger. This works, but only in Linux and only for a file in test/ folder.

Debugging a single file seems like a basic task in development, so I’ve been wondering if there’s an easier way to do it by using fpm’s built-in commands.

If there’s not, wouldn’t it be beneficial to put some kind of solution in fpm/Modern Fortran documentation, even if it is using VSCode tasks.? That --runner thing took me a decepctively amount of time to figure out for a newbie such as myself.

4 Likes

I don’t know if you found a way to use a similar method with fully fledged project with fpm, but I haven’t.

I like working with Fortran now because of fpm, but having a debug command like fpm debug would help a lot.