Skip to content

Commit 9bd7889

Browse files
committed
Error when listings don’t match any files
Fixes #5463
1 parent c66a75b commit 9bd7889

2 files changed

Lines changed: 66 additions & 55 deletions

File tree

‎news/changelog-1.4.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
- ([#6777](https://github.com/quarto-dev/quarto-cli/issues/6777)): Add support for complex fields like `citaiton.container-title` when includes custom fields in listings.
8282
- ([#6706](https://github.com/quarto-dev/quarto-cli/issues/6706)): If there is no text displayed for external links in navigation items (navbar, sidebar), display the href value as the text.
8383
- ([#6903](https://github.com/quarto-dev/quarto-cli/issues/6903)): Don't display the `path` field for external paths provided in metadata files.
84+
- ([#5463](https://github.com/quarto-dev/quarto-cli/issues/5463)): Error if the `contents` of a listing match no items.
8485

8586
## Websites
8687

‎src/project/types/website/listing/website-listing-read.ts‎

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -743,66 +743,76 @@ async function readContents(
743743
const files = filterListingFiles(contentGlobs);
744744
debug(`[listing] matches ${files.include.length} files:`);
745745

746-
for (const file of files.include) {
747-
if (!files.exclude.includes(file)) {
748-
if (isYamlPath(file)) {
749-
debug(`[listing] Reading YAML file ${file}`);
750-
const yaml = readYaml(file);
751-
if (Array.isArray(yaml)) {
752-
const items = yaml as Array<unknown>;
753-
for (const yamlItem of items) {
754-
if (typeof (yamlItem) === "object") {
755-
const { item, source } = await listItemFromMeta(
756-
yamlItem as Metadata,
757-
project,
758-
listing,
759-
dirname(file),
760-
);
761-
validateItem(listing, item, (field: string) => {
762-
return `An item from the file '${file}' is missing the required field '${field}'.`;
763-
});
764-
listingItemSources.add(source);
765-
listingItems.push(item);
766-
} else {
767-
throw new Error(
768-
`Unexpected listing contents in file ${file}. The array may only contain listing items, not paths or other types of data.`,
769-
);
770-
}
746+
const readFiles = files.include.filter((file) => {
747+
return !files.exclude.includes(file);
748+
});
749+
if (readFiles.length === 0) {
750+
const projRelativePath = relative(project.dir, source);
751+
throw new Error(
752+
`The listing in '${projRelativePath}' using the following contents:\n- ${
753+
contentGlobs.join("\n- ")
754+
}\ndoesn't match any files or folders.`,
755+
);
756+
}
757+
758+
for (const file of readFiles) {
759+
if (isYamlPath(file)) {
760+
debug(`[listing] Reading YAML file ${file}`);
761+
const yaml = readYaml(file);
762+
if (Array.isArray(yaml)) {
763+
const items = yaml as Array<unknown>;
764+
for (const yamlItem of items) {
765+
if (typeof (yamlItem) === "object") {
766+
const { item, source } = await listItemFromMeta(
767+
yamlItem as Metadata,
768+
project,
769+
listing,
770+
dirname(file),
771+
);
772+
validateItem(listing, item, (field: string) => {
773+
return `An item from the file '${file}' is missing the required field '${field}'.`;
774+
});
775+
listingItemSources.add(source);
776+
listingItems.push(item);
777+
} else {
778+
throw new Error(
779+
`Unexpected listing contents in file ${file}. The array may only contain listing items, not paths or other types of data.`,
780+
);
771781
}
772-
} else if (typeof (yaml) === "object") {
773-
const { item, source } = await listItemFromMeta(
774-
yaml as Metadata,
775-
project,
776-
listing,
777-
dirname(file),
778-
);
779-
validateItem(listing, item, (field: string) => {
780-
return `The item defined in file '${file}' is missing the required field '${field}'.`;
781-
});
782-
listingItemSources.add(source);
783-
listingItems.push(item);
784-
} else {
785-
throw new Error(
786-
`Unexpected listing contents in file ${file}. The file should contain only one more listing items.`,
787-
);
788782
}
783+
} else if (typeof (yaml) === "object") {
784+
const { item, source } = await listItemFromMeta(
785+
yaml as Metadata,
786+
project,
787+
listing,
788+
dirname(file),
789+
);
790+
validateItem(listing, item, (field: string) => {
791+
return `The item defined in file '${file}' is missing the required field '${field}'.`;
792+
});
793+
listingItemSources.add(source);
794+
listingItems.push(item);
789795
} else {
790-
const isFile = Deno.statSync(file).isFile;
791-
if (isFile) {
792-
debug(`[listing] Reading file ${file}`);
793-
const item = await listItemFromFile(file, project, listing);
794-
if (item) {
795-
validateItem(listing, item, (field: string) => {
796-
return `The file ${file} is missing the required field '${field}'.`;
797-
});
798-
799-
if (item.item.title === undefined) {
800-
debug(`[listing] Missing Title in File ${file}`);
801-
}
796+
throw new Error(
797+
`Unexpected listing contents in file ${file}. The file should contain only one more listing items.`,
798+
);
799+
}
800+
} else {
801+
const isFile = Deno.statSync(file).isFile;
802+
if (isFile) {
803+
debug(`[listing] Reading file ${file}`);
804+
const item = await listItemFromFile(file, project, listing);
805+
if (item) {
806+
validateItem(listing, item, (field: string) => {
807+
return `The file ${file} is missing the required field '${field}'.`;
808+
});
802809

803-
listingItemSources.add(item.source);
804-
listingItems.push(item.item);
810+
if (item.item.title === undefined) {
811+
debug(`[listing] Missing Title in File ${file}`);
805812
}
813+
814+
listingItemSources.add(item.source);
815+
listingItems.push(item.item);
806816
}
807817
}
808818
}

0 commit comments

Comments
 (0)