lima-vm/lima · error

invalid value for number of cpus, must be >= 0

Error message

invalid value for number of cpus, must be >= 0

What it means

When building yq expressions for the --cpus flag, YQExpressions reads the flag value with flags.GetInt and rejects negative numbers because a VM cannot have a negative CPU count. This error is returned when --cpus is set to a value below 0.

Source

Thrown at cmd/limactl/editflags/editflags.go:232

// YQExpressions returns YQ expressions.
func YQExpressions(flags *flag.FlagSet, newInstance bool, params map[string]string) ([]string, error) {
	type def struct {
		flagName                 string
		exprFunc                 func(*flag.Flag) ([]string, error)
		onlyValidForNewInstances bool
		experimental             bool
	}
	d := defaultExprFunc
	defs := []def{
		{
			"cpus",
			func(_ *flag.Flag) ([]string, error) {
				numCpus, err := flags.GetInt("cpus")
				if err != nil {
					return nil, err
				}
				if numCpus < 0 {
					return nil, errors.New("invalid value for number of cpus, must be >= 0")
				}
				return []string{fmt.Sprintf(".cpus = %d", numCpus)}, nil
			},
			false,
			false,
		},
		{
			"dns",
			func(_ *flag.Flag) ([]string, error) {
				ipSlice, err := flags.GetIPSlice("dns")
				if err != nil {
					return nil, err
				}
				ips := make([]string, len(ipSlice))
				for i, ip := range ipSlice {
					ips[i] = `"` + ip.String() + `"`
				}
				expr := fmt.Sprintf(".dns += [%s] | .dns |= unique | .hostResolver.enabled=false", strings.Join(ips, ","))

View on GitHub (pinned to dd909d0973)

Solutions

  1. Pass a non-negative integer: --cpus 0 or a positive count (0 means leave/default behavior)
  2. Fix the script's CPU-count calculation so it cannot go negative
  3. Omit --cpus entirely to keep the template default

Example fix

// before
limactl create template://_default --cpus=-1
// after
limactl create template://_default --cpus=4
Defensive patterns

Strategy: validation

Validate before calling

cpus, _ := flags.GetInt("cpus")
if cpus < 0 {
	return fmt.Errorf("--cpus must be >= 0, got %d", cpus)
}

Type guard

func validCpuCount(n int) bool {
	return n >= 0
}

Try / catch

exprs, err := editflags.YQExpressions(flagSet, newInstance, params)
if err != nil {
	if strings.Contains(err.Error(), "number of cpus") {
		return fmt.Errorf("fix --cpus: %w", err)
	}
	return err
}

Prevention

When it happens

Trigger: Running limactl create/edit with --cpus=-1 (or any negative integer); a script computing a CPU count that yields a negative value.

Common situations: Scripts using arithmetic that underflows (e.g. counting CPUs and subtracting); users typing '-1' intending 'auto/unlimited'; copying a dash into the value.

Related errors


AI-assisted analysis of lima-vm/lima@dd909d0973 (2026-09-01). Data as JSON: /api/errors/ec47d94ef5743884. Report an issue: GitHub.