Skip to content

Commit 290f596

Browse files
authored
fix: resolve editor paths from repo root (#347)
1 parent 8faad67 commit 290f596

4 files changed

Lines changed: 30 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All notable user-visible changes to Hunk are documented in this file.
1212

1313
### Fixed
1414

15+
- Fixed the `e` editor shortcut when Hunk is launched from a repo subdirectory.
1516
- Fixed VCS auto-detection so a Git repository nested under a parent Jujutsu workspace still uses Git mode by default.
1617

1718
## [0.13.1] - 2026-05-19

src/ui/App.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,14 @@ export function App({
437437
}, [refreshCurrentInput]);
438438

439439
const triggerEditSelectedFile = useCallback(() => {
440+
const basePath =
441+
bootstrap.input.kind === "vcs" ||
442+
bootstrap.input.kind === "show" ||
443+
bootstrap.input.kind === "stash-show"
444+
? bootstrap.changeset.sourceLabel
445+
: undefined;
440446
const message = openSelectedFileInEditor({
447+
basePath,
441448
file: selectedFile,
442449
renderer,
443450
selectedHunk: review.selectedHunk,
@@ -452,6 +459,8 @@ export function App({
452459
triggerRefreshCurrentInput();
453460
}
454461
}, [
462+
bootstrap.changeset.sourceLabel,
463+
bootstrap.input.kind,
455464
canRefreshCurrentInput,
456465
renderer,
457466
review.selectedHunk,

src/ui/lib/openInEditor.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { describe, expect, test } from "bun:test";
2-
import { buildEditorCommand, shouldSuspendForEditor } from "./openInEditor";
2+
import { resolve } from "node:path";
3+
import {
4+
buildEditorCommand,
5+
resolveEditableFilePath,
6+
shouldSuspendForEditor,
7+
} from "./openInEditor";
38

49
describe("open in editor helpers", () => {
510
test("builds vi-style editor args without shell quoting", () => {
@@ -59,4 +64,10 @@ describe("open in editor helpers", () => {
5964
expect(shouldSuspendForEditor('"C:\\Program Files\\Cursor\\cursor.exe"')).toBe(false);
6065
expect(shouldSuspendForEditor("nvim")).toBe(true);
6166
});
67+
68+
test("resolves repo-relative diff paths from the diff source path", () => {
69+
expect(resolveEditableFilePath("src/main.tsx", "/tmp/project")).toBe(
70+
resolve("/tmp/project", "src/main.tsx"),
71+
);
72+
});
6273
});

src/ui/lib/openInEditor.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ export function buildEditorCommand({
7171
return { command, args: [...editorArgs, filePath] };
7272
}
7373

74+
/** Resolve diff paths relative to their source repo instead of the launch cwd. */
75+
export function resolveEditableFilePath(filePath: string, basePath = process.cwd()) {
76+
return resolve(basePath, filePath);
77+
}
78+
7479
/** Open the selected file in $EDITOR, suspending TUI for terminal editors. */
7580
export function openSelectedFileInEditor({
81+
basePath,
7682
file,
7783
renderer,
7884
selectedHunk,
7985
}: {
86+
basePath?: string;
8087
file: DiffFile | undefined;
8188
renderer: Pick<CliRenderer, "suspend" | "resume" | "isDestroyed">;
8289
selectedHunk: DiffFile["metadata"]["hunks"][number] | undefined;
@@ -90,7 +97,7 @@ export function openSelectedFileInEditor({
9097
return "$EDITOR is not set.";
9198
}
9299

93-
const absolutePath = resolve(process.cwd(), file.path);
100+
const absolutePath = resolveEditableFilePath(file.path, basePath);
94101
if (!existsSync(absolutePath)) {
95102
return `Cannot edit ${file.path}: file does not exist on disk.`;
96103
}

0 commit comments

Comments
 (0)