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
7 changes: 7 additions & 0 deletions .changeset/sour-rules-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

Fix `wrangler types` resolution for env-qualified service entrypoints

`wrangler types` now resolves named `services` entrypoints back to the secondary Worker's source module when the secondary config uses an environment-specific `name` override. This restores strongly typed `Service<typeof import(...).Entrypoint>` output for multi-config type generation instead of falling back to an unresolved `Service` comment.
93 changes: 93 additions & 0 deletions packages/wrangler/src/__tests__/type-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,99 @@ describe("generate types", () => {
`);
});

it("should resolve named service entrypoints when env overrides the worker name", async ({
expect,
}) => {
fs.mkdirSync("primary");
fs.mkdirSync("secondary");

fs.writeFileSync(
"./secondary/index.ts",
`import { WorkerEntrypoint } from 'cloudflare:workers';
export default { async fetch() {} };
export class SomeEntrypoint extends WorkerEntrypoint {}
`
);
fs.writeFileSync(
"./secondary/wrangler.jsonc",
JSON.stringify({
compatibility_date: "2022-01-12",
name: "secondary-worker",
main: "./index.ts",
env: {
staging: {
name: "secondary-worker-staging",
},
},
}),
"utf-8"
);

fs.writeFileSync(
"./primary/index.ts",
"export default { async fetch() {} };"
);
fs.writeFileSync(
"./primary/wrangler.jsonc",
JSON.stringify({
compatibility_date: "2022-01-12",
name: "primary-worker",
main: "./index.ts",
services: [
{
binding: "SERVICE_A",
service: "secondary-worker",
entrypoint: "SomeEntrypoint",
},
],
env: {
staging: {
name: "primary-worker-staging",
services: [
{
binding: "SERVICE_A",
service: "secondary-worker-staging",
entrypoint: "SomeEntrypoint",
},
],
},
},
}),
"utf-8"
);

await runWrangler(
"types --include-runtime=false -c primary/wrangler.jsonc -c secondary/wrangler.jsonc --path primary/worker-configuration.d.ts"
);

expect(std.out).toMatchInlineSnapshot(`
"
⛅️ wrangler x.x.x
──────────────────
- Found Worker 'secondary-worker' at 'secondary/index.ts' (secondary/wrangler.jsonc)
Generating project types...

declare namespace Cloudflare {
interface GlobalProps {
mainModule: typeof import("./index");
}
interface StagingEnv {
SERVICE_A: Service<typeof import("../secondary/index").SomeEntrypoint>;
}
interface Env {
SERVICE_A: Service<typeof import("../secondary/index").SomeEntrypoint>;
}
}
interface Env extends Cloudflare.Env {}

────────────────────────────────────────────────────────────
✨ Types written to primary/worker-configuration.d.ts

📣 Remember to rerun 'wrangler types' after you change your wrangler.jsonc file.
"
`);
});

it("should create a DTS file at the location that the command is executed from", async ({
expect,
}) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/wrangler/src/type-generation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,10 @@ export const typesCommand = createCommand({
config: secondaryConfig.configPath,
env: envName,
});
const envKey = envConfig.name;
const envEntry = await getEntry({}, envConfig, "types");
const envKey = envEntry.name;
if (envKey && envKey !== key && !secondaryEntries.has(envKey)) {
secondaryEntries.set(envKey, serviceEntry);
secondaryEntries.set(envKey, envEntry);
}
}
} else {
Expand Down
Loading