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

Allow to reorder engine prioritization with engines key in _quarto.yml #11807

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
47 changes: 43 additions & 4 deletions src/execute/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export function engineValidExtensions(): string[] {
);
}

export function markdownExecutionEngine(markdown: string, flags?: RenderFlags) {
export function markdownExecutionEngine(
markdown: string,
reorderedEngines: Map<string, ExecutionEngine>,
flags?: RenderFlags,
) {
// read yaml and see if the engine is declared in yaml
// (note that if the file were a non text-file like ipynb
// it would have already been claimed via extension)
Expand All @@ -106,7 +110,7 @@ export function markdownExecutionEngine(markdown: string, flags?: RenderFlags) {
if (yaml) {
// merge in command line fags
yaml = mergeConfigs(yaml, flags?.metadata);
for (const [_, engine] of kEngines) {
for (const [_, engine] of reorderedEngines) {
if (yaml[engine.name]) {
return engine;
}
Expand All @@ -123,7 +127,7 @@ export function markdownExecutionEngine(markdown: string, flags?: RenderFlags) {

// see if there is an engine that claims this language
for (const language of languages) {
for (const [_, engine] of kEngines) {
for (const [_, engine] of reorderedEngines) {
if (engine.claimsLanguage(language)) {
return engine;
}
Expand All @@ -143,6 +147,38 @@ export function markdownExecutionEngine(markdown: string, flags?: RenderFlags) {
return markdownEngine;
}

function reorderEngines(project: ProjectContext) {
// TODO: add `engines` to schema
jkrumbiegel marked this conversation as resolved.
Show resolved Hide resolved
const userSpecifiedOrder: string[] =
project.config?.engines as string[] | undefined ?? [];

for (const key of userSpecifiedOrder) {
if (!kEngines.has(key)) {
throw new Error(
`'${key}' was specified in the list of engines in the project settings but it is not a valid engine. Available engines are ${
Array.from(kEngines.keys()).join(", ")
}`,
);
}
}

const reorderedEngines = new Map<string, ExecutionEngine>();

// Add keys in the order of userSpecifiedOrder first
for (const key of userSpecifiedOrder) {
reorderedEngines.set(key, kEngines.get(key)!); // Non-null assertion since we verified the keys are in the map
}

// Add the rest of the keys from the original map
for (const [key, value] of kEngines) {
if (!reorderedEngines.has(key)) {
reorderedEngines.set(key, value);
}
}

return reorderedEngines;
}

export async function fileExecutionEngine(
file: string,
flags: RenderFlags | undefined,
Expand All @@ -158,8 +194,10 @@ export async function fileExecutionEngine(
return undefined;
}

const reorderedEngines = reorderEngines(project);

// try to find an engine that claims this extension outright
for (const [_, engine] of kEngines) {
for (const [_, engine] of reorderedEngines) {
if (engine.claimsFile(file, ext)) {
return engine;
}
Expand All @@ -175,6 +213,7 @@ export async function fileExecutionEngine(
try {
return markdownExecutionEngine(
markdown ? markdown.value : Deno.readTextFileSync(file),
reorderedEngines,
flags,
);
} catch (error) {
Expand Down
25 changes: 20 additions & 5 deletions src/execute/julia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from "../deno_ral/path.ts";
import { MappedString, mappedStringFromFile } from "../core/mapped-text.ts";
import { partitionMarkdown } from "../core/pandoc/pandoc-partition.ts";
import { readYamlFromMarkdown } from "../core/yaml.ts";
import { asMappedString } from "../core/lib/mapped-text.ts";
import { ProjectContext } from "../project/types.ts";
import {
DependenciesOptions,
Expand Down Expand Up @@ -46,13 +47,21 @@ import {
executeResultIncludes,
} from "./jupyter/jupyter.ts";
import { isWindows } from "../deno_ral/platform.ts";
import {
isJupyterPercentScript,
markdownFromJupyterPercentScript,
} from "./jupyter/percent.ts";

export interface JuliaExecuteOptions extends ExecuteOptions {
julia_cmd: string;
oneShot: boolean; // if true, the file's worker process is closed before and after running
supervisor_pid?: number;
}

function isJuliaPercentScript(file: string) {
return isJupyterPercentScript(file, [".jl"]);
}

export const juliaEngine: ExecutionEngine = {
name: kJuliaEngine,

Expand All @@ -68,12 +77,12 @@ export const juliaEngine: ExecutionEngine = {

validExtensions: () => [],

claimsFile: (file: string, ext: string) => false,
claimsFile: (file: string, _ext: string) => {
return isJuliaPercentScript(file);
},

claimsLanguage: (language: string) => {
// we don't claim `julia` so the old behavior of using the jupyter
// backend by default stays intact
return false; // language.toLowerCase() === "julia";
return language.toLowerCase() === "julia";
},

partitionedMarkdown: async (file: string) => {
Expand Down Expand Up @@ -109,7 +118,13 @@ export const juliaEngine: ExecutionEngine = {
},

markdownForFile(file: string): Promise<MappedString> {
return Promise.resolve(mappedStringFromFile(file));
if (isJupyterPercentScript(file)) {
jkrumbiegel marked this conversation as resolved.
Show resolved Hide resolved
return Promise.resolve(
asMappedString(markdownFromJupyterPercentScript(file)),
);
} else {
return Promise.resolve(mappedStringFromFile(file));
}
},

execute: async (options: ExecuteOptions): Promise<ExecuteResult> => {
Expand Down
6 changes: 4 additions & 2 deletions src/execute/jupyter/jupyter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ export const jupyterEngine: ExecutionEngine = {
isJupyterPercentScript(file);
},

claimsLanguage: (_language: string) => {
return false;
claimsLanguage: (language: string) => {
// jupyter has to claim julia so that julia may also claim it without changing the old behavior
// of preferring jupyter over julia engine by default
return language.toLowerCase() === "julia";
},

markdownForFile(file: string): Promise<MappedString> {
Expand Down
9 changes: 5 additions & 4 deletions src/execute/jupyter/percent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export const kJupyterPercentScriptExtensions = [
".r",
];

export function isJupyterPercentScript(file: string) {
export function isJupyterPercentScript(file: string, extensions?: string[]) {
const ext = extname(file).toLowerCase();
if (kJupyterPercentScriptExtensions.includes(ext)) {
const availableExtensions = extensions ?? kJupyterPercentScriptExtensions;
if (availableExtensions.includes(ext)) {
const text = Deno.readTextFileSync(file);
return !!text.match(/^\s*#\s*%%+\s+\[markdown|raw\]/);
} else {
Expand Down Expand Up @@ -77,10 +78,10 @@ export function markdownFromJupyterPercentScript(file: string) {
let rawContent = cellContent(cellLines);
const format = cell.header?.metadata?.["format"];
const mimeType = cell.header.metadata?.[kCellRawMimeType];
if (typeof (mimeType) === "string") {
if (typeof mimeType === "string") {
const rawBlock = mdRawOutput(mimeType, lines(rawContent));
rawContent = rawBlock || rawContent;
} else if (typeof (format) === "string") {
} else if (typeof format === "string") {
rawContent = mdFormatOutput(format, lines(rawContent));
}
markdown += rawContent;
Expand Down
5 changes: 5 additions & 0 deletions src/resources/schema/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@
#
# In general, full json schema would allow negative assertions,
# but that makes our error localization heuristics worse. So we hack.

- name: engines
schema:
arrayOf: string
description: "List execution engines you want to give priority when determining which engine should render a notebook. If two engines have support for a notebook, the one listed earlier will be chosen. Quarto's default order is 'knitr', 'jupyter', 'markdown', 'julia'."
1 change: 1 addition & 0 deletions tests/docs/engine/invalid-project/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engines: ["invalid-engine"]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project:
type: default
engines: ["julia"]
10 changes: 10 additions & 0 deletions tests/docs/smoke-all/engine-reordering/julia-engine/notebook.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```{julia}
using Test
@test haskey(
Base.loaded_modules,
Base.PkgId(
Base.UUID("38328d9c-a911-4051-bc06-3f7f556ffeda"),
"QuartoNotebookWorker",
)
)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project:
type: default
engines: ["jupyter"]
10 changes: 10 additions & 0 deletions tests/docs/smoke-all/engine-reordering/jupyter-engine/notebook.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```{julia}
using Test
@test haskey(
Base.loaded_modules,
Base.PkgId(
Base.UUID("7073ff75-c697-5162-941a-fcdaad2a7d2a"),
"IJulia",
)
)
```
19 changes: 19 additions & 0 deletions tests/smoke/engine/invalid-engine-in-project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { assertRejects } from "testing/asserts";
import { quarto } from "../../../src/quarto.ts";
import { test } from "../../test.ts";

test(
{
name: "invalid engines option errors",
execute: async () => {
assertRejects(
async () => {await quarto(["render", "docs/engine/invalid-project/notebook.qmd"])},
Error,
"'invalid-engine' was specified in the list of engines in the project settings but it is not a valid engine",
)
},
type: "smoke",
context: {},
verify: [],
}
)
Loading