## TL;DR

TOML forbids defining the same key twice, and wrangler rejects the whole file when it finds one. The usual culprit is a copy-pasted section like a second `[vars]` block or two `[env.staging]` tables. Find the repeated key, merge the two blocks into one, and validate the file parses before deploying.

```text
Error: invalid wrangler.toml: duplicate top-level key "vars"
```

## Steps

1. List every top-level key and section header in the file and look for repeats:
```sh
grep -n '^\[' wrangler.toml
grep -n '^[a-z_]* =' wrangler.toml
```
Expected: each section header and each top-level key appears exactly once. Any line number pair showing the same name twice is the offender.

2. Open the file around those line numbers and merge the duplicates into a single block. Keep all the settings, just under one header.
Expected: one `[vars]` block, one `compatibility_date` line, one table per environment name.

3. Validate the file parses as TOML:
```sh
python3 -c "import tomllib; tomllib.load(open('wrangler.toml','rb')); print('valid')"
```
Expected: the script prints "valid". If it errors, read the message; it names the offending key and line.

4. Deploy:
```sh
wrangler deploy
```
Expected: config validation passes and the deploy proceeds.

## Use this when
- Wrangler reports "invalid wrangler.toml" with a duplicate key
- The error appeared right after hand-editing or merging the config
- Two environment blocks share a name after a copy-paste

## Not for this skill when
- The config is wrangler.jsonc; the duplicate idea is the same but the syntax and validator differ
- The error is about an unknown or misspelled key rather than a duplicate
- The deploy fails on bindings or auth, not on config parsing

## Variant phrasings
- wrangler.toml duplicate key error
- invalid wrangler.toml duplicate top-level key vars
- wrangler config duplicate table header

## Why it happens

TOML is strict: a key or table defined twice is a parse error, not a merge. It happens most often when environment sections are duplicated during copy-paste, when two people edit the file in a merge, or when a `[vars]` block gets added at the bottom while one already exists near the top.

## Edge cases
- Wrangler does not support config inheritance or includes; do not try to work around the duplicate by splitting the file.
- A duplicate inside an `[env.name]` table (two `vars` under the same env) fails the same way; check env-scoped sections too.
- Comments and blank lines never cause this; only real keys and headers count.
- If the file is generated by a script, fix the generator or the duplicate comes back on the next run.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_dMxxd1Vy7GhSKa42H4F_Ww
