fix(install): write the global OpenCode plugin to ~/.config/opencode (#3398)
A global `graphify install --platform opencode` passed Path(".") as the
plugin base and joined `.opencode/plugins/graphify.js` onto it, so the
"global" plugin landed in whatever directory the command ran from and
loaded only for that project. OpenCode's global plugin dir is
~/.config/opencode/plugins, with no `.opencode/` segment, so the base
path alone could not be swapped.
The plugin helpers now take the OpenCode config dir itself, chosen by
_opencode_dir(): <project>/.opencode for a project install and
~/.config/opencode for a global one. `graphify uninstall` removes the
global plugin as well, and the OpenCode 2 plugin refresh also checks the
global dir. The legacy `.opencode/plugins/graphify.js` entry that older
releases wrote into opencode.json is still matched by its old string.
The slash-command half of the issue no longer applies: v8 has no
_install_opencode_command.
(cherry picked from commit c5cb910b78)
This commit is contained in:
1 parent
2859f5b9b0
commit
b4686ea4af
4 files changed
+114
-27
No files matched your search
+13
-3
@@ -122,6 +122,7 @@ from graphify.install import ( # noqa: E402,F401
|
||||
_OPENCODE_PLUGIN_HEADER,
|
||||
_drop_opencode_config_entry,
|
||||
_opencode_config_names_plugin,
|
||||
_opencode_dir,
|
||||
_opencode_plugin_is_v1_only,
|
||||
_PLATFORM_CONFIG,
|
||||
_skill_lock,
|
||||
@@ -429,12 +430,21 @@ def _refresh_v1_opencode_plugins() -> None:
|
||||
if os.environ.get("GRAPHIFY_NO_AUTO_REFRESH", "").strip().lower() in ("1", "true", "yes"):
|
||||
return
|
||||
try:
|
||||
directories = dict.fromkeys(d.resolve() for d in (Path.cwd(), Path.home()))
|
||||
# The cwd and home .opencode dirs are where installs before #3398 wrote,
|
||||
# ~/.config/opencode is the global install's dir since.
|
||||
directories = dict.fromkeys(
|
||||
d.resolve()
|
||||
for d in (
|
||||
_opencode_dir(project=True, project_dir=Path.cwd()),
|
||||
_opencode_dir(project=True, project_dir=Path.home()),
|
||||
_opencode_dir(project=False),
|
||||
)
|
||||
)
|
||||
except (OSError, RuntimeError):
|
||||
return
|
||||
loadable: "bool | None" = None # asked once, and only when an old plugin is found
|
||||
for directory in directories:
|
||||
plugin_file = directory / _OPENCODE_PLUGIN_PATH
|
||||
plugin_file = directory / _OPENCODE_PLUGIN_PATH.relative_to(".opencode")
|
||||
|
||||
def pending() -> "bool | None":
|
||||
"""True for an old plugin, False for a stale entry only, None for neither."""
|
||||
@@ -486,7 +496,7 @@ def _refresh_v1_opencode_plugins() -> None:
|
||||
elif dropped:
|
||||
print(
|
||||
f"graphify: removed the stale graphify entry from "
|
||||
f"{directory / _OPENCODE_CONFIG_PATH}; set GRAPHIFY_NO_AUTO_REFRESH=1 to disable",
|
||||
f"{directory / _OPENCODE_CONFIG_PATH.name}; set GRAPHIFY_NO_AUTO_REFRESH=1 to disable",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
|
||||
+39
-24
@@ -1011,7 +1011,7 @@ def install(platform: str = "claude", *, project: bool = False, project_dir: Pat
|
||||
)
|
||||
|
||||
if platform == "opencode":
|
||||
_install_opencode_plugin(project_dir if project else Path("."))
|
||||
_install_opencode_plugin(_opencode_dir(project=project, project_dir=project_dir))
|
||||
|
||||
if project:
|
||||
_print_project_git_add_hint([_project_scope_root(skill_dst, project_dir)])
|
||||
@@ -1778,6 +1778,18 @@ export default {
|
||||
"""
|
||||
_OPENCODE_PLUGIN_PATH = Path(".opencode") / "plugins" / "graphify.js"
|
||||
_OPENCODE_CONFIG_PATH = Path(".opencode") / "opencode.json"
|
||||
|
||||
|
||||
def _opencode_dir(*, project: bool, project_dir: Path | None = None) -> Path:
|
||||
"""OpenCode's config dir: <project>/.opencode, or ~/.config/opencode globally.
|
||||
|
||||
OpenCode loads plugins from <dir>/plugins/ in both. The global dir has no
|
||||
.opencode/ segment, so it cannot be built by joining _OPENCODE_PLUGIN_PATH
|
||||
onto a base the way the project one is (#3398).
|
||||
"""
|
||||
if project:
|
||||
return (project_dir or Path(".")) / ".opencode"
|
||||
return Path.home() / ".config" / "opencode"
|
||||
# First line of every plugin graphify has written, back to the first one (#71).
|
||||
_OPENCODE_PLUGIN_HEADER = "// graphify OpenCode plugin"
|
||||
def _opencode_plugin_is_v1_only(body: str) -> bool:
|
||||
@@ -1788,14 +1800,14 @@ def _opencode_plugin_is_v1_only(body: str) -> bool:
|
||||
with "Plugin must export a default definition" (#3554, #3732).
|
||||
"""
|
||||
return body.startswith(_OPENCODE_PLUGIN_HEADER) and "export default" not in body
|
||||
def _opencode_config_names_plugin(project_dir: Path) -> bool:
|
||||
def _opencode_config_names_plugin(opencode_dir: Path) -> bool:
|
||||
"""True when opencode.json still mentions the bare plugin path."""
|
||||
try:
|
||||
raw = (project_dir / _OPENCODE_CONFIG_PATH).read_text(encoding="utf-8")
|
||||
raw = (opencode_dir / _OPENCODE_CONFIG_PATH.name).read_text(encoding="utf-8")
|
||||
except (OSError, UnicodeDecodeError):
|
||||
return False
|
||||
return f'"{_OPENCODE_PLUGIN_PATH.as_posix()}"' in raw
|
||||
def _drop_opencode_config_entry(project_dir: Path) -> bool:
|
||||
def _drop_opencode_config_entry(opencode_dir: Path) -> bool:
|
||||
"""Remove the plugin entry older releases wrote into opencode.json.
|
||||
|
||||
The entry was the bare path ``.opencode/plugins/graphify.js``. OpenCode
|
||||
@@ -1803,7 +1815,7 @@ def _drop_opencode_config_entry(project_dir: Path) -> bool:
|
||||
never needed, and OpenCode 2 reads a bare path as an npm package name and
|
||||
fails to install it (#3732). Returns True when the config changed.
|
||||
"""
|
||||
config_file = project_dir / _OPENCODE_CONFIG_PATH
|
||||
config_file = opencode_dir / _OPENCODE_CONFIG_PATH.name
|
||||
entry = _OPENCODE_PLUGIN_PATH.as_posix()
|
||||
try:
|
||||
raw = config_file.read_text(encoding="utf-8")
|
||||
@@ -1816,7 +1828,7 @@ def _drop_opencode_config_entry(project_dir: Path) -> bool:
|
||||
# would drop them, so say what to remove instead.
|
||||
if f'"{entry}"' in raw:
|
||||
print(
|
||||
f" {_OPENCODE_CONFIG_PATH} -> not plain JSON, left as is; "
|
||||
f" {config_file} -> not plain JSON, left as is; "
|
||||
f'remove "{entry}" from its plugin list by hand'
|
||||
)
|
||||
return False
|
||||
@@ -1834,27 +1846,28 @@ def _drop_opencode_config_entry(project_dir: Path) -> bool:
|
||||
# ensure_ascii=False: keep the user's non-ASCII values readable.
|
||||
_write_text_atomic(config_file, json.dumps(config, indent=2, ensure_ascii=False))
|
||||
return changed
|
||||
def _install_opencode_plugin(project_dir: Path) -> None:
|
||||
"""Write graphify.js into .opencode/plugins/, where OpenCode discovers it.
|
||||
def _install_opencode_plugin(opencode_dir: Path) -> None:
|
||||
"""Write graphify.js into <opencode_dir>/plugins/, where OpenCode discovers it.
|
||||
|
||||
Both files are replaced atomically. The CLI refresh can run this while
|
||||
OpenCode is watching them, and it must never see half a plugin.
|
||||
``opencode_dir`` comes from _opencode_dir. Both files are replaced
|
||||
atomically. The CLI refresh can run this while OpenCode is watching them,
|
||||
and it must never see half a plugin.
|
||||
"""
|
||||
plugin_file = project_dir / _OPENCODE_PLUGIN_PATH
|
||||
plugin_file = opencode_dir / _OPENCODE_PLUGIN_PATH.relative_to(".opencode")
|
||||
_write_text_atomic(plugin_file, _OPENCODE_PLUGIN_JS)
|
||||
print(f" {_OPENCODE_PLUGIN_PATH} -> tool.execute.before hook written")
|
||||
print(f" {plugin_file} -> tool.execute.before hook written")
|
||||
|
||||
if _drop_opencode_config_entry(project_dir):
|
||||
print(f" {_OPENCODE_CONFIG_PATH} -> stale plugin entry removed")
|
||||
def _uninstall_opencode_plugin(project_dir: Path) -> None:
|
||||
if _drop_opencode_config_entry(opencode_dir):
|
||||
print(f" {opencode_dir / _OPENCODE_CONFIG_PATH.name} -> stale plugin entry removed")
|
||||
def _uninstall_opencode_plugin(opencode_dir: Path) -> None:
|
||||
"""Remove graphify.js plugin and deregister from opencode.json."""
|
||||
plugin_file = project_dir / _OPENCODE_PLUGIN_PATH
|
||||
plugin_file = opencode_dir / _OPENCODE_PLUGIN_PATH.relative_to(".opencode")
|
||||
if plugin_file.exists():
|
||||
plugin_file.unlink()
|
||||
print(f" {_OPENCODE_PLUGIN_PATH} -> removed")
|
||||
print(f" {plugin_file} -> removed")
|
||||
|
||||
if _drop_opencode_config_entry(project_dir):
|
||||
print(f" {_OPENCODE_CONFIG_PATH} -> plugin deregistered")
|
||||
if _drop_opencode_config_entry(opencode_dir):
|
||||
print(f" {opencode_dir / _OPENCODE_CONFIG_PATH.name} -> plugin deregistered")
|
||||
def _resolve_graphify_exe(project: bool = False) -> str:
|
||||
"""Return the absolute path to the graphify executable, with forward slashes.
|
||||
|
||||
@@ -1976,7 +1989,7 @@ def _agents_install(project_dir: Path, platform: str, project: bool = False) ->
|
||||
if platform == "codex":
|
||||
_install_codex_hook(project_dir or Path("."), project=project)
|
||||
elif platform == "opencode":
|
||||
_install_opencode_plugin(project_dir or Path("."))
|
||||
_install_opencode_plugin(_opencode_dir(project=True, project_dir=project_dir))
|
||||
elif platform == "kilo":
|
||||
_install_kilo_plugin(project_dir or Path("."))
|
||||
|
||||
@@ -2127,7 +2140,7 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None:
|
||||
if not target.exists():
|
||||
print("No AGENTS.md found in current directory - nothing to do")
|
||||
if platform == "opencode":
|
||||
_uninstall_opencode_plugin(project_dir or Path("."))
|
||||
_uninstall_opencode_plugin(_opencode_dir(project=True, project_dir=project_dir))
|
||||
elif platform == "kilo":
|
||||
_uninstall_kilo_plugin(project_dir or Path("."))
|
||||
return
|
||||
@@ -2137,7 +2150,7 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None:
|
||||
if cleaned is None:
|
||||
print("graphify section not found in AGENTS.md - nothing to do")
|
||||
if platform == "opencode":
|
||||
_uninstall_opencode_plugin(project_dir or Path("."))
|
||||
_uninstall_opencode_plugin(_opencode_dir(project=True, project_dir=project_dir))
|
||||
elif platform == "kilo":
|
||||
_uninstall_kilo_plugin(project_dir or Path("."))
|
||||
return
|
||||
@@ -2151,7 +2164,7 @@ def _agents_uninstall(project_dir: Path, platform: str = "") -> None:
|
||||
print(f"AGENTS.md was empty after removal - deleted {target.resolve()}")
|
||||
|
||||
if platform == "opencode":
|
||||
_uninstall_opencode_plugin(project_dir or Path("."))
|
||||
_uninstall_opencode_plugin(_opencode_dir(project=True, project_dir=project_dir))
|
||||
elif platform == "kilo":
|
||||
_uninstall_kilo_plugin(project_dir or Path("."))
|
||||
def _kilo_uninstall_global() -> list[str]:
|
||||
@@ -2327,7 +2340,9 @@ def uninstall_all(project_dir: Path | None = None, purge: bool = False) -> None:
|
||||
# The generic agents platform's user-scope skill lives at ~/.agents/skills,
|
||||
# which neither the AGENTS.md cleanup nor amp's removal reaches.
|
||||
_remove_skill_file("agents")
|
||||
_uninstall_opencode_plugin(pd)
|
||||
_uninstall_opencode_plugin(_opencode_dir(project=True, project_dir=pd))
|
||||
# `graphify install --platform opencode` writes the plugin here (#3398).
|
||||
_uninstall_opencode_plugin(_opencode_dir(project=False))
|
||||
_uninstall_codex_hook(pd)
|
||||
|
||||
# Git hook
|
||||
|
||||
@@ -983,6 +983,54 @@ def test_opencode_agents_install_writes_plugin(tmp_path):
|
||||
assert "tool.execute.before" in plugin.read_text()
|
||||
|
||||
|
||||
def test_opencode_global_install_writes_plugin_to_config_dir(tmp_path, monkeypatch):
|
||||
"""A global install puts the plugin in ~/.config/opencode/plugins, where
|
||||
OpenCode loads global plugins, and writes nothing under the cwd (#3398)."""
|
||||
from graphify.install import install
|
||||
|
||||
cwd = tmp_path / "some-project"
|
||||
cwd.mkdir()
|
||||
monkeypatch.chdir(cwd)
|
||||
|
||||
install("opencode")
|
||||
|
||||
plugin = Path.home() / ".config" / "opencode" / "plugins" / "graphify.js"
|
||||
assert "tool.execute.before" in plugin.read_text(encoding="utf-8")
|
||||
assert not (cwd / ".opencode").exists()
|
||||
assert not (Path.home() / ".config" / "opencode" / ".opencode").exists()
|
||||
|
||||
|
||||
def test_opencode_project_install_keeps_plugin_in_project(tmp_path, monkeypatch):
|
||||
from graphify.install import install
|
||||
|
||||
elsewhere = tmp_path / "elsewhere"
|
||||
elsewhere.mkdir()
|
||||
monkeypatch.chdir(elsewhere)
|
||||
proj = tmp_path / "proj"
|
||||
proj.mkdir()
|
||||
|
||||
install("opencode", project=True, project_dir=proj)
|
||||
|
||||
assert (proj / ".opencode" / "plugins" / "graphify.js").exists()
|
||||
assert not (Path.home() / ".config" / "opencode" / "plugins").exists()
|
||||
assert not (elsewhere / ".opencode").exists()
|
||||
|
||||
|
||||
def test_uninstall_all_removes_the_global_opencode_plugin(tmp_path, monkeypatch):
|
||||
from graphify.install import install, uninstall_all
|
||||
|
||||
proj = tmp_path / "proj"
|
||||
proj.mkdir()
|
||||
monkeypatch.chdir(proj)
|
||||
install("opencode")
|
||||
plugin = Path.home() / ".config" / "opencode" / "plugins" / "graphify.js"
|
||||
assert plugin.exists()
|
||||
|
||||
uninstall_all(proj)
|
||||
|
||||
assert not plugin.exists()
|
||||
|
||||
|
||||
def test_opencode_plugin_reminder_has_no_backticks(tmp_path):
|
||||
"""The bash reminder string must not contain backticks or $(...) (regression test for #1413).
|
||||
|
||||
|
||||
@@ -420,3 +420,17 @@ def test_install_and_hook_commands_skip_it(monkeypatch, cmd):
|
||||
except (_Stop, SystemExit):
|
||||
pass
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_a_v1_plugin_in_the_global_config_dir_is_rewritten(tmp_path, monkeypatch, capsys):
|
||||
"""A global `graphify install --platform opencode` writes the plugin to
|
||||
~/.config/opencode/plugins (#3398), so the refresh looks there too."""
|
||||
plugin = Path.home() / ".config" / "opencode" / "plugins" / "graphify.js"
|
||||
plugin.parent.mkdir(parents=True)
|
||||
plugin.write_text(V1_PLUGIN, encoding="utf-8")
|
||||
monkeypatch.chdir(tmp_path)
|
||||
|
||||
mainmod._refresh_v1_opencode_plugins()
|
||||
|
||||
assert plugin.read_text(encoding="utf-8") == _OPENCODE_PLUGIN_JS
|
||||
assert "graphify: refreshed" in capsys.readouterr().err
|
||||
Reference in new issue
Block a user