Cannot turn off checking of pragma(s) suppressing important diagnostics when including external C++ headers

Hi everyone,

I am developing an R package that uses cpp11 to interface C++ functions with R. One of the C++ functions uses JSON for Modern C++ to parse a JSON file, which is a single header file placed in src/include/json.hpp and then included in the C++ file with #include "include/json.hpp".

My issue is that devtools::check() warns about pragma(s) suppressing important diagnostics:

── R CMD check results ──────────────────── cppheader 0.0.0.9000 ────
Duration: 8.1s

❯ checking pragmas in C/C++ headers and code ... WARNING
  File which contains pragma(s) suppressing important diagnostics
    ‘src/include/json.hpp’

I don't want to mess with json.hpp. From what I can gather from Section 8 in R Internals, this is controlled by the environment variable _R_CHECK_PRAGMAS_, which defaults to FALSE for non-CRAN checks but TRUE for CRAN checks.

I get the warning even though NOT_CRAN is true when testing locally. I also get the warning even when I explicitly set this environment variable to FALSE, and also set NOT_CRAN to true (since it seems to disappear when specifying env_vars), like so:

devtools::check(
  document = TRUE,
  env_vars = c(
    "_R_CHECK_PRAGMAS_" = "FALSE",
    "NOT_CRAN" = "true"
  )
)

The environment variables are picked up, but I still get the warning:

══ Checking════════════════════════════════════════════════
Setting env vars:
• _R_CHECK_CRAN_INCOMING_REMOTE_               : FALSE
• _R_CHECK_CRAN_INCOMING_                      : FALSE
• _R_CHECK_FORCE_SUGGESTS_                     : FALSE
• _R_CHECK_PACKAGES_USED_IGNORE_UNUSED_IMPORTS_: FALSE
• _R_CHECK_PRAGMAS_                            : FALSE
• NOT_CRAN                                     : true

...

── R CMD check results ──────────────────── cppheader 0.0.0.9000 ────
Duration: 8.3s

❯ checking pragmas in C/C++ headers and code ... WARNING
  File which contains pragma(s) suppressing important diagnostics
    ‘src/include/json.hpp’

I've tested this in a toy package, here's the essentials:

get_hello.cpp
#include <cpp11.hpp>
#include "include/json.hpp"
#include <fstream>

using json = nlohmann::json;

[[cpp11::register]]
cpp11::writable::list get_hello_(const std::string& json_path) {
  using namespace cpp11::literals;

  // read json file
  std::ifstream file(json_path);

  // parse json file
  json j;
  file >> j;

  // extract "hello" element
  std::string hello = j["hello"];

  // create and return cpp11 list
  cpp11::writable::list results = cpp11::writable::list({
    "hello"_nm = hello
  });

  return results;
}
get_hello.R
#' Read "hello" from JSON
#'
#' This function reads the "hello" key from a JSON file.
#'
#' @param json_path Path to the JSON file
#' @return A list containing the value of the "hello" key
#' @export
get_hello <- function(json_path) {
  get_hello_(json_path)
}
hello.json
{
  "hello": "world"
}
DESCRIPTION
Package: cppheader
Type: Package
Title: A test package for C++ headers
Version: 0.0.0.9000
Maintainer: CRDF (Carl Delfin) <crdf@novonordisk.com>
Author:  CRDF (Carl Delfin) <crdf@novonordisk.com>
Description: A test package for external C++ headers.
License: MPL (>= 2.0)
Encoding: UTF-8
RoxygenNote: 7.3.2
Suggests: 
    testthat (>= 3.0.0),
    devtools
Config/testthat/edition: 3
Config/testthat/parallel: TRUE
LinkingTo: 
    cpp11

And output works as expected:

> get_hello("inst/extdata/hello.json")
$hello
[1] "world"

So far the only workaround seems to be to allow warnings, which is not ideal. Am I missing something obvious here? Is there some other environment variable that controls this?

Thanks!

This probably needs to be fixed in cpp11. First check if it was already fixed in their repo. If not, report an issue or submit a PR.

Until the new cpp11 version is published you can depend on the development version.

If you want to publish your package before that, you can tell CRAN that the problem is in cpp11. Or, if you would rather avoid that, you can vendor cpp11 into your package (temporarily).

Thanks for the quick reply! I raised an issue for cpp11 and the problem seems not to be on their end. We tested with the latest development version from GitHub, and still got the error.

FWIW, the proposed solution was to replace all #pragma with # pragma (note the space); that did the trick.

(But I guess it still doesn't solve the root issue, namely how can this check be turned off?)

Right, I thought that header was in cpp11. If it is not then it is indeed not their problem.