{"record":{"id":"96139f4351a2a0d5","repo":"hashicorp/nomad","slug":"failed-to-convert-q-to-a-log-index-v","errorCode":null,"errorMessage":"failed to convert %q to a log index: %v","messagePattern":"failed to convert %q to a log index: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/fs_endpoint.go","lineNumber":892,"sourceCode":"// error is returned.\nfunc logIndexes(entries []*cstructs.AllocFileInfo, task, logType string) (indexTupleArray, error) {\n\tvar indexes []indexTuple\n\tprefix := fmt.Sprintf(\"%s.%s.\", task, logType)\n\tfor _, entry := range entries {\n\t\tif entry.IsDir {\n\t\t\tcontinue\n\t\t}\n\n\t\t// If nothing was trimmed, then it is not a match\n\t\tidxStr := strings.TrimPrefix(entry.Name, prefix)\n\t\tif idxStr == entry.Name {\n\t\t\tcontinue\n\t\t}\n\n\t\t// Convert to an int\n\t\tidx, err := strconv.Atoi(idxStr)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to convert %q to a log index: %v\", idxStr, err)\n\t\t}\n\n\t\tindexes = append(indexes, indexTuple{idx: int64(idx), entry: entry})\n\t}\n\n\treturn indexTupleArray(indexes), nil\n}\n\n// notFoundErr is returned when a log is requested but cannot be found.\n// Implements agent.HTTPCodedError but does not reference it to avoid circular\n// imports.\ntype notFoundErr struct {\n\ttaskName string\n\tlogType  string\n}\n\nfunc (e notFoundErr) Error() string {\n\treturn fmt.Sprintf(\"log entry for task %q and log type %q not found\", e.taskName, e.logType)","sourceCodeStart":874,"sourceCodeEnd":910,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/client/fs_endpoint.go#L874-L910","documentation":"logIndexes parses log file names of the form <task>.<stdout|stderr>.<index> in the alloc's log directory. When the extracted index component cannot be parsed as an integer with strconv.Atoi, it returns this error, aborting the listing. A malformed filename in the log directory causes the whole log request to fail.","triggerScenarios":"A file in the alloc log directory whose name has a non-numeric index segment — files created by user tasks or external processes writing into /alloc/logs, or corrupted/renamed Nomad log files.","commonSituations":"Tasks writing their own files into the shared /alloc/logs directory with arbitrary names; tooling symlinks or backups like web.stdout.0.bak; manual copies of log files with altered names; older Nomad versions producing different naming schemes.","solutions":["Remove or relocate non-Nomad files (e.g. *.bak, *.tmp) from the alloc's /alloc/logs directory.","Configure the task to write its own output elsewhere (task's own dir, not /alloc/logs).","Check for symlinks or copies in the log dir created by backup/collector tooling and exclude them.","If caused by a Nomad version mismatch, ensure client and server versions are compatible."],"exampleFix":"// before\n# task writes diagnostics into /alloc/logs\ncommand = \"myapp > /alloc/logs/web.stdout.0.txt 2>&1\"\n// after\n# write task output outside the Nomad log dir\ncommand = \"myapp > /local/myapp.log 2>&1\"","handlingStrategy":"validation","validationCode":"entries, _, err := client.AllocFS().List(alloc, \"/alloc/logs\", nil)\nif err != nil {\n    return err\n}\nre := regexp.MustCompile(`^[^.]+\\.(stdout|stderr)\\.\\d+$`)\nfor _, e := range entries {\n    if !re.MatchString(e.Name) {\n        return fmt.Errorf(\"unexpected file in log dir: %s\", e.Name)\n    }\n}","typeGuard":"func isIndexParseErr(err error) bool {\n    return strings.Contains(err.Error(), \"to a log index\")\n}","tryCatchPattern":"if err := readLogs(); err != nil {\n    if isIndexParseErr(err) {\n        return fmt.Errorf(\"clean non-Nomad files from /alloc/logs and retry\")\n    }\n    return err\n}","preventionTips":["Never write task output files into /alloc/logs.","Keep backups/renames (*.bak, *.tmp) out of the log directory.","Audit the log dir with ls before programmatic log reads in shared environments."],"tags":["nomad","client","logs","parsing","filesystem"],"backgroundTag":"log-file-name-parse-failed","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"}