{"record":{"id":"7cb808df642ef2b1","repo":"hashicorp/nomad","slug":"affinity-weight-cannot-be-zero","errorCode":null,"errorMessage":"Affinity weight cannot be zero","messagePattern":"Affinity weight cannot be zero","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/structs.go","lineNumber":10233,"sourceCode":"\t\tif _, err := semver.NewConstraint(a.RTarget); err != nil {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Semver affinity is invalid: %v\", err))\n\t\t}\n\tcase \"=\", \"==\", \"is\", \"!=\", \"not\", \"<\", \"<=\", \">\", \">=\":\n\t\tif a.RTarget == \"\" {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Operator %q requires an RTarget\", a.Operand))\n\t\t}\n\tdefault:\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Unknown affinity operator %q\", a.Operand))\n\t}\n\n\t// Ensure we have an LTarget\n\tif a.LTarget == \"\" {\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"No LTarget provided but is required\"))\n\t}\n\n\t// Ensure that weight is between -100 and 100, and not zero\n\tif a.Weight == 0 {\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Affinity weight cannot be zero\"))\n\t}\n\n\tif a.Weight > 100 || a.Weight < -100 {\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Affinity weight must be within the range [-100,100]\"))\n\t}\n\n\treturn mErr.ErrorOrNil()\n}\n\n// DiffID fulfills the DiffableWithID interface.\nfunc (a *Affinity) DiffID() string {\n\treturn a.String()\n}\n\n// Spread is used to specify desired distribution of allocations according to weight\ntype Spread struct {\n\t// Attribute is the node attribute used as the spread criteria\n\tAttribute string","sourceCodeStart":10215,"sourceCodeEnd":10251,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/structs.go#L10215-L10251","documentation":"Affinity.Validate() enforces that the affinity weight is non-zero and within [-100, 100]. A weight of exactly 0 means the affinity has no effect, so Nomad treats it as a configuration mistake and rejects the job with 'Affinity weight cannot be zero'.","triggerScenarios":"Registering a job whose affinity has weight = 0, either explicitly in the HCL affinity block or by omitting... no — omission defaults to 50; this fires only when weight is explicitly set to 0 in HCL or the /v1/jobs payload.","commonSituations":"Setting weight = 0 to 'disable' an affinity (instead of deleting the block), template math computing 0, or API clients defaulting weight to 0 instead of leaving it unset.","solutions":["Remove the weight field (defaults to 50) or set a non-zero value like weight = 50.","If the affinity should be disabled, delete the affinity block instead of zeroing its weight.","Use a negative weight (e.g. -50) if you want an anti-affinity, not zero."],"exampleFix":"// before\naffinity {\n  attribute = \"${node.datacenter}\"\n  value     = \"dc1\"\n  weight    = 0\n}\n// after\naffinity {\n  attribute = \"${node.datacenter}\"\n  value     = \"dc1\"\n  weight    = 50\n}","handlingStrategy":"validation","validationCode":"if a.Weight == 0 { return errors.New(\"affinity weight must be non-zero; remove the block to disable\") }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Delete affinity blocks instead of setting weight = 0","Use negative weights for anti-affinity, never zero","Omit weight to use the default of 50"],"tags":["nomad","affinity","weight","job-spec","validation"],"backgroundTag":"invalid-parameter-range","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"}