Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/quiet-foxes-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: `wrangler vectorize list --json` and `wrangler vectorize list-metadata-index --json` now output valid json without extra log lines
52 changes: 52 additions & 0 deletions packages/wrangler/src/__tests__/vectorize/vectorize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,36 @@ describe("vectorize commands", () => {
`);
});


it("should handle listing vectorize indexes with valid JSON output", async () => {
mockVectorizeV2Request();
await runWrangler("vectorize list --json");
expect(JSON.parse(std.out)).toMatchInlineSnapshot(`
[
{
"config": {
"dimensions": 1536,
"metric": "euclidean",
},
"created_on": "2024-07-11T13:02:18.00268Z",
"description": "test-desc",
"modified_on": "2024-07-11T13:02:18.00268Z",
"name": "test-index",
},
{
"config": {
"dimensions": 32,
"metric": "dot-product",
},
"created_on": "2024-07-11T13:02:18.00268Z",
"description": "another-desc",
"modified_on": "2024-07-11T13:02:18.00268Z",
"name": "another-index",
},
]
`);
});

it("should handle a get on a vectorize V1 index", async () => {
mockVectorizeRequest();
await runWrangler("vectorize get test-index --deprecated-v1=true");
Expand Down Expand Up @@ -879,6 +909,28 @@ describe("vectorize commands", () => {
`);
});


it("should handle list-metadata-index with valid JSON output", async () => {
mockVectorizeV2Request();
await runWrangler("vectorize list-metadata-index test-index --json");
expect(JSON.parse(std.out)).toMatchInlineSnapshot(`
[
{
"indexType": "string",
"propertyName": "string-prop",
},
{
"indexType": "number",
"propertyName": "num-prop",
},
{
"indexType": "boolean",
"propertyName": "bool-prop",
},
]
`);
});

it("should handle delete metadata index", async () => {
mockVectorizeV2Request();
await runWrangler(
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/vectorize/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const vectorizeListCommand = createCommand({
},
},
async handler(args, { config }) {
logger.log(`πŸ“‹ Listing Vectorize indexes...`);
if (!args.json) {
logger.log(`πŸ“‹ Listing Vectorize indexes...`);
}
const indexes = await listIndexes(config, args.deprecatedV1);

if (indexes.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ”΄ Empty index list with --json flag outputs no JSON instead of []

When wrangler vectorize list --json is called and there are no indexes, the empty-check at packages/wrangler/src/vectorize/list.ts:33 runs before the JSON output at line 43. This causes the handler to log a warning via logger.warn() and return early without ever outputting JSON. A caller expecting valid JSON on stdout will get an empty string, which is not valid JSON. The fix should move the args.json check before the empty-length check, or output [] for the empty case when --json is set.

(Refers to lines 33-41)

Open in Devin Review

Was this helpful? React with πŸ‘ or πŸ‘Ž to provide feedback.

Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/vectorize/listMetadataIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const vectorizeListMetadataIndexCommand = createCommand({
},
positionalArgs: ["name"],
async handler(args, { config }) {
logger.log(`πŸ“‹ Fetching metadata indexes...`);
if (!args.json) {
logger.log(`πŸ“‹ Fetching metadata indexes...`);
}
const res = await listMetadataIndex(config, args.name);

if (res.metadataIndexes.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ”΄ Empty metadata index list with --json flag outputs no JSON instead of []

When wrangler vectorize list-metadata-index <name> --json is called and there are no metadata indexes, the empty-check at packages/wrangler/src/vectorize/listMetadataIndex.ts:34 runs before the JSON output at line 44. This causes the handler to log a warning via logger.warn() and return early without ever outputting JSON. A caller expecting valid JSON on stdout will get an empty string, which is not valid JSON. The fix should move the args.json check before the empty-length check, or output [] for the empty case when --json is set.

(Refers to lines 34-42)

Open in Devin Review

Was this helpful? React with πŸ‘ or πŸ‘Ž to provide feedback.

Expand Down