Merge pull request #3092 from code-yeongyu/fix/prepublish-legacy-config

fix(shared): close legacy config migration gaps
This commit is contained in:
YeonGyu-Kim
2026-04-04 01:46:52 +09:00
committed by GitHub
15 changed files with 243 additions and 95 deletions
+34 -1
View File
@@ -1,18 +1,26 @@
import { describe, expect, test, beforeEach, afterEach } from "bun:test"
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
import { detectExternalNotificationPlugin, getNotificationConflictWarning, detectExternalSkillPlugin, getSkillPluginConflictWarning } from "./external-plugin-detector"
import * as fs from "node:fs"
import * as path from "node:path"
import * as os from "node:os"
async function importFreshExternalPluginDetectorModule(): Promise<typeof import("./external-plugin-detector")> {
return import(`./external-plugin-detector?test=${Date.now()}-${Math.random()}`)
}
describe("external-plugin-detector", () => {
let tempDir: string
let tempHomeDir: string
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "omo-test-"))
tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), "omo-home-"))
})
afterEach(() => {
mock.restore()
fs.rmSync(tempDir, { recursive: true, force: true })
fs.rmSync(tempHomeDir, { recursive: true, force: true })
})
describe("detectExternalNotificationPlugin", () => {
@@ -399,6 +407,31 @@ describe("external-plugin-detector", () => {
expect(result.pluginName).toBe("opencode-skills")
})
test("should detect user-level opencode-skills when project config exists without plugins", async () => {
// given
const projectConfigDir = path.join(tempDir, ".opencode")
const userConfigDir = path.join(tempHomeDir, ".config", "opencode")
fs.mkdirSync(projectConfigDir, { recursive: true })
fs.mkdirSync(userConfigDir, { recursive: true })
fs.writeFileSync(path.join(projectConfigDir, "opencode.json"), JSON.stringify({}))
fs.writeFileSync(path.join(userConfigDir, "opencode.json"), JSON.stringify({ plugin: ["opencode-skills"] }))
const nodeOs = await import("node:os")
mock.module("node:os", () => ({
...nodeOs,
homedir: () => tempHomeDir,
}))
const { detectExternalSkillPlugin: detectExternalSkillPluginFresh } = await importFreshExternalPluginDetectorModule()
// when
const result = detectExternalSkillPluginFresh(tempDir)
// then
expect(result.detected).toBe(true)
expect(result.pluginName).toBe("opencode-skills")
expect(result.allPlugins).toEqual(["opencode-skills"])
})
test("should NOT match opencode-skills-extra (suffix variation)", () => {
// given - plugin with similar name but different suffix
const opencodeDir = path.join(tempDir, ".opencode")
+1 -48
View File
@@ -3,15 +3,8 @@
* Used to prevent crashes from concurrent notification plugins.
*/
import * as fs from "node:fs"
import * as path from "node:path"
import * as os from "node:os"
import { loadOpencodePlugins } from "./load-opencode-plugins"
import { log } from "./logger"
import { parseJsoncSafe } from "./jsonc-parser"
interface OpencodeConfig {
plugin?: string[]
}
/**
* Known notification plugins that conflict with oh-my-opencode's session-notification.
@@ -34,46 +27,6 @@ const KNOWN_SKILL_PLUGINS = [
"@opencode/skills",
]
function getWindowsAppdataDir(): string | null {
return process.env.APPDATA || null
}
function getConfigPaths(directory: string): string[] {
const crossPlatformDir = path.join(os.homedir(), ".config")
const paths = [
path.join(directory, ".opencode", "opencode.json"),
path.join(directory, ".opencode", "opencode.jsonc"),
path.join(crossPlatformDir, "opencode", "opencode.json"),
path.join(crossPlatformDir, "opencode", "opencode.jsonc"),
]
if (process.platform === "win32") {
const appdataDir = getWindowsAppdataDir()
if (appdataDir) {
paths.push(path.join(appdataDir, "opencode", "opencode.json"))
paths.push(path.join(appdataDir, "opencode", "opencode.jsonc"))
}
}
return paths
}
function loadOpencodePlugins(directory: string): string[] {
for (const configPath of getConfigPaths(directory)) {
try {
if (!fs.existsSync(configPath)) continue
const content = fs.readFileSync(configPath, "utf-8")
const result = parseJsoncSafe<OpencodeConfig>(content)
if (result.data) {
return result.data.plugin ?? []
}
} catch {
continue
}
}
return []
}
/**
* Check if a plugin entry matches a known notification plugin.
* Handles various formats: "name", "name@version", "npm:name", "file://path/name"
+58
View File
@@ -0,0 +1,58 @@
import * as fs from "node:fs"
import * as os from "node:os"
import * as path from "node:path"
import { parseJsoncSafe } from "./jsonc-parser"
interface OpencodeConfig {
plugin?: string[]
}
function getWindowsAppdataDir(): string | null {
return process.env.APPDATA || null
}
function getConfigPaths(directory: string): string[] {
const crossPlatformDir = path.join(os.homedir(), ".config")
const paths = [
path.join(directory, ".opencode", "opencode.json"),
path.join(directory, ".opencode", "opencode.jsonc"),
path.join(crossPlatformDir, "opencode", "opencode.json"),
path.join(crossPlatformDir, "opencode", "opencode.jsonc"),
]
if (process.platform === "win32") {
const appdataDir = getWindowsAppdataDir()
if (appdataDir) {
paths.push(path.join(appdataDir, "opencode", "opencode.json"))
paths.push(path.join(appdataDir, "opencode", "opencode.jsonc"))
}
}
return paths
}
export function loadOpencodePlugins(directory: string): string[] {
const pluginEntries: string[] = []
const seenPluginEntries = new Set<string>()
for (const configPath of getConfigPaths(directory)) {
try {
if (!fs.existsSync(configPath)) continue
const content = fs.readFileSync(configPath, "utf-8")
const result = parseJsoncSafe<OpencodeConfig>(content)
const plugins = result.data?.plugin ?? []
for (const plugin of plugins) {
if (seenPluginEntries.has(plugin)) continue
seenPluginEntries.add(plugin)
pluginEntries.push(plugin)
}
} catch {
continue
}
}
return pluginEntries
}
@@ -26,7 +26,7 @@ mock.module("./logger", () => ({
log: mockLog,
}))
mock.module("./migrate-legacy-plugin-entry", () => ({
mock.module("./plugin-entry-migrator", () => ({
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
}))
@@ -1,6 +1,6 @@
import { checkForLegacyPluginEntry } from "./legacy-plugin-warning"
import { log } from "./logger"
import { migrateLegacyPluginEntry } from "./migrate-legacy-plugin-entry"
import { migrateLegacyPluginEntry } from "./plugin-entry-migrator"
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "./plugin-identity"
function toCanonicalEntry(entry: string): string {
@@ -18,15 +18,19 @@ describe("migrateLegacyConfigFile", () => {
describe("#given oh-my-opencode.jsonc exists but oh-my-openagent.jsonc does not", () => {
describe("#when migrating the config file", () => {
it("#then copies to oh-my-openagent.jsonc", () => {
it("#then writes oh-my-openagent.jsonc and renames the legacy file to a backup", () => {
const legacyPath = join(testDir, "oh-my-opencode.jsonc")
const backupPath = join(testDir, "oh-my-opencode.jsonc.bak")
writeFileSync(legacyPath, '{ "agents": {} }')
const result = migrateLegacyConfigFile(legacyPath)
expect(result).toBe(true)
expect(existsSync(join(testDir, "oh-my-openagent.jsonc"))).toBe(true)
expect(existsSync(legacyPath)).toBe(false)
expect(existsSync(backupPath)).toBe(true)
expect(readFileSync(join(testDir, "oh-my-openagent.jsonc"), "utf-8")).toBe('{ "agents": {} }')
expect(readFileSync(backupPath, "utf-8")).toBe('{ "agents": {} }')
})
})
})
+51 -5
View File
@@ -1,4 +1,4 @@
import { existsSync, copyFileSync, renameSync } from "node:fs"
import { closeSync, existsSync, fsyncSync, openSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"
import { join, dirname, basename } from "node:path"
import { log } from "./logger"
@@ -10,6 +10,49 @@ function buildCanonicalPath(legacyPath: string): string {
return join(dir, `${CONFIG_BASENAME}${ext}`)
}
function writeFileAtomically(filePath: string, content: string): void {
const tempPath = `${filePath}.tmp`
writeFileSync(tempPath, content, "utf-8")
const tempFileDescriptor = openSync(tempPath, "r")
try {
fsyncSync(tempFileDescriptor)
} finally {
closeSync(tempFileDescriptor)
}
renameSync(tempPath, filePath)
}
function archiveLegacyConfigFile(legacyPath: string): boolean {
const backupPath = `${legacyPath}.bak`
try {
renameSync(legacyPath, backupPath)
log("[migrateLegacyConfigFile] Legacy config was migrated and renamed to backup. Update the canonical file only.", {
legacyPath,
backupPath,
})
return true
} catch (renameError) {
try {
rmSync(legacyPath)
log("[migrateLegacyConfigFile] Legacy config was migrated and removed after backup rename failed. Update the canonical file only.", {
legacyPath,
backupPath,
renameError,
})
return true
} catch (removeError) {
log("[migrateLegacyConfigFile] WARNING: canonical config was written but the legacy file still exists and will be ignored. Remove or rename it manually.", {
legacyPath,
backupPath,
renameError,
removeError,
})
return false
}
}
}
export function migrateLegacyConfigFile(legacyPath: string): boolean {
if (!existsSync(legacyPath)) return false
if (!basename(legacyPath).startsWith(LEGACY_CONFIG_BASENAME)) return false
@@ -18,14 +61,17 @@ export function migrateLegacyConfigFile(legacyPath: string): boolean {
if (existsSync(canonicalPath)) return false
try {
copyFileSync(legacyPath, canonicalPath)
log("[migrateLegacyConfigFile] Copied legacy config to canonical path", {
const content = readFileSync(legacyPath, "utf-8")
writeFileAtomically(canonicalPath, content)
const archivedLegacyConfig = archiveLegacyConfigFile(legacyPath)
log("[migrateLegacyConfigFile] Migrated legacy config to canonical path", {
from: legacyPath,
to: canonicalPath,
archivedLegacyConfig,
})
return true
return archivedLegacyConfig
} catch (error) {
log("[migrateLegacyConfigFile] Failed to copy legacy config file", { legacyPath, error })
log("[migrateLegacyConfigFile] Failed to migrate legacy config file", { legacyPath, error })
return false
}
}
+1
View File
@@ -0,0 +1 @@
export { migrateLegacyPluginEntry } from "./migrate-legacy-plugin-entry"