Skip to content

Commit 3f626cc

Browse files
committed
feat: add option to start a debugpy session for debugging purpose
1 parent 88ef689 commit 3f626cc

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,15 @@
374374
"description": "If the port is specified, connect to the language server previously started at the given port.",
375375
"scope": "resource"
376376
},
377+
"robotcode.languageServer.extraArgs": {
378+
"type": "array",
379+
"default": [],
380+
"items": {
381+
"type": "string"
382+
},
383+
"markdownDescription": "Specifies extra arguments to be passed to the `robotcode language-server` command line tool (i.e.: [`--log`, `--log-level=TRACE`, `--log-calls`]).",
384+
"scope": "resource"
385+
},
377386
"robotcode.debugLauncher.mode": {
378387
"type": "string",
379388
"default": "stdio",

packages/core/src/robotcode/core/utils/debugpy.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Sequence, Tuple, Union
1+
from typing import Optional, Sequence, Tuple, Union
22

33
from ..logging import LoggingDescriptor
44
from .net import find_free_port
@@ -43,20 +43,19 @@ def enable_debugpy(port: int, addresses: Union[Sequence[str], str, None] = None)
4343
return False
4444

4545

46-
def start_debugpy(end_point: Union[Tuple[str, int], int], wait_for_client: bool) -> bool:
46+
def start_debugpy(end_point: Union[Tuple[str, int], int], wait_for_client: bool) -> Optional[int]:
4747
if is_debugpy_installed():
4848
import debugpy
4949

5050
if isinstance(end_point, int):
5151
end_point = ("127.0.0.1", end_point)
5252

5353
real_port = find_free_port(end_point[1])
54-
if real_port != end_point[1]:
55-
_logger.warning(lambda: f"start debugpy session on port {real_port}")
54+
5655
debugpy.listen((end_point[0], real_port))
5756

5857
if wait_for_client:
5958
_logger.info("wait for debugpy client")
6059
debugpy.wait_for_client()
61-
return True
62-
return False
60+
return real_port
61+
return None

src/robotcode/cli/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@
106106
type=str,
107107
help="Path to the launcher script. This is an internal option.",
108108
)
109+
@click.option(
110+
"--debugpy",
111+
is_flag=True,
112+
help="Starts a debugpy session. "
113+
"**This is an internal option and should only be use if you want to debug _RobotCode_.**",
114+
)
115+
@click.option(
116+
"--debugpy-port",
117+
type=int,
118+
default=5678,
119+
show_default=True,
120+
help="Defines the port to use for the debugpy session.",
121+
)
122+
@click.option(
123+
"--debugpy-wait-for-client",
124+
is_flag=True,
125+
help="Waits for a debugpy client to connect before starting the debugpy session.",
126+
)
109127
@click.version_option(version=__version__, prog_name="robotcode")
110128
@pass_application
111129
def robotcode(
@@ -122,6 +140,9 @@ def robotcode(
122140
log_calls: bool,
123141
default_path: Optional[List[str]],
124142
launcher_script: Optional[str] = None,
143+
debugpy: bool = False,
144+
debugpy_port: int = 5678,
145+
debugpy_wait_for_client: bool = False,
125146
) -> None:
126147
"""\b
127148
_____ _ _ _____ _
@@ -155,6 +176,23 @@ def robotcode(
155176

156177
logging.basicConfig(level=log_level, format="%(name)s:%(levelname)s: %(message)s")
157178

179+
if debugpy:
180+
from robotcode.core.utils.debugpy import start_debugpy, wait_for_debugpy_connected
181+
182+
app.verbose(f"Try to start a debugpy session on port {debugpy_port}")
183+
184+
real_port = start_debugpy(debugpy_port, False)
185+
186+
if real_port is not None:
187+
if real_port != debugpy_port:
188+
app.verbose(f"Debugpy session started on port {real_port}")
189+
190+
if debugpy_wait_for_client:
191+
app.verbose("Waiting for debugpy client to connect...")
192+
wait_for_debugpy_connected()
193+
else:
194+
app.verbose("Could not start debugpy session. Enable logging for more information.")
195+
158196

159197
robotcode.add_command(config)
160198
robotcode.add_command(profiles)

vscode-client/languageclientsmanger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export class LanguageClientsManager {
271271

272272
this._pythonValidPythonAndRobotEnv.set(folder, true);
273273

274-
const robotCodeExtraArgs = config.get<string[]>("extraArgs", []);
274+
const robotCodeExtraArgs = config.get<string[]>("languageServer.extraArgs", []);
275275

276276
const args: string[] = ["-u", "-X", "utf8", this.pythonManager.robotCodeMain];
277277
const serverArgs: string[] = [...robotCodeExtraArgs, "language-server"];

0 commit comments

Comments
 (0)