diff --git a/src/rmarkdown/knit.ts b/src/rmarkdown/knit.ts index 149d39290..f490d600d 100644 --- a/src/rmarkdown/knit.ts +++ b/src/rmarkdown/knit.ts @@ -64,6 +64,7 @@ export class RMarkdownKnitManager extends RMarkdownManager { return await this.knitWithProgress( { + workingDirectory: knitWorkingDir, fileName: docName, filePath: rDocumentPath, scriptArgs: scriptValues, diff --git a/src/rmarkdown/manager.ts b/src/rmarkdown/manager.ts index 6bd98113c..490eb039c 100644 --- a/src/rmarkdown/manager.ts +++ b/src/rmarkdown/manager.ts @@ -18,6 +18,7 @@ export interface IKnitRejection { const rMarkdownOutput: vscode.OutputChannel = vscode.window.createOutputChannel('R Markdown'); interface IKnitArgs { + workingDirectory: string; filePath: string; fileName: string; scriptArgs: Record; @@ -35,8 +36,7 @@ export abstract class RMarkdownManager { // so that we can't spam the knit/preview button protected busyUriStore: Set = new Set(); - protected getKnitDir(knitDir: string, docPath?: string): string { - const currentDocumentWorkspace = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(docPath) ?? vscode.window.activeTextEditor?.document?.uri)?.uri?.fsPath ?? undefined; + protected getKnitDir(knitDir: string, docPath: string): string { switch (knitDir) { // the directory containing the R Markdown document case KnitWorkingDirectory.documentDirectory: { @@ -44,6 +44,7 @@ export abstract class RMarkdownManager { } // the root of the current workspace case KnitWorkingDirectory.workspaceRoot: { + const currentDocumentWorkspace = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(docPath) ?? vscode.window.activeTextEditor?.document?.uri)?.uri?.fsPath ?? undefined; return currentDocumentWorkspace.replace(/\\/g, '/').replace(/['"]/g, '\\"'); } // the working directory of the attached terminal, NYI @@ -74,11 +75,12 @@ export abstract class RMarkdownManager { `-f`, `${scriptPath}` ]; - const processOptions = { + const processOptions: cp.SpawnOptions = { env: { ...process.env, ...scriptArgs - } + }, + cwd: args.workingDirectory, }; let childProcess: DisposableProcess; diff --git a/src/rmarkdown/preview.ts b/src/rmarkdown/preview.ts index e5a9466f6..645776153 100644 --- a/src/rmarkdown/preview.ts +++ b/src/rmarkdown/preview.ts @@ -328,6 +328,7 @@ export class RMarkdownPreviewManager extends RMarkdownManager { return await this.knitWithProgress( { + workingDirectory: knitWorkingDir, fileName: fileName, filePath: filePath, scriptPath: extensionContext.asAbsolutePath('R/rmarkdown/preview.R'),