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:
@@ -27,6 +27,7 @@ describe("checkForLegacyPluginEntry", () => {
|
||||
expect(result.hasLegacyEntry).toBe(true)
|
||||
expect(result.hasCanonicalEntry).toBe(false)
|
||||
expect(result.legacyEntries).toEqual(["oh-my-opencode"])
|
||||
expect(result.configPath).toBe(join(testConfigDir, "opencode.json"))
|
||||
})
|
||||
|
||||
it("detects a version-pinned legacy plugin entry", () => {
|
||||
@@ -77,5 +78,6 @@ describe("checkForLegacyPluginEntry", () => {
|
||||
expect(result.hasLegacyEntry).toBe(false)
|
||||
expect(result.hasCanonicalEntry).toBe(false)
|
||||
expect(result.legacyEntries).toEqual([])
|
||||
expect(result.configPath).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,6 +13,7 @@ export interface LegacyPluginCheckResult {
|
||||
hasLegacyEntry: boolean
|
||||
hasCanonicalEntry: boolean
|
||||
legacyEntries: string[]
|
||||
configPath: string | null
|
||||
}
|
||||
|
||||
function getOpenCodeConfigPath(overrideConfigDir?: string): string | null {
|
||||
@@ -42,14 +43,14 @@ function isCanonicalPluginEntry(entry: string): boolean {
|
||||
export function checkForLegacyPluginEntry(overrideConfigDir?: string): LegacyPluginCheckResult {
|
||||
const configPath = getOpenCodeConfigPath(overrideConfigDir)
|
||||
if (!configPath) {
|
||||
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [] }
|
||||
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath: null }
|
||||
}
|
||||
|
||||
try {
|
||||
const content = readFileSync(configPath, "utf-8")
|
||||
const parseResult = parseJsoncSafe<OpenCodeConfig>(content)
|
||||
if (!parseResult.data) {
|
||||
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [] }
|
||||
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath }
|
||||
}
|
||||
|
||||
const legacyEntries = (parseResult.data.plugin ?? []).filter(isLegacyPluginEntry)
|
||||
@@ -59,8 +60,9 @@ export function checkForLegacyPluginEntry(overrideConfigDir?: string): LegacyPlu
|
||||
hasLegacyEntry: legacyEntries.length > 0,
|
||||
hasCanonicalEntry,
|
||||
legacyEntries,
|
||||
configPath,
|
||||
}
|
||||
} catch {
|
||||
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [] }
|
||||
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath: null }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
|
||||
import { afterAll, afterEach, beforeEach, describe, expect, it, mock, spyOn } from "bun:test"
|
||||
import type { LegacyPluginCheckResult } from "./legacy-plugin-warning"
|
||||
|
||||
function createLegacyPluginCheckResult(
|
||||
@@ -8,13 +8,15 @@ function createLegacyPluginCheckResult(
|
||||
hasLegacyEntry: false,
|
||||
hasCanonicalEntry: false,
|
||||
legacyEntries: [],
|
||||
configPath: null,
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
const mockCheckForLegacyPluginEntry = mock(() => createLegacyPluginCheckResult())
|
||||
|
||||
const mockLog = mock(() => {})
|
||||
const mockMigrateLegacyPluginEntry = mock(() => false)
|
||||
let consoleWarnSpy: ReturnType<typeof spyOn>
|
||||
|
||||
mock.module("./legacy-plugin-warning", () => ({
|
||||
checkForLegacyPluginEntry: mockCheckForLegacyPluginEntry,
|
||||
@@ -24,6 +26,10 @@ mock.module("./logger", () => ({
|
||||
log: mockLog,
|
||||
}))
|
||||
|
||||
mock.module("./migrate-legacy-plugin-entry", () => ({
|
||||
migrateLegacyPluginEntry: mockMigrateLegacyPluginEntry,
|
||||
}))
|
||||
|
||||
afterAll(() => {
|
||||
mock.restore()
|
||||
})
|
||||
@@ -36,16 +42,24 @@ describe("logLegacyPluginStartupWarning", () => {
|
||||
beforeEach(() => {
|
||||
mockCheckForLegacyPluginEntry.mockReset()
|
||||
mockLog.mockReset()
|
||||
mockMigrateLegacyPluginEntry.mockReset()
|
||||
consoleWarnSpy = spyOn(console, "warn").mockImplementation(() => {})
|
||||
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue(createLegacyPluginCheckResult())
|
||||
mockMigrateLegacyPluginEntry.mockReturnValue(false)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
consoleWarnSpy?.mockRestore()
|
||||
})
|
||||
|
||||
describe("#given OpenCode config contains legacy plugin entries", () => {
|
||||
it("logs the legacy entries with canonical replacements", async () => {
|
||||
it("#then logs the legacy entries with canonical replacements", async () => {
|
||||
//#given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue(createLegacyPluginCheckResult({
|
||||
hasLegacyEntry: true,
|
||||
legacyEntries: ["oh-my-opencode", "oh-my-opencode@3.13.1"],
|
||||
configPath: "/tmp/opencode.json",
|
||||
}))
|
||||
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
|
||||
|
||||
@@ -63,10 +77,45 @@ describe("logLegacyPluginStartupWarning", () => {
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
it("#then emits console.warn about the rename", async () => {
|
||||
//#given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue(createLegacyPluginCheckResult({
|
||||
hasLegacyEntry: true,
|
||||
legacyEntries: ["oh-my-opencode@latest"],
|
||||
configPath: "/tmp/opencode.json",
|
||||
}))
|
||||
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
|
||||
|
||||
//#when
|
||||
logLegacyPluginStartupWarning()
|
||||
|
||||
//#then
|
||||
expect(consoleWarnSpy).toHaveBeenCalled()
|
||||
const firstCall = consoleWarnSpy.mock.calls[0]?.[0] as string
|
||||
expect(firstCall).toContain("oh-my-opencode")
|
||||
expect(firstCall).toContain("oh-my-openagent")
|
||||
})
|
||||
|
||||
it("#then attempts auto-migration of the opencode.json", async () => {
|
||||
//#given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue(createLegacyPluginCheckResult({
|
||||
hasLegacyEntry: true,
|
||||
legacyEntries: ["oh-my-opencode"],
|
||||
configPath: "/tmp/opencode.json",
|
||||
}))
|
||||
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
|
||||
|
||||
//#when
|
||||
logLegacyPluginStartupWarning()
|
||||
|
||||
//#then
|
||||
expect(mockMigrateLegacyPluginEntry).toHaveBeenCalledWith("/tmp/opencode.json")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given OpenCode config uses only canonical plugin entries", () => {
|
||||
it("does not log a startup warning", async () => {
|
||||
it("#then does not log a startup warning", async () => {
|
||||
//#given
|
||||
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
|
||||
|
||||
@@ -75,6 +124,27 @@ describe("logLegacyPluginStartupWarning", () => {
|
||||
|
||||
//#then
|
||||
expect(mockLog).not.toHaveBeenCalled()
|
||||
expect(consoleWarnSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given migration succeeds", () => {
|
||||
it("#then logs success message to console", async () => {
|
||||
//#given
|
||||
mockCheckForLegacyPluginEntry.mockReturnValue(createLegacyPluginCheckResult({
|
||||
hasLegacyEntry: true,
|
||||
legacyEntries: ["oh-my-opencode@latest"],
|
||||
configPath: "/tmp/opencode.json",
|
||||
}))
|
||||
mockMigrateLegacyPluginEntry.mockReturnValue(true)
|
||||
const { logLegacyPluginStartupWarning } = await importFreshStartupWarningModule()
|
||||
|
||||
//#when
|
||||
logLegacyPluginStartupWarning()
|
||||
|
||||
//#then
|
||||
const calls = consoleWarnSpy.mock.calls.map((c) => c[0] as string)
|
||||
expect(calls.some((c) => c.includes("Auto-migrated"))).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { checkForLegacyPluginEntry } from "./legacy-plugin-warning"
|
||||
import { log } from "./logger"
|
||||
import { migrateLegacyPluginEntry } from "./migrate-legacy-plugin-entry"
|
||||
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "./plugin-identity"
|
||||
|
||||
function toCanonicalEntry(entry: string): string {
|
||||
@@ -20,9 +21,27 @@ export function logLegacyPluginStartupWarning(): void {
|
||||
return
|
||||
}
|
||||
|
||||
const suggestedEntries = result.legacyEntries.map(toCanonicalEntry)
|
||||
|
||||
log("[OhMyOpenCodePlugin] Legacy plugin entry detected in OpenCode config", {
|
||||
legacyEntries: result.legacyEntries,
|
||||
suggestedEntries: result.legacyEntries.map(toCanonicalEntry),
|
||||
suggestedEntries,
|
||||
hasCanonicalEntry: result.hasCanonicalEntry,
|
||||
})
|
||||
|
||||
console.warn(
|
||||
`[oh-my-openagent] WARNING: Your opencode.json uses the legacy package name "${LEGACY_PLUGIN_NAME}".`
|
||||
+ ` The package has been renamed to "${PLUGIN_NAME}".`
|
||||
+ ` Attempting auto-migration...`,
|
||||
)
|
||||
|
||||
const migrated = migrateLegacyPluginEntry(result.configPath!)
|
||||
if (migrated) {
|
||||
console.warn(`[oh-my-openagent] Auto-migrated opencode.json: ${result.legacyEntries.join(", ")} -> ${suggestedEntries.join(", ")}`)
|
||||
} else {
|
||||
console.warn(
|
||||
`[oh-my-openagent] Could not auto-migrate. Please manually update your opencode.json:`
|
||||
+ ` ${result.legacyEntries.map((e, i) => `"${e}" -> "${suggestedEntries[i]}"`).join(", ")}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
|
||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { migrateLegacyConfigFile } from "./migrate-legacy-config-file"
|
||||
|
||||
describe("migrateLegacyConfigFile", () => {
|
||||
let testDir = ""
|
||||
|
||||
beforeEach(() => {
|
||||
testDir = join(tmpdir(), `omo-migrate-config-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
mkdirSync(testDir, { recursive: true })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(testDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
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", () => {
|
||||
const legacyPath = join(testDir, "oh-my-opencode.jsonc")
|
||||
writeFileSync(legacyPath, '{ "agents": {} }')
|
||||
|
||||
const result = migrateLegacyConfigFile(legacyPath)
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(existsSync(join(testDir, "oh-my-openagent.jsonc"))).toBe(true)
|
||||
expect(readFileSync(join(testDir, "oh-my-openagent.jsonc"), "utf-8")).toBe('{ "agents": {} }')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given oh-my-opencode.json exists but oh-my-openagent.json does not", () => {
|
||||
describe("#when migrating the config file", () => {
|
||||
it("#then copies to oh-my-openagent.json", () => {
|
||||
const legacyPath = join(testDir, "oh-my-opencode.json")
|
||||
writeFileSync(legacyPath, '{ "agents": {} }')
|
||||
|
||||
const result = migrateLegacyConfigFile(legacyPath)
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(existsSync(join(testDir, "oh-my-openagent.json"))).toBe(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given oh-my-openagent.jsonc already exists", () => {
|
||||
describe("#when attempting migration", () => {
|
||||
it("#then returns false and does not overwrite", () => {
|
||||
const legacyPath = join(testDir, "oh-my-opencode.jsonc")
|
||||
const canonicalPath = join(testDir, "oh-my-openagent.jsonc")
|
||||
writeFileSync(legacyPath, '{ "old": true }')
|
||||
writeFileSync(canonicalPath, '{ "new": true }')
|
||||
|
||||
const result = migrateLegacyConfigFile(legacyPath)
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(readFileSync(canonicalPath, "utf-8")).toBe('{ "new": true }')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given the file does not exist", () => {
|
||||
describe("#when attempting migration", () => {
|
||||
it("#then returns false", () => {
|
||||
const result = migrateLegacyConfigFile(join(testDir, "oh-my-opencode.jsonc"))
|
||||
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given the file is not a legacy config file", () => {
|
||||
describe("#when attempting migration", () => {
|
||||
it("#then returns false", () => {
|
||||
const nonLegacyPath = join(testDir, "something-else.jsonc")
|
||||
writeFileSync(nonLegacyPath, "{}")
|
||||
|
||||
const result = migrateLegacyConfigFile(nonLegacyPath)
|
||||
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,31 @@
|
||||
import { existsSync, copyFileSync, renameSync } from "node:fs"
|
||||
import { join, dirname, basename } from "node:path"
|
||||
|
||||
import { log } from "./logger"
|
||||
import { CONFIG_BASENAME, LEGACY_CONFIG_BASENAME } from "./plugin-identity"
|
||||
|
||||
function buildCanonicalPath(legacyPath: string): string {
|
||||
const dir = dirname(legacyPath)
|
||||
const ext = basename(legacyPath).includes(".jsonc") ? ".jsonc" : ".json"
|
||||
return join(dir, `${CONFIG_BASENAME}${ext}`)
|
||||
}
|
||||
|
||||
export function migrateLegacyConfigFile(legacyPath: string): boolean {
|
||||
if (!existsSync(legacyPath)) return false
|
||||
if (!basename(legacyPath).startsWith(LEGACY_CONFIG_BASENAME)) return false
|
||||
|
||||
const canonicalPath = buildCanonicalPath(legacyPath)
|
||||
if (existsSync(canonicalPath)) return false
|
||||
|
||||
try {
|
||||
copyFileSync(legacyPath, canonicalPath)
|
||||
log("[migrateLegacyConfigFile] Copied legacy config to canonical path", {
|
||||
from: legacyPath,
|
||||
to: canonicalPath,
|
||||
})
|
||||
return true
|
||||
} catch (error) {
|
||||
log("[migrateLegacyConfigFile] Failed to copy legacy config file", { legacyPath, error })
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
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 { migrateLegacyPluginEntry } from "./migrate-legacy-plugin-entry"
|
||||
|
||||
describe("migrateLegacyPluginEntry", () => {
|
||||
let testDir = ""
|
||||
|
||||
beforeEach(() => {
|
||||
testDir = join(tmpdir(), `omo-migrate-entry-${Date.now()}-${Math.random().toString(36).slice(2)}`)
|
||||
mkdirSync(testDir, { recursive: true })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(testDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
describe("#given opencode.json contains oh-my-opencode plugin entry", () => {
|
||||
describe("#when migrating the config", () => {
|
||||
it("#then replaces oh-my-opencode with oh-my-openagent", () => {
|
||||
const configPath = join(testDir, "opencode.json")
|
||||
writeFileSync(configPath, JSON.stringify({ plugin: ["oh-my-opencode@latest"] }, null, 2))
|
||||
|
||||
const result = migrateLegacyPluginEntry(configPath)
|
||||
|
||||
expect(result).toBe(true)
|
||||
const content = readFileSync(configPath, "utf-8")
|
||||
expect(content).toContain("oh-my-openagent@latest")
|
||||
expect(content).not.toContain("oh-my-opencode")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given opencode.json contains bare oh-my-opencode entry", () => {
|
||||
describe("#when migrating the config", () => {
|
||||
it("#then replaces with oh-my-openagent", () => {
|
||||
const configPath = join(testDir, "opencode.json")
|
||||
writeFileSync(configPath, JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2))
|
||||
|
||||
const result = migrateLegacyPluginEntry(configPath)
|
||||
|
||||
expect(result).toBe(true)
|
||||
const content = readFileSync(configPath, "utf-8")
|
||||
expect(content).toContain('"oh-my-openagent"')
|
||||
expect(content).not.toContain("oh-my-opencode")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given opencode.json contains pinned oh-my-opencode version", () => {
|
||||
describe("#when migrating the config", () => {
|
||||
it("#then preserves the version pin", () => {
|
||||
const configPath = join(testDir, "opencode.json")
|
||||
writeFileSync(configPath, JSON.stringify({ plugin: ["oh-my-opencode@3.11.0"] }, null, 2))
|
||||
|
||||
const result = migrateLegacyPluginEntry(configPath)
|
||||
|
||||
expect(result).toBe(true)
|
||||
const content = readFileSync(configPath, "utf-8")
|
||||
expect(content).toContain("oh-my-openagent@3.11.0")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given opencode.json already uses oh-my-openagent", () => {
|
||||
describe("#when checking for migration", () => {
|
||||
it("#then returns false and does not modify the file", () => {
|
||||
const configPath = join(testDir, "opencode.json")
|
||||
const original = JSON.stringify({ plugin: ["oh-my-openagent@latest"] }, null, 2)
|
||||
writeFileSync(configPath, original)
|
||||
|
||||
const result = migrateLegacyPluginEntry(configPath)
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(readFileSync(configPath, "utf-8")).toBe(original)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given config file does not exist", () => {
|
||||
describe("#when attempting migration", () => {
|
||||
it("#then returns false", () => {
|
||||
const result = migrateLegacyPluginEntry(join(testDir, "nonexistent.json"))
|
||||
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs"
|
||||
|
||||
import { log } from "./logger"
|
||||
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "./plugin-identity"
|
||||
|
||||
export function migrateLegacyPluginEntry(configPath: string): boolean {
|
||||
if (!existsSync(configPath)) return false
|
||||
|
||||
try {
|
||||
const content = readFileSync(configPath, "utf-8")
|
||||
if (!content.includes(LEGACY_PLUGIN_NAME)) return false
|
||||
|
||||
const updated = content.replaceAll(LEGACY_PLUGIN_NAME, PLUGIN_NAME)
|
||||
if (updated === content) return false
|
||||
|
||||
writeFileSync(configPath, updated, "utf-8")
|
||||
log("[migrateLegacyPluginEntry] Auto-migrated opencode.json plugin entry", {
|
||||
configPath,
|
||||
from: LEGACY_PLUGIN_NAME,
|
||||
to: PLUGIN_NAME,
|
||||
})
|
||||
return true
|
||||
} catch (error) {
|
||||
log("[migrateLegacyPluginEntry] Failed to migrate opencode.json", { configPath, error })
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user