windmill-labs/windmill · error · Error

Invalid subgrid selected: ${key}, max subgrids: ${subgrids}

Error message

Invalid subgrid selected: ${key}, max subgrids: ${subgrids}

What it means

Thrown by `insertNewGridItem` when the selected `subGridIndex` on the focused grid item is negative or >= the parent's `numberOfSubgrids`. After this check the code initializes `app.subgrids[key] = []` so the index bound comes from the parent's declared subgrid count.

Source

Thrown at frontend/src/lib/components/apps/editor/appUtils.ts:583

	const key = focusedGrid
		? `${focusedGrid?.parentComponentId}-${focusedGrid?.subGridIndex ?? 0}`
		: undefined

	if (key && app.subgrids[key] === undefined) {
		let parent = findGridItemById(app.grid, app.subgrids, key)?.data
		let subgrids = parent?.numberOfSubgrids
		if (subgrids === undefined) {
			throw Error(
				`Invalid subgrid selected, the parent has no subgrids: ${key}, parent: ${JSON.stringify(
					parent
				)}`
			)
		}
		if (
			focusedGrid?.subGridIndex &&
			(focusedGrid?.subGridIndex < 0 || focusedGrid?.subGridIndex >= subgrids)
		) {
			throw Error(`Invalid subgrid selected: ${key}, max subgrids: ${subgrids}`)
		}
		// If ever the subgrid is undefined, we want to make sure it is defined
		app.subgrids[key] = []
	}

	let grid = focusedGrid ? app.subgrids[key!] : app.grid

	const newItem = createNewGridItem(
		grid,
		id,
		data,
		columns,
		initialPosition,
		recOverride,
		fixed,
		shouldNotFindSpace
	)
	grid.push(newItem)

View on GitHub (pinned to e474e8803c)

Solutions

  1. Set focusedGrid.subGridIndex to a value in [0, numberOfSubgrids) of the parent before inserting
  2. Verify the parent component's numberOfSubgrids matches your intended index (e.g. index 1 requires numberOfSubgrids >= 2)
  3. Fix stale app JSON so subGridIndex values are consistent with current numberOfSubgrids
  4. Clamp or reset subGridIndex to 0 when the destination component is unknown

Example fix

// before
focusedGrid.subGridIndex = 3 // parent has numberOfSubgrids: 2
// after
const max = findGridItemById(app.grid, app.subgrids, key)?.data?.numberOfSubgrids ?? 0
focusedGrid.subGridIndex = Math.min(Math.max(focusedGrid.subGridIndex ?? 0, 0), Math.max(max - 1, 0))
Defensive patterns

Strategy: validation

Validate before calling

function isValidSubgridIndex(app, key, subGridIndex) {
  const parent = findGridItemById(app.grid, app.subgrids, key)
  const max = parent?.data?.numberOfSubgrids ?? 0
  return Number.isInteger(subGridIndex) && subGridIndex >= 0 && subGridIndex < max
}

Type guard

function hasValidSubGridIndex(g): g is typeof g & { subGridIndex: number } {
  return typeof g?.subGridIndex === 'number' && g.subGridIndex >= 0
}

Try / catch

try {
  insertNewGridItem(app, key, item)
} catch (e) {
  if (String(e.message).startsWith('Invalid subgrid selected:')) {
    focusedGrid.subGridIndex = 0
    insertNewGridItem(app, key, item)
  } else throw e
}

Prevention

When it happens

Trigger: Inserting into a subgrid via newItem/handlePaste/moveComponentBetweenSubgrids/moveToRoot where focusedGrid.subGridIndex is out of [0, numberOfSubgrids) — e.g. subGridIndex 2 on a parent with 2 subgrids (valid indexes 0 and 1), a negative index, or an index left over from a component whose subgrid count shrank.

Common situations: Programmatic component insertion with a hardcoded subGridIndex; pasting into a copied component whose source parent had more subgrids than the destination; app JSON patched to reduce numberOfSubgrids while children still reference the old indexes.

Related errors


AI-assisted analysis of windmill-labs/windmill@e474e8803c (2026-09-03). Data as JSON: /api/errors/baf277bff83109cb. Report an issue: GitHub.