From 00a44fc8463bc562f5d58e04befa84d943d9e45e Mon Sep 17 00:00:00 2001 From: Zach Shallbetter Date: Mon, 23 Nov 2026 23:46:37 -0800 Subject: [PATCH] fix: report Wrangler config parse locations --- .changeset/migration-config-location.md | 8 ++++++ .../src/__tests__/commands/migrate.test.ts | 25 +++++++++++++++++++ packages/cli/src/lib/wrangler-migration.ts | 21 ++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 .changeset/migration-config-location.md diff --git a/.changeset/migration-config-location.md b/.changeset/migration-config-location.md new file mode 100644 index 0000000000..63764ffc7b --- /dev/null +++ b/.changeset/migration-config-location.md @@ -0,0 +1,8 @@ +--- +"cf": patch +--- + +Show where a Wrangler config fails to parse + +Include the filename, line, and column in the migration error so users +can go directly to the syntax problem. diff --git a/packages/cli/src/__tests__/commands/migrate.test.ts b/packages/cli/src/__tests__/commands/migrate.test.ts index 95f72cfd63..552f29fde6 100644 --- a/packages/cli/src/__tests__/commands/migrate.test.ts +++ b/packages/cli/src/__tests__/commands/migrate.test.ts @@ -1,3 +1,4 @@ +import { readFile, stat } from "node:fs/promises"; import path from "node:path"; import { runInTempDir, seed } from "@cloudflare/workers-utils/test-helpers"; import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; @@ -7,6 +8,7 @@ import { } from "../../lib/wrangler-migration.js"; import { mockOutputStreams } from "../helpers/capture-output.js"; import { runCf } from "../helpers/run-cf.js"; +import type * as Codemods from "@cloudflare/codemods"; const { migrateWranglerToCf } = vi.hoisted(() => ({ migrateWranglerToCf: vi.fn(), @@ -150,6 +152,29 @@ describe("cf migrate", () => { expect(std.err).toContain("Migration preview complete."); }); + it("reports the location of malformed Wrangler JSONC", async () => { + const file = "config/wrangler.jsonc"; + const contents = '{\n "name": '; + await seed({ [file]: contents }); + const actual = await vi.importActual( + "@cloudflare/codemods" + ); + migrateWranglerToCf.mockImplementationOnce(actual.migrateWranglerToCf); + + const location = `${path.normalize(file)}:2:11: ValueExpected`; + await expect( + runCf(["migrate", file, "--dry-run", "--bundler", "vite"]) + ).rejects.toThrow(location); + + expect(std.err).toContain(location); + expect(std.out).toBe(""); + expect(await readFile(file, "utf8")).toBe(contents); + await expect(stat("config/cloudflare.config.ts")).rejects.toHaveProperty( + "code", + "ENOENT" + ); + }); + it("requires an explicit path when no Wrangler config is found", async () => { await expect(runCf(["migrate"])).rejects.toThrow( `No Wrangler config found in ${process.cwd()}. Pass its path to cf migrate.` diff --git a/packages/cli/src/lib/wrangler-migration.ts b/packages/cli/src/lib/wrangler-migration.ts index a65ce58198..7298f61318 100644 --- a/packages/cli/src/lib/wrangler-migration.ts +++ b/packages/cli/src/lib/wrangler-migration.ts @@ -180,6 +180,27 @@ export async function runWranglerMigration( dryRun, force: options.force ?? false, installDependencies: options.installDependencies ?? true, + }).catch((error: unknown) => { + if (error instanceof Error && "location" in error) { + const { location } = error; + if ( + typeof location === "object" && + location !== null && + "file" in location && + typeof location.file === "string" && + "line" in location && + typeof location.line === "number" && + "column" in location && + typeof location.column === "number" + ) { + const file = path.relative(process.cwd(), location.file); + throw new Error( + `${file}:${location.line}:${location.column + 1}: ${error.message}`, + { cause: error } + ); + } + } + throw error; }); printResult(