{"record":{"id":"927442e762153438","repo":"kubernetes/kops","slug":"cannot-set-field-q-v","errorCode":null,"errorMessage":"cannot set field %q: %v","messagePattern":"cannot set field %q: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"util/pkg/reflectutils/access.go","lineNumber":52,"sourceCode":"\ttargetFieldPath, err := ParseFieldPath(targetPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cannot parse field path %q: %w\", targetPath, err)\n\t}\n\n\tfieldSet := false\n\n\tvisitor := func(path *FieldPath, field *reflect.StructField, v reflect.Value) error {\n\t\tif !targetFieldPath.HasPrefixMatch(path) {\n\t\t\treturn nil\n\t\t}\n\n\t\tif targetFieldPath.Matches(path) {\n\t\t\tif !v.CanSet() {\n\t\t\t\treturn fmt.Errorf(\"cannot set field %q (marked immutable)\", path)\n\t\t\t}\n\n\t\t\tif err := setType(v, newValue); err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot set field %q: %v\", path, err)\n\t\t\t}\n\n\t\t\tfieldSet = true\n\t\t\treturn nil\n\t\t}\n\n\t\t// Partial match, append the next explicitly indexed slice element.\n\t\tif v.Kind() == reflect.Slice {\n\t\t\tif len(targetFieldPath.elements) > len(path.elements) {\n\t\t\t\tnext := targetFieldPath.elements[len(path.elements)]\n\t\t\t\tif next.Type == FieldPathElementTypeArrayIndex && next.number == v.Len() {\n\t\t\t\t\tif !v.CanSet() {\n\t\t\t\t\t\treturn fmt.Errorf(\"cannot set field %q (marked immutable)\", path)\n\t\t\t\t\t}\n\n\t\t\t\t\tnewLen := v.Len() + 1\n\t\t\t\t\tgrown := reflect.MakeSlice(v.Type(), newLen, newLen)\n\t\t\t\t\treflect.Copy(grown, v)","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/util/pkg/reflectutils/access.go#L34-L70","documentation":"When the parsed field path matches, SetString calls setType to coerce the string newValue to the field's type; this error wraps any coercion failure (e.g. setting a non-numeric string on an int field, invalid bool, or wrong time format). The path itself was correct; the VALUE was not convertible.","triggerScenarios":"SetString(obj, \"spec.nodeCount\", \"many\") on an int field; \"true\"/\"false\" misspellings on bool fields; setting a duration/time field with an unparseable string.","commonSituations":"kops set cluster-field overrides from templates where a variable renders as empty or non-numeric; users passing version strings to numeric fields; region names into boolean flags.","solutions":["Check the target field's Go type in the kops API and supply a value it can parse (strconv-compatible)","Quote/escape values in shell so an empty variable doesn't reach setType","Validate the value with strconv.ParseInt/ParseBool yourself before calling SetString"],"exampleFix":"// before\nreflectutils.SetString(ig, \"spec.minSize\", \"three\")\n// after\nreflectutils.SetString(ig, \"spec.minSize\", \"3\")","handlingStrategy":"validation","validationCode":"// coerce-check before SetString\nif n, err := strconv.Atoi(val); err != nil && fieldIsInt(path) {\n\treturn fmt.Errorf(\"field %s needs an integer, got %q\", path, val)\n}","typeGuard":"func canParseAsInt(s string) bool { _, err := strconv.Atoi(s); return err == nil }\nfunc canParseAsBool(s string) bool { _, err := strconv.ParseBool(s); return err == nil }","tryCatchPattern":"if err := reflectutils.SetString(target, path, val); err != nil {\n\tif strings.Contains(err.Error(), \"cannot set field\") && !strings.Contains(err.Error(), \"immutable\") {\n\t\treturn fmt.Errorf(\"value %q not convertible for field %s: %w\", val, path, err)\n\t}\n\treturn err\n}","preventionTips":["Check the Go type of each field before overriding it","Validate numeric/boolean values with strconv before the call","Guard shell variables so they never render as empty strings","Keep a table of cluster-field names and their expected value formats"],"tags":["reflection","type-conversion","setstring"],"backgroundTag":"type-conversion-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}