Closed aryoda closed 3 years ago
Brodie Gaslam wrote an email on Wed, 12 May 2021 and pointed me to the cause of the problem (thanks a lot for this execellent analysis and work :-)
Dear Juergen,
Your package tryCatchLog has started to fail on CRAN:
This almost certainly due to recent changes to r-devel which change the display-width treatment of control characters. For example, it used to be the case that:
nchar("hello\n", type="width")
1 6
But now:
nchar("hello\n", type="width")
1 5
This is because the trailing newline is now treated as zero width. Character counts (as opposed to display width calculations) are unaffected:
nchar("hello\n")
1 6
From the documentation for
?nchar
we see:Control characters are given width zero in multi-byte locales, but are usually given width one in single-byte ones (as their positions are often undefined and maybe re-used as in CP1252 vs Latin-1).
It turns out R was incorrectly implementing the "width zero" part.
You are impacted because you use
strtrim
, which internally uses similar logic tonchar(, type="width")
.?strtrim
also states:‘Width’ is interpreted as the display width in a monospaced font. What happens with non-printable characters (such as backspace, tab) is implementation-dependent and may depend on the locale (e.g., they may be included in the count or they may be omitted).
This affects tests which involving newlines in the text.
Unfortunately because the width of control characters is not strictly defined, it is possible that new versions of R, or the current version of R run in different locales, could produce different outcomes. Thus, to avoid more headaches from this I would encourage you to update your tests in such a way that they are not dependent on what the display width of control characters such as newlines is (or display width at all).
In your specific case it seems you could replace newlines by spaces and run
strtrim
on that, possibly recording the character locations of newlines and re-inserting them if preserving them is important. Or just usesubstr
which operates on characters and will be unaffected, but will truncate words.Best,
Brodie.
First diagnoses via debugging using R-devel on Ubuntu 20.04:
This problem is no bug in tryCatchLog
but caused by the unit tests with changed actual values due to a bug fix in R-devel
(in other word: the curse of extensive unit testing ;-)
── Error (test_build_log_entry.R:69:3): stack trace is correct ─────────────────
Error: invalid 'x' type in 'x && y'
This misleading error message is caused by a wrong function call parameter in this unit test line:
https://github.com/aryoda/tryCatchLog/blob/master/tests/testthat/test_build_log_entry.R#L69
I forgot the info =
and this problem was not occurring until the expect_equal
check fails.
The correct syntax is:
expect_equal(log.entry$full.stack.trace, expected_FST, info = "full stack trace")
After fixing this all three unit tests fail for the same reason as described by Brodie above:
Due to the new width counting of (trailing?) new lines which cause a different result then expected before (defined via "old" R versions) as soon as the stack trace string is "cut" to the given width set by
options("width" = 129)
Example (simplified excerpt of a stack trace line of the second failing unit test):
Expected:
msg <- condit
Actual with R-devel:
msg <- conditi
Observe that the actual result contains now one more character since the trailing new line is no longer counted as contributing to the width (count) where the output shall be cut if it is longer!
Approach to fix the failing unit tests:
if (nchar("hello\n", type = "width") == 5) ...
in the unit tests to check for existence of the fix in base R
and use the according expected values.The above approach did not work as expected:
nchar("hello\n")
returns 6 and the old unit tests workednchar("hello\n")
still returns 6 but the old unit tests did no longer work because strtrim
seems to count newlines differently nownchar("hello\n")
returns 5 and the old unit tests did no longer work because strtrim
seems to count newlines differently nowI did not want to introduce 3-way if-branching in the unit tests and decided to increment the maximum value of the maxwidth
argument in limitedLabels()
from 1000 to 2000 so that unit test strings are no longer cut at a certain position which is different depending on the R version.
Now the units do work again on the above R versions.
I have submitted the new package version 1.2.3 at CRAN today
After fixing an moved link and re-submitting the package as version 1.2.4 the updated version is on CRAN now since 10 minutes :-)
https://cran.r-project.org/web/packages/tryCatchLog/index.html
Thanks a lot again to Brodie Gaslam for his valuable work to narrow down the cause of the problems, this really saved me a lot of time :-)
See release 1.2.4:
https://github.com/aryoda/tryCatchLog/releases/tag/v1.2.4_CRAN
Prof. Brian Ripley sent an email on Tue, 11 May 2021:
According to the logs the unit tests fail only on R-devel and R patched:
The failing unit tests are these three ones:
and
and