Repository navigation
R: Add new checkInstalled() function for R packages and use in R formatter - #1930
Conversation
|
This is an exciting development! I know this is still a draft PR but I wanted to put this on the radar: I think we're going to want the ability to express a minimum version of such a package. Is that already in your current plans? |
|
Yep, if you look over at amalthea, it is an optional arg so you can do it like |
|
What do you think of exposing minimum version in |
|
Oh, of course, yep 🙈 I did not plumb that through. |
|
Discussion topic, maybe for our next R dev meeting -- should we provide a facility for RPCs to be defined in the .R files that live in the R extension (e.g. alongside startup.R)? It would be a lot easier all around if both sides of the RPC were in the same repository, plus eliminates a category of "kernel isn't in sync with extension" errors. |
| if (pkgVersion) { | ||
| isInstalled = await this.callMethod('is_installed', pkgName, pkgVersion); | ||
| } else { | ||
| isInstalled = await this.callMethod('is_installed', pkgName); | ||
| } |
There was a problem hiding this comment.
I don't suppose there is a more elegant way to pass/not pass the optional arg through ...args: any[] than just if/else?
There was a problem hiding this comment.
I think you can just pass undefined stuff through transparently and it won't get added to the args array? Don't quote me on that.
There was a problem hiding this comment.
FWIW I did try that first and Positron was very unhappy. I see errors like this:
[error] Error: write after end
at new NodeError (node:internal/errors:399:5)
at _write (node:internal/streams/writable:322:11)
at Writable.write (node:internal/streams/writable:337:10)
at /Users/juliasilge/Work/rstudio/positron/extensions/positron-r/node_modules/vscode-jsonrpc/lib/node/ril.js:90:29
at new Promise (<anonymous>)
at WritableStreamWrapper.write (/Users/juliasilge/Work/rstudio/positron/extensions/positron-r/node_modules/vscode-jsonrpc/lib/node/ril.js:80:16)
at StreamMessageWriter.doWrite (/Users/juliasilge/Work/rstudio/positron/extensions/positron-r/node_modules/vscode-jsonrpc/lib/common/messageWriter.js:100:33)
at /Users/juliasilge/Work/rstudio/positron/extensions/positron-r/node_modules/vscode-jsonrpc/lib/common/messageWriter.js:91:29
| }); | ||
| }); | ||
|
|
||
| this.execute(`install.packages("${pkgName}")`, |
There was a problem hiding this comment.
We can't tell whether installation worked here, so I don't update the cache until another round through checkInstalled(), i.e. a second call to this.callMethod('is_installed').
| private _state: positron.RuntimeState = positron.RuntimeState.Uninitialized; | ||
|
|
||
| /** Cache for which packages we know are installed in this runtime **/ | ||
| private _packageCache = new Array<RPackageInstallation>(); |
There was a problem hiding this comment.
This cache is just alive through the lifetime of the runtime, which I think is good enough. Trying to write to storage doesn't seem worth it at this point to me given how easy it would be for that cache to be bad between sessions for that runtime.
There was a problem hiding this comment.
Very much agreed, we should recompute this at least once per session.
jmcphers
left a comment
There was a problem hiding this comment.
LGTM pending localization fixes!
| if (!isInstalled) { | ||
| const install = await positron.window.showSimpleModalDialogPrompt( | ||
| 'Missing R package', | ||
| `Package ${pkgName} required but not installed.`, |
There was a problem hiding this comment.
Nit: All these strings need to be marked for localization.
| /** | ||
| * Checks whether a package is installed in the runtime. | ||
| * @param pkgName The name of the package to check | ||
| * @param intendedUse A string describing the intended use of the package, for a warning message |
There was a problem hiding this comment.
I don't think it is possible to compose a warning message in a language-neutral way; we should probably either have the caller pass in the whole warning message to display, or let the caller handle warning messages themselves.
There was a problem hiding this comment.
OK, I think I'll just take out the warning message. Without that UI treatment, what it will look like is:
- The user tries to format their document
- They get the modal because they don't have styler installed
- They click "Cancel"
- Nothing happens (the formatter just returns with an empty edit)
That seems reasonable to me, and can work the same for other situations, i.e. the requested action just gets canceled.
Addresses #1629 with a new function in the R extension, and then uses it to check for styler in the R formatter. We can use this new function throughout the extension, like for testthat, devtools, etc.
Requires posit-dev/ark#174 so we'll need to merge that and bump ark.