@kealthas-dev/opencode-gitbash-edit-path-fix 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/gitbash-edit-path-fix.ts +56 -0
- package/package.json +18 -4
- package/path-convert.ts +48 -0
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# gitbash-edit-path-fix
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Works around an opencode bug on Windows + Git Bash: `edit` and `write` never re-resolve Git-Bash/MSYS/Cygwin/WSL-style absolute paths such as `/c/Users/x/file.ts`, and `apply_patch` resolves the same form against the current drive. Node's `path.win32.isAbsolute()` already calls that "absolute" (root-relative, no drive letter), so the tools pass the literal string to `fs`, which Windows resolves against the *current* drive instead of the intended one. `read` is unaffected because it runs its path through `FSUtil.normalizePath()` first. Full investigation, including the closed-without-merge upstream fix (`anomalyco/opencode#22800`): `docs/lessons-learned.md`.
|
|
4
|
+
|
|
5
|
+
- Package: `@kealthas-dev/opencode-gitbash-edit-path-fix`, source `gitbash-edit-path-fix.ts`, helpers `path-convert.ts`.
|
|
6
|
+
- Hooks `tool.execute.before`:
|
|
7
|
+
- `edit` and `write`: rewrites `args.filePath` the way opencode's own `windowsPath()` does.
|
|
8
|
+
- `apply_patch`: rewrites the `*** Add File:` / `*** Delete File:` / `*** Update File:` / `*** Move to:` header paths in `args.patchText`.
|
|
9
|
+
- It mutates the existing `args` object in place rather than reassigning `output.args`, because the real tool reads the same `args` object right after the hook (source citation in the file's header comment). No-op off `win32`.
|
|
10
|
+
- The helpers live in `path-convert.ts` rather than the plugin entry file: opencode 1.19.x calls every exported function of a plugin module as a plugin factory, so exporting them from the entry file breaks loading. Tests import them from `path-convert.ts`.
|
|
11
|
+
- Not yet published and not in `deploy/opencode.json.example`, see `TODO.md`.
|
|
12
|
+
|
|
13
|
+
Tests: `tests/unit/gitbash-edit-path-fix.test.mjs` covers the conversion for all four path forms plus negatives, the in-place rewrite for `edit`/`write`, the `apply_patch` header rewrite, and an end-to-end reproduction of `edit.ts`'s branch via `path.win32`. It overrides `process.platform` because the sandbox is Linux, so it proves the plugin's logic, not that real Windows opencode honours the mutation. `tests/capability-probes/gitbash-edit-path-fix.probe.mjs` is the real check (Windows only) and `.github/workflows/probe-gitbash-path-bug.yml` runs it on a Windows runner.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode-ai/plugin";
|
|
2
|
+
import { rewritePatchText, toWindowsPath } from "./path-convert.ts";
|
|
3
|
+
|
|
4
|
+
// Works around anomalyco/opencode#22799 on Windows + Git Bash. Git Bash/MSYS/
|
|
5
|
+
// Cygwin/WSL report absolute paths as e.g. "/c/Users/franco/file.ts" instead of
|
|
6
|
+
// "C:\Users\franco\file.ts". Node's path.win32.isAbsolute() treats a leading
|
|
7
|
+
// "/" as already absolute (root-relative, no drive letter), so the tools that
|
|
8
|
+
// branch on `path.isAbsolute(params.filePath) ? params.filePath : path.join(...)`
|
|
9
|
+
// - edit.ts and write.ts - pass that literal string to fs, which Windows then
|
|
10
|
+
// resolves against the *current* drive (e.g. "D:\c\Users\franco\file.ts")
|
|
11
|
+
// instead of "C:\...". apply_patch.ts has the analogous bug via
|
|
12
|
+
// `path.resolve(instance.directory, hunk.path)` on each hunk's path.
|
|
13
|
+
//
|
|
14
|
+
// read.ts doesn't have this problem because it runs the result through
|
|
15
|
+
// FSUtil.normalizePath() (which itself runs fs-util.ts's windowsPath() first)
|
|
16
|
+
// before touching the filesystem; the three tools above never do.
|
|
17
|
+
//
|
|
18
|
+
// A fix for exactly this existed once - anomalyco/opencode#22800 - but that PR
|
|
19
|
+
// was closed without merging, so it never landed. Rather than fork/patch
|
|
20
|
+
// opencode itself, this plugin reproduces the same fix as a
|
|
21
|
+
// `tool.execute.before` hook:
|
|
22
|
+
// - edit/write: rewrite `output.args.filePath`.
|
|
23
|
+
// - apply_patch: rewrite the `*** Add File:` / `*** Delete File:` /
|
|
24
|
+
// `*** Update File:` / `*** Move to:` header paths in `output.args.patchText`.
|
|
25
|
+
//
|
|
26
|
+
// Mutating `output.args` in place (not reassigning it) is required: opencode
|
|
27
|
+
// passes the same `args` object reference to the hook wrapper and then to the
|
|
28
|
+
// real tool, so a property write is visible afterward while `output.args = ...`
|
|
29
|
+
// would only rebind the wrapper's property.
|
|
30
|
+
const FIELD_TOOLS = new Set(["edit", "write"]);
|
|
31
|
+
const TEXT_TOOLS = new Set(["apply_patch"]);
|
|
32
|
+
|
|
33
|
+
export const GitBashEditPathFix: Plugin = async () => {
|
|
34
|
+
return {
|
|
35
|
+
"tool.execute.before": async (input, output) => {
|
|
36
|
+
// Real gate for production: this deployment's target machine is Windows
|
|
37
|
+
// (see docs/deployment-environment.md), and on any other platform these
|
|
38
|
+
// path forms are either not ambiguous (POSIX) or not produced by the
|
|
39
|
+
// shell at all, so there's nothing to fix.
|
|
40
|
+
if (process.platform !== "win32") return;
|
|
41
|
+
if (FIELD_TOOLS.has(input.tool)) {
|
|
42
|
+
const filePath = output.args?.filePath;
|
|
43
|
+
if (typeof filePath !== "string") return;
|
|
44
|
+
const converted = toWindowsPath(filePath);
|
|
45
|
+
if (converted === undefined) return;
|
|
46
|
+
output.args.filePath = converted;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (TEXT_TOOLS.has(input.tool)) {
|
|
50
|
+
const patchText = output.args?.patchText;
|
|
51
|
+
if (typeof patchText !== "string") return;
|
|
52
|
+
output.args.patchText = rewritePatchText(patchText);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kealthas-dev/opencode-gitbash-edit-path-fix",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "gitbash-edit-path-fix.ts opencode plugin (rewrites Git-Bash/MSYS/Cygwin/WSL-style absolute paths like \"/c/Users/x\" to \"C:/Users/x\" before the built-in edit/write/apply_patch tools run on Windows, working around anomalyco/opencode#22799 whose fix in #22800 was closed without merging), published to npm so opencode's own npm-plugin loader can install it directly from a bare package name in opencode.json's plugin array - see CLAUDE.md. Written in TypeScript against @opencode-ai/plugin's Plugin type.",
|
|
6
|
+
"main": "gitbash-edit-path-fix.ts",
|
|
7
|
+
"files": ["gitbash-edit-path-fix.ts", "path-convert.ts"],
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/Keal-thas/opencode-toolkit.git",
|
|
14
|
+
"directory": "plugins/gitbash-edit-path-fix"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@opencode-ai/plugin": "^1.0.0",
|
|
18
|
+
"@types/node": "^22.0.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/path-convert.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Pure path helpers for gitbash-edit-path-fix. Kept in a separate module so the
|
|
2
|
+
// plugin entry file exports only the plugin itself: opencode 1.19.x calls every
|
|
3
|
+
// exported function of a plugin module as a plugin factory, so an exported
|
|
4
|
+
// helper here would be invoked with no arguments and break plugin loading.
|
|
5
|
+
|
|
6
|
+
const PATCH_HEADERS = ["*** Add File:", "*** Delete File:", "*** Update File:", "*** Move to:"];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Converts a Git-Bash/MSYS/Cygwin/WSL-style drive-rooted absolute path into a
|
|
10
|
+
* real Windows path, e.g. "/c/Users/x" -> "C:/Users/x". Returns undefined when
|
|
11
|
+
* `value` doesn't match any of the recognized forms (including when it's
|
|
12
|
+
* already an ordinary Windows or POSIX path) - the caller treats that as "leave
|
|
13
|
+
* it alone".
|
|
14
|
+
*
|
|
15
|
+
* Mirrors anomalyco/opencode's packages/core/src/fs-util.ts `windowsPath()`
|
|
16
|
+
* exactly: same four patterns, same order (order matters - each pattern's
|
|
17
|
+
* anchoring keeps them mutually exclusive, so "/cygdrive/c/..." can't be
|
|
18
|
+
* mis-caught by the plainer "/c/..." pattern).
|
|
19
|
+
*/
|
|
20
|
+
export function toWindowsPath(value: string): string | undefined {
|
|
21
|
+
const converted = value
|
|
22
|
+
.replace(/^\/([a-zA-Z]):(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
23
|
+
.replace(/^\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
24
|
+
.replace(/^\/cygdrive\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`)
|
|
25
|
+
.replace(/^\/mnt\/([a-zA-Z])(?:\/|$)/, (_, drive) => `${drive.toUpperCase()}:/`);
|
|
26
|
+
return converted === value ? undefined : converted;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Rewrites the file-path headers of an apply_patch patch text, returning the
|
|
31
|
+
* text unchanged when none of them need converting. Only the header line's
|
|
32
|
+
* path is touched; hunk content is left byte-for-byte intact.
|
|
33
|
+
*/
|
|
34
|
+
export function rewritePatchText(text: string): string {
|
|
35
|
+
return text
|
|
36
|
+
.split("\n")
|
|
37
|
+
.map((line) => {
|
|
38
|
+
for (const header of PATCH_HEADERS) {
|
|
39
|
+
if (!line.startsWith(header)) continue;
|
|
40
|
+
const raw = line.slice(header.length).trim();
|
|
41
|
+
const converted = toWindowsPath(raw);
|
|
42
|
+
if (converted === undefined) return line;
|
|
43
|
+
return `${header} ${converted}`;
|
|
44
|
+
}
|
|
45
|
+
return line;
|
|
46
|
+
})
|
|
47
|
+
.join("\n");
|
|
48
|
+
}
|