rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
165 stars 35 forks source link

Strange error when using RStudio addin with rstudioapi::setCursorPosition for the first time in a given session #211

Open dmkaplan2000 opened 3 years ago

dmkaplan2000 commented 3 years ago

I have been encountering a strange error when using a rstudio addin that I created that I am not sure even how to begin to debug and that would seem to be a rstudioapi bug related to the setCursorPosition function. The addin is the Insert filled data chunk addin of the knitrdata package (driven by the function knitrdata::insert_data_chunk_dialog). The basic functioning of the addin is that you select some options and when you hit the Create Chunk button it inserts a chunk of text into the active document in the RStudio editor. The first and only the first time I use the addin in a given RStudio session, the addin returns an error at the point where it uses rstudioapi::setCursorPosition here. In addition to the fact that this happens only the first time the addin is used (subsequent calls produce no error), the error is strange because it seems to happen in random different places in the process of setting the cursor position (i.e., the traceback is not always the same each time I generate the error even if all the steps I carried out to produce it are the same). It is as if there are several asynchronous operations being carried out, and which one runs into the problem varies slightly from run to run. I have looked at the tracebacks and at least the original setCursorPosition call seems correct (i.e., the position indicated is a valid position in the active document). At the end of this message I include a couple of screenshots showing a couple examples of the errors that can be produced.

The essential code (stripped of unnecessary code) related to rstudioapi that seems to be causing the problem is:

# Active document stuff
context = rstudioapi::getSourceEditorContext()
ln = context$selection[[1]]$range$start["row"]
dp = rstudioapi::document_position(ln,1) # position object

# Run shiny app for addin
chunk = create_data_chunk_dialog(title=title,infobar=infobar)

# Insert text
rstudioapi::insertText(dp,paste0(chunk,platform.newline(),collapse=""),context$id)

# Set position - sometimes causes errors for some unknown reason
rstudioapi::setCursorPosition(dp,context$id)

The questions I have are:

1) Is this bug my fault in some way or is this a rstudioapi problem? 2) Is there a fix or workaround? 3) If not, how would I go about debugging this problem? As it happens differently each time and only happens the first time I launch the addin, I am at a loss of what to do.

My RStudio installation is as follows:

Screenshots

Screenshot from 2020-12-07 11-41-44 Screenshot from 2020-12-07 11-57-05 Screenshot from 2020-12-07 11-58-28

dmkaplan2000 commented 3 years ago

First, this bug apparently has nothing specific to do with rstudioapi::setCursorPosition as commenting out this line just moves the error to another line of the addin function (such as the rstudioapi::insertText line).

Second, I have found something of a solution to this bug by moving all rstudioapi related code into the Shiny server function itself. After doing this, I no longer receive the error. I would still like to understand why this change fixes the problem as doing this is not ideal as it limits some ways I would have liked to use the function and I see no clear reason why this should fix the problem.

kevinushey commented 3 years ago

First thing I would ask: do you see this issue with the preview release of RStudio, alongside the development version of rstudioapi?

That said, some of rstudioapi's operations are not synchronous (and unfortunately we do not document which operations are synchronous, and which are not, which is a somewhat severe limitation ...) which could be responsible for the behavior you're seeing.

A simple way of testing that hypothesis would be to add a short Sys.sleep(0.1) after API calls, to see if that resolves the issue.

dmkaplan2000 commented 3 years ago

After a lot of playing around, I think I have an idea where this problem is coming from. Changing to the development release of RStudio made the problem go away, but when I reverted to rstudio 1.3, I couldn't make the error occur again, which made me think that the error had something to do with the way I was using RStudio. After a lot of tests, it seems to be caused by the 'Install and restart' button of the 'Build' tab in RStudio. If I open the project for my package in RStudio and use that button, the error appears indefinitely when I try to use the addin. If, however, I install the package from a tar.gz or from CRAN, it goes away. Updating to rstudio development cures the problems in all cases.

Also, the solution of moving the rstudioapi code into the Shiny app seems to be more bandade than solution. When I do that, the addin works, but it seems to be primarily because RStudio doesn't react to the error as in the command window I get a message about an "interrupt" occurring:

> knitrdata:::insert_data_chunk_dialog()
Loading required package: shiny

Listening on http://127.0.0.1:3694
later: interrupt occurred while executing callback.
dmkaplan2000 commented 3 years ago

Also, I tried the solution of adding sleep commands and that moved the error onto those sleep commands. My impression is that the error is going to happen, but where and when are somewhat random.