hashicorp/nomad · error

Check %s is invalid: may only specify task the check belongs

Error message

Check %s is invalid: may only specify task the check belongs to, got %q

What it means

Like services, a check inside a task-level service may only specify the task it belongs to; a check with task_name set to a different task is rejected. This keeps check execution tied to the task that owns the service.

Source

Thrown at nomad/structs/structs.go:7501

		if len(task.Services) == 0 {
			continue
		}

		for _, service := range task.Services {

			// Ensure no task-level service can only specify the task it belongs to.
			if service.TaskName != "" && service.TaskName != task.Name {
				mErr.Errors = append(mErr.Errors,
					fmt.Errorf("Service %s is invalid: may only specify task the service belongs to, got %q", service.Name, service.TaskName),
				)
			}

			// Ensure no task-level checks can only specify the task they belong to.
			for _, check := range service.Checks {
				if check.TaskName != "" && check.TaskName != task.Name {
					mErr.Errors = append(mErr.Errors,
						fmt.Errorf("Check %s is invalid: may only specify task the check belongs to, got %q", check.Name, check.TaskName),
					)
				}
			}

			// Track that we have seen this service id
			id := unique{service.Name, task.Name, service.PortLabel}
			if !idSet.Insert(id) {
				// accumulate duplicates for a single error later on
				idDuplicateSet.Insert(id)
			}

			// Track that we have seen this service provider
			providerSet.Insert(service.Provider)
		}
	}

	for i, service := range tg.Services {

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Remove the task field from the check so it inherits the service's task.
  2. Set the check's task to the name of the task that contains the service.
  3. Move the service (with the check) to the group-level services block if cross-task targeting is intended.

Example fix

// before
check "c" { task="api" }
// after
check "c" { }
Defensive patterns

Strategy: validation

Validate before calling

for _, c := range svc.Checks {
  if c.TaskName != "" && c.TaskName != enclosingTaskName {
    return fmt.Errorf("check %q: task %q != enclosing %q", c.Name, c.TaskName, enclosingTaskName)
  }
}

Prevention

When it happens

Trigger: A check inside a task's service block sets task = "other-task" (different from the enclosing task), triggering validation of service.Checks.

Common situations: Copy-pasting a check from a group-level service; refactoring jobs and moving checks between tasks; HCL templating that fills task names incorrectly.

Related errors


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