Skip to content

Make it easier to debugger to do source mapping with cpp11::cpp_source #156

Closed
@renkun-ken

Description

@renkun-ken

I have a repo vscode-rcpp-demo to demonstrate how to debug Rcpp C++ code in a package project in VSCode. I also create a similar repo vscode-cpp11-demo to do the same for cpp11. Both work nicely if the project is an R package.

However, in both cases, we cannot debug C++ code with Rcpp::sourceCpp() and cpp11::cpp_source() unless we configure source mapping for the C++ debugger. I raised an issue RcppCore/Rcpp#1041 on Rcpp side, but it seems not trivial to change how the compilation works to support this.

I do some experiment with cpp11, and it seems straightforward to support this: just allow the user to customize https://github.com/r-lib/cpp11/blob/master/R/source.R#L69 so that the directory is pre-determined, and then we can trigger the C++ debugger with the following in a non-package project:

./code.cpp:

#include <cpp11.hpp>
#include <iostream>
using namespace cpp11;

[[cpp11::register]]
double calc_sum(doubles x)
{
  double sum = 0;
  for (int i = 0; i < x.size(); ++i) {
    std::cout << i << std::endl;
    sum += x[i];
  }
  return sum;
}

./code.R:

cpp11::cpp_source("code.cpp", tmpdir = "./tmp")
calc_sum(c(1, 2, 3))

and create a source mapping in .vscode/launch.json:

"sourceFileMap":{
        "${workspaceFolder}/tmp/src/": "${workspaceFolder}/"
      }

Then the breakpoints and other debugger features could work correctly with the source mapping between the original source files and the copied and generated source files in ./tmp/src.

Do you think if it makes sense to allow the user to supply the temp dir for debugging purposes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions