Skip to content

Rationalize interactions with the R runtime(s) - #1952

Merged
jennybc merged 9 commits into
mainfrom
feature/r-runtime-refactor
Dec 20, 2023
Merged

jennybc merged 9 commits into
mainfrom
feature/r-runtime-refactor

Conversation

@jennybc

@jennybc jennybc commented Dec 15, 2023 •

Copy link
Copy Markdown
Member

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 a Map of all the R runtimes registered with the extension host (i.e. first you must call runtime = getRunningRRuntime(runtimes), where runtimes is currently provisioned in positron-r's activate() function, then passed around.

This PR started as an attempt to make it so that fewer functions and features need to know about this runtimes map, then grew to also include the lastRuntimePath.

This has become my main question:

What's the intent of the runtimePath in LanguageRuntimeMetadata? At least in the R extension, what we seem to need downstream is not runtimePath, but instead what I've introduced here as runtimeBinpath = the path to an R executable. When the metadata only includes runtimePath, 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.

Comment thread extensions/positron-r/src/formatting.ts Resolved
Comment thread extensions/positron-r/src/provider.ts Outdated
Comment on lines +305 to +313
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;
}

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 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

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.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

a class inside positron-r (RRuntimeManager or something worthy of that name)

I really like that idea and my opinion is that it would be a better way to go.

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.

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.

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'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!

Comment thread extensions/positron-r/src/runtime.ts Resolved
Comment thread extensions/positron-r/src/extension.ts Resolved
@jennybc jennybc changed the title Ask for registered runtimes at the time of need Centralize interactions with the R runtime(s) Dec 16, 2023
Comment thread extensions/positron-r/src/provider.ts Resolved
Comment thread extensions/positron-r/src/runtime.ts Resolved
Comment thread extensions/positron-r/src/provider.ts Outdated
runtimeName,
runtimeShortName,
runtimePath,
runtimeBinpath: rHome.binpath,

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.

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.

constructor(pth: string, current: boolean = false) {
Logger.info(`Candidate R binary at ${pth}`);
this.binpath = pth;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

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.

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.

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.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks to me like for Python we have something more like binpath:

Screenshot 2023-12-19 at 2 54 29 PM

So maybe this should in fact switch over?

@juliasilge juliasilge Dec 19, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 = '';

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've brought this inside RRuntimeManager, since we have it now. And rebranded it as lastBinpath, which is what we actually need.

Comment thread extensions/positron-r/src/tasks.ts Outdated
'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()"`

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.

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`;

@jennybc jennybc Dec 19, 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.

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).

Comment thread src/positron-dts/positron.d.ts Resolved
@jennybc
jennybc marked this pull request as ready for review December 19, 2023 19:29
@jennybc jennybc changed the title Centralize interactions with the R runtime(s) Rationalize interactions with the R runtime(s) Dec 19, 2023
@jennybc

jennybc commented Dec 20, 2023

Copy link
Copy Markdown
Member Author

@juliasilge and I just chatted and I'm going to simplify this to just use the existing runtimePath metadata field for the path to the binary. Stay tuned.

Comment thread extensions/positron-r/src/provider.ts Resolved
Comment thread extensions/positron-r/src/provider.ts Resolved
Comment on lines +22 to +36
getRuntimesMap(): Map<string, RRuntime> {
return this.runtimes;
}

setLastBinpath(path: string) {
this.lastBinpath = path;
}

hasLastBinpath(): boolean {
return this.lastBinpath !== '';
}

getLastBinpath(): string {
return this.lastBinpath;
}

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.

With the exception of setLastBinpath(), we could just make the runtimes and lastBinpath fields public readonly vs. having these getters. What's best?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think public readonly.

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 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.

Comment thread extensions/positron-r/src/runtime.ts Resolved
@jennybc

jennybc commented Dec 20, 2023

Copy link
Copy Markdown
Member Author

@juliasilge I think this now implements the plan we discussed this morning.

Comment thread extensions/positron-r/src/tasks.ts Resolved

@juliasilge juliasilge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Comment thread extensions/positron-r/src/provider.ts Resolved
Comment on lines +22 to +36
getRuntimesMap(): Map<string, RRuntime> {
return this.runtimes;
}

setLastBinpath(path: string) {
this.lastBinpath = path;
}

hasLastBinpath(): boolean {
return this.lastBinpath !== '';
}

getLastBinpath(): string {
return this.lastBinpath;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think public readonly.

Comment thread extensions/positron-r/src/runtime.ts Resolved
// 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');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Using checkInstalled() like this really shows the improvement we get from this PR 🙌

@jennybc
jennybc merged commit 1d612b2 into main Dec 20, 2023
@jennybc
jennybc deleted the feature/r-runtime-refactor branch January 17, 2024 20:22
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