Using `conflicted::conflicts_prefer()` in `.Rprofile` breaks one of my packages

Using conflicted::conflicts_prefer() in .Rprofile breaks one of my packages (pakret) that cites other R packages in R Markdown/Quarto.

My package is supposed to write the bibtex entries of cited packages when knitr ends but it's not happening when using conflicted in an .Rprofile.

Here's a reprex of the problem:

withr::with_tempdir({
  dir <- getwd()
  
  # .bib file
  file.create(file.path(dir, "refs.bib"))
  
  # .Rprofile
  readr::write_file(
    pakret:::dedent("
      library(dplyr)
      conflicted::conflicts_prefer(dplyr::filter)
    "),
    file = file.path(dir, ".Rprofile")
  )
  
  # .Rmd file
  rmd <- file.path(dir, "test.Rmd")
  readr::write_file(
    pakret:::dedent("
      ---
      bibliography: refs.bib
      ---
      
      ```{r}
      #| include: false
      library(pakret)
      ```
      
      `r pkrt('tidyverse')`
      
      ## References
    "),
    file = rmd
  )
  
  render <- function(...) {
    rmarkdown::render(..., output_format = "md_document", quiet = TRUE)
  }
  
  callr::r(render, list(input = rmd, knit_root_dir = dir))

  file <- list.files(dir, pattern = "\\.md$", full.names = TRUE)
  cat(readr::read_file(file))
})
#> the ‘tidyverse’ package version 2.0.0 (**tidyverse?**)
#> 
#> ## References

Created on 2024-12-19 with reprex v2.1.1

I'm relying on parent frames to identify when knitr finishes to save bibtex entries:

.onLoad <- function(...) {
  do.call(set, .__settings__)
  if (is_rendering_context()) {
    set(render = TRUE)
    bib_init()
    terminate()
  }
}

terminate <- function() {
  withr::defer(bib_write(), envir = parent.frame(5L))
}

I suspect conflicted::conflicts_prefer() messes things up with the frames which stops my package from detecting when knitr ends. That said, I'm not sure why that would only happen when using that function in the .Rprofile.

Commenting/removing conflicted::conflicts_prefer() from the .Rprofile fixes the problem.

Any idea what could happen and if there's any fix on my side?

There's an open issue on conflicted's repo about problems when used in an .Rprofile, not sure if that's related to my problem though.

Just tried it with withr:::knitr_exit_frame() and that works so I guess I'll implement a similar function in my package for now.

terminate <- function() {
  exit_frame <- withr:::knitr_exit_frame(parent.frame())
  withr::defer(bib_write(), envir = exit_frame)
}