Layer reached twice by package name has its config merged twice; `seenLayerDirs` never sees it
Environment
Resolved versions, printed by the reproduction run rather than read from package.json. They are at the top of repro.log in the repo below.
nuxt |
4.5.2 |
nitropack |
2.13.4 |
c12 |
3.3.4 |
defu |
6.1.7 |
node |
v24.20.0 |
| OS | Darwin 24.6.0 |
| Package manager | npm (workspaces) |
Reproduction
npm install
npm run reproIt builds the same app twice, once with each spelling of extends, prints the merged config and the layer list, then exits non-zero if the behaviour below is not observed.
Describe the bug
A layer reached by two paths in the extends graph has its config merged once per path. Every array it declares comes out doubled.
Nuxt already guards against this. seenLayerDirs in @nuxt/kit's layer loader was added in #35712 to fix #34667, which reported this same symptom as "scheduled tasks executed twice if scheduled in a layer". The guard works, but only when the layer is spelled as a path.
Same graph, same versions, only the spelling of extends differs:
extends: ['@repro/layer-a', '@repro/layer-b'] // package names
// nitro.cloudflare.wrangler.triggers.crons -> ['0 0 * * *', '0 0 * * *']
// nitro.scheduledTasks['0 0 * * *'] -> ['someTask', 'someTask']
extends: ['./packages/a', './packages/b'] // relative paths, same graph
// nitro.cloudflare.wrangler.triggers.crons -> ['0 0 * * *']
// nitro.scheduledTasks['0 0 * * *'] -> ['someTask']The cause looks like the existence check that the guard sits behind:
const path = aliased ?? resolve(base, source)
if (!existsSync(path)) return
const layerDir = canonicalLayerDir(path)
if (seenLayerDirs.has(layerDir)) return { config: {}, cwd: layerDir, source }A package specifier resolves to a path that does not exist on disk, so the hook returns before it reaches seenLayerDirs, and c12 goes on to resolve and merge that layer again. Paths and aliases pass the check and are deduplicated. That part is a guess; the contrast above is what we measured.
Nothing looks wrong from the outside. nuxt.options._layers lists the layer exactly once, with a unique cwd, while the merged config carries both copies. A reader checking the layer list concludes the diamond is already handled.
Additional context
Two ways this surfaced in a real app.
The Cloudflare deploy fails. The doubled cron reaches .output/server/wrangler.json and the API rejects it. It rejects on the PUT .../schedules call after the script has uploaded, so the Worker goes live with a red deploy and its schedules untouched:
duplicate cron found: 0 0 * * * [code: 10100]The scheduled task runs twice. nitro.scheduledTasks is an object, so its keys collapse on merge, but the task list under each key is an array, and it does not. The built bundle carries tasks: ["someTask","someTask"], and nitro's in-flight guard does not collapse the pair, because runTask yields before it records the promise. The task body executes twice, concurrently, on every fire. This half is preset-independent and raises no error anywhere. No nitro release addresses it, and it has nothing to do with Cloudflare cron generation.
Arrays elsewhere in the config are unaffected because Nuxt deduplicates them downstream itself: modules by identity, css by an explicit filter, imports.dirs by unimport. Nothing deduplicates nitro.*, so that is where it shows.
Related: nitrojs/nitro#4155 reports the double task execution against Nitro. unjs/defu#136 and unjs/c12#309 both rule the array concatenation itself as intended, which is why this is filed here rather than there.
Source: nuxt/nuxt