{"record":{"id":"bbff01c5dd69112b","repo":"go-delve/delve","slug":"cannot-rebuild-a-binary","errorCode":null,"errorMessage":"cannot rebuild a binary","messagePattern":"cannot rebuild a binary","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"service/debugger/debugger.go","lineNumber":540,"sourceCode":"\t}\n\tvar grp *proc.TargetGroup\n\tvar err error\n\n\tif rebuild {\n\t\tswitch d.config.ExecuteKind {\n\t\tcase ExecutingGeneratedFile:\n\t\t\terr = gobuild.GoBuild(d.processArgs[0], d.config.Packages, d.config.BuildFlags)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"could not rebuild process: %s\", err)\n\t\t\t}\n\t\tcase ExecutingGeneratedTest:\n\t\t\terr = gobuild.GoTestBuild(d.processArgs[0], d.config.Packages, d.config.BuildFlags)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"could not rebuild process: %s\", err)\n\t\t\t}\n\t\tdefault:\n\t\t\t// We cannot build a process that we didn't start, because we don't know how it was built.\n\t\t\treturn nil, errors.New(\"cannot rebuild a binary\")\n\t\t}\n\t}\n\n\tif recorded {\n\t\trun, stop, err2 := gdbserial.RecordAsync(d.processArgs, d.config.WorkingDir, false, d.config.Stdin, d.config.Stdout, d.config.Stderr)\n\t\tif err2 != nil {\n\t\t\treturn nil, err2\n\t\t}\n\n\t\td.recordingStart(stop)\n\t\tgrp, err = d.recordingRun(run)\n\t\td.recordingDone()\n\t} else {\n\t\tgrp, err = d.Launch(d.processArgs, d.config.WorkingDir)\n\t}\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"could not launch process: %s\", err)\n\t}","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/service/debugger/debugger.go#L522-L558","documentation":"Restart-by-rebuild is only possible when Delve launched the target itself and therefore knows the build command. This error is thrown by Restart when the current target was started by attaching to or executing a prebuilt binary (or core) and a restart that requires recompilation is requested. Delve refuses because it cannot know the original build flags and inputs.","triggerScenarios":"Calling Debugger.Restart (or `dlv restart`) on a session where the target was started via `dlv attach`, `dlv exec`, or a core dump, and the restart path falls through to the default branch that would need to rebuild the executable.","commonSituations":"Developers attaching to an already-built binary who then try to restart after changing code; CI pipelines restarting exec-mode sessions; using `restart` with `--rerun` style flags against a non-Delve-built binary.","solutions":["Relaunch the session with `dlv debug` or `dlv test` so Delve owns the build, then restart works","Rebuild the binary yourself and start a fresh session with `dlv exec <binary>`","Use `restart` only in sessions started in debug/test mode"],"exampleFix":"// before\ndlv exec ./app   // restart fails: cannot rebuild a binary\n// after\ndlv debug .      // Delve builds it; `restart` now rebuilds and relaunches","handlingStrategy":"validation","validationCode":"func canRestart(cfg *service.Config, startedByDelve bool) error {\n    if !startedByDelve && cfg.AttachPid != 0 {\n        return errors.New(\"restart requires a delve-launched session (use dlv debug/dlv test)\")\n    }\n    return nil\n}","typeGuard":"func isRestartable(recording bool, attachPid int, coreFile string) bool {\n    return attachPid == 0 && coreFile == \"\"\n}","tryCatchPattern":"_, err := dbg.Restart(rebuild, rerun)\nif err != nil && err.Error() == \"cannot rebuild a binary\" {\n    // fall back: rebuild externally, then relaunch with dbg.Launch/exec\n}","preventionTips":["Use `dlv debug`/`dlv test` when you plan to restart during iteration","Treat `dlv exec`/`dlv attach` sessions as non-restartable in tooling","Script your own rebuild+relaunch loop for exec-mode debugging"],"tags":["restart","build","debugger"],"backgroundTag":"non-rebuildable-target","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}