Skip to content

Commit 111315c

Browse files
committed
Validate LSP position before requiring a server
Move the beyond-EOF line check ahead of get_client() so invalid positions fail fast with a clear error even when no LSP server binary is installed (as on CI).
1 parent d3451c3 commit 111315c

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

  • python_agent_harness/tools

‎python_agent_harness/tools/lsp.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,23 @@ def run(self, args: dict, ctx: ToolContext) -> str:
122122
if line < 1 or character < 1:
123123
return "Error: line and character must be >= 1"
124124

125+
# Validate the requested line before spinning up a server so a bad
126+
# position fails fast even when no LSP binary is installed.
125127
try:
126-
client, _ = get_client(path, ctx.cwd)
127128
text = Path(path).read_text(encoding="utf-8", errors="replace")
129+
except (OSError, UnicodeError) as e:
130+
return f"Error: failed to read {raw_path}: {e}"
131+
lines = text.splitlines(keepends=True)
132+
if line > len(lines):
133+
return f"Error: line {line} is beyond end of file ({len(lines)} lines)"
134+
135+
try:
136+
client, _ = get_client(path, ctx.cwd)
128137
uri = Path(path).as_uri()
129138
client.open_document(uri, text)
130139

131140
# LSP positions are 0-based. Character conversion to the negotiated
132141
# encoding is done here using the source line.
133-
lines = text.splitlines(keepends=True)
134-
if line > len(lines):
135-
return f"Error: line {line} is beyond end of file ({len(lines)} lines)"
136142
source_line = lines[line - 1].rstrip("\r\n")
137143
py_index = min(character - 1, len(source_line))
138144
if client.position_encoding == "utf-8":

0 commit comments

Comments
 (0)