hashicorp/nomad · error

Task group count can't be negative

Error message

Task group count can't be negative

What it means

A task group must declare at least one task; TaskGroup.Validate appends this error when len(tg.Tasks) == 0. The only tolerated case (lone Consul gateways injected by the connect mutator) is handled elsewhere, so an empty user-supplied group always fails.

Source

Thrown at nomad/structs/structs.go:7134

				services = append(services, service)
			}
		}
	}
	return services
}

// Validate is used to check a task group for reasonable configuration
func (tg *TaskGroup) Validate(j *Job) error {
	var mErr *multierror.Error

	if tg.Name == "" {
		mErr = multierror.Append(mErr, errors.New("Missing task group name"))
	} else if strings.Contains(tg.Name, "\000") {
		mErr = multierror.Append(mErr, errors.New("Task group name contains null character"))
	}

	if tg.Count < 0 {
		mErr = multierror.Append(mErr, errors.New("Task group count can't be negative"))
	}

	if len(tg.Tasks) == 0 {
		// could be a lone consul gateway inserted by the connect mutator
		mErr = multierror.Append(mErr, errors.New("Missing tasks for task group"))
	}

	if tg.Disconnect != nil {
		if err := tg.Disconnect.Validate(j); err != nil {
			mErr = multierror.Append(mErr, err)
		}
	}

	if tg.MaxRunDuration != nil {
		if *tg.MaxRunDuration <= 0 {
			mErr = multierror.Append(mErr, errors.New("MaxRunDuration must be greater than zero"))
		}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Add at least one task block inside each group.
  2. Fix indentation in HCL so the task stanza is inside the group.
  3. When generating jobs programmatically, assert len(tasks) > 0 before submission.

Example fix

// before
group "web" {
  # task "server" { ... } accidentally commented
}
// after
group "web" {
  task "server" {
    driver = "docker"
    config { image = "nginx" }
  }
}
Defensive patterns

Strategy: validation

Validate before calling

if len(tg.Tasks) == 0 {
    return errors.New("task group requires at least one task")
}

Prevention

When it happens

Trigger: Submitting a job where a group stanza contains no task blocks; building TaskGroup structs in code without appending any Task.

Common situations: HCL group block whose task stanza was commented out or mis-indented; JSON jobs generated dynamically where the task list ended up empty; refactor that moved tasks to another group.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/b08a6101f375fb9e. Report an issue: GitHub.