Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render output in a temp directory, when in an R package #435

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,13 @@
"default": true,
"markdownDescription": "Reveal the preview panel after document render."
},
"quarto.render.rPackageOutputDirectory": {
"order": 15,
"scope": "window",
"type": "boolean",
"default": true,
"markdownDescription": "Render output files in a temporary directory, when in an R package."
},
Comment on lines +1076 to +1082
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is new configuration, so users can turn off this behavior if they prefer:

Screenshot 2024-05-08 at 4 08 13 PM

I want to argue that this should be true by default, because otherwise, R package developers need to add all these output files to both .gitignore and .Rbuildignore.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would recommend doing a bit of work ahead of time to make sure that this is not going to create other bugs. In an R package, will users mostly preview README.md files and vignettes? In that case, I wonder if the right behavior is to use --output-dir only in these special cases.

Like we discussed, relocating files for an .html target is unlikely to be robust because of things like CSS resource paths; in that case, we tend to recommend to use --output-dir together with embed-resources

Copy link
Contributor

Choose a reason for hiding this comment

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

With that said, this is a pain in the neck to test in an automated fashion. It might be good enough to test this by making a mock test harness over at https://github.com/quarto-dev/quarto-cli. I can help with that; I think it's prudent to do it. Because quarto-cli won't generally know the ways in which the extension calls quarto render, quarto preview etc, we have a pretty big regression risk unless we do something about it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In that case, I wonder if the right behavior is to use --output-dir only in these special cases.

I'm going to get some more feedback on this from folks, but my current belief is that there are basically no cases where you want to check in generated code such as HTML, libs, etc, for an R package.

The one exception is README.Rmd being rendered to README.md but a) none of that works for people right now because of output: github_document and b) we can encourage them to use devtools::build_readme() for this purpose. If we want to be more forward thinking, we could exclude README.Rmd and README.qmd from this treatment, rather than make a list of files to include for this treatment.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm going to get some more feedback on this from folks, but my current belief is that there are basically no cases where you want to check in generated code such as HTML, libs, etc, for an R package.

I believe that! Really my only concern is that quarto preview itself might break under common use cases in R packages, and I'd hate for us to enable something by default that doesn't work well in these scenarios.

"quarto.visualEditor.fontSize": {
"order": 31,
"scope": "resource",
Expand Down
27 changes: 27 additions & 0 deletions apps/vscode/src/providers/preview/preview-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ export function isQuartoShinyKnitrDoc(

}

export async function isRPackage(): Promise<boolean> {
const descriptionLines = await parseRPackageDescription();
if (!descriptionLines) {
return false;
}
const packageLines = descriptionLines.filter(line => line.startsWith('Package:'));
const typeLines = descriptionLines.filter(line => line.startsWith('Type:'));
const typeIsPackage = (typeLines.length > 0
? typeLines[0].toLowerCase().includes('package')
: false);
const typeIsPackageOrMissing = typeLines.length === 0 || typeIsPackage;
return packageLines.length > 0 && typeIsPackageOrMissing;
}

async function parseRPackageDescription(): Promise<string[]> {
if (vscode.workspace.workspaceFolders !== undefined) {
const folderUri = vscode.workspace.workspaceFolders[0].uri;
const fileUri = vscode.Uri.joinPath(folderUri, 'DESCRIPTION');
try {
const bytes = await vscode.workspace.fs.readFile(fileUri);
const descriptionText = Buffer.from(bytes).toString('utf8');
const descriptionLines = descriptionText.split(/(\r?\n)/);
return descriptionLines;
} catch { }
}
return [''];
}

export async function renderOnSave(engine: MarkdownEngine, document: TextDocument) {
// if its a notebook and we don't have a save hook for notebooks then don't
Expand Down
14 changes: 14 additions & 0 deletions apps/vscode/src/providers/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
haveNotebookSaveEvents,
isQuartoShinyDoc,
isQuartoShinyKnitrDoc,
isRPackage,
renderOnSave,
} from "./preview-util";

Expand Down Expand Up @@ -448,6 +449,9 @@ class PreviewManager {
// terminal options
const options = terminalOptions(kPreviewWindowTitle, target, this.previewEnv_);

// is this workspace an R package?
const isRPackageWorkspace = await isRPackage();

// is this is a shiny doc?
const isShiny = isQuartoShinyDoc(this.engine_, doc);
const useServeCommand = this.usesQuartoServeCommand(doc);
Expand Down Expand Up @@ -477,6 +481,12 @@ class PreviewManager {
cmd.push("--no-watch-inputs");
}

// use temp output-dir for R package
if (isRPackageWorkspace && this.previewRPackageDirConfig()) {
cmd.push("--output-dir", tmp.dirSync().name);
cmd.push("--embed-resources");
}

// send terminal command
await sendTerminalCommand(this.terminal_, this.previewEnv_, this.quartoContext_, cmd);

Expand Down Expand Up @@ -711,6 +721,10 @@ class PreviewManager {
return this.quartoConfig().get("render.previewReveal", true);
}

private previewRPackageDirConfig(): boolean {
return this.quartoConfig().get("render.rPackageOutputDirectory", true);
}

private quartoConfig() {
return vscode.workspace.getConfiguration("quarto");
}
Expand Down