{"record":{"id":"12b80e4a48988ef9","repo":"hashicorp/nomad","slug":"could-not-find-task-named-s-found-s","errorCode":null,"errorMessage":"Could not find task named: %s, found:\n%s","messagePattern":"Could not find task named: (.+?), found:\n(.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/alloc_restart.go","lineNumber":179,"sourceCode":"\n\treturn 0\n}\n\nfunc validateTaskExistsInAllocation(taskName string, alloc *api.Allocation) error {\n\ttg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)\n\tif tg == nil {\n\t\treturn fmt.Errorf(\"Could not find allocation task group: %s\", alloc.TaskGroup)\n\t}\n\n\tfoundTaskNames := make([]string, len(tg.Tasks))\n\tfor i, task := range tg.Tasks {\n\t\tfoundTaskNames[i] = task.Name\n\t\tif task.Name == taskName {\n\t\t\treturn nil\n\t\t}\n\t}\n\n\treturn fmt.Errorf(\"Could not find task named: %s, found:\\n%s\", taskName, formatList(foundTaskNames))\n}\n\nfunc (c *AllocRestartCommand) Synopsis() string {\n\treturn \"Restart a running allocation\"\n}\n\nfunc (c *AllocRestartCommand) AutocompleteFlags() complete.Flags {\n\treturn mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),\n\t\tcomplete.Flags{\n\t\t\t\"-all-tasks\": complete.PredictNothing,\n\t\t\t\"-verbose\":   complete.PredictNothing,\n\t\t\t\"-task\":      complete.PredictAnything,\n\t\t})\n}\n\nfunc (c *AllocRestartCommand) AutocompleteArgs() complete.Predictor {\n\t// Here we attempt to autocomplete allocations for any position of arg.\n\t// We should eventually try to auto complete the task name if the arg is","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/alloc_restart.go#L161-L197","documentation":"`nomad alloc restart <alloc-id> -task <name>` validates the task name against the tasks of the allocation's task group. The group was found, but none of its tasks match the requested name, so the error lists all valid task names via formatList. This is a user-input error: the -task value doesn't correspond to any task in that group.","triggerScenarios":"Running `nomad alloc restart <alloc-id> -task foo` where the allocation's task group contains tasks other than \"foo\" — typical after a task rename in a newer job version, or supplying a task name from a different group/job.","commonSituations":"Copy-pasting a task name from an old job file after a rename (e.g. \"webapp\" → \"web\"); scripting restarts with a hardcoded task name that changed between deploys; confusing the task name with the task group name or the service name.","solutions":["Use the task names listed in the error message itself (the \"found:\" list) and re-run with a valid -task value.","List current tasks with `nomad alloc status <alloc-id>` or `nomad job inspect <job>` and pick the correct one.","Update automation/CI scripts to derive the task name dynamically from the allocation instead of hardcoding it.","If the task should exist, check whether the allocation is from an old job version and target the newest allocation for the job."],"exampleFix":"// before\nnomad alloc restart 1f2a3b4c -task webapp\n// Error: Could not find task named: webapp, found:\\n  * web\n\n// after\nnomad alloc restart 1f2a3b4c -task web\n\n// in scripts: resolve the task dynamically instead of hardcoding\nalloc, _, _ := client.Allocations().Info(allocID, nil)\ntg, _ := lookupAllocTask(alloc) // returns the single task's name when unambiguous","handlingStrategy":"validation","validationCode":"tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)\nif tg == nil { return fmt.Errorf(\"task group %q not found\", alloc.TaskGroup) }\nvalid := map[string]bool{}\nfor _, t := range tg.Tasks { valid[t.Name] = true }\nif !valid[taskName] {\n    names := make([]string, 0, len(tg.Tasks))\n    for _, t := range tg.Tasks { names = append(names, t.Name) }\n    return fmt.Errorf(\"task %q invalid; valid tasks: %v\", taskName, names)\n}\nreturn nil","typeGuard":"func taskExistsInAlloc(alloc *api.Allocation, taskName string) bool {\n    tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)\n    if tg == nil { return false }\n    for _, t := range tg.Tasks {\n        if t.Name == taskName { return true }\n    }\n    return false\n}","tryCatchPattern":"if err := validateTaskExistsInAllocation(taskName, alloc); err != nil {\n    if strings.Contains(err.Error(), \"Could not find task named\") {\n        // parse the \"found:\" list from the error and pick/confirm the right task,\n        // or re-run `nomad alloc status <alloc-id>` to get current task names\n        return fmt.Errorf(\"use a task from: %s (got %q)\", extractFoundList(err), taskName)\n    }\n    return err\n}","preventionTips":["Read the \"found:\" list in the error message — it enumerates every valid task name.","Use `nomad alloc status <alloc-id>` to list tasks before running alloc restart.","Avoid hardcoding task names in scripts; derive them from the allocation or job spec at runtime.","After task renames in job files, redeploy and re-resolve names before restarting tasks."],"tags":["nomad","cli","task-name","invalid-input"],"backgroundTag":"invalid-task-name","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}