{"record":{"id":"45295f4480faa65b","repo":"golang/go","slug":"failed-to-read-trace-file-w","errorCode":null,"errorMessage":"failed to read trace file: %w","messagePattern":"failed to read trace file: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/trace/main.go","lineNumber":98,"sourceCode":"\tflag.Parse()\n\tcounter.Inc(\"trace/invocations\")\n\tcounter.CountFlags(\"trace/flag:\", *flag.CommandLine)\n\n\t// Go 1.7 traces embed symbol info and does not require the binary.\n\t// But we optionally accept binary as first arg for Go 1.5 traces.\n\tswitch flag.NArg() {\n\tcase 1:\n\t\ttraceFile = flag.Arg(0)\n\tcase 2:\n\t\tprogramBinary = flag.Arg(0)\n\t\ttraceFile = flag.Arg(1)\n\tdefault:\n\t\tflag.Usage()\n\t}\n\n\ttracef, err := os.Open(traceFile)\n\tif err != nil {\n\t\tlogAndDie(fmt.Errorf(\"failed to read trace file: %w\", err))\n\t}\n\tdefer tracef.Close()\n\n\t// Get the size of the trace file.\n\tfi, err := tracef.Stat()\n\tif err != nil {\n\t\tlogAndDie(fmt.Errorf(\"failed to stat trace file: %v\", err))\n\t}\n\ttraceSize := fi.Size()\n\n\t// Handle requests for profiles.\n\tif *pprofFlag != \"\" {\n\t\tparsed, err := parseTrace(tracef, traceSize)\n\t\tif err != nil {\n\t\t\tlogAndDie(err)\n\t\t}\n\t\tvar f traceviewer.ProfileFunc\n\t\tswitch *pprofFlag {","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/trace/main.go#L80-L116","documentation":"The `go tool trace` command failed to open the trace file specified on the command line. This is a standard OS-level file open error wrapped with context via `logAndDie`, which prints the error and exits. The trace file is a binary file produced by `runtime/trace` containing execution trace events from a Go program.","triggerScenarios":"Fires at trace/main.go:96-98 when `os.Open(traceFile)` returns an error. The trace file path comes from command-line arguments (either the only argument or the second argument when a binary is also specified). The `logAndDie` function prints the error to stderr and calls `os.Exit(1)`.","commonSituations":"Typo in the trace file path. The trace file was deleted or never generated. Insufficient file permissions. Wrong working directory when using a relative path. The file path was accidentally given as the first of two args (interpreted as program binary, not trace file). Trying to open a directory instead of a file.","solutions":["Verify the trace file exists: `ls -la <trace_file>`.","Check file permissions: ensure read access.","If using relative paths, verify the working directory or switch to an absolute path.","Ensure the trace file was generated correctly: use `runtime/trace.Start(f)` and `runtime/trace.Stop()` in your program.","Verify you're passing the correct number of arguments: `go tool trace <trace_file>` or `go tool trace <binary> <trace_file>`."],"exampleFix":"# Before: wrong path or missing file\ngo tool trace trace.out  # file doesn't exist\n\n# After: generate trace file first, then verify path\n# In Go program:\n#   f, _ := os.Create(\"trace.out\")\n#   trace.Start(f)\n#   ... run workload ...\n#   trace.Stop()\n#   f.Close()\n\nls -la trace.out\ngo tool trace trace.out","handlingStrategy":"validation","validationCode":"// Validate trace file exists before passing to go tool trace\ntraceFile := flag.Arg(0)\nif info, err := os.Stat(traceFile); err != nil {\n    log.Fatalf(\"Trace file not found: %s\", traceFile)\n} else if info.IsDir() {\n    log.Fatalf(\"Trace path is a directory, not a file: %s\", traceFile)\n} else if info.Size() == 0 {\n    log.Fatalf(\"Trace file is empty: %s\", traceFile)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify the trace file path with ls before running go tool trace.","Use absolute paths to avoid working-directory confusion.","Ensure runtime/trace.Start/Stop completed successfully before using the trace file.","Check that you pass the trace file as the correct argument (first or second depending on whether binary is also given)."],"tags":["trace","file-io","runtime-trace","command-line","go-tool"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}