Skip to content
Closed
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
8 changes: 8 additions & 0 deletions .changeset/migration-config-location.md
Original file line number Diff line number Diff line change
@@ -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.
25 changes: 25 additions & 0 deletions packages/cli/src/__tests__/commands/migrate.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(),
Expand Down Expand Up @@ -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<typeof Codemods>(
"@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.`
Expand Down
21 changes: 21 additions & 0 deletions packages/cli/src/lib/wrangler-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down