docker/compose · error

invalid indentation size: %d

Error message

invalid indentation size: %d

What it means

Returned by preferredIndentationStr in `docker compose viz` when the --indentation flag value is negative. The function builds the indentation string with strings.Repeat, which panics on negative counts, so size < 0 is explicitly rejected first with this error. size == 0 is valid (no indentation).

Source

Thrown at cmd/compose/viz.go:97

	}

	// build graph
	graphStr, _ := backend.Viz(ctx, project, api.VizOptions{
		IncludeNetworks:  opts.includeNetworks,
		IncludePorts:     opts.includePorts,
		IncludeImageName: opts.includeImageName,
		Indentation:      opts.indentationStr,
	})

	fmt.Println(graphStr)

	return nil
}

// preferredIndentationStr returns a single string given the indentation preference
func preferredIndentationStr(size int, useSpace bool) (string, error) {
	if size < 0 {
		return "", fmt.Errorf("invalid indentation size: %d", size)
	}

	indentationStr := "\t"
	if useSpace {
		indentationStr = " "
	}
	return strings.Repeat(indentationStr, size), nil
}

View on GitHub (pinned to ddc4b044b6)

Solutions

  1. Pass a non-negative size: `docker compose viz --indentation 2 --spaces`.
  2. Use 0 when you want no indentation.
  3. Clamp computed values in scripts to >= 0.

Example fix

# before
docker compose viz --indentation -1

# after
docker compose viz --indentation 2 --spaces
Defensive patterns

Strategy: validation

Validate before calling

INDENT=$(( ${INDENTATION:-2} )); (( INDENT < 0 )) && INDENT=0
docker compose viz --indentation "$INDENT" --spaces

Prevention

When it happens

Trigger: `docker compose viz --indentation -1`; passing --indentation 0 with --spaces is fine, but any negative integer hits the guard; script-computed indentation going negative.

Common situations: Typos or a shell variable that resolves to a negative number; users assuming negative means 'compact' output; piping a computed indent level that underflows in arithmetic.

Related errors


AI-assisted analysis of docker/compose@ddc4b044b6 (2026-08-15). Data as JSON: /api/errors/4ccda06eba245ea9. Report an issue: GitHub.