Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

namespace vs standalone #1853

Closed
jonthegeek opened this issue Jun 6, 2023 · 8 comments
Closed

namespace vs standalone #1853

jonthegeek opened this issue Jun 6, 2023 · 8 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@jonthegeek
Copy link
Contributor

jonthegeek commented Jun 6, 2023

I'm not certain if this is a usethis issue, or an rlang issue, or neither and I'm just not understanding something.

I'm experimenting with the new use_standalone(), and I immediately ran into confusion. Here's a ~reprex (it's package-creation-level so not really a fully independent reprex) to see what I'm seeing:

# Create a mostly-empty package, then
use_standalone("r-lib/rlang", "types-check")
devtools::document(".")
devtools::load_all(".")

# Try to use anything that's in the standalone, such as
check_bool()
#> Error in env_get_list(nms = c("format_arg", "format_code"), last = topenv(),  : 
#>   could not find function "env_get_list"

Don't all the rlang functions in there either need to be namespaced or imported? I see that you're using that standalone in usethis, but I also see that you have import(rlang) in your NAMESPACE, so maybe that's masking this requirement? I also see that the namespacing was removed in rlang to avoid issues, maybe that helped to mask this issue?

To avoid the loop of errors that the namespacing caused in rlang, does the # imports: rlang (>= 1.1.0) directive need to be taken as "add @import IMPORTPKGNAME to PKGNAME-package.R"?

If there's a simpler solution that I'm not seeing, please let me know!

@jennybc
Copy link
Member

jennybc commented Jun 6, 2023

@lionel- as the main designer/implementer of the standalone file system, do you care to weigh in? Happy to get suggestions for things we should refine in usethis code or docs.

@jennybc jennybc added the bug an unexpected problem or unintended behavior label Jun 6, 2023
@lionel-
Copy link
Member

lionel- commented Jun 6, 2023

I think this should have been fixed by #1789? Do you have the latest version of usethis @jonthegeek?

@jonthegeek
Copy link
Contributor Author

jonthegeek commented Jun 6, 2023

I think this should have been fixed by #1789? Do you have the latest version of usethis @jonthegeek?

Yes, I tried both latest dev and CRAN, but they're currently the same anyway I believe 😊.

It adds rlang to Imports (in description), but that isn't enough; rlang needs to be added as a full import(rlang) in NAMESPACE in order to use all the rlang functions without namespacing.

@lionel-
Copy link
Member

lionel- commented Jun 6, 2023

oh I thought it'd do that automatically in the usethis imports section.

@lionel-
Copy link
Member

lionel- commented Jun 6, 2023

I'll take another look, thanks for the report

@lionel-
Copy link
Member

lionel- commented Jun 12, 2023

I think ideally we'd solve this by leveraging roxygen2 and adding #' @import rlang in the standalone file. This way the file can precisely determine its imports.

Now I remember that I already tried this but ran into the issue that roxygen2 adds an import(rlang) entry in the NAMESPACE file of rlang itself, which causes loading issues.

@hadley Would it be reasonable to change roxygen2 so it ignores such self-import directives?

@hadley
Copy link
Member

hadley commented Jun 12, 2023

I think we should just document this. I don't think it's worth putting a bunch of effort into use_standalone() to make it 100% bullet proof.

@jonthegeek
Copy link
Contributor Author

Documentation would have covered my original issue. I wasn't sure if I was misunderstanding something, so telling me I was right would have covered it.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 25, 2024
# roxygen2 7.3.0

## New features

* `@docType package` now works more like documenting `"_PACKAGE"`,
  creating a `{packagename}-package` alias and clearly suggesting that
  you should switch to `"_PACKAGE"` instead (#1491).

* `_PACKAGE` will no longer generate an alias for your package name if
  a function of the same name exists (#1160).

* The NAMESPACE roclet now reports if you have S3 methods that are missing
  an `@export` tag. All S3 methods need to be `@export`ed (which confusingly
  really registers the method) even if the generic is not. This avoids rare,
  but hard to debug, problems (#1175). You can suppress the warning with
  `@exportS3Method NULL` (#1550).

* The `NAMESPACE` roclet once again regenerates imports _before_ loading
  package code and parsing roxygen blocks. This has been the goal for a long
  time (#372), but we accidentally broke it when adding support for code
  execution in markdown blocks. This resolves a family of problems where you
  somehow bork your `NAMESPACE` and can't easily get out of it because you
  can't re-document the package because your code doesn't reload.

## Minor improvements and bug fixes

* If you document a function from another package it is automatically
  imported. Additionally, if you set `@rdname` or `@name` you can opt out
  of the default `reexports` topic generation and provide your own docs
  (#1408).

* Generate correct usage for S4 methods with non-syntactic class names.

* The `ROXYGEN_PKG` env var provides the name of the package being documented
  (#1517).

* `@describeIn foo` now suggests that you might want `@rdname` instead
  (#1493). It also gives a more informative warning if you use it with an
  unsupported type (#1490).

* In `DESCRIPTION`, URLs containing escapes in `URL` and `BugReports` are
  now correctly handled (@HenningLorenzen-ext-bayer, #1415). Authors can now
  have multiple email addresses (@jmbarbone, #1487).

* `escape_examples()` is now exported (#1450).

* `@exportS3Method` provides the needed metadata to generate correct usage
  for S3 methods, just like `@method` (#1202).

* `is_s3_generic()` now ignores non-function objects when looking for a
  candidate function. I believe this is closer to how R operates.

* `@import` and friends are now ignored if they try to import from the
  package being documented. This is useful to add self-dependencies in
  standalone files meant to be used in other packages (r-lib/usethis#1853).

* `@importFrom` throws a friendlier error if you try and import a non-existing
  functions (@MichaelChirico, #1409).

* `@include` now gives an informative warning if you use a path that doesn't
  exist (#1497).

* `@inherit` can now also inherit from `@format` (#1293).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants