You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
styler executed via console and from within R does not always yield same result #187
When removing any of the above tokens, the problem does not occur anymore (I tried a few). It's very strange. E.g. replacing r <- function(y, s, g = 10) with r <- function(y, s g) will give correct results.
I think I managed to narrow down the problem further. The parse data is already different for both methods (at least the parents). The nested parse data is then different again since the nesting is done differently I guess. Looking into it further just now.
This is the flat parse data (interactive is the one run in R (which is correct), non_interactive the one that is run from the command line (which is not correct)).
>non_interactive# A tibble: 147 x 12line1col1line2col2idparenttokenterminaltextshorttoken_beforetoken_after<int><int><int><int><int><int><chr><lgl><chr><chr><chr><chr>1111110COMMENTTRUE# # COMMENT2212140COMMENTTRUE# # COMMENT COMMENT3313170COMMENTTRUE# # COMMENT SYMBOL441181227235exprFALSE<NA><NA>541411012SYMBOLTRUErrCOMMENTLEFT_ASSIGN6414112227exprFALSE<NA><NA>7434411227LEFT_ASSIGNTRUE<-<-SYMBOLFUNCTION846181226227exprFALSE<NA><NA>94641313226FUNCTIONTRUEfunctionfunctLEFT_ASSIGN'('1041441414226'('TRUE ( ( FUNCTIONSYMBOL_FORMALS# ... with 137 more rows>interactive# A tibble: 147 x 12line1col1line2col2idparenttokenterminaltextshorttoken_beforetoken_after<int><int><int><int><int><int><chr><lgl><chr><chr><chr><chr>111111-227COMMENTTRUE# # COMMENT221214-227COMMENTTRUE# # COMMENT COMMENT331317-227COMMENTTRUE# # COMMENT SYMBOL4411812270exprFALSE<NA><NA>541411012SYMBOLTRUErrCOMMENTLEFT_ASSIGN6414112227exprFALSE<NA><NA>7434411227LEFT_ASSIGNTRUE<-<-SYMBOLFUNCTION846181226227exprFALSE<NA><NA>94641313226FUNCTIONTRUEfunctionfunctLEFT_ASSIGN'('1041441414226'('TRUE ( ( FUNCTIONSYMBOL_FORMALS# ... with 137 more rows
Not really. Even with the option --interactive, I can't reproduce what I get in an interactive session. Can you double check? I think simply -> undo -> save some times does not work, at least the git tab of RStudio sometimes is not getting it. That's why I also change one of the token texts to be sure.
I really wonder why utils::getParseData() assigns different IDs and parents depending ok whether ran interactively or not.
I think one (pragmatic) solution would be to make sure tokens have a parent that is not a terminal. If it's is a terminal, we can assign the parent of the parent to the token as the parent. That's may fit into the package for handling the inconsistency in the parse data (see #100). I just tried it and it works for the case at hand. Line / col values of the parse data are corrected with the approach outlined above.
Alternatively, we may attempt to find out the cause of the problem.
changed the title [-]styler executed via console and from within R does not yield same result[/-][+]styler executed via console and from within R does not always yield same result[/+]on Sep 7, 2017
I am pretty sure you don't see this interactively because the parser has to run on the input expression, and this bug only happens the very first time something is parsed.
If you have a small reproducible example consider including it in the bug report, if there is independent verification maybe the root cause could be sorted out.
If Kirill already has access to the R bug reporting system, maybe he could add my example to Jim Hester's bug report since I just realized that I need an R Core member to manually add me to the bug reporting system in order to comment on an existing bug report.
Thanks, catching up. Rather than posting to a four-year-old ticket I'd try to fix the error in R-core and submit a PR there. IIUC, I need to call parse() on the input below, both from an interactive session and from a session started with R -q -e?
Thanks, I am happy to hear it worked. How to deal with base R fixes, it depends. In this case I think using the workaround unnecessarily is undesirable due to performance overhead, so I think it would make sense to add a guard on specific R version. Even though not terribly pretty, you could for instead use the double parsing only when getRversion() <= "3.5.1" || R.version$`svn rev` < 75178.
Activity
lorenzwalthert commentedon Sep 7, 2017
I was able to reproduce the problem with a smaller code snippet
When removing any of the above tokens, the problem does not occur anymore (I tried a few). It's very strange. E.g. replacing
r <- function(y, s, g = 10)withr <- function(y, s g)will give correct results.krlmlr commentedon Sep 7, 2017
Thanks. Can you confirm that this problem also occurs when starting R from the terminal and then running
style_file()?lorenzwalthert commentedon Sep 7, 2017
Yes, that is the case
lorenzwalthert commentedon Sep 7, 2017
I think I managed to narrow down the problem further. The parse data is already different for both methods (at least the parents). The nested parse data is then different again since the nesting is done differently I guess. Looking into it further just now.
This is the flat parse data (
interactiveis the one run in R (which is correct),non_interactivethe one that is run from the command line (which is not correct)).lorenzwalthert commentedon Sep 7, 2017
This is the nested parse data
lorenzwalthert commentedon Sep 7, 2017
id 227 in
non_interactivehas parent 235, which is a comment. I think that's the problem.krlmlr commentedon Sep 7, 2017
So this is triggered by
--interactive? 8.5 out of 10 for weirdness...lorenzwalthert commentedon Sep 7, 2017
Not really. Even with the option
--interactive, I can't reproduce what I get in an interactive session. Can you double check? I think simply -> undo -> save some times does not work, at least the git tab of RStudio sometimes is not getting it. That's why I also change one of the token texts to be sure.I really wonder why utils::getParseData() assigns different IDs and parents depending ok whether ran interactively or not.
lorenzwalthert commentedon Sep 7, 2017
I think one (pragmatic) solution would be to make sure tokens have a parent that is not a terminal. If it's is a terminal, we can assign the parent of the parent to the token as the parent. That's may fit into the package for handling the inconsistency in the parse data (see #100). I just tried it and it works for the case at hand. Line / col values of the parse data are corrected with the approach outlined above.
Alternatively, we may attempt to find out the cause of the problem.
lorenzwalthert commentedon Sep 7, 2017
I created #188, please have a look. It solves the specific case introduced in this issue.
[-]styler executed via console and from within R does not yield same result[/-][+]styler executed via console and from within R does not always yield same result[/+]jimhester commentedon Sep 8, 2017
Pretty sure this is a bug in the R parser, it is not initialized properly the first time parse is run https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=16041.
lintr works around this by always running parse twice https://github.com/jimhester/lintr/blob/aa70430282852afa15a61cc69771e71dd32e1aec/R/get_source_expressions.R#L121-L129
I am pretty sure you don't see this interactively because the parser has to run on the input expression, and this bug only happens the very first time something is parsed.
If you have a small reproducible example consider including it in the bug report, if there is independent verification maybe the root cause could be sorted out.
lorenzwalthert commentedon Sep 8, 2017
Thanks for looking at that @jimhester. Indeed, using two identical
parse()statements prevents the problem from occurring, soR -q -e 'styler::style_file("R/debug.r")'gives the same result as running
For the problematic case introduced above.
Regarding the reprex, if I get you right, I could save the above code snippet as
file_causing_error.Rand show:And in particular, show the offending values of "parent".
And submit a new bug report and make reference to Bug 16041.
jimhester commentedon Sep 8, 2017
I wouldn't submit a new bug report, rather comment on the existing one, I am pretty sure it is the same bug.
lorenzwalthert commentedon Sep 8, 2017
Ok, I will do it that way then.
lorenzwalthert commentedon Sep 8, 2017
As far as styler goes, I suggest to adapt @jimhester's solution and parse twice.
lorenzwalthert commentedon Sep 9, 2017
If Kirill already has access to the R bug reporting system, maybe he could add my example to Jim Hester's bug report since I just realized that I need an R Core member to manually add me to the bug reporting system in order to comment on an existing bug report.
krlmlr commentedon Sep 15, 2017
Thanks, catching up. Rather than posting to a four-year-old ticket I'd try to fix the error in R-core and submit a PR there. IIUC, I need to call
parse()on the input below, both from an interactive session and from a session started withR -q -e?kalibera commentedon Aug 24, 2018
I've fixed 16041 in R-devel, 75178. Could you please check if this fixes also the problem you found using
styler? Thanksremove repeated parsing for testing purposes r-lib#187
lorenzwalthert commentedon Aug 24, 2018
Ok, cool. Let's see.
lorenzwalthert commentedon Aug 24, 2018
After having temporarily fixed a problem with our CI tool
tic, we see the expected result:https://travis-ci.org/lorenzwalthert/styler/builds/420091391
What is the best practice to deal with fixed bugs in base R? Circumvent them explicitly only depending on the R version? Or any time?
kalibera commentedon Aug 24, 2018
Thanks, I am happy to hear it worked. How to deal with base R fixes, it depends. In this case I think using the workaround unnecessarily is undesirable due to performance overhead, so I think it would make sense to add a guard on specific R version. Even though not terribly pretty, you could for instead use the double parsing only when
getRversion() <= "3.5.1" || R.version$`svn rev` < 75178.