{"record":{"id":"4d2a803db90200b4","repo":"k3s-io/k3s","slug":"failed-to-parse-config-location-s-w","errorCode":null,"errorMessage":"failed to parse config location %s: %w","messagePattern":"failed to parse config location (.+?): %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/configfilearg/parser.go","lineNumber":336,"sourceCode":"\tswitch k := v.(type) {\n\tcase string:\n\t\treturn []any{k}\n\tcase []any:\n\t\treturn k\n\tdefault:\n\t\tstr := strings.TrimSpace(ToString(v))\n\t\tif str == \"\" {\n\t\t\treturn nil\n\t\t}\n\t\treturn []any{str}\n\t}\n}\n\n// readConfigFileData returns the contents of a local or remote file\nfunc readConfigFileData(file string) ([]byte, error) {\n\tu, err := url.Parse(file)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to parse config location %s: %w\", file, err)\n\t}\n\n\tswitch u.Scheme {\n\tcase \"http\", \"https\":\n\t\tresp, err := http.Get(file)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to read http config %s: %w\", file, err)\n\t\t}\n\t\tdefer resp.Body.Close()\n\t\treturn io.ReadAll(resp.Body)\n\tdefault:\n\t\treturn os.ReadFile(file)\n\t}\n}\n","sourceCodeStart":318,"sourceCodeEnd":351,"githubUrl":"https://github.com/k3s-io/k3s/blob/6ba341e396edc16b8dcae978a7c5e3ac7ee5606e/pkg/configfilearg/parser.go#L318-L351","documentation":"readConfigFileData parses the value passed via `--config` / K3S_CONFIG_FILE with url.Parse to decide between an http(s) fetch and a local file read. url.Parse fails on very few inputs - almost exclusively strings containing control characters (newline, NUL, etc.), since nearly anything else is syntactically parseable as a URL.","triggerScenarios":"Passing a config location containing control characters or otherwise unparsable URL syntax to --config (pkg/configfilearg/parser.go:334-337), e.g. a shell/env variable with an embedded newline or CR.","commonSituations":"Config path read from an env var or file with a trailing newline/CR not stripped; copy-paste from Windows docs introducing CR characters; script-generated paths using unquoted command substitution.","solutions":["Quote the path and strip whitespace: use an absolute path like --config /etc/rancher/k3s/config.yaml.","Check the variable for hidden characters: `printf '%s' \"$K3S_CONFIG_FILE\" | od -c` and remove any \\n / \\r / NUL bytes.","If the value comes from a script, assign it without command substitution newlines: K3S_CONFIG_FILE=$(head -1 path.txt | tr -d '\\r\\n')."],"exampleFix":"# before\nK3S_CONFIG_FILE=\"$(cat /etc/k3s-config-path.txt)\"  # includes trailing newline -> url.Parse fails\nk3s server --config \"$K3S_CONFIG_FILE\"\n\n# after\nK3S_CONFIG_FILE=\"$(tr -d '\\r\\n' < /etc/k3s-config-path.txt)\"\nk3s server --config \"$K3S_CONFIG_FILE\"","handlingStrategy":"validation","validationCode":"// Reject config locations containing control characters before launching k3s:\nfunc validConfigLocation(s string) bool {\n    if strings.TrimSpace(s) == \"\" {\n        return false\n    }\n    for _, r := range s {\n        if r < 0x20 || r == 0x7f {\n            return false\n        }\n    }\n    _, err := url.Parse(s)\n    return err == nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always quote --config values in shell scripts and strip newlines from variables read from files.","Prefer absolute local paths for config files.","Lint generated config paths with od -c when k3s reports URL parse failures."],"tags":["config","url-parsing","cli","shell"],"backgroundTag":null,"analyzedSha":"6ba341e396edc16b8dcae978a7c5e3ac7ee5606e","analyzedAt":"2026-08-15T16:27:54.286Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}