Skip to content

Prompt for Worker name interactively when not provided during wrangler deploy#13699

Draft
dario-piotrowicz wants to merge 1 commit intomainfrom
dario/wrangler-deploy-prompt-for-name
Draft

Prompt for Worker name interactively when not provided during wrangler deploy#13699
dario-piotrowicz wants to merge 1 commit intomainfrom
dario/wrangler-deploy-prompt-for-name

Conversation

@dario-piotrowicz
Copy link
Copy Markdown
Member

When deploying without a name in your Wrangler config or --name CLI argument, wrangler deploy now interactively prompts you to choose a Worker name instead of failing. The default suggestion is derived from the entry-point filename (e.g. my-api.tsmy-api), falling back to the current directory name when the entry-point has a generic name like index.js or worker.ts. Input is validated against Cloudflare's Worker naming rules (lowercase alphanumeric and dashes, max 63 characters). In non-interactive or CI environments, the existing error message is still shown.


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: self-explanatory improvement

A picture of a cute animal (not mandatory, but encouraged)

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 27, 2026

🦋 Changeset detected

Latest commit: d185565

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Copy Markdown
Contributor

✅ All changesets look good

Review Summary

File reviewed: .changeset/interactive-worker-name-prompt.md

Validation Results

Check Status Notes
Version Type minor is appropriate for this new interactive feature
Changelog Quality Excellent description explaining the feature, default behavior, validation rules, and CI handling
Markdown Headers No h1/h2/h3 headers present
Analytics N/A - no analytics collection mentioned
Dependabot N/A - not a dependency update
Experimental Features N/A - this is a direct UX improvement to wrangler deploy

Description Quality

The changeset provides a comprehensive description that includes:

  • Clear explanation of the new interactive prompting behavior
  • Default suggestion logic (entry-point filename → directory name fallback)
  • Input validation rules (lowercase alphanumeric and dashes, max 63 chars)
  • Behavior in non-interactive/CI environments (preserves existing error)

@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk Bot commented Apr 27, 2026

I see the inconsistency: lines 23 and 30 say "Worker names" but lines 38 and 45 say "Project names". However, this was pre-existing in the old code. Not something introduced by this PR.

Now let me look at one potentially real issue — the validate callback trims the value before checking, but the regex allows spaces. So "my worker" would trim to "my worker" (no change since spaces are internal, not leading/trailing), and checkWorkerNameValidity("my worker") would return { valid: true } because the regex allows spaces. Then name.trim() returns "my worker" — a name with a space. This would be an invalid worker name that passes validation.

Wait, let me re-examine: the regex is /[^a-z0-9- ]/g. The character class [^a-z0-9- ] includes a space character (between - and ]). So spaces are in the allowed set, meaning checkWorkerNameValidity will NOT flag spaces as invalid.

This is a bug — but it's pre-existing from the old code. The space in the regex was there before this PR. However, this PR is now exposing it to a new codepath (the interactive prompt validation), where users can type spaces and have them accepted. In the old autoconfig flow, the name was derived from directory/project names which are less likely to contain spaces, and went through toValidWorkerName which would also not strip spaces.

Actually, let me think more carefully. Is a Worker name with a space actually invalid per the Cloudflare API? The docs say "lowercase alphanumeric and dashes". If so, the space in the regex is a bug. But since this PR is just moving the code and the regex is unchanged, I'd note it but not block on it.

Let me now focus on what's actually new in this PR and check for real issues:

  1. The promptForWorkerName function — looks solid
  2. The refactoring to extract validation to a shared util — clean
  3. The addition of .toLowerCase() in toValidWorkerName — improvement
  4. Tests — reasonable coverage

Let me check one more thing — the truncate then might end with dash issue:

