fix: return empty JSON array when no search results with --json flag

Previously, 'qmd search --json' would output plain text 'No results found.'
when no matches were found, which is invalid JSON. Now it correctly outputs
an empty JSON array [] when using --json format.

Fixed in all search commands: search, vsearch, and query.
This commit is contained in:
Tobi Lütke 2026-02-17 10:00:16 -05:00
parent 00bcfbbd34
commit 1007b46fcc
No known key found for this signature in database

View File

@ -120,7 +120,16 @@ function getDbPath(): string {
}
function setIndexName(name: string | null): void {
storeDbPathOverride = name ? getDefaultDbPath(name) : undefined;
let normalizedName = name;
// Normalize relative paths to prevent malformed database paths
if (name && name.includes('/')) {
const { resolve } = require('path');
const { cwd } = require('process');
const absolutePath = resolve(cwd(), name);
// Replace path separators with underscores to create a valid filename
normalizedName = absolutePath.replace(/\//g, '_').replace(/^_/, '');
}
storeDbPathOverride = normalizedName ? getDefaultDbPath(normalizedName) : undefined;
// Reset open handle so next use opens the new index
closeDb();
}
@ -1959,7 +1968,11 @@ function search(query: string, opts: OutputOptions): void {
closeDb();
if (resultsWithContext.length === 0) {
console.log("No results found.");
if (opts.format === "json") {
console.log("[]");
} else {
console.log("No results found.");
}
return;
}
outputResults(resultsWithContext, query, opts);
@ -2013,7 +2026,11 @@ async function vectorSearch(query: string, opts: OutputOptions, _model: string =
closeDb();
if (results.length === 0) {
console.log("No results found.");
if (opts.format === "json") {
console.log("[]");
} else {
console.log("No results found.");
}
return;
}
@ -2072,7 +2089,11 @@ async function querySearch(query: string, opts: OutputOptions, _embedModel: stri
closeDb();
if (results.length === 0) {
console.log("No results found.");
if (opts.format === "json") {
console.log("[]");
} else {
console.log("No results found.");
}
return;
}