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

[[<-.custom produces unexpected exported NAMESPACE values #1217

Closed
schloerke opened this issue Mar 31, 2021 · 3 comments
Closed

[[<-.custom produces unexpected exported NAMESPACE values #1217

schloerke opened this issue Mar 31, 2021 · 3 comments

Comments

@schloerke
Copy link

I've made a reprex below showing that the tag's $val which appears to be a "" is not actually a "". This causes the roxy_tag_ns.roxy_tag_export function logic to fail.

tmpfile <- tempfile()
cat(file = tmpfile, "
#' @export​
`[[<-.custom` <- function(a,b,c) {
  a
}

#' @export​
`[<-.custom` <- function(a,b,c) {
  a
}
"
)

blocks <- roxygen2::parse_file(tmpfile)
blocks
#> [[1]]
#> <roxy_block> [file11399528b5575:3]
#>   $tag
#>     [line:  2] @export '​' {parsed}
#>     [????:???] @usage '<generated>' {parsed}
#>     [????:???] @.formals '<generated>' {parsed}
#>     [????:???] @backref '/var/folders/0k/bxg5lhr92sq74mb1d446ql540000gp/...' {parsed}
#>   $call   `[[<-.custom` <- function(a, b, c) { ...
#>   $object <s3method> 
#>     $topic [[<-.custom
#>     $alias [[<-.custom
#> 
#> [[2]]
#> <roxy_block> [file11399528b5575:8]
#>   $tag
#>     [line:  7] @export '​' {parsed}
#>     [????:???] @usage '<generated>' {parsed}
#>     [????:???] @.formals '<generated>' {parsed}
#>     [????:???] @backref '/var/folders/0k/bxg5lhr92sq74mb1d446ql540000gp/...' {parsed}
#>   $call   `[<-.custom` <- function(a, b, c) { ...
#>   $object <s3method> 
#>     $topic [<-.custom
#>     $alias [<-.custom

# Unexpected behavior
roxygen2:::blocks_to_ns(blocks)
#> [1] "export(\"​\")"

# The values appear to be a double quote
blocks[[1]]$tags[[1]]$raw
#> [1] "​"
blocks[[1]]$tags[[1]]$val
#> [1] "​"
# Does not equal a double quote
blocks[[1]]$tags[[1]]$raw == ""
#> [1] FALSE
blocks[[1]]$tags[[1]]$val == ""
#> [1] FALSE

# Since it does not equal a `""`, the then the `identical(x$val, "")` check fails in
roxygen2:::roxy_tag_ns.roxy_tag_export
#> function (x, block, env, import_only = FALSE) 
#> {
#>     if (import_only) {
#>         return()
#>     }
#>     if (identical(x$val, "")) {
#>         default_export(block$object, block)
#>     }
#>     else {
#>         export(x$val)
#>     }
#> }
#> <bytecode: 0x7fa5114865c8>
#> <environment: namespace:roxygen2>

# `$raw` is copied over here...
roxygen2:::tag_words_line
#> function (x) 
#> {
#>     x$val <- str_trim(x$raw)
#>     if (str_detect(x$val, "\n")) {
#>         roxy_tag_warning(x, "may only span a single line")
#>     }
#>     else if (!rdComplete(x$val)) {
#>         roxy_tag_warning(x, "mismatched braces or quotes")
#>     }
#>     else {
#>         x$val <- str_split(x$val, "\\s+")[[1]]
#>         x
#>     }
#> }
#> <bytecode: 0x7fa5211d3600>
#> <environment: namespace:roxygen2>

# Expected behavior
fixed_blocks <- blocks
fixed_blocks[[1]]$tags[[1]]$val <- ""
fixed_blocks[[2]]$tags[[1]]$val <- ""
roxygen2:::blocks_to_ns(fixed_blocks)
#> [1] "S3method(\"[<-\",custom)"  "S3method(\"[[<-\",custom)"

Created on 2021-03-31 by the reprex package (v0.3.0)


I don't know if the original error is here:

_["raw"] = Rcpp::String(stripTrailingNewline(vals[i])),

Changing the string value to "" fixes my problem, but is not a solution.


Link to where $val is set (at least for my example):

roxygen2/R/tag-parser.R

Lines 135 to 136 in 6c1e42f

tag_words_line <- function(x) {
x$val <- str_trim(x$raw)

schloerke added a commit to rstudio/htmltools that referenced this issue Apr 1, 2021
@schloerke
Copy link
Author

Did more debugging with @cpsievert ...

Turns out, x$raw is not equal to "". If we call charToRaw(x$raw), I was expecting raw(0). It is actually charToRaw(x$raw) # e2 80 8b. This value is a "Zero width space". Weird!

Looking at the c++ code more, if the line below was disabled, the Zero Width Space problem would go away.

line.consumeText(&curVal);

@schloerke
Copy link
Author

Work-around for now. #' @rawNamespace S3method("[[<-", custom)

@schloerke
Copy link
Author

I'm going to blame slack.app. I can see there is a non-ascii character in my code (even the reprex).

When copying a text from a snippet in slack, the hidden UTF-8 characters were added in. 🤦

Screen Shot 2021-04-01 at 3 15 48 PM

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

No branches or pull requests

1 participant