Repository navigation
Run cells of any language with a Positron runtime - #1237
michaelmayer2 wants to merge 3 commits into
Conversation
In Positron, only R, Python, Julia and C# cells ran in the console.
Cells for runtimes that other extensions register, such as {sas} from
the SAS extension, got no Run Cell lens, and the run commands did
nothing on them.
Track the languages of registered Positron runtimes and treat them as
executable, running their cells with the same executor as the built-in
languages.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
juliasilge
left a comment
There was a problem hiding this comment.
Thank you for this PR and for the detailed write-up! We're excited to get SAS working well in Positron. 🙌
When inline output is off, the Quarto extension does decide which cells can run, so we do need a change here in this extension. We actually have the same problem for {ggsql} cells as well. For example, with inline output off, Cmd+Enter and the code lenses do nothing for either a ggsql or SAS document.
This PR is not quite the right approach yet for Quarto support, though. Right now, this fills the list from getRegisteredRuntimes() and onDidRegisterRuntime, but a runtime registers only after its extension activates and discovers an interpreter or a profile. For SAS, this occurs only after a connection profile exists. Before that, {sas} cells get no lenses, and the lenses come back only on the next lens refresh. When inline output is on, Positron uses a different source to decide this, the languageRuntimes contribution in the package.json of each extension (hasLanguageRuntimeProvider in runtimeStartup.ts). As a result, the two modes can disagree.
A better approach is to change the source of the language list, so that both modes agree on which cells can run:
- Read the
languageRuntimescontributions in the Quarto extension. CollectpackageJSON.contributes.languageRuntimes[].languageIdfromvscode.extensions.all, and refresh the list onvscode.extensions.onDidChange. This is synchronous, it is available before other extensions activate, and it matches the source that Positron uses for inline output. - Keep the registered runtimes from this PR as a second source, and use the union of the two lists. This covers an extension that registers a runtime but has no manifest entry.
- Test in a Positron dev build with inline output off, with
{ggsql}and{sas}cells. Test the lenses, Run Above, Run Next Cell, and Cmd+Enter.
I logged posit-dev/positron#17432 for us to improve this situation, but this PR doesn't need to wait for that.
For SAS, the SAS extension needs to also add this contribution to its package.json (ggsql already does this):
"contributes": {
"languageRuntimes": [{ "languageId": "sas" }]
}After a Quarto extension release includes this PR, the SAS extension will need to hide the {sas} lenses from posit-dev/positron-sas-extension#163 for that Quarto version. If it doesn't, each {sas} cell will show two Run Cell lenses.
One more item to think through is that right now codeWithoutOptionsFromBlock removes option lines only for languages that use #|. Please make sure that the option lines in {ggsql} cells do not cause errors when they go to the console.
Use the `languageRuntimes` contributions in the extensions' package.json
as the main source of languages whose cells run in a Positron console,
as Positron does for inline output (`hasLanguageRuntimeProvider`), and
keep the registered runtimes as a second source. The contributions are
known before the extensions activate or discover a runtime, so e.g.
`{sas}` cells are runnable before a SAS connection profile exists, and
both modes agree on which cells can run.
Strip the leading option lines of these languages before sending the
code to the console, using each language's own comment characters
(`#|` for ggsql, where `#` isn't a comment, and `*| ...;` for sas). The
matching is shared with cellOptions().
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the careful review! I've reworked the PR along those lines (c75a7a4):
Thanks for logging posit-dev/positron#17432! |
…s runtime languages Builds on quarto-dev#1237, which runs cells of any language that an extension contributes or registers a Positron runtime for. On top of that: - ask Positron (`_quartoHostOwnedCellLanguages`, `_quartoHostCellRuntimeLanguages`) which cell languages it serves and which runtime runs each one in a document, so that e.g. `{sql}` cells run in the ggsql runtime; a language counts if Positron names a runtime for it or it has a runtime of its own, and Positron's mapping comes first - let Positron explain a cell it cannot run - strip cell option lines for every language, with its own comment characters, also from the visual editor (this covers quarto-dev#1237's stripping for runtime languages, using its `leadingOptionYaml`) - decide which cells can run once per document, for Run All/Above/Below, Run Next/Previous and the code lenses Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… a missing command - ggsql cell options may be written `#|` as well as `--|`: quarto-cli and the ggsql docs use `#|`, so those lines are honored (eval, metadata), stripped before running, decorated and highlighted again, as in quarto-dev#1237 - create the per-document runtime languages cache with its listeners when the Positron host is created, so a visual editor that re-asks on a host refresh no longer reads the stale answer - remember for 30s, or until the next host refresh, that Positron lacks `_quartoHostCellRuntimeLanguages`, rather than sending a failing command plus `getCommands` on every document version - CHANGELOG: one entry for option stripping, ggsql's two option prefixes, and how the explanation is reached Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ready cover - documentExecutableLanguages starts from the host's executableLanguages, which quarto-dev#1237 already widens with the languages that have a Positron runtime, so the run commands, code lenses and visual editor share one base, and hostExecutableLanguages is Positron's per-document answer only - leave out of the question to `_quartoHostCellRuntimeLanguages` any language with a Positron runtime of its own (which Positron decides the same way, through hasRuntimeProvider) or with an executor in this extension. In practice only `{sql}` and languages nothing runs are asked about. This also stops shell cells (bash, sh, shell) being handed to a Positron runtime rather than this extension's executor Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With quarto-dev#1237, the extension already knows which languages have a Positron runtime, which is how Positron decides them too. What `_quartoHostCellRuntimeLanguages` added on top was one rule: a `{sql}` cell runs in ggsql when ggsql has a runtime, except in a knitr document. Make that rule local (`positronRuntimeLanguage`) and remove the query: - the Positron host's executableLanguages(visualMode, document, engine) adds sql when ggsql runs it in the document; cellExecutorForLanguage hands sql cells to a ggsql executor - documentExecutableLanguages is synchronous again, so the code lenses are too; hostExecutableLanguages, the per-document cache and the missing-command bookkeeping are gone - the visual editor goes back to quarto-dev#1237's: it reads the host's executable languages when it opens, with no push (VSC_VE_ExecutableLanguagesChanged) - `_quartoHostExplainUnrunnableCell` and `_quartoHostOwnedCellLanguages` stay Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Quarto extension no longer asks which runtime runs each cell language (quarto-dev/quarto#1239). With quarto-dev/quarto#1237 it reads the languages that have a runtime provider itself, and decides the one mapping this command added, `{sql}` cells running in ggsql except under knitr, on its own. `_quartoHostOwnedCellLanguages` and `_quartoHostExplainUnrunnableCell` stay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
juliasilge
left a comment
There was a problem hiding this comment.
Thank you so much for revamping the approach here and making the change to the SAS extension! 🙌
I have just a couple of minor points/questions, and then can you also tighten up the release notes so they are more in line with the level of detail of existing bullets?
| vscode.extensions.onDidChange(() => { | ||
| contributedLanguages = contributedRuntimeLanguages(vscode.extensions.all); | ||
| }); | ||
|
|
||
| runtime.getRegisteredRuntimes().then( | ||
| (runtimes) => runtimes.forEach((metadata) => registeredLanguages.add(metadata.languageId)), | ||
| () => { } | ||
| ); | ||
| runtime.onDidRegisterRuntime((metadata) => registeredLanguages.add(metadata.languageId)); |
There was a problem hiding this comment.
It is possible for registeredLanguages and contributedLanguages to change after Quarto computes the code lenses, but nothing tells the code lens provider to refresh here since quartoCellExecuteCodeLensProvider in providers/cell/codelens.ts has no onDidChangeCodeLenses.
As a result, a new language can stay without a Run Cell lens for quite some time. This occurs when getRegisteredRuntimes() resolves, when onDidRegisterRuntime fires, or when extensions.onDidChange adds a contribution. The cells of that language get a lens only after a different event refreshes the lenses, for example an edit to the document.
Can we fire an event here when one of the two sets gets a new language? And then give this event to the provider as onDidChangeCodeLenses.
|
|
||
| runtime.getRegisteredRuntimes().then( | ||
| (runtimes) => runtimes.forEach((metadata) => registeredLanguages.add(metadata.languageId)), | ||
| () => { } |
There was a problem hiding this comment.
If getRegisteredRuntimes() rejects, this handler ignores the error and then the cells of a language that only registers a runtime never get a Run Cell lens, and we don't put anything in the Quarto log.
positronExtensionHost already has an outputChannel and calls trackRuntimeLanguages(). Actually, I notice that this file already logs other errors with outputChannel?.error(...) too. Can we give the channel to trackRuntimeLanguages and log the error here:
function trackRuntimeLanguages(outputChannel?: vscode.LogOutputChannel) {
// ...
runtime.getRegisteredRuntimes().then(
(runtimes) => runtimes.forEach((metadata) => registeredLanguages.add(metadata.languageId)),
(error) => outputChannel?.error(`Failed to get registered runtimes: ${error}`)
);| trackingRuntimes = true; | ||
|
|
||
| // the contributions are read synchronously, so they are known right away; | ||
| // the subscriptions below live as long as the extension host |
There was a problem hiding this comment.
The rest of the extension pushes its disposables into context.subscriptions in activate (for example the code lens provider in main.ts), so this is different from the usual pattern. Would it work to use the more typical approach? I think trackRuntimeLanguages can return the two disposables, then main.ts can push them into context.subscriptions, maybe through extensionHost(...).
If you prefer to keep this as it is for some specific reason, I do think we could support it as well.
| // characters (`*| ... ;` for sas, `#|` for languages without an entry | ||
| // in kLangCommentChars, such as ggsql, where `#` isn't a comment) | ||
| const blockLines = lines(codeForExecutableLanguageBlock(token)); | ||
| const optionLines = leadingOptionYaml(language, blockLines).length; |
There was a problem hiding this comment.
I'm not a SAS expert, but from reading the code it looks like there is a small edge case where leadingOptionYaml treats a line as an option only if it ends with the ; suffix. So a line like *| label: a without the ; stops the scan, and this code sends the line to the console. SAS reads * as the start of a comment that continues to the next ;, right? As a result, the first statement of the cell becomes part of the comment, and no error occurs with this:
*| label: a
proc print data=x; run;Even though proc print would not run. Should we do something to help a user not get confused by this? If you think this one is fine, I am happy to defer on it!
`runLanguage` preferred any cell language with an executor in this
extension, so in Positron a document of `{ggsql}` or `{sas}` cells plus one
`{bash}` cell ran only the bash cell. Go back to quarto-dev#1237's choice (the first
cell, the cell at the cursor, or the nearest one above), and run every cell
that shares its runtime: the host's new optional cellRuntimeLanguage says
which, and Positron's answers from positronRuntimeLanguage, so `{sql}` and
`{ggsql}` cells run together in ggsql. Hosts without it (VS Code) group by
cell language, as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Problem
In Positron with inline output off, Quarto runs cells only for
julia,python,csharpandr, the hard-coded cases incellExecutorForLanguageinsrc/host/positron.ts. Other languages fall back to the static executor list. So cells for runtimes that extensions add, such as{ggsql}(ggsql extension) or{sas}(SAS extension, common through the sasquatch R package), get no Run Cell lens.quarto.runCurrentCelland the other run commands do nothing on them. With inline output on, Positron decides instead, from thelanguageRuntimescontributions (hasLanguageRuntimeProvider), so the two modes disagree.Change
Languages with a Positron runtime (
positron.ts) come from two sources, as inhasLanguageRuntimeProvider:languageRuntimescontributions in thepackage.jsonof the installed extensions (vscode.extensions.all), re-read onvscode.extensions.onDidChange. This read is synchronous and known before those extensions activate or discover a runtime. The parsing is the purecontributedRuntimeLanguages().getRegisteredRuntimes()andonDidRegisterRuntime), for an extension that registers a runtime without the contribution.Both are cached because
hasExecutor()is synchronous. They are filled only in Positron, so VS Code is unaffected even with such extensions installed.The existing Positron executor moves unchanged into
positronRuntimeExecutor(). The built-in languages use it as before, and in thedefault:branch any language with a Positron runtime uses it too.hasExecutor()and the host'sexecutableLanguagesinclude these languages, so lenses, Run Above / Run Next Cell and the shortcuts work for them.Option lines:
codeWithoutOptionsFromBlockalso strips the leading option lines of languages with a Positron runtime, using each language's comment characters fromkLangCommentChars:#|, and its docs use#|too. But#isn't a comment in ggsql (--,//and/* */are), so those lines used to reach the console as code.*| …;.The matching is the one
cellOptions()uses, now shared asleadingOptionYaml(). Languages without a runtime behave as before.Positron's longer-term fix is tracked in posit-dev/positron#17432.
Testing
tsc --noEmitand eslint pass.cellExecutors.test.tspass. In the fullmainsuite, the only failures are 4 Convert.qmd→.ipynbtests, which also fail on this branch's base on my machine.languageRuntimescontribution (Positron: declare the SAS language runtime in the manifest posit-dev/positron-sas-extension#167), and ggsql 0.5.2:{ggsql}and{sas}cells show lenses before any console exists.🤖 Generated with Claude Code