Skip to content

fix(interpolate_package): Check path and improve error messages - #763

Merged
gadenbuie merged 6 commits into
mainfrom
fix/interpolate-package
Oct 2, 2025
Merged

gadenbuie merged 6 commits into
mainfrom
fix/interpolate-package

Conversation

@gadenbuie

Copy link
Copy Markdown
Collaborator
  • Updates the interpolate_package() docs to correct inst/prompts path
  • Adds a check that the package prompt path exists and throw with informative error if not.
  • Adds checks that path is scalar in interpolate_package() and interpolate_file().

Before

Before this PR, interpolate_package() falis with an error about invalid "nchars" argument because system.file() returns "" for a missing path and file.size("") returns NA.

library(ellmer)

interpolate_package("btw", "missing.md")
#> Warning in file(con, "rb"): file("") only supports open = "w+" and open =
#> "w+b": using the former
#> Warning in readChar(path, file.size(path)): text connection used with
#> readChar(), results may be incorrect
#> Error in readChar(path, file.size(path)): invalid 'nchars' argument

In Positron at least, you typically only see the nchars part, like this

Error in `readChar()`:
! invalid 'nchars' argument

which is confusing -- my first thought is usually that I've done something wrong with the template strings.

After

pkgload::load_all()
#> ℹ Loading ellmer

interpolate_package("btw", "missing.md")
#> Error in `interpolate_package()`:
#> ! btw does not have "missing.md" in its prompts/ directory.
#> ℹ Run `dir(system.file("prompts", package = "btw"))` to see available prompts.

Comment thread R/interpolate.R Resolved
Comment thread R/interpolate.R Resolved
Comment thread R/interpolate.R
interpolate_file(path, ..., .envir = .envir)
check_string(path)

path_pkg <- system.file("prompts", path, package = package)

@gadenbuie gadenbuie Oct 1, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, one bit of friction here is that pkgload shims system.file() in the global env and in the loaded package namespace, so interpolate_package() doesn't work with dev packages.

Where I've needed that, I've written an internal interpolate_package() that replicated ellmer's implementation. But it'd be nice if there were a way around this. (The Shiny team has a system_file() function in Winston's staticimports package, but maybe there's rlang helper I haven't encountered yet?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's probably worth testing if package is a dev package (i.e. implement the shim ourselves).

@gadenbuie gadenbuie Oct 2, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I captured this discussion in issue #766

@gadenbuie
gadenbuie merged commit d3100e7 into main Oct 2, 2025
@gadenbuie
gadenbuie deleted the fix/interpolate-package branch October 2, 2025 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants