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

create ProjectContext in single-file mode to guarantee engine and target caching #8771

Merged
merged 1 commit into from
Feb 19, 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
4 changes: 3 additions & 1 deletion src/command/preview/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { previewShiny } from "./preview-shiny.ts";
import { serve } from "../serve/serve.ts";
import { fileExecutionEngine } from "../../execute/engine.ts";
import { notebookContext } from "../../render/notebook/notebook-context.ts";
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";

export const previewCommand = new Command()
.name("preview")
Expand Down Expand Up @@ -275,7 +276,8 @@ export const previewCommand = new Command()
if (Deno.statSync(file).isFile) {
// get project and preview format
const nbContext = notebookContext();
const project = await projectContext(dirname(file), nbContext);
const project = (await projectContext(dirname(file), nbContext)) ||
singleFileProjectContext(file, nbContext);
const formats = await (async () => {
const services = renderServices(nbContext);
try {
Expand Down
7 changes: 5 additions & 2 deletions src/command/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import {
rswURL,
} from "../../core/previewurl.ts";
import { notebookContext } from "../../render/notebook/notebook-context.ts";
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";

export async function resolvePreviewOptions(
options: ProjectPreview,
Expand Down Expand Up @@ -365,11 +366,13 @@ export async function previewFormat(
if (format) {
return format;
}
const nbContext = notebookContext();
project = project || singleFileProjectContext(file, nbContext);
formats = formats ||
await withRenderServices(
notebookContext(),
nbContext,
(services: RenderServices) =>
renderFormats(file, services, "all", project),
renderFormats(file, services, "all", project!),
);
format = Object.keys(formats)[0] || "html";
return format;
Expand Down
9 changes: 4 additions & 5 deletions src/command/render/render-contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export async function renderContexts(
options: RenderOptions,
forExecute: boolean,
notebookContext: NotebookContext,
project?: ProjectContext,
project: ProjectContext,
cloneOptions: boolean = true,
enforceProjectFormats: boolean = true,
): Promise<Record<string, RenderContext>> {
Expand Down Expand Up @@ -304,11 +304,10 @@ export async function renderContexts(
if (engineClaimReason === "markdown") {
// since the content decided the engine, and the content now changed,
// we need to re-evaluate the engine and target based on new content.
const { engine, target } = await fileExecutionEngineAndTarget(
const { engine, target } = await project.fileExecutionEngineAndTarget(
file.path,
options.flags,
markdown,
project,
true,
);
context.engine = engine;
context.target = target;
Expand All @@ -327,7 +326,7 @@ export async function renderFormats(
file: string,
services: RenderServices,
to = "all",
project?: ProjectContext,
project: ProjectContext,
): Promise<Record<string, Format>> {
const contexts = await renderContexts(
{ path: file },
Expand Down
14 changes: 8 additions & 6 deletions src/command/render/render-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ export async function renderFiles(
files: RenderFile[],
options: RenderOptions,
notebookContext: NotebookContext,
alwaysExecuteFiles?: string[],
pandocRenderer?: PandocRenderer,
project?: ProjectContext,
alwaysExecuteFiles: string[] | undefined,
pandocRenderer: PandocRenderer | undefined,
project: ProjectContext,
): Promise<RenderFilesResult> {
// provide default renderer
pandocRenderer = pandocRenderer || defaultPandocRenderer(options, project);
Expand Down Expand Up @@ -368,7 +368,7 @@ export async function renderFile(
file: RenderFile,
options: RenderOptions,
services: RenderServices,
project?: ProjectContext,
project: ProjectContext,
enforceProjectFormats: boolean = true,
): Promise<RenderFilesResult> {
// provide default renderer
Expand Down Expand Up @@ -420,7 +420,7 @@ async function renderFileInternal(
lifetime: Lifetime,
file: RenderFile,
options: RenderOptions,
project: ProjectContext | undefined,
project: ProjectContext,
pandocRenderer: PandocRenderer,
files: RenderFile[],
tempContext: TempContext,
Expand Down Expand Up @@ -456,6 +456,8 @@ async function renderFileInternal(
const { engine, target } = await fileExecutionEngineAndTarget(
file.path,
options.flags,
undefined,
project,
);
const validationResult = await validateDocumentFromSource(
target.markdown,
Expand Down Expand Up @@ -695,7 +697,7 @@ async function renderFileInternal(
// default pandoc renderer immediately renders each execute result
function defaultPandocRenderer(
_options: RenderOptions,
_project?: ProjectContext,
_project: ProjectContext,
): PandocRenderer {
const renderCompletions: PandocRenderCompletion[] = [];
const renderedFiles: RenderedFile[] = [];
Expand Down
24 changes: 21 additions & 3 deletions src/command/render/render-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { renderProject } from "./project.ts";
import { renderFiles } from "./render-files.ts";
import { resourceFilesFromRenderedFile } from "./resources.ts";
import { RenderFlags, RenderOptions, RenderResult } from "./types.ts";
import { fileExecutionEngine } from "../../execute/engine.ts";
import {
fileExecutionEngine,
fileExecutionEngineAndTarget,
} from "../../execute/engine.ts";

import {
isProjectInputFile,
Expand All @@ -33,6 +36,7 @@ import { initYamlIntelligenceResourcesFromFilesystem } from "../../core/schema/u
import { kTextPlain } from "../../core/mime.ts";
import { normalizePath } from "../../core/path.ts";
import { notebookContext } from "../../render/notebook/notebook-context.ts";
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";

export async function render(
path: string,
Expand Down Expand Up @@ -95,11 +99,24 @@ export async function render(
// validate that we didn't get any project-only options
validateDocumentRenderFlags(options.flags);

// NB: singleFileProjectContext is currently not fully-featured
context = singleFileProjectContext(path, nbContext, options.flags);

// otherwise it's just a file render
const result = await renderFiles([{ path }], options, nbContext);
const result = await renderFiles(
[{ path }],
options,
nbContext,
undefined,
undefined,
context,
);

// get partitioned markdown if we had result files
const engine = fileExecutionEngine(path);
const { engine } = await context.fileExecutionEngineAndTarget(
path,
undefined,
);
const partitioned = (engine && result.files.length > 0)
? await engine.partitionedMarkdown(path)
: undefined;
Expand All @@ -126,6 +143,7 @@ export async function render(
};
})),
error: result.error,
baseDir: normalizePath(dirname(path)),
};

if (!renderResult.error && engine?.postRender) {
Expand Down
2 changes: 1 addition & 1 deletion src/command/render/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface RenderContext {
engine: ExecutionEngine;
format: Format;
libDir: string;
project?: ProjectContext;
project: ProjectContext;
active: boolean;
}

Expand Down
4 changes: 3 additions & 1 deletion src/command/serve/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { previewFormat } from "../preview/preview.ts";
import { withRenderServices } from "../render/render-services.ts";
import { notebookContext } from "../../render/notebook/notebook-context.ts";
import { RenderServices } from "../render/types.ts";
import { singleFileProjectContext } from "../../project/types/single-file/single-file.ts";

export const serveCommand = new Command()
.name("serve")
Expand Down Expand Up @@ -65,7 +66,8 @@ export const serveCommand = new Command()
const { host, port } = await resolveHostAndPort(options);

const nbContext = notebookContext();
const context = await projectContext(input, nbContext);
const context = (await projectContext(input, nbContext)) ||
singleFileProjectContext(input, nbContext);
const formats = await withRenderServices(
nbContext,
(services: RenderServices) =>
Expand Down
13 changes: 7 additions & 6 deletions src/core/jupyter/jupyter-embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function unsupportedEmbed(path: string) {
export async function ensureNotebookContext(
markdown: string,
services: RenderServices,
context?: ProjectContext,
context: ProjectContext,
) {
const regex = placeholderRegex();
let match = regex.exec(markdown);
Expand Down Expand Up @@ -353,9 +353,9 @@ export async function replaceNotebookPlaceholders(
};
}

function resolveNbPath(input: string, path: string, context?: ProjectContext) {
function resolveNbPath(input: string, path: string, context: ProjectContext) {
// If this is a project, absolute means project relative
if (context) {
if (!context.isSingleFile) {
const projectMatch = path.match(/^[\\/](.*)/);
if (projectMatch) {
return join(context.dir, projectMatch[1]);
Expand All @@ -368,8 +368,9 @@ function resolveNbPath(input: string, path: string, context?: ProjectContext) {
if (isAbsolute(input)) {
return join(dirname(input), path);
} else {
const baseDir = context ? context.dir : Deno.cwd();
return join(baseDir, dirname(input), path);
const baseDir = context.isSingleFile ? Deno.cwd() : context.dir;
const result = join(baseDir, dirname(input), path);
return result;
}
}
}
Expand Down Expand Up @@ -803,7 +804,7 @@ function resolveRange(rangeRaw?: string) {
function jupyterFromNotebookOrQmd(
nbAbsPath: string,
services: RenderServices,
project?: ProjectContext,
project: ProjectContext,
) {
// See if we can resolve non-notebooks. Note that this
// requires that we have pre-rendered any notebooks that we discover
Expand Down
Loading
Loading