github/copilot-sdk · error

failed to parse port

Error message

failed to parse port: %w

What it means

The CLI server printed a line matching the port regex, but the captured number could not be converted to an integer via strconv.Atoi. This is an internal invariant violation: the regex matched something that is not a valid Go int (e.g. overflow or malformed digits), and the process is killed before returning.

Solutions

  1. Check CLI and client library versions match; upgrade the library to support the CLI's current port format.
  2. Capture the offending stdout line (enable debug logging) to see what the regex matched.
  3. If on a 32-bit platform, test on 64-bit or report the overflow.
  4. File an issue with the stdout line if the CLI output looks correct.
Defensive patterns

Strategy: retry

Try / catch

if err := client.Start(ctx); err != nil {
    if strings.Contains(err.Error(), "failed to parse port") {
        return fmt.Errorf("CLI/client version mismatch on port handshake: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: The CLI printed a port-like token that fails Atoi — port number exceeding int range, empty/near-empty match, or a regex that matched more of the line than intended.

Common situations: Version mismatch where a new CLI version changed its stdout port-report format; locale/formatting changes in CLI output; 32-bit platforms where large ports overflow int.

Understand the failure class

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/8a966dc8b34bf794. Report an issue: GitHub.

Appendix: source

Thrown at go/client.go:2246

				}
				return errors.Join(baseErr, killErr)
			case <-c.processDone:
				killErr := c.killProcess()
				baseErr := errors.New("CLI server process exited before reporting port")
				if buf, ok := proc.Stderr.(*truncbuffer.TruncBuffer); ok {
					if stderr := strings.TrimSpace(buf.String()); stderr != "" {
						baseErr = fmt.Errorf("%w; stderr: %s", baseErr, stderr)
					}
				}
				return errors.Join(baseErr, killErr)
			default:
				if scanner.Scan() {
					line := scanner.Text()
					if matches := portRegex.FindStringSubmatch(line); len(matches) > 1 {
						port, err := strconv.Atoi(matches[1])
						if err != nil {
							killErr := c.killProcess()
							return errors.Join(fmt.Errorf("failed to parse port: %w", err), killErr)
						}
						c.actualPort = port
						return nil
					}
				}
			}
		}
	}
}

// startInProcess loads the native runtime library and wires the JSON-RPC client
// to its FFI byte streams.
func (c *Client) startInProcess(ctx context.Context) error {
	if !inProcessAvailable {
		return errors.New("in-process transport unavailable: rebuild with -tags copilot_inprocess on a supported platform")
	}

	cliEntrypoint := c.cliPath

View on GitHub (pinned to cd8cf15dc3)