{"record":{"id":"a65aa231f2a701fb","repo":"docker/cli","slug":"failed-to-parse-v-as-a-rational-number","errorCode":null,"errorMessage":"failed to parse %v as a rational number","messagePattern":"failed to parse (.+?) as a rational number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"opts/opts.go","lineNumber":362,"sourceCode":"\t*c = NanoCPUs(cpus)\n\treturn err\n}\n\n// Type returns the type\nfunc (*NanoCPUs) Type() string {\n\treturn \"decimal\"\n}\n\n// Value returns the value in int64\nfunc (c *NanoCPUs) Value() int64 {\n\treturn int64(*c)\n}\n\n// ParseCPUs takes a string ratio and returns an integer value of nano cpus\nfunc ParseCPUs(value string) (int64, error) {\n\tcpu, ok := new(big.Rat).SetString(value)\n\tif !ok {\n\t\treturn 0, fmt.Errorf(\"failed to parse %v as a rational number\", value)\n\t}\n\tnano := cpu.Mul(cpu, big.NewRat(1e9, 1))\n\tif !nano.IsInt() {\n\t\treturn 0, errors.New(\"value is too precise\")\n\t}\n\treturn nano.Num().Int64(), nil\n}\n\n// ParseLink parses and validates the specified string as a link format (name:alias)\nfunc ParseLink(val string) (string, string, error) {\n\tif val == \"\" {\n\t\treturn \"\", \"\", errors.New(\"empty string specified for links\")\n\t}\n\t// We expect two parts, but restrict to three to allow detecting invalid formats.\n\tarr := strings.SplitN(val, \":\", 3)\n\n\t// TODO(thaJeztah): clean up this logic!!\n\tif len(arr) > 2 {","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/opts/opts.go#L344-L380","documentation":"Thrown by ParseCPUs (opts.go:362) when big.Rat.SetString cannot parse the input as a rational number. NanoCPUs expects a decimal or fractional CPU count (e.g. 0.5, 1, 2.5); SetString fails on empty strings, letters, multiple dots, trailing operators, or any non-numeric token. The value is parsed as a ratio then scaled to nanocpus (×1e9).","triggerScenarios":"Calling NanoCPUs.Set or ParseCPUs with '', 'abc', '1.2.3', '1/2/3', '0x5', '+', or a value with locale-specific separators like '1,5'. big.Rat.SetString returns ok=false at line 360, so line 362 fires.","commonSituations":"Setting `--cpus` with a non-numeric value, a comma decimal from a European locale, a percentage like '50%', or an empty env var. Also from templating that injects units ('2cores') or whitespace.","solutions":["Pass a plain decimal CPU count: `--cpus 0.5`, `--cpus 2`, or `--cpus 2.5`.","Remove any unit suffixes (cores, vCPU) and surrounding whitespace.","If using a locale with comma decimals, convert ',' to '.' before passing.","Pre-validate with strconv.ParseFloat and reformat to a clean decimal string."],"exampleFix":"// before\n--cpus 2cores\n// after\n--cpus 2","handlingStrategy":"validation","validationCode":"// Pre-validate a CPU count string as a rational number before ParseCPUs.\nif _, ok := new(big.Rat).SetString(strings.TrimSpace(cpu)); !ok {\n    return fmt.Errorf(\"%q is not a valid CPU count\", cpu)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass plain decimals: 0.5, 1, 2.5.","Strip units ('cores', 'vCPU') and whitespace.","Convert locale commas to dots before submission.","Default unset CPU values rather than sending empty strings."],"tags":["docker","cpu","validation","cli","resource-limits"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}