Skip to content

R: Add new checkInstalled() function for R packages and use in R formatter - #1930

Merged
juliasilge merged 11 commits into
mainfrom
check-installed
Dec 11, 2023
Merged

juliasilge merged 11 commits into
mainfrom
check-installed

Conversation

@juliasilge

Copy link
Copy Markdown
Member

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.

@jennybc

jennybc commented Dec 7, 2023

Copy link
Copy Markdown
Member

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?

@juliasilge

juliasilge commented Dec 7, 2023 •

Copy link
Copy Markdown
Member Author

Yep, if you look over at amalthea, it is an optional arg so you can do it like await runtime.callMethod('is_installed', 'testthat', '3.0.0').

@jennybc

jennybc commented Dec 7, 2023

Copy link
Copy Markdown
Member

What do you think of exposing minimum version in checkInstalled()? We could also do that in a later refinement. I know I will need it (a minimum version) for testthat and devtools and we could tackle it then.

@juliasilge

Copy link
Copy Markdown
Member Author

Oh, of course, yep 🙈 I did not plumb that through.

@jmcphers

jmcphers commented Dec 8, 2023

Copy link
Copy Markdown
Collaborator

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.

Comment thread extensions/positron-r/src/formatting.ts Resolved
Comment on lines +317 to +321
if (pkgVersion) {
isInstalled = await this.callMethod('is_installed', pkgName, pkgVersion);
} else {
isInstalled = await this.callMethod('is_installed', pkgName);
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't suppose there is a more elegant way to pass/not pass the optional arg through ...args: any[] than just if/else?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}")`,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>();

@juliasilge juliasilge Dec 8, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very much agreed, we should recompute this at least once per session.

@juliasilge
juliasilge marked this pull request as ready for review December 8, 2023 20:26

@jmcphers jmcphers left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending localization fixes!

Comment thread extensions/positron-r/src/runtime.ts Outdated
if (!isInstalled) {
const install = await positron.window.showSimpleModalDialogPrompt(
'Missing R package',
`Package ${pkgName} required but not installed.`,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: All these strings need to be marked for localization.

Comment thread extensions/positron-r/src/runtime.ts Outdated
/**
* 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Successfully merging this pull request may close these issues.

3 participants