Repository navigation
Rationalize interactions with the R runtime(s) - #1952
Conversation
| async function getRegisteredRRuntimes(): Promise<Map<string, RRuntime>> { | ||
| const registeredRuntimes = await positron.runtime.getRegisteredRuntimes(); | ||
| const registeredRRuntimes = registeredRuntimes.filter(runtime => runtime instanceof RRuntime); | ||
| const out = new Map<string, RRuntime>(); | ||
| for (const runtime of registeredRRuntimes) { | ||
| out.set(runtime.metadata.runtimeId, runtime as RRuntime); | ||
| } | ||
| return out; | ||
| } |
There was a problem hiding this comment.
This is the key point: instead of setting up the runtimes map in activate() and passing it around, we get the registered R runtimes at the time of need.
There was a problem hiding this comment.
When I originally worked on this, we decided against going over to Positron for the registered runtimes since the R extension is what registers the runtimes to start with. It's different than getting the running runtime because only Positron knows which runtime is running at any given time, but the R extension is what registers the runtimes in the first place. We decided at the time it was better to pass around the runtimes within the extension rather than ask Positron. You probably noticed that getRegisteredRuntimes() is used by the notebook controller extension, which is in a different situation than the R extension in terms of the registered runtimes.
Is there a way to still get to a simple checkInstalled('pkg_name') without going over to Positron to get the registered runtimes? After the extension does registerLanguageRuntimeDiscoverer() they are available over in the R extension. Could this be reorganized so it's in, say, the extension context or some globally available thing?
Or alternatively maybe @jmcphers can weigh in with a new or updated thought on whether it's a good call to ask for the registered runtimes from Positron.
There was a problem hiding this comment.
The other thing I thought about doing was to create a class inside positron-r (RRuntimeManager or something worthy of that name) to hold onto this runtimes map, instead of plumbing it through everywhere. Perhaps something along those lines is a good way to spare all of the other features from needing to have runtimes explicitly handed down through all of their functions.
There was a problem hiding this comment.
a class inside positron-r (
RRuntimeManageror something worthy of that name)
I really like that idea and my opinion is that it would be a better way to go.
There was a problem hiding this comment.
There's nothing wrong with asking Positron for the registered runtimes, but it is somewhat unnecessary because this extension is also where the runtimes were registered. I think it's fine to have an RRuntimeManager and double-register each runtime -- once on the Positron side, then feed the very same runtime into our own manager. Event listeners in RRuntimeManager can keep track of which one is live in Positron.
There was a problem hiding this comment.
I've introduced RRuntimeManager into this PR now. But I did it before I read this comment. I'm not double-registering but am passing the extension's map of runtimes to rRuntimeDiscoverer(). Hopefully that is acceptable? It appears to be working!
| runtimeName, | ||
| runtimeShortName, | ||
| runtimePath, | ||
| runtimeBinpath: rHome.binpath, |
There was a problem hiding this comment.
The binpath is sort of the most trusted piece of info from rHome. The R installation discovery actually runs off of a binpath, then figures out everything else from there. I think it's possible that runtimePath should actually just be the (bin)path? But for now, I've added it as an extra field.
positron/extensions/positron-r/src/r-installation.ts
Lines 40 to 43 in 739dea9
There was a problem hiding this comment.
I feel confused about runtimePath vs. runtimeBinpath. Can you say a little more about the difference?
If we do need to change the runtime metadata, we'll need to do PR to the Python extension as well (merge Python extension PR first, then bump the submodule in this PR, then merge here).
There was a problem hiding this comment.
One easy place to inspect and compare these two is in a discovered R installation, e.g. on macOS:
2023-12-19 13:02:19.866 [info] R installation discovered: {
...
"binpath": "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/bin/R",
"homepath": "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources",
...
}
What we currently call runtimePath in the positron.LanguageRuntimeMetadata corresponds to homepath above. What I've added in this PR as runtimeBinpath corresponds to binpath above.
I'm not sure why we have made homepath (runtimePath) readily available in the runtime metadata, but not binpath (runtimeBinpath). At least in the R extension, what we actually need and use is binpath. So we've been DIYing it by appending stuff to homepath a.k.a. runtimePath. But that is fussy and what you need to append can vary by OS and architecture.
We already have the binpath (it's how we discovered the R installation in the first place). So I propose exposing it in the metadata.
It is even possible that runtimePath should just be this binary path.
There was a problem hiding this comment.
For more comparison, here's what the python extension stores in runtimePath:
2023-12-19 13:02:20.083 [info] pythonRuntimeProvider: registering runtime for interpreter /opt/homebrew/bin/python3 with id 1f496a74fbe4a46ce3e0c9dc3d5e351b
which is more equivalent to /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/bin/R than /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources.
There was a problem hiding this comment.
I happened to be working on #1969 and realized that in fact, we need binpath to be able to register a runtime on demand. I do think we should change what's stored here in the metadata for R (but not add a new metadata element, since Python already stores the analogous info in the runtimePath slot).
| import { RHtmlWidget, getResourceRoots } from './htmlwidgets'; | ||
| import { randomUUID } from 'crypto'; | ||
|
|
||
| export let lastRuntimePath = ''; |
There was a problem hiding this comment.
I've brought this inside RRuntimeManager, since we have it now. And rebranded it as lastBinpath, which is what we actually need.
| 'task': 'r.task.packageCheck', | ||
| 'message': vscode.l10n.t('{taskName}', { taskName: 'Check R package' }), | ||
| 'shellExecution': `${lastRuntimePath}/R -e "devtools::check()"` | ||
| 'shellExecution': `${manager.getLastBinpath()} -e "devtools::check()"` |
There was a problem hiding this comment.
Here's one example where we create a path to the R binary, based on a runtimePath. But this method (appending /R) isn't going to be portable across operating systems.
We've already got this platform-specific logic in the runtime discovery code, so I think we should expose and use that.
| if (!lastRuntimePath) { | ||
| throw new Error(`No running R runtime to use for package testing.`); | ||
| } | ||
| const rBinPath = `${lastRuntimePath}/bin/R`; |
There was a problem hiding this comment.
I am eliminating this whole function, which we inherited from the 3rd party extension, but it's got yet another example of building a binary path (this time appending /bin/R).
|
@juliasilge and I just chatted and I'm going to simplify this to just use the existing |
| getRuntimesMap(): Map<string, RRuntime> { | ||
| return this.runtimes; | ||
| } | ||
|
|
||
| setLastBinpath(path: string) { | ||
| this.lastBinpath = path; | ||
| } | ||
|
|
||
| hasLastBinpath(): boolean { | ||
| return this.lastBinpath !== ''; | ||
| } | ||
|
|
||
| getLastBinpath(): string { | ||
| return this.lastBinpath; | ||
| } |
There was a problem hiding this comment.
With the exception of setLastBinpath(), we could just make the runtimes and lastBinpath fields public readonly vs. having these getters. What's best?
There was a problem hiding this comment.
I didn't think this through thoroughly. All of these fields need to be mutable, but I don't want that to happen willy nilly. So public readonly doesn't fit the bill. I'm going to leave this as is.
|
@juliasilge I think this now implements the plan we discussed this morning. |
juliasilge
left a comment
There was a problem hiding this comment.
Thank you for working on this!
If we can
- change the name of the new manager to something a bit more detailed
- switch out those getters
I call this done from my perspective! 👍
I will merge these changes into #1969 from main once it is done.
| getRuntimesMap(): Map<string, RRuntime> { | ||
| return this.runtimes; | ||
| } | ||
|
|
||
| setLastBinpath(path: string) { | ||
| this.lastBinpath = path; | ||
| } | ||
|
|
||
| hasLastBinpath(): boolean { | ||
| return this.lastBinpath !== ''; | ||
| } | ||
|
|
||
| getLastBinpath(): string { | ||
| return this.lastBinpath; | ||
| } |
| // devtools 2.4.0 was released 2021-04-07 | ||
| // chosen as minimum version because that's when test_active_file() was introduced | ||
| // indirectly imposes requirement for testthat >= 3.0.2 | ||
| const devtoolsInstalled = await checkInstalled('devtools', '2.4.0'); |
There was a problem hiding this comment.
Using checkInstalled() like this really shows the improvement we get from this PR 🙌

This PR is motivated by me trying to use the new
checkInstalled()facilities in the testing feature, as promised in #1629 (comment).As it stands the "check installed" feature requires an explicit handle for the running R runtime (i.e. you call
runtime.checkInstalled(...)), which in turn requires having aMapof all the R runtimes registered with the extension host (i.e. first you must callruntime = getRunningRRuntime(runtimes), whereruntimesis currently provisioned in positron-r'sactivate()function, then passed around.This PR started as an attempt to make it so that fewer functions and features need to know about this
runtimesmap, then grew to also include thelastRuntimePath.This has become my main question:
What's the intent of the
runtimePathinLanguageRuntimeMetadata? At least in the R extension, what we seem to need downstream is notruntimePath, but instead what I've introduced here asruntimeBinpath= the path to an R executable. When the metadata only includesruntimePath, we end up re-creating the binpath in various places, in different ways in the extension, which I think we should avoid. I'll make some more comments inline.