Skip to content

Commit f467415

Browse files
authored
[vitest-pool-workers] Fix Workflow binding's script_name when it matches its own Worker name (#10033)
* fix: make vitest-pool-workers not break if Workflow binding's script_name matches its own Worker name removing try catch since error gets thrown either way use userConfigPath instead of redirected one (#10005) Revert "use userConfigPath instead of redirected one (#10005)" This reverts commit 5c11fe7. fix deep import prettier run fix: make vitest-pool-workers not break if Workflow binding's script_name matches its own Worker name * apply suggestions from code review * code review suggestion: delete script_name instead of replacing it
1 parent 26ffa05 commit f467415

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

.changeset/spotty-pants-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vitest-pool-workers": patch
3+
---
4+
5+
Make vitest-pool-workers not break when a Workflow binding script_name matches its Worker name

packages/vitest-pool-workers/src/pool/index.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "miniflare";
2424
import semverSatisfies from "semver/functions/satisfies.js";
2525
import { createMethodsRPC } from "vitest/node";
26+
import { experimental_readRawConfig } from "wrangler";
2627
import { workerdBuiltinModules } from "../shared/builtin-modules";
2728
import { createChunkingSocket } from "../shared/chunking-socket";
2829
import { CompatibilityFlagAssertions } from "./compatibility-flag-assertions";
@@ -328,8 +329,36 @@ function fixupDurableObjectBindingsToSelf(
328329
return result;
329330
}
330331

332+
function getWranglerWorkerName(
333+
relativeWranglerConfigPath?: string
334+
): string | undefined {
335+
if (!relativeWranglerConfigPath) {
336+
return undefined;
337+
}
338+
const wranglerConfigObject = experimental_readRawConfig({
339+
config: relativeWranglerConfigPath,
340+
});
341+
return wranglerConfigObject.rawConfig.name;
342+
}
343+
344+
function updateWorkflowsScriptNames(
345+
runnerWorker: WorkerOptions,
346+
wranglerWorkerName: string | undefined
347+
): void {
348+
const workflows = runnerWorker.workflows;
349+
if (!workflows || wranglerWorkerName === undefined) {
350+
return;
351+
}
352+
for (const workflow of Object.values(workflows)) {
353+
if (workflow.scriptName === wranglerWorkerName) {
354+
delete workflow.scriptName;
355+
}
356+
}
357+
}
358+
331359
function fixupWorkflowBindingsToSelf(
332-
worker: SourcelessWorkerOptions
360+
worker: SourcelessWorkerOptions,
361+
relativeWranglerConfigPath: string | undefined
333362
): Set<string> {
334363
// TODO(someday): may need to extend this to take into account other workers
335364
// if doing multi-worker tests across workspace projects
@@ -340,8 +369,21 @@ function fixupWorkflowBindingsToSelf(
340369
}
341370
for (const key of Object.keys(worker.workflows)) {
342371
const designator = worker.workflows[key];
372+
373+
let workerName: string | undefined;
374+
// If the designator's scriptName matches its own Worker name,
375+
// use that as the worker name, otherwise use the vitest worker's name
376+
const wranglerWorkerName = getWranglerWorkerName(
377+
relativeWranglerConfigPath
378+
);
379+
if (wranglerWorkerName && designator.scriptName === wranglerWorkerName) {
380+
workerName = wranglerWorkerName;
381+
} else {
382+
workerName = worker.name;
383+
}
384+
343385
// `designator` hasn't been validated at this point
344-
if (isWorkflowDesignatorToSelf(designator, worker.name)) {
386+
if (isWorkflowDesignatorToSelf(designator, workerName)) {
345387
result.add(designator.className);
346388
// Shallow clone to avoid mutating config
347389
worker.workflows[key] = {
@@ -448,7 +490,7 @@ function buildProjectWorkerOptions(
448490
fixupDurableObjectBindingsToSelf(runnerWorker)
449491
).sort();
450492
const workflowClassNames = Array.from(
451-
fixupWorkflowBindingsToSelf(runnerWorker)
493+
fixupWorkflowBindingsToSelf(runnerWorker, relativeWranglerConfigPath)
452494
).sort();
453495

454496
if (
@@ -634,6 +676,13 @@ function buildProjectMiniflareOptions(
634676
// --> single instance with single runner worker
635677
// Multiple Workers, Isolated Storage:
636678
// --> multiple instances each with single runner worker
679+
680+
// Set Workflows scriptName to the runner worker name if it matches the Wrangler worker name
681+
const wranglerWorkerName = getWranglerWorkerName(
682+
project.options.wrangler?.configPath
683+
);
684+
updateWorkflowsScriptNames(runnerWorker, wranglerWorkerName);
685+
637686
return {
638687
...SHARED_MINIFLARE_OPTIONS,
639688
inspectorPort,
@@ -653,6 +702,12 @@ function buildProjectMiniflareOptions(
653702
testWorker.bindings = { ...testWorker.bindings };
654703
testWorker.bindings[SELF_NAME_BINDING] = testWorker.name;
655704

705+
// Set Workflows scriptName to the test worker name if it matches the Wrangler worker name
706+
const wranglerWorkerName = getWranglerWorkerName(
707+
project.options.wrangler?.configPath
708+
);
709+
updateWorkflowsScriptNames(testWorker, wranglerWorkerName);
710+
656711
testWorkers.push(testWorker);
657712
}
658713
return {

0 commit comments

Comments
 (0)