Skip to content

Commit

Permalink
Supply RSTUDIO_PANDOC to packaging tasks, including R Markdown render (
Browse files Browse the repository at this point in the history
…#3856)

Several beta users have already reported trouble rendering documents
with Positron because the render tasks can't find Pandoc, even though it
ships with Positron.

This change supplies the`RSTUDIO_PANDOC` environment variable when
running packaging tasks and R Markdown rendering jobs. It's done using
the same logic we use to supply `RSTUDIO_PANDOC` to the main R session.

Addresses #3776.

### QA Notes

The embedded Pandoc is only used when there's no other Pandoc on the
`$PATH`. To test this, uninstall all your Pandocies (make sure `which
pandoc` returns nothing) first so that only the bundled Pandoc is
available.

This also touches the code that injects Pandoc into the main R session,
so make sure e.g. `rmarkdown::pandoc_version()` still works there.
  • Loading branch information
jmcphers authored Jul 8, 2024
1 parent e246589 commit 82b335e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
23 changes: 23 additions & 0 deletions extensions/positron-r/src/pandoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*---------------------------------------------------------------------------------------------
* Copyright (C) 2024 Posit Software, PBC. All rights reserved.
* Licensed under the Elastic License 2.0. See LICENSE.txt for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { existsSync } from 'fs';
import * as path from 'path';

/**
* Discovers the path to the pandoc executable that ships with Positron.
*
* @returns The path to the pandoc executable, if it exists.
*/
export function getPandocPath(): string | undefined {
const pandocPath = path.join(vscode.env.appRoot,
process.platform === 'darwin' ?
path.join('bin', 'pandoc') :
path.join('..', '..', 'bin', 'pandoc'));
if (existsSync(pandocPath)) {
return pandocPath;
}
}
9 changes: 3 additions & 6 deletions extensions/positron-r/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { randomUUID } from 'crypto';
import { handleRCode } from './hyperlink';
import { RSessionManager } from './session-manager';
import { EXTENSION_ROOT_DIR } from './constants';
import { existsSync } from 'fs';
import { getPandocPath } from './pandoc';

interface RPackageInstallation {
packageName: string;
Expand Down Expand Up @@ -704,11 +704,8 @@ export function createJupyterKernelSpec(
//
// On MacOS, the binary path lives alongside the app bundle; on other
// platforms, it's a couple of directories up from the app root.
const pandocPath = path.join(vscode.env.appRoot,
process.platform === 'darwin' ?
path.join('bin', 'pandoc') :
path.join('..', '..', 'bin', 'pandoc'));
if (existsSync(pandocPath)) {
const pandocPath = getPandocPath();
if (pandocPath) {
env['RSTUDIO_PANDOC'] = pandocPath;
}

Expand Down
13 changes: 12 additions & 1 deletion extensions/positron-r/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as vscode from 'vscode';
import { RSessionManager } from './session-manager';
import { getEditorFilePathForCommand } from './commands';
import { getPandocPath } from './pandoc';

export class RPackageTaskProvider implements vscode.TaskProvider {

Expand Down Expand Up @@ -57,6 +58,15 @@ export async function getRPackageTasks(editorFilePath?: string): Promise<vscode.
package: 'rmarkdown'
}
];

// if we have a local copy of Pandoc available, forward it to the R session
// so that it can be used to render R Markdown documents (etc)
const env: any = {};
const pandocPath = getPandocPath();
if (pandocPath) {
env['RSTUDIO_PANDOC'] = pandocPath;
}

// the explicit quoting treatment is necessary to avoid headaches on Windows, with PowerShell
return taskData.map(data => new vscode.Task(
{ type: 'rPackageTask', task: data.task, pkg: data.package },
Expand All @@ -65,7 +75,8 @@ export async function getRPackageTasks(editorFilePath?: string): Promise<vscode.
'R',
new vscode.ShellExecution(
binpath,
['-e', { value: data.rcode, quoting: vscode.ShellQuoting.Strong }]
['-e', { value: data.rcode, quoting: vscode.ShellQuoting.Strong }],
{ env }
),
[]
));
Expand Down

0 comments on commit 82b335e

Please sign in to comment.