From 40f6a5ab68bf79355b69fbf0a9a47cb7c289ec6f Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Thu, 6 Aug 2026 08:21:33 -0500 Subject: [PATCH] Rd file names R CMD build will not throw away write_rd() sanitized two shapes -- a leading dot, and %op% -- and left everything else to chance. `$.foo`, `[[.foo` and `$<-.foo` are ordinary S3 methods, and the files named after them are not legal file names on every platform R runs on. R CMD build does not fail on those. It prints * excluding invalid files Subdirectory 'man' contains invalid file names: ... and carries on, and the R CMD check that follows reads a tarball those files are no longer in, so it has nothing to report. glinty shipped four undocumented S3 methods that way with four green CI jobs over it. The report came from a human running R CMD build by hand. rd_filename() applies roxygen2's substitution table, so a package moving between the two tools does not churn its man directory, and the two cases tinyrox already handled come out spelled exactly as they were: %||% is still grapes-or-or-grapes, .onLoad still dot-onLoad. The \name{} inside the file keeps the real topic, so help resolves under it either way. document() also refuses now if a name it just wrote would still be dropped. A silent failure in both directions deserves something that says no. --- DESCRIPTION | 2 +- NEWS.md | 5 ++ R/document.R | 1 + R/rd.R | 106 ++++++++++++++++++++++-------- inst/tinytest/test_rd_filenames.R | 81 +++++++++++++++++++++++ man/RD_NAME_SUBS.Rd | 19 ++++++ man/check_rd_filenames.Rd | 21 ++++++ man/rd_filename.Rd | 29 ++++++++ 8 files changed, 234 insertions(+), 30 deletions(-) create mode 100644 inst/tinytest/test_rd_filenames.R create mode 100644 man/RD_NAME_SUBS.Rd create mode 100644 man/check_rd_filenames.Rd create mode 100644 man/rd_filename.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 5590e82..0e36aa5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: tinyrox Title: Minimal R Documentation Generator -Version: 0.4.1 +Version: 0.4.1.1 Date: 2026-08-04 Authors@R: c( person("Troy", "Hernandez", email = "troy@cornball.ai", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 95b535e..250cba3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# tinyrox 0.4.1.1 + +* Rd file names are portable for every topic, not just `%op%` and a leading dot. `$.foo`, `[[.foo` and `$<-.foo` are ordinary S3 methods, and the files named after them were being **dropped from the tarball**: `R CMD build` prints "excluding invalid files" and carries on, and the `R CMD check` that follows reads a tarball those files are no longer in, so nothing fails. glinty shipped four undocumented S3 methods that way with every CI job green. +* The substitution table is roxygen2's, so a package moving between the two tools does not churn its `man` directory, and the two cases tinyrox already handled (`%||%`, a leading dot) come out spelled exactly as before. `document()` now refuses outright if a name it wrote would still be dropped. + # tinyrox 0.4.1 Two Rd-generation fixes. diff --git a/R/document.R b/R/document.R index 2fc0d91..bdda2ed 100644 --- a/R/document.R +++ b/R/document.R @@ -90,6 +90,7 @@ document <- function(path = ".", message("Generating Rd files...") } rd_files <- generate_all_rd(blocks, path, cran_check) + check_rd_filenames(rd_files) if (!silent) { message("Generated ", length(rd_files), " Rd file(s).") } diff --git a/R/rd.R b/R/rd.R index 542ea1b..9365080 100644 --- a/R/rd.R +++ b/R/rd.R @@ -510,41 +510,89 @@ write_rd <- function(content, name, path = ".") { dir.create(man_dir, recursive = TRUE) } - # Sanitize filename for Rd (like roxygen2) - # - Internal functions starting with . -> dot-name - # - Infix operators %X% -> grapes-X-grapes - filename <- name - if (grepl("^\\.", filename)) { - filename <- gsub("^\\.", "dot-", filename) - } else if (grepl("^%.*%$", filename)) { - # Encode infix operator: %||% -> grapes-or-or-grapes - # Only encode special characters, keep alphanumeric sequences intact - inner <- gsub("^%|%$", "", filename) - # Replace special characters with _WORD_ markers first - inner <- gsub("\\|", "_OR_", inner) - inner <- gsub("&", "_AND_", inner) - inner <- gsub("\\+", "_PLUS_", inner) - inner <- gsub("-", "_MINUS_", inner) - inner <- gsub("\\*", "_TIMES_", inner) - inner <- gsub("/", "_DIV_", inner) - inner <- gsub("<", "_LT_", inner) - inner <- gsub(">", "_GT_", inner) - inner <- gsub("=", "_EQ_", inner) - inner <- gsub("!", "_NOT_", inner) - # Convert markers to lowercase with - separators - inner <- gsub("_([A-Z]+)_", "-\\L\\1-", inner, perl = TRUE) - # Clean up multiple - and leading/trailing - - inner <- gsub("-+", "-", inner) - inner <- gsub("^-|-$", "", inner) - filename <- paste0("grapes-", inner, "-grapes") - } - filepath <- file.path(man_dir, paste0(filename, ".Rd")) + filepath <- file.path(man_dir, paste0(rd_filename(name), ".Rd")) writeLines(content, filepath, useBytes = TRUE) filepath } +#' Refuse to Leave a File Name R CMD build Will Drop +#' +#' rd_filename() is supposed to make every name portable. This says so +#' out loud rather than trusting it, because the failure is silent in +#' both directions: `R CMD build` drops the file without an error, and +#' the `R CMD check` that follows examines a tarball the file is no +#' longer in. +#' +#' @param files Character vector of paths just written. +#' @return invisible(TRUE) +#' @keywords internal +check_rd_filenames <- function(files) { + bad <- basename(files)[!grepl("^[A-Za-z0-9._-]+$", basename(files))] + if (length(bad) > 0) { + stop("these Rd file names are not portable, and R CMD build would ", + "drop them from the tarball without failing: ", + paste(bad, collapse = ", "), call. = FALSE) + } + invisible(TRUE) +} + +#' What a Topic Is Called on Disk +#' +#' Replacements applied to a topic name to make a file name, longest +#' key first so `[<-` is read before `[`. This is the table roxygen2 +#' uses, so a package moving between the two does not churn its `man` +#' directory, and the two names tinyrox already handled -- `%||%` and +#' a leading dot -- come out exactly as they did. +#' +#' @keywords internal +RD_NAME_SUBS <- c("[<-" = "-subset-", "[" = "-sub-", "<-" = "-set-", + "::" = "-", "!" = "-not-", "&" = "-and-", "|" = "-or-", + "*" = "-times-", "+" = "-plus-", "^" = "-pow-", + "\"" = "-quote-", "#" = "-hash-", "$" = "-cash-", + "%" = "-grapes-", "'" = "-single-quote-", + "(" = "-open-paren-", ")" = "-close-paren-", + ":" = "-colon-", ";" = "-semi-colon-", "<" = "-less-than-", + "==" = "-equals-", "=" = "-equals-", + ">" = "-greater-than-", "?" = "-help-", "@" = "-at-", + "]" = "-close-brace-", "\\" = "-backslash-", + "/" = "-slash-", "`" = "-tick-", "{" = "-open-curly-", + "}" = "-close-", "~" = "-twiddle-") + +#' Turn a Topic Name into a Portable File Name +#' +#' A topic can be called anything R can name, and `$.foo`, `[[.foo` +#' and `$<-.foo` are ordinary S3 methods. Those characters are not +#' legal in a file name on every platform R runs on, and `R CMD build` +#' does not warn and carry on: it prints "excluding invalid files" and +#' **drops them from the tarball**. The package then installs with no +#' documentation for those methods, and `R CMD check` reports nothing, +#' because it checks the tarball the files are no longer in. Green, +#' and missing its docs. +#' +#' The `\\name{}` inside the file keeps the real topic, so help still +#' resolves under it; only the file on disk is renamed. +#' +#' @param name Topic name. +#' @return A file name stem, with no .Rd extension. +#' @keywords internal +rd_filename <- function(name) { + out <- name + for (i in seq_along(RD_NAME_SUBS)) { + out <- gsub(names(RD_NAME_SUBS)[[i]], RD_NAME_SUBS[[i]], out, + fixed = TRUE) + } + # Anything still outside the portable set becomes a separator + # rather than being left to break the build later. + out <- gsub("[^A-Za-z0-9_.-]+", "-", out) + out <- gsub("-+", "-", out) + out <- gsub("^-|-$", "", out) + # A leading dot hides the file on unix and is dropped by some + # archivers; .onLoad and friends are ordinary topics. + gsub("^\\.", "dot-", out) +} + #' Identify a tinyrox-Generated Rd File #' #' Returns TRUE only if the file's first line is exactly RD_MARKER. This is diff --git a/inst/tinytest/test_rd_filenames.R b/inst/tinytest/test_rd_filenames.R new file mode 100644 index 0000000..dd65af8 --- /dev/null +++ b/inst/tinytest/test_rd_filenames.R @@ -0,0 +1,81 @@ +# What a topic is called on disk. +# +# A topic can be called anything R can name, and `$.foo`, `[[.foo` and +# `$<-.foo` are ordinary S3 methods. Those characters are not legal in +# a file name everywhere R runs, and R CMD build does not warn and +# carry on -- it prints "excluding invalid files" and drops them from +# the tarball. The package installs with no docs for those methods, +# and R CMD check reports nothing, because it checks the tarball the +# files are no longer in. +# +# glinty shipped four undocumented S3 methods that way. + +rd_filename <- tinyrox:::rd_filename +check_rd_filenames <- tinyrox:::check_rd_filenames + +# --- the four that were being dropped --- +expect_equal(rd_filename("$.glinty_input"), "cash-.glinty_input") +expect_equal(rd_filename("[[.glinty_input"), "sub-sub-.glinty_input") +expect_equal(rd_filename("$<-.glinty_output"), "cash-set-.glinty_output") +expect_equal(rd_filename("[[<-.glinty_output"), "sub-subset-.glinty_output") + +# --- what tinyrox already handled keeps its old name --- +# +# The table is roxygen2's, and these two come out of it unchanged, so +# adopting it renames nothing that was already working. +expect_equal(rd_filename("%||%"), "grapes-or-or-grapes") +expect_equal(rd_filename(".hidden"), "dot-hidden") +expect_equal(rd_filename(".onLoad"), "dot-onLoad") + +# --- ordinary names are left alone --- +for (name in c("document", "write_rd", "run_app", "as.character.foo", + "x2", "a.b.c", "with_underscores_and.dots")) { + expect_equal(rd_filename(name), name, info = name) +} + +# --- the rest of the operators --- +expect_equal(rd_filename("+.difftime"), "plus-.difftime") +expect_equal(rd_filename("[.data.frame"), "sub-.data.frame") +expect_equal(rd_filename("<-.foo"), "set-.foo") +expect_equal(rd_filename("=="), "equals") +expect_equal(rd_filename("!.foo"), "not-.foo") + +# --- every result is portable, whatever went in --- +# +# The point is not the spelling, it is that nothing reaches disk that +# the build would throw away. +awkward <- c("$.a", "[[.b", "[<-.c", "%in%", "?.d", "@.e", "/.f", "\\.g", + "{.h", "~.i", "|.j", "&.k", "*.l", "^.m", "#.n", "`.o", + ";.p", "(.q", ").r", "<.s", ">.t", ":.u", "::v", "'.w", + "\".x", "}.y", "].z") +for (name in awkward) { + out <- rd_filename(name) + expect_true(grepl("^[A-Za-z0-9._-]+$", out), + info = paste(name, "->", out)) + # no leading dot, no doubled or dangling separators + expect_false(startsWith(out, "."), info = out) + expect_false(grepl("--", out, fixed = TRUE), info = out) + expect_false(startsWith(out, "-") || endsWith(out, "-"), info = out) +} + +# and distinct topics stay distinct, or one would overwrite the other +expect_equal(anyDuplicated(vapply(awkward, rd_filename, character(1))), 0L) + +# --- the guard --- +expect_true(check_rd_filenames(c("man/ok.Rd", "man/also-ok.Rd"))) +expect_error(check_rd_filenames("man/$.foo.Rd"), pattern = "not portable") +expect_error(check_rd_filenames(c("man/fine.Rd", "man/[[.bar.Rd")), + pattern = "drop them from the tarball") +expect_true(check_rd_filenames(character(0))) + +# --- end to end: a package with these methods builds with them --- +# +# Writing the file is the half that used to work. This checks the half +# that did not: that the name it lands under survives R CMD build. +pkg <- file.path(tempdir(), "rdnames") +dir.create(file.path(pkg, "man"), recursive = TRUE, showWarnings = FALSE) +written <- tinyrox:::write_rd("\\name{$.foo}\n\\title{x}\n", "$.foo", pkg) +expect_true(file.exists(written)) +expect_equal(basename(written), "cash-.foo.Rd") +expect_true(grepl("^[A-Za-z0-9._-]+$", basename(written))) +unlink(pkg, recursive = TRUE) diff --git a/man/RD_NAME_SUBS.Rd b/man/RD_NAME_SUBS.Rd new file mode 100644 index 0000000..ca5010b --- /dev/null +++ b/man/RD_NAME_SUBS.Rd @@ -0,0 +1,19 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\docType{data} +\name{RD_NAME_SUBS} +\alias{RD_NAME_SUBS} +\title{What a Topic Is Called on Disk} +\format{ +An object of class \code{character} of length 32. +} +\usage{ +RD_NAME_SUBS +} +\description{ +Replacements applied to a topic name to make a file name, longest +key first so `[<-` is read before `[`. This is the table roxygen2 +uses, so a package moving between the two does not churn its `man` +directory, and the two names tinyrox already handled -- `\%||\%` and +a leading dot -- come out exactly as they did. +} +\keyword{internal} diff --git a/man/check_rd_filenames.Rd b/man/check_rd_filenames.Rd new file mode 100644 index 0000000..7737a62 --- /dev/null +++ b/man/check_rd_filenames.Rd @@ -0,0 +1,21 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{check_rd_filenames} +\alias{check_rd_filenames} +\title{Refuse to Leave a File Name R CMD build Will Drop} +\usage{ +check_rd_filenames(files) +} +\arguments{ +\item{files}{Character vector of paths just written.} +} +\value{ +invisible(TRUE) +} +\description{ +rd_filename() is supposed to make every name portable. This says so +out loud rather than trusting it, because the failure is silent in +both directions: `R CMD build` drops the file without an error, and +the `R CMD check` that follows examines a tarball the file is no +longer in. +} +\keyword{internal} diff --git a/man/rd_filename.Rd b/man/rd_filename.Rd new file mode 100644 index 0000000..2cd5c6e --- /dev/null +++ b/man/rd_filename.Rd @@ -0,0 +1,29 @@ +% tinyrox says don't edit this manually, but it can't stop you! +\name{rd_filename} +\alias{rd_filename} +\title{Turn a Topic Name into a Portable File Name} +\usage{ +rd_filename(name) +} +\arguments{ +\item{name}{Topic name.} +} +\value{ +A file name stem, with no .Rd extension. +} +\description{ +A topic can be called anything R can name, and `$.foo`, `[[.foo` +and `$<-.foo` are ordinary S3 methods. Those characters are not +legal in a file name on every platform R runs on, and `R CMD build` +does not warn and carry on: it prints "excluding invalid files" and +**drops them from the tarball**. The package then installs with no +documentation for those methods, and `R CMD check` reports nothing, +because it checks the tarball the files are no longer in. Green, +and missing its docs. +} +\details{ +The `\\name{}` inside the file keeps the real topic, so help still +resolves under it; only the file on disk is renamed. + +} +\keyword{internal}