cli/cli · error

loading ssh config: %w

Error message

loading ssh config: %w

What it means

Returned by firstConfiguredKeyPair while scanning 'ssh -G' output for identityfile entries: the first configured IdentityFile was found, but keypairForPrivateKey failed on it for a reason other than the file not existing (which is skipped with continue). This means the key path exists but cannot be turned into a usable KeyPair.

Source

Thrown at pkg/cmd/codespace/ssh.go:513

	sshGCmd := exec.CommandContext(ctx, sshExe, sshGArgs...)
	configBytes, err := sshGCmd.Output()
	if err != nil {
		return nil, fmt.Errorf("could not load ssh configuration: %w", err)
	}

	configLines := strings.Split(string(configBytes), "\n")
	for _, line := range configLines {
		line = strings.TrimSpace(line)

		if strings.HasPrefix(line, "identityfile ") {
			privateKeyPath := strings.SplitN(line, " ", 2)[1]

			keypair, err := keypairForPrivateKey(privateKeyPath)
			if errors.Is(err, errKeyFileNotFound) {
				continue
			}
			if err != nil {
				return nil, fmt.Errorf("loading ssh config: %w", err)
			}

			return keypair, nil
		}
	}

	return nil, errKeyFileNotFound
}

// keypairForPrivateKey returns the KeyPair with the specified private key if it and the public key both exist
func keypairForPrivateKey(privateKeyPath string) (*ssh.KeyPair, error) {
	if strings.HasPrefix(privateKeyPath, "~") {
		userHomeDir, err := os.UserHomeDir()
		if err != nil {
			return nil, fmt.Errorf("getting home dir: %w", err)
		}

		// os.Stat can't handle ~, so convert it to the real path

View on GitHub (pinned to 0eeec0b92e)

Solutions

  1. Ensure HOME (Unix) / USERPROFILE (Windows) is set in the shell running gh
  2. Replace '~'-relative IdentityFile paths with absolute paths in ssh config
  3. Check the wrapped error text to see which key path failed and fix or remove that IdentityFile entry
Defensive patterns

Strategy: validation

Validate before calling

if os.Getenv("HOME") == "" && runtime.GOOS != "windows" {
    return errors.New("HOME unset; tilde IdentityFile paths cannot expand")
}

Prevention

When it happens

Trigger: An 'identityfile <path>' line is present in the ssh -G output and keypairForPrivateKey(privateKeyPath) returns an error that is not errKeyFileNotFound, e.g. os.UserHomeDir() failing while expanding a '~' prefixed path.

Common situations: HOME or USERPROFILE environment variable unset so '~/.ssh/id_rsa' cannot expand; key path with '~' in a position where replacement produces an invalid path.

Related errors


AI-assisted analysis of cli/cli@0eeec0b92e (2026-08-15). Data as JSON: /api/errors/42e7ec195c612b7b. Report an issue: GitHub.