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

Bugfix/9253 #9254

Merged
merged 2 commits into from
Apr 2, 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
21 changes: 20 additions & 1 deletion src/core/deno/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
* Debugging utilities.
*/

export const getStackAsArray = (offset?: number) => {
return (new Error().stack ?? "").split("\n").slice(offset ?? 2);
};

export const getStack = (offset?: number) => {
return "Stack:\n" +
(new Error().stack ?? "").split("\n").slice(offset ?? 2).join("\n");
getStackAsArray(offset ? offset + 1 : 3).join("\n");
};

// use debugPrint instead of console.log so it's easy to find stray print statements
Expand All @@ -18,3 +22,18 @@ export const getStack = (offset?: number) => {
export const debugPrint = (...data: any[]) => {
console.log(...data);
};

export const debugLogWithStack = async (...data: unknown[]) => {
const payload = {
payload: data,
stack: getStackAsArray(),
timestamp: new Date().toISOString(),
};
await Deno.writeTextFile(
"/tmp/stack-debug.json",
JSON.stringify(payload) + "\n",
{
append: true,
},
);
};
11 changes: 7 additions & 4 deletions src/core/handlers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {
HandlerContextResults,
IncludeState,
LanguageCellHandlerContext,
LanguageCellHandlerOptions,
LanguageHandler,
Expand Down Expand Up @@ -341,11 +342,13 @@ const processMarkdownIncludes = async (
includeHandler.context.options.state = {};
}
if (!includeHandler.context.options.state.include) {
includeHandler.context.options.state.include = {};
includeHandler.context.options.state.include = {
includes: [],
};
}
const includeState: Record<string, string> = includeHandler.context.options
const includeState = includeHandler.context.options
.state
.include as Record<string, string>;
.include as IncludeState;

// search for include shortcodes in the cell content
for (let i = 0; i < newCells.length; ++i) {
Expand All @@ -361,7 +364,7 @@ const processMarkdownIncludes = async (
throw new Error("Include directive needs filename as a parameter");
}
if (filename) {
includeState[filename] = param;
includeState.includes.push({ source: filename, target: param });
}
lines[j] = await standaloneInclude(includeHandler.context, param);
}
Expand Down
11 changes: 6 additions & 5 deletions src/core/handlers/include-standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (C) 2023 Posit Software, PBC
*/

import { LanguageCellHandlerContext } from "./types.ts";
import { IncludeState, LanguageCellHandlerContext } from "./types.ts";

import {
asMappedString,
Expand All @@ -29,10 +29,11 @@ export const standaloneInclude = async (
handlerContext.options.state = {};
}
if (!handlerContext.options.state.include) {
handlerContext.options.state.include = {};
handlerContext.options.state.include = {
includes: [],
};
}
const includeState: Record<string, string> = handlerContext.options.state
.include as Record<string, string>;
const includeState = handlerContext.options.state.include as IncludeState;

const retrieveInclude = async (filename: string) => {
const path = handlerContext.resolvePath(filename);
Expand Down Expand Up @@ -78,7 +79,7 @@ export const standaloneInclude = async (
throw new Error("Include directive needs file parameter");
}

includeState[filename] = path;
includeState.includes.push({ source: filename, target: params[0] });
await retrieveInclude(params[0]);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/handlers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "../../config/constants.ts";
import { Format, FormatDependency, FormatExtras } from "../../config/types.ts";
import { PandocIncludes } from "../../execute/types.ts";
import { FileInclusion } from "../../project/types.ts";
import { DirectiveCell, QuartoMdCell } from "../lib/break-quarto-md-types.ts";
import { EitherString, MappedString } from "../lib/text-types.ts";
import { ConcreteSchema } from "../lib/yaml-schema/types.ts";
Expand Down Expand Up @@ -171,3 +172,10 @@ export interface LanguageHandler {
languageName: string;
languageClass?: string | ((options: LanguageCellHandlerOptions) => string);
}

/**
* Type for the state object tracked by the include TS handler
*/
export type IncludeState = {
includes: FileInclusion[];
};
6 changes: 6 additions & 0 deletions src/import_map_additional_entries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"imports": {
"https://deno.land/std@0.196.0/encoding/": "./vendor/deno-land/std@0-196-0/encoding/",
"https://deno.land/std@0.196.0/path/": "./vendor/deno-land/std@0-196-0/path/"
}
}
3 changes: 2 additions & 1 deletion src/project/project-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getFrontMatterSchema } from "../core/lib/yaml-schema/front-matter.ts";
import { normalizePath, pathWithForwardSlashes } from "../core/path.ts";
import { readAndValidateYamlFromFile } from "../core/schema/validated-yaml.ts";
import {
FileInclusion,
FileInformation,
kProjectOutputDir,
kProjectType,
Expand Down Expand Up @@ -373,7 +374,7 @@ export async function projectResolveFullMarkdownForFile(
try {
const result = await expandIncludes(markdown, options, file);
cache.fullMarkdown = result;
cache.includeMap = options.state?.include as Record<string, string>;
cache.includeMap = options.state?.include.includes as FileInclusion[];
return result;
} finally {
temp.cleanup();
Expand Down
10 changes: 7 additions & 3 deletions src/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ export const kProjectResources = "resources";

export const kProjectWatchInputs = "watch-inputs";

export interface FileInformation {
export type FileInclusion = {
source: string;
target: string;
};
export type FileInformation = {
fullMarkdown?: MappedString;
includeMap?: Record<string, string>;
}
includeMap?: FileInclusion[];
};

export interface ProjectContext {
dir: string;
Expand Down
13 changes: 8 additions & 5 deletions src/quarto-core/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
} from "../command/render/resources.ts";
import { kLocalDevelopment, quartoConfig } from "../core/quarto.ts";

import { ProjectConfig, ProjectFiles } from "../project/types.ts";
import {
FileInclusion,
ProjectConfig,
ProjectFiles,
} from "../project/types.ts";
import { cssFileResourceReferences } from "../core/css.ts";
import { projectExcludeDirs } from "../project/project-shared.ts";
import { normalizePath, safeExistsSync } from "../core/path.ts";
Expand All @@ -32,10 +36,9 @@ import { withRenderServices } from "../command/render/render-services.ts";
import { notebookContext } from "../render/notebook/notebook-context.ts";
import { RenderServices } from "../command/render/types.ts";
import { singleFileProjectContext } from "../project/types/single-file/single-file.ts";
import { debugPrint, getStack } from "../core/deno/debug.ts";

export interface FileInspection {
includeMap: Record<string, string>;
includeMap: FileInclusion[];
}

export interface InspectedConfig {
Expand Down Expand Up @@ -94,7 +97,7 @@ export async function inspectConfig(path?: string): Promise<InspectedConfig> {
const engine = await fileExecutionEngine(file, undefined, context);
await context.resolveFullMarkdownForFile(engine, file);
fileInformation[file] = {
includeMap: context.fileInformationCache.get(file)?.includeMap ?? {},
includeMap: context.fileInformationCache.get(file)?.includeMap ?? [],
};
}
const config: InspectedProjectConfig = {
Expand Down Expand Up @@ -191,7 +194,7 @@ export async function inspectConfig(path?: string): Promise<InspectedConfig> {
fileInformation: {
[path]: {
includeMap: context.fileInformationCache.get(path)?.includeMap ??
{},
[],
},
},
};
Expand Down
1 change: 1 addition & 0 deletions tests/docs/websites/issue-9253/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
3 changes: 3 additions & 0 deletions tests/docs/websites/issue-9253/_include.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
An include.

{{< include _include2.qmd >}}
5 changes: 5 additions & 0 deletions tests/docs/websites/issue-9253/_include2.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Another include!

```{python}
print("hello, world")
```
19 changes: 19 additions & 0 deletions tests/docs/websites/issue-9253/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
project:
type: website

website:
title: "Issue-9253"
navbar:
left:
- href: index.qmd
text: Home
- about.qmd

format:
html:
theme: cosmo
css: styles.css
toc: true



5 changes: 5 additions & 0 deletions tests/docs/websites/issue-9253/about.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: "About"
---

About this site
9 changes: 9 additions & 0 deletions tests/docs/websites/issue-9253/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: "Issue-9253"
---

This is a Quarto website.

To learn more about Quarto websites visit <https://quarto.org/docs/websites>.

{{< include _include.qmd >}}
1 change: 1 addition & 0 deletions tests/docs/websites/issue-9253/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* css styles */
7 changes: 6 additions & 1 deletion tests/smoke/inspect/inspect-include.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright (C) 2020-2024 Posit Software, PBC
*
*/
import { assertObjectMatch } from "https://deno.land/std@0.93.0/assert/assert_object_match.ts";
import { existsSync } from "../../../src/deno_ral/fs.ts";
import {
ExecuteOutput,
Expand All @@ -23,7 +24,11 @@ import { assert } from "testing/asserts.ts";
verify: async (outputs: ExecuteOutput[]) => {
assert(existsSync("docs/inspect/foo.json"));
const json = JSON.parse(Deno.readTextFileSync("docs/inspect/foo.json"));
assert(json.fileInformation["docs/inspect/foo.qmd"].includeMap["docs/inspect/foo.qmd"] === "_bar.qmd");
assertObjectMatch(json.fileInformation["docs/inspect/foo.qmd"].includeMap[0],
{
source: input,
target: "_bar.qmd"
});
}
}
],
Expand Down
43 changes: 43 additions & 0 deletions tests/smoke/inspect/inspect-recursive-include.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* inspect-recursive-include.test.ts
*
* Copyright (C) 2020-2024 Posit Software, PBC
*
*/
import { assertObjectMatch } from "https://deno.land/std@0.93.0/assert/assert_object_match.ts";
import { existsSync } from "../../../src/deno_ral/fs.ts";
import { FileInformation } from "../../../src/project/types.ts";
import {
ExecuteOutput,
testQuartoCmd,
} from "../../test.ts";
import { assert, assertEquals } from "testing/asserts.ts";

(() => {
const input = "docs/websites/issue-9253/index.qmd";
const output = "docs/websites/issue-9253/index.json";
testQuartoCmd(
"inspect",
[input, output],
[
{
name: "inspect-include",
verify: async (outputs: ExecuteOutput[]) => {
assert(existsSync(output));
const json = JSON.parse(Deno.readTextFileSync(output));
const info = json.fileInformation["docs/websites/issue-9253/index.qmd"];
const includeMap: FileInformation[] = info.includeMap;
assertObjectMatch(info.includeMap[0], { target: "_include.qmd" });
assertObjectMatch(info.includeMap[1], { source: "_include.qmd", target: "_include2.qmd" });
}
}
],
{
teardown: async () => {
if (existsSync(output)) {
Deno.removeSync(output);
}
}
},
);
})();
Loading