{"record":{"id":"b36b653a9bc2b944","repo":"hashicorp/nomad","slug":"check-s-is-invalid-may-only-specify-task-the-che","errorCode":null,"errorMessage":"Check %s is invalid: may only specify task the check belongs to, got %q","messagePattern":"Check (.+?) is invalid: may only specify task the check belongs to, got %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/structs.go","lineNumber":7501,"sourceCode":"\n\t\tif len(task.Services) == 0 {\n\t\t\tcontinue\n\t\t}\n\n\t\tfor _, service := range task.Services {\n\n\t\t\t// Ensure no task-level service can only specify the task it belongs to.\n\t\t\tif service.TaskName != \"\" && service.TaskName != task.Name {\n\t\t\t\tmErr.Errors = append(mErr.Errors,\n\t\t\t\t\tfmt.Errorf(\"Service %s is invalid: may only specify task the service belongs to, got %q\", service.Name, service.TaskName),\n\t\t\t\t)\n\t\t\t}\n\n\t\t\t// Ensure no task-level checks can only specify the task they belong to.\n\t\t\tfor _, check := range service.Checks {\n\t\t\t\tif check.TaskName != \"\" && check.TaskName != task.Name {\n\t\t\t\t\tmErr.Errors = append(mErr.Errors,\n\t\t\t\t\t\tfmt.Errorf(\"Check %s is invalid: may only specify task the check belongs to, got %q\", check.Name, check.TaskName),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Track that we have seen this service id\n\t\t\tid := unique{service.Name, task.Name, service.PortLabel}\n\t\t\tif !idSet.Insert(id) {\n\t\t\t\t// accumulate duplicates for a single error later on\n\t\t\t\tidDuplicateSet.Insert(id)\n\t\t\t}\n\n\t\t\t// Track that we have seen this service provider\n\t\t\tproviderSet.Insert(service.Provider)\n\t\t}\n\t}\n\n\tfor i, service := range tg.Services {\n","sourceCodeStart":7483,"sourceCodeEnd":7519,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/structs.go#L7483-L7519","documentation":"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.","triggerScenarios":"A check inside a task's service block sets task = \"other-task\" (different from the enclosing task), triggering validation of service.Checks.","commonSituations":"Copy-pasting a check from a group-level service; refactoring jobs and moving checks between tasks; HCL templating that fills task names incorrectly.","solutions":["Remove the task field from the check so it inherits the service's task.","Set the check's task to the name of the task that contains the service.","Move the service (with the check) to the group-level services block if cross-task targeting is intended."],"exampleFix":"// before\ncheck \"c\" { task=\"api\" }\n// after\ncheck \"c\" { }","handlingStrategy":"validation","validationCode":"for _, c := range svc.Checks {\n  if c.TaskName != \"\" && c.TaskName != enclosingTaskName {\n    return fmt.Errorf(\"check %q: task %q != enclosing %q\", c.Name, c.TaskName, enclosingTaskName)\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Omit task on checks inside task services","Keep checks next to the service they probe","Grep job files for 'task =' inside check blocks after refactors"],"tags":["nomad","service","check","consul","validation"],"backgroundTag":"invalid-service-task-reference","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"}