Consider toValidWorkerName("a".repeat(60) + "---" + "b".repeat(5)):

  • .toLowerCase() → no change
  • .replaceAll("_", "-") → no change
  • .replace(invalidWorkerNameCharsRegex, "-") → no change
  • .replace(invalidWorkerNameStartEndRegex, "") → no change (doesn't start/end with dash)
  • .slice(0, 63)"aaa...aaa---" (60 a's + 3 dashes = 63 chars, ending with dashes!)

This would return a name ending with dashes. But again, this is pre-existing.

Let me focus on the one actionable issue I see that's introduced by this PR: the validate callback in promptForWorkerName trims the value, but checkWorkerNameValidity allows spaces (due to the pre-existing regex). This means a user could enter "my worker" at the prompt, validation passes (spaces allowed by regex), trim doesn't remove internal spaces, and the name "my worker" gets used — which will fail at the Cloudflare API.

Actually wait — I need to be more precise. The space is allowed by the regex. But would Cloudflare's API reject it? Let me check the Cloudflare docs reference in the comment. The regex [^a-z0-9- ] means "not lowercase, not digit, not dash, not space". So the space is intentionally allowed? That seems wrong for a Worker name.

But this is pre-existing code. The PR just moved it. I shouldn't flag pre-existing issues unless they're made worse.

Is there anything new the PR introduces that's problematic?

One thing: the validate does value.trim() but the return name.trim() at the end only trims leading/trailing whitespace. If a user enters " my-worker ", validate passes (after trim → "my-worker"), but the returned value is "my-worker" (trimmed). That's fine.

But if a user enters "my worker" (space in the middle), validate trims to "my worker", checkWorkerNameValidity("my worker") returns valid (space in regex), and the returned name is "my worker". This is a problem, but it's because of the pre-existing regex bug.

Let me check if there are any actual issues I should flag. Let me re-read the full diff one more time carefully...

Looking at deploy/index.ts line 567:

const rawDefault = !genericNames.includes(scriptBasename.toLowerCase())
    ? scriptBasename
    : path.basename(process.cwd());

The genericNames check is case-insensitive (scriptBasename.toLowerCase()), but the list only has lowercase entries. So Worker.tsscriptBasename = "Worker".toLowerCase() = "worker" → matches "worker" → falls back to cwd. Good.

But src.ts or main.ts are also fairly generic names that might not be great Worker names. However, that's a product decision, not a bug.

I think this PR is clean. The code change is logical, the tests cover the key scenarios, the refactoring is straightforward. The pre-existing issues (space in regex, inconsistent error messages) are not introduced by this PR.

LGTM

github run

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 27, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@13699

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@13699

miniflare

npm i https://pkg.pr.new/miniflare@13699

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@13699

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@13699

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@13699

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@13699

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@13699

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@13699

wrangler

npm i https://pkg.pr.new/wrangler@13699

commit: d185565

@dario-piotrowicz dario-piotrowicz marked this pull request as ready for review April 27, 2026 13:47
@workers-devprod workers-devprod requested review from a team and ascorbic and removed request for a team April 27, 2026 13:48
@workers-devprod
Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • packages/wrangler/src/tests/deploy/core.test.ts: [@cloudflare/wrangler]
  • packages/wrangler/src/tests/utils-worker-name-validation.test.ts: [@cloudflare/wrangler]
  • packages/wrangler/src/autoconfig/details/index.ts: [@cloudflare/wrangler]
  • packages/wrangler/src/deploy/index.ts: [@cloudflare/wrangler]
  • packages/wrangler/src/utils/worker-name-validation.ts: [@cloudflare/wrangler]

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review


Prompt for Worker name interactively when not provided during `wrangler deploy`

When deploying without a `name` in your Wrangler config or `--name` CLI argument, `wrangler deploy` now interactively prompts you to choose a Worker name instead of failing. The default suggestion is derived from the entry-point filename (e.g. `my-api.ts` → `my-api`), falling back to the current directory name when the entry-point has a generic name like `index.js` or `worker.ts`. Input is validated against Cloudflare's Worker naming rules (lowercase alphanumeric and dashes, max 63 characters). In non-interactive or CI environments, the existing error message is still shown.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if we should simplify the default suggestion to always use the current directory name.

Deriving it from the entrypoint filename could be confusing because it's hard to define what counts as a generic name here. Some users might reasonably expect api.js or server.ts to be considered generic too.

The current directory name feels simpler and more predictable as a default. 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

mh... but I was wondering... what if the user has a bunch of different worker entrypoint files in the same directory? 🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, that could happen. I'm not sure it's something we need to optimize the default for though, as I would still expect most people uses a monorepo setup if they have multiple workers.

But I don't feel strongly here, so happy to leave it up to you. :)

Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin Apr 28, 2026

Choose a reason for hiding this comment

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

How about if you run:

cd my-project
wrangler deploy src/blah-worker.js

It could say:

> Please choose a worker name:
- `my-project` (current working directory name)
- `src` (entry-point containing directory name)
- `blah-worker` (entry-point file name)
- enter your preferred name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

then the user could just select which they wanted to use

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The current solution uses the file or directory name as the default value, but the user can still type and use whatever name they prefer, I feel like giving them a list to chose from might be going a bit too far 🤔

{ telemetryMessage: true }
);
} else {
name = await promptForWorkerName(entry.file);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we create / update the wrangler config with the name they selected? Just so they don't run into this prompt again next time and accidentally uses a different name.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think that if there is a config file, it should error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

mh... yes... I think it does error since wrangler should fail at the config validation and not reach this point... I'll have a look to make sure that's the case 🤔

const defaultName = toValidWorkerName(rawDefault);

const name = await prompt(
"What would you like your Worker to be named? (Note: You can also set the name via the `--name` CLI argument or `name` in your Wrangler config file)",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need the note in the prompt text? It makes the question quite long, and at this point the user mostly needs to answer what name they want.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I agree that it makes the question too long! 😓

But it is still useful info... I guess we could present it after the question has been answered, would that work for you?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure if I follow. Why would the user cancel the prompt and use the flag instead? 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

mh.... no mostly, regardless on whether the user accepts or cancels, it's good to tell them that they can set it in their config or with the cli flag, so that next time they can use that and not get this prompt again

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see. I guess that's why I was suggesting if we can generate a wrangler config with the name they selected. If we are not doing that, this makes sense :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I see. I guess that's why I was suggesting if we can generate a wrangler config with the name they selected.

The idea here is not to try to force users to necessarily have a config file, basically this is mainly trying to satisfy users that just want to run wrangler deploy against a single plain file without needing any extra configuration or flags

Comment on lines +17 to +50
export function checkWorkerNameValidity(
input: string
): { valid: false; cause: string } | { valid: true } {
if (!input) {
return {
valid: false,
cause: "Worker names cannot be empty.",
};
}

if (input.match(invalidWorkerNameStartEndRegex)) {
return {
valid: false,
cause: "Worker names cannot start or end with a dash.",
};
}

if (input.match(invalidWorkerNameCharsRegex)) {
return {
valid: false,
cause:
"Project names must only contain lowercase characters, numbers, and dashes.",
};
}

if (input.length > workerNameLengthLimit) {
return {
valid: false,
cause: "Project names must be less than 63 characters.",
};
}

return { valid: true };
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be possible to reuse or share logic with the existing name validation in packages/workers-utils/src/config/validation-helpers.ts? I'd prefer to keep the prompt validation pretty minimal and avoid adding another slightly different version of Worker name validation.

@emily-shen
Copy link
Copy Markdown
Contributor

copying over a comment from #13701 - we should consolidate this with existing logic in handleMaybeAssetsDeployment

@dario-piotrowicz
Copy link
Copy Markdown
Member Author

converting to draft as I might just wait to merge #13701 first 🙂

@dario-piotrowicz dario-piotrowicz marked this pull request as draft April 28, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

5 participants