fix(#2823): auto-migrate legacy plugin name and warn users at startup
- logLegacyPluginStartupWarning now emits console.warn (visible to user, not just log file) when oh-my-opencode is detected in opencode.json - Auto-migrates opencode.json plugin entry from oh-my-opencode to oh-my-openagent (with backup) - plugin-config.ts: add console.warn when loading legacy config filename - test: 10 tests covering migration, console output, edge cases
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
|
||||
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { autoMigrateLegacyPluginEntry } from "./auto-migrate"
|
||||
|
||||
describe("autoMigrateLegacyPluginEntry", () => {
|
||||
let testConfigDir = ""
|
||||
|
||||
beforeEach(() => {
|
||||
testConfigDir = join(tmpdir(), `omo-legacy-migrate-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
mkdirSync(testConfigDir, { recursive: true })
|
||||
process.env.OPENCODE_CONFIG_DIR = testConfigDir
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(testConfigDir, { recursive: true, force: true })
|
||||
delete process.env.OPENCODE_CONFIG_DIR
|
||||
})
|
||||
|
||||
describe("#given opencode.json has a bare legacy plugin entry", () => {
|
||||
it("#then replaces oh-my-opencode with oh-my-openagent", () => {
|
||||
// given
|
||||
writeFileSync(
|
||||
join(testConfigDir, "opencode.json"),
|
||||
JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2) + "\n",
|
||||
)
|
||||
|
||||
// when
|
||||
const result = autoMigrateLegacyPluginEntry()
|
||||
|
||||
// then
|
||||
expect(result.migrated).toBe(true)
|
||||
expect(result.from).toBe("oh-my-opencode")
|
||||
expect(result.to).toBe("oh-my-openagent")
|
||||
const saved = JSON.parse(readFileSync(join(testConfigDir, "opencode.json"), "utf-8"))
|
||||
expect(saved.plugin).toEqual(["oh-my-openagent"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given opencode.json has a version-pinned legacy entry", () => {
|
||||
it("#then preserves the version suffix", () => {
|
||||
// given
|
||||
writeFileSync(
|
||||
join(testConfigDir, "opencode.json"),
|
||||
JSON.stringify({ plugin: ["oh-my-opencode@3.10.0"] }, null, 2) + "\n",
|
||||
)
|
||||
|
||||
// when
|
||||
const result = autoMigrateLegacyPluginEntry()
|
||||
|
||||
// then
|
||||
expect(result.migrated).toBe(true)
|
||||
expect(result.from).toBe("oh-my-opencode@3.10.0")
|
||||
expect(result.to).toBe("oh-my-openagent@3.10.0")
|
||||
const saved = JSON.parse(readFileSync(join(testConfigDir, "opencode.json"), "utf-8"))
|
||||
expect(saved.plugin).toEqual(["oh-my-openagent@3.10.0"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given both canonical and legacy entries exist", () => {
|
||||
it("#then removes legacy entry and keeps canonical", () => {
|
||||
// given
|
||||
writeFileSync(
|
||||
join(testConfigDir, "opencode.json"),
|
||||
JSON.stringify({ plugin: ["oh-my-openagent", "oh-my-opencode"] }, null, 2) + "\n",
|
||||
)
|
||||
|
||||
// when
|
||||
const result = autoMigrateLegacyPluginEntry()
|
||||
|
||||
// then
|
||||
expect(result.migrated).toBe(true)
|
||||
const saved = JSON.parse(readFileSync(join(testConfigDir, "opencode.json"), "utf-8"))
|
||||
expect(saved.plugin).toEqual(["oh-my-openagent"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given no config file exists", () => {
|
||||
it("#then returns migrated false", () => {
|
||||
// given - empty dir
|
||||
|
||||
// when
|
||||
const result = autoMigrateLegacyPluginEntry()
|
||||
|
||||
// then
|
||||
expect(result.migrated).toBe(false)
|
||||
expect(result.from).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given opencode.jsonc has comments and a legacy entry", () => {
|
||||
it("#then preserves comments and replaces entry", () => {
|
||||
// given
|
||||
writeFileSync(
|
||||
join(testConfigDir, "opencode.jsonc"),
|
||||
'{\n // my config\n "plugin": ["oh-my-opencode"]\n}\n',
|
||||
)
|
||||
|
||||
// when
|
||||
const result = autoMigrateLegacyPluginEntry()
|
||||
|
||||
// then
|
||||
expect(result.migrated).toBe(true)
|
||||
const content = readFileSync(join(testConfigDir, "opencode.jsonc"), "utf-8")
|
||||
expect(content).toContain("// my config")
|
||||
expect(content).toContain("oh-my-openagent")
|
||||
expect(content).not.toContain("oh-my-opencode")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given only canonical entry exists", () => {
|
||||
it("#then returns migrated false and leaves file untouched", () => {
|
||||
// given
|
||||
const original = JSON.stringify({ plugin: ["oh-my-openagent"] }, null, 2) + "\n"
|
||||
writeFileSync(join(testConfigDir, "opencode.json"), original)
|
||||
|
||||
// when
|
||||
const result = autoMigrateLegacyPluginEntry()
|
||||
|
||||
// then
|
||||
expect(result.migrated).toBe(false)
|
||||
const content = readFileSync(join(testConfigDir, "opencode.json"), "utf-8")
|
||||
expect(content).toBe(original)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,81 @@
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs"
|
||||
|
||||
import { parseJsoncSafe } from "../../shared/jsonc-parser"
|
||||
import { getOpenCodeConfigPaths } from "../../shared/opencode-config-dir"
|
||||
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity"
|
||||
|
||||
export interface MigrationResult {
|
||||
migrated: boolean
|
||||
from: string | null
|
||||
to: string | null
|
||||
configPath: string | null
|
||||
}
|
||||
|
||||
interface OpenCodeConfig {
|
||||
plugin?: string[]
|
||||
}
|
||||
|
||||
function isLegacyEntry(entry: string): boolean {
|
||||
return entry === LEGACY_PLUGIN_NAME || entry.startsWith(`${LEGACY_PLUGIN_NAME}@`)
|
||||
}
|
||||
|
||||
function isCanonicalEntry(entry: string): boolean {
|
||||
return entry === PLUGIN_NAME || entry.startsWith(`${PLUGIN_NAME}@`)
|
||||
}
|
||||
|
||||
function toLegacyCanonical(entry: string): string {
|
||||
if (entry === LEGACY_PLUGIN_NAME) return PLUGIN_NAME
|
||||
if (entry.startsWith(`${LEGACY_PLUGIN_NAME}@`)) {
|
||||
return `${PLUGIN_NAME}${entry.slice(LEGACY_PLUGIN_NAME.length)}`
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
function detectOpenCodeConfigPath(): string | null {
|
||||
const paths = getOpenCodeConfigPaths({ binary: "opencode", version: null })
|
||||
if (existsSync(paths.configJsonc)) return paths.configJsonc
|
||||
if (existsSync(paths.configJson)) return paths.configJson
|
||||
return null
|
||||
}
|
||||
|
||||
export function autoMigrateLegacyPluginEntry(): MigrationResult {
|
||||
const configPath = detectOpenCodeConfigPath()
|
||||
if (!configPath) return { migrated: false, from: null, to: null, configPath: null }
|
||||
|
||||
try {
|
||||
const content = readFileSync(configPath, "utf-8")
|
||||
const parseResult = parseJsoncSafe<OpenCodeConfig>(content)
|
||||
if (!parseResult.data?.plugin) return { migrated: false, from: null, to: null, configPath }
|
||||
|
||||
const plugins = parseResult.data.plugin
|
||||
const legacyEntries = plugins.filter(isLegacyEntry)
|
||||
if (legacyEntries.length === 0) return { migrated: false, from: null, to: null, configPath }
|
||||
|
||||
const hasCanonical = plugins.some(isCanonicalEntry)
|
||||
const from = legacyEntries[0]
|
||||
const to = toLegacyCanonical(from)
|
||||
|
||||
const normalized = hasCanonical
|
||||
? plugins.filter((p) => !isLegacyEntry(p))
|
||||
: plugins.map((p) => (isLegacyEntry(p) ? toLegacyCanonical(p) : p))
|
||||
|
||||
const isJsonc = configPath.endsWith(".jsonc")
|
||||
if (isJsonc) {
|
||||
const pluginArrayRegex = /((?:"plugin"|plugin)\s*:\s*)\[([\s\S]*?)\]/
|
||||
const match = content.match(pluginArrayRegex)
|
||||
if (match) {
|
||||
const formattedPlugins = normalized.map((p) => `"${p}"`).join(",\n ")
|
||||
const newContent = content.replace(pluginArrayRegex, `$1[\n ${formattedPlugins}\n ]`)
|
||||
writeFileSync(configPath, newContent)
|
||||
return { migrated: true, from, to, configPath }
|
||||
}
|
||||
}
|
||||
|
||||
const parsed = JSON.parse(content) as Record<string, unknown>
|
||||
parsed.plugin = normalized
|
||||
writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n")
|
||||
return { migrated: true, from, to, configPath }
|
||||
} catch {
|
||||
return { migrated: false, from: null, to: null, configPath }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
|
||||
import type { MigrationResult } from "./auto-migrate"
|
||||
|
||||
const mockCheckForLegacyPluginEntry = mock(() => ({
|
||||
hasLegacyEntry: false,
|
||||
hasCanonicalEntry: false,
|
||||
legacyEntries: [] as string[],
|
||||
}))
|
||||
|
||||
const mockAutoMigrate = mock((): MigrationResult => ({
|
||||
migrated: false,
|
||||
from: null,
|
||||
to: null,
|
||||
configPath: null,
|
||||
}))
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const mockShowToast = mock((_arg: any) => Promise.resolve())
|
||||
const mockLog = mock(() => {})
|
||||
|
||||
mock.module("../../shared/legacy-plugin-warning", () => ({
|
||||
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
|
||||
}))
|
||||
|
||||
mock.module("../../shared/logger", () => ({
|
||||
log: mockLog,
|
||||
}))
|
||||
|
||||
mock.module("./auto-migrate", () => ({
|
||||
autoMigrateLegacyPluginEntry: mockAutoMigrate,
|
||||
}))
|
||||
|
||||
afterAll(() => {
|
||||
mock.restore()
|
||||
})
|
||||
|
||||
function createMockCtx() {
|
||||
return {
|
||||
client: {
|
||||
tui: { showToast: mockShowToast },
|
||||
},
|
||||
directory: "/tmp/test",
|
||||
} as never
|
||||
}
|
||||
|
||||
function createEvent(type: string, parentID?: string) {
|
||||
return {
|
||||
event: {
|
||||
type,
|
||||
properties: parentID ? { info: { parentID } } : { info: {} },
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async function importFreshModule() {
|
||||
return import(`./hook?t=${Date.now()}-${Math.random()}`)
|
||||
}
|
||||
|
||||
describe("createLegacyPluginToastHook", () => {
|
||||
beforeEach(() => {
|
||||
mockCheckForLegacyPluginEntry.mockReset()
|
||||
mockAutoMigrate.mockReset()
|
||||
mockShowToast.mockReset()
|
||||
mockLog.mockReset()
|
||||
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue({
|
||||
hasLegacyEntry: false,
|
||||
hasCanonicalEntry: true,
|
||||
legacyEntries: [],
|
||||
})
|
||||
mockAutoMigrate.mockReturnValue({ migrated: false, from: null, to: null, configPath: null })
|
||||
mockShowToast.mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
describe("#given no legacy entry exists", () => {
|
||||
it("#then does not show a toast", async () => {
|
||||
// given
|
||||
const { createLegacyPluginToastHook } = await importFreshModule()
|
||||
const hook = createLegacyPluginToastHook(createMockCtx())
|
||||
|
||||
// when
|
||||
await hook.event(createEvent("session.created"))
|
||||
|
||||
// then
|
||||
expect(mockShowToast).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given legacy entry exists and migration succeeds", () => {
|
||||
it("#then shows success toast", async () => {
|
||||
// given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue({
|
||||
hasLegacyEntry: true,
|
||||
hasCanonicalEntry: false,
|
||||
legacyEntries: ["oh-my-opencode"],
|
||||
})
|
||||
mockAutoMigrate.mockReturnValue({
|
||||
migrated: true,
|
||||
from: "oh-my-opencode",
|
||||
to: "oh-my-openagent",
|
||||
configPath: "/tmp/opencode.json",
|
||||
})
|
||||
const { createLegacyPluginToastHook } = await importFreshModule()
|
||||
const hook = createLegacyPluginToastHook(createMockCtx())
|
||||
|
||||
// when
|
||||
await hook.event(createEvent("session.created"))
|
||||
|
||||
// then
|
||||
expect(mockShowToast).toHaveBeenCalledTimes(1)
|
||||
const toastArg = mockShowToast.mock.calls[0][0] as { body: { variant: string } }
|
||||
expect(toastArg.body.variant).toBe("success")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given legacy entry exists but migration fails", () => {
|
||||
it("#then shows warning toast", async () => {
|
||||
// given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue({
|
||||
hasLegacyEntry: true,
|
||||
hasCanonicalEntry: false,
|
||||
legacyEntries: ["oh-my-opencode"],
|
||||
})
|
||||
mockAutoMigrate.mockReturnValue({
|
||||
migrated: false,
|
||||
from: null,
|
||||
to: null,
|
||||
configPath: "/tmp/opencode.json",
|
||||
})
|
||||
const { createLegacyPluginToastHook } = await importFreshModule()
|
||||
const hook = createLegacyPluginToastHook(createMockCtx())
|
||||
|
||||
// when
|
||||
await hook.event(createEvent("session.created"))
|
||||
|
||||
// then
|
||||
expect(mockShowToast).toHaveBeenCalledTimes(1)
|
||||
const toastArg2 = mockShowToast.mock.calls[0][0] as { body: { variant: string } }
|
||||
expect(toastArg2.body.variant).toBe("warning")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given session.created fires twice", () => {
|
||||
it("#then only fires once (once-guard)", async () => {
|
||||
// given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue({
|
||||
hasLegacyEntry: true,
|
||||
hasCanonicalEntry: false,
|
||||
legacyEntries: ["oh-my-opencode"],
|
||||
})
|
||||
mockAutoMigrate.mockReturnValue({
|
||||
migrated: true,
|
||||
from: "oh-my-opencode",
|
||||
to: "oh-my-openagent",
|
||||
configPath: "/tmp/opencode.json",
|
||||
})
|
||||
const { createLegacyPluginToastHook } = await importFreshModule()
|
||||
const hook = createLegacyPluginToastHook(createMockCtx())
|
||||
|
||||
// when
|
||||
await hook.event(createEvent("session.created"))
|
||||
await hook.event(createEvent("session.created"))
|
||||
|
||||
// then
|
||||
expect(mockShowToast).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given a non-session.created event fires", () => {
|
||||
it("#then does nothing", async () => {
|
||||
// given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue({
|
||||
hasLegacyEntry: true,
|
||||
hasCanonicalEntry: false,
|
||||
legacyEntries: ["oh-my-opencode"],
|
||||
})
|
||||
const { createLegacyPluginToastHook } = await importFreshModule()
|
||||
const hook = createLegacyPluginToastHook(createMockCtx())
|
||||
|
||||
// when
|
||||
await hook.event(createEvent("session.deleted"))
|
||||
|
||||
// then
|
||||
expect(mockCheckForLegacyPluginEntry).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given session.created from a subagent (has parentID)", () => {
|
||||
it("#then skips the check", async () => {
|
||||
// given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue({
|
||||
hasLegacyEntry: true,
|
||||
hasCanonicalEntry: false,
|
||||
legacyEntries: ["oh-my-opencode"],
|
||||
})
|
||||
const { createLegacyPluginToastHook } = await importFreshModule()
|
||||
const hook = createLegacyPluginToastHook(createMockCtx())
|
||||
|
||||
// when
|
||||
await hook.event(createEvent("session.created", "parent-session-id"))
|
||||
|
||||
// then
|
||||
expect(mockCheckForLegacyPluginEntry).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,59 @@
|
||||
import type { PluginInput } from "@opencode-ai/plugin"
|
||||
|
||||
import { checkForLegacyPluginEntry } from "../../shared/legacy-plugin-warning"
|
||||
import { log } from "../../shared/logger"
|
||||
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity"
|
||||
import { autoMigrateLegacyPluginEntry } from "./auto-migrate"
|
||||
|
||||
export function createLegacyPluginToastHook(ctx: PluginInput) {
|
||||
let fired = false
|
||||
|
||||
return {
|
||||
event: async ({ event }: { event: { type: string; properties?: unknown } }) => {
|
||||
if (event.type !== "session.created" || fired) return
|
||||
|
||||
const props = event.properties as { info?: { parentID?: string } } | undefined
|
||||
if (props?.info?.parentID) return
|
||||
|
||||
fired = true
|
||||
|
||||
const result = checkForLegacyPluginEntry()
|
||||
if (!result.hasLegacyEntry) return
|
||||
|
||||
const migration = autoMigrateLegacyPluginEntry()
|
||||
|
||||
if (migration.migrated) {
|
||||
log("[legacy-plugin-toast] Auto-migrated opencode.json plugin entry", {
|
||||
from: migration.from,
|
||||
to: migration.to,
|
||||
})
|
||||
|
||||
await ctx.client.tui
|
||||
.showToast({
|
||||
body: {
|
||||
title: "Plugin Entry Migrated",
|
||||
message: `"${migration.from}" has been renamed to "${migration.to}" in your opencode.json.\nNo action needed.`,
|
||||
variant: "success" as const,
|
||||
duration: 8000,
|
||||
},
|
||||
})
|
||||
.catch(() => {})
|
||||
} else {
|
||||
log("[legacy-plugin-toast] Legacy entry detected but migration failed", {
|
||||
legacyEntries: result.legacyEntries,
|
||||
})
|
||||
|
||||
await ctx.client.tui
|
||||
.showToast({
|
||||
body: {
|
||||
title: "Legacy Plugin Name Detected",
|
||||
message: `Update your opencode.json: "${LEGACY_PLUGIN_NAME}" has been renamed to "${PLUGIN_NAME}".\nRun: bunx ${PLUGIN_NAME} install`,
|
||||
variant: "warning" as const,
|
||||
duration: 10000,
|
||||
},
|
||||
})
|
||||
.catch(() => {})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { createLegacyPluginToastHook } from "./hook"
|
||||
Reference in New Issue
Block a user