From 1007b46fcc0e606e683deb2dd36427efc6bcb87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobi=20L=C3=BCtke?= Date: Tue, 17 Feb 2026 10:00:16 -0500 Subject: [PATCH] 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. --- src/qmd.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/qmd.ts b/src/qmd.ts index 244578f..c049c85 100755 --- a/src/qmd.ts +++ b/src/qmd.ts @@ -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; }