QuantConnect/lean-cli

Debugging in Libraries in LeanCLI

Closed this issue · 2 comments

Community contributor fixed library debugging on his environment using the fix below.. Should be made defaults.

The trick is to add
"sourceFileMap": {
"/Library": "${workspaceRoot}/Library"
}
to the root launch.json. Moving the launch.json to root and adding this should be defaults for the Lean CLI imo.

TLDR: the launch.json example listed at the end of this comment should be added in the root .vscode directory on lean init.

This fix was specifically for C# projects in VSCode.

For example:

lean init
lean create-project Test --language csharp

To get the debugger to attach, I had to move Test/.vscode/launch.json to the root .vscode/launch.json. IMO this file should be created by default on lean init or when the first project is created.

Furthermore, libraries created by the CLI will not be found.

lean create-project Library/MyLibrary --language csharp
lean library add Test Library/MyLibrary
lean backtest Test --debug vsdbg    

The VSCode Debug Console will show the Library's symbols are found if launch.json has "moduleLoad": true. However, stepping into a Library function will show an "The editor could not be opened because the file was not found." error:

Screen Shot 2023-02-19 at 20 12 34

I eventually discovered there was a hiccup in the path resolution. If adding the following to the root .vscode/launch.json, you are able to step into the library and set breakpoints in them:

"sourceFileMap": {
    "/Library": "${workspaceRoot}/Library"
}

Screen Shot 2023-02-19 at 20 19 36

The final .vscode/launch.json file is as follows:

{
  "configurations": [
    {
      "name": "Lean CLI",
      "request": "attach",
      "type": "coreclr",
      "processId": "1",
      "pipeTransport": {
        "pipeCwd": "${workspaceRoot}",
        "pipeProgram": "docker",
        "pipeArgs": ["exec", "-i", "lean_cli_vsdbg"],
        "debuggerPath": "/root/vsdbg/vsdbg",
        "quoteArgs": false
      },
      "logging": {
        "moduleLoad": false
      },
      "sourceFileMap": {
        "/Library": "${workspaceRoot}/Library"
      }
    }
  ]
}

I noticed that the docs here do specify opening the debugger from the project directory, not the workspace. This does not work as stated:

% cd Test
% lean backtest Test --debug vsdbg

Usage: lean backtest [OPTIONS] PROJECT

Try 'lean backtest --help' for help or go to the following url for a list of common errors:
https://www.lean.io/docs/v2/lean-cli/key-concepts/troubleshooting#02-Common-Errors

Error: Invalid value for 'PROJECT': Path 'Test' does not exist.

However lean backtest ../Test --debug vsdbg does work, but then VS Code can't find the debugger:

Screen Shot 2023-02-20 at 15 25 17

I think the solution above still makes the most sense.

PS this youtube video should get updated to reflect any new changes.