can1357/oh-my-pi · error

Host "${name}" already exists in ${filePath}

Error message

Host "${name}" already exists in ${filePath}

What it means

addSSHHost refuses to overwrite: after reading the existing config it checks hosts[name] and throws if a host with the same name already exists at that file path. Updates must go through updateSSHHost instead.

Source

Thrown at packages/coding-agent/src/ssh/config-writer.ts:102

 */
export async function addSSHHost(filePath: string, name: string, hostConfig: SSHHostConfig): Promise<void> {
	// Validate host name
	const nameError = validateHostName(name);
	if (nameError) {
		throw new Error(nameError);
	}

	// Validate host field
	if (!hostConfig.host) {
		throw new Error("Host address cannot be empty");
	}

	// Read existing config
	const existing = await readSSHConfigFile(filePath);

	// Check for duplicate name
	if (existing.hosts?.[name]) {
		throw new Error(`Host "${name}" already exists in ${filePath}`);
	}

	// Add host
	const updated: SSHConfigFile = {
		...existing,
		hosts: {
			...existing.hosts,
			[name]: hostConfig,
		},
	};

	// Write back
	await writeSSHConfigFile(filePath, updated);
}

/**
 * Update an existing SSH host in a config file.
 * If the host doesn't exist, this will add it.

View on GitHub (pinned to 9690622007)

Solutions

  1. Use updateSSHHost(filePath, name, cfg) to modify an existing entry
  2. Pick a different unique alias for the new host
  3. Call removeSSHHost first if replacement is intended, then addSSHHost
  4. Check readSSHConfigFile(filePath).hosts[name] before adding to branch add-vs-update in your handler

Example fix

// before
await addSSHHost(path, "prod", cfg); // throws if exists
// after
const existing = (await readSSHConfigFile(path)).hosts?.["prod"];
if (existing) await updateSSHHost(path, "prod", cfg);
else await addSSHHost(path, "prod", cfg);
Defensive patterns

Strategy: validation

Validate before calling

const existing = (await readSSHConfigFile(filePath)).hosts?.[name];
if (existing) await updateSSHHost(filePath, name, cfg);
else await addSSHHost(filePath, name, cfg);

Type guard

null

Try / catch

try { await addSSHHost(p, name, cfg); } catch (e) { if (e instanceof Error && e.message.includes("already exists")) await updateSSHHost(p, name, cfg); else throw e; }

Prevention

When it happens

Trigger: Calling addSSHHost for a name already present in existing.hosts of the target config file; running the add CLI command twice with the same alias.

Common situations: Re-running an add command after a previous successful add, importing host lists that contain duplicate aliases, or users expecting add to behave as upsert.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/1040049147b4e850. Report an issue: GitHub.