|
27 | 27 | PluginError, |
28 | 28 | run_bounded_process, |
29 | 29 | ) |
| 30 | +from run_entrypoint import run_message # noqa: E402 |
30 | 31 |
|
31 | 32 | _JUNIT_REPORT_REL = ".pytest_cache/lithe-junit.xml" |
32 | | -_RUN_MODULE = ( |
33 | | - "import runpy, sys; sys.path.insert(0, sys.argv.pop(1)); " |
34 | | - "runpy.run_module(sys.argv.pop(1), run_name='__main__', alter_sys=True)" |
35 | | -) |
| 33 | +_ENTRYPOINT_LAUNCHER = str(Path(_SCRIPTS_DIR) / "run_entrypoint.py") |
36 | 34 |
|
37 | 35 | def file_uri_to_path(uri: str) -> Optional[Path]: |
38 | 36 | parsed = urlparse(uri) |
@@ -186,8 +184,16 @@ def create_build_provider_server() -> JsonRpcServer: |
186 | 184 | workspace_root_holder: dict[str, Path] = {"root": Path.cwd().resolve()} |
187 | 185 | degraded_model_roots: set[str] = set() |
188 | 186 | state_lock = threading.Lock() |
| 187 | + configured_interpreter = False |
| 188 | + locale = "" |
189 | 189 |
|
190 | 190 | def _on_init(params: dict[str, Any]) -> None: |
| 191 | + nonlocal configured_interpreter, locale |
| 192 | + initialization = params.get("initializationOptions") |
| 193 | + if isinstance(initialization, dict): |
| 194 | + configured_interpreter = bool(initialization.get("pythonExecutable")) |
| 195 | + if isinstance(params.get("locale"), str): |
| 196 | + locale = params["locale"] |
191 | 197 | folders = params.get("workspaceFolders") |
192 | 198 | if isinstance(folders, list) and folders: |
193 | 199 | first = folders[0] |
@@ -382,41 +388,49 @@ def handle_prepare_run(params: dict[str, Any], ctx: CallContext) -> dict[str, An |
382 | 388 | if (params.get("run") != "main" or params.get("target") is not None |
383 | 389 | or not isinstance(project, dict) or project.get("path") != norm_root |
384 | 390 | or not isinstance(launch, dict)): |
385 | | - raise PluginError("python.invalidRunEntrypoint", "Run this file from its Python project.") |
| 391 | + raise PluginError("python.invalidRunEntrypoint", run_message("invalid", locale)) |
386 | 392 |
|
387 | 393 | entrypoint = launch.get("entrypoint") |
388 | 394 | if not isinstance(entrypoint, str): |
389 | | - raise PluginError("python.invalidRunEntrypoint", "Select a Python file to run.") |
| 395 | + raise PluginError("python.invalidRunEntrypoint", run_message("invalid", locale)) |
390 | 396 | kind, _, relative_file = entrypoint.partition(":") |
391 | 397 | if (kind not in ("script", "module") or relative_file.startswith("/") |
392 | 398 | or any(char in relative_file for char in ("\\", ":", "\x00"))): |
393 | | - raise PluginError("python.invalidRunEntrypoint", "Select a Python file inside the project.") |
| 399 | + raise PluginError("python.invalidRunEntrypoint", run_message("invalid", locale)) |
394 | 400 |
|
395 | 401 | # Saved configurations can outlive their source or its main guard. |
396 | 402 | entries = inspect_file_entrypoints(instance_dir, relative_file, project) |
397 | 403 | if not entries or entries[0]["entrypoint"] != entrypoint: |
398 | | - raise PluginError("python.staleRunEntrypoint", "The saved Python entry point is unavailable. Open a runnable Python file and run it again.") |
| 404 | + raise PluginError("python.staleRunEntrypoint", run_message("stale", locale)) |
399 | 405 |
|
400 | 406 | working_directory = launch.get("workingDirectory") or norm_root |
| 407 | + source = str(instance_dir / relative_file) |
401 | 408 | if kind == "script": |
402 | | - program = [str(instance_dir / relative_file)] |
403 | | - elif relative_file.startswith("src/") or working_directory != norm_root: |
404 | | - # Keep module imports rooted in the build instance even when using a custom cwd. |
405 | | - module_root = instance_dir / "src" if relative_file.startswith("src/") else instance_dir |
406 | | - program = ["-c", _RUN_MODULE, str(module_root), entries[0]["name"]] |
| 409 | + program = [source] |
407 | 410 | else: |
408 | | - program = ["-m", entries[0]["name"]] |
| 411 | + module_root = instance_dir / "src" if relative_file.startswith("src/") else instance_dir |
| 412 | + program = [_ENTRYPOINT_LAUNCHER, "package", str(module_root), entries[0]["name"], source, locale] |
409 | 413 |
|
410 | 414 | environment = dict(params.get("environment") or {}) |
411 | 415 | environment.update(launch.get("environment") or {}) |
| 416 | + if kind == "module": |
| 417 | + environment.setdefault("PYTHONIOENCODING", "utf-8") |
| 418 | + arguments = [ |
| 419 | + *_string_args(launch.get("runtimeArguments")), |
| 420 | + *program, |
| 421 | + *_string_args(params.get("optionArguments")), |
| 422 | + *_string_args(launch.get("arguments", params.get("userArguments"))), |
| 423 | + ] |
| 424 | + if configured_interpreter: |
| 425 | + tool = params.get("tool") |
| 426 | + executable = tool.get("executable") if isinstance(tool, dict) else None |
| 427 | + if not isinstance(executable, str) or not Path(executable).is_absolute(): |
| 428 | + raise PluginError("python.interpreterUnavailable", run_message("interpreter", locale)) |
| 429 | + environment.setdefault("PYTHON_EXECUTABLE", executable) |
| 430 | + arguments = ["-I", "-B", _ENTRYPOINT_LAUNCHER, "interpreter", executable, *arguments] |
412 | 431 | return { |
413 | 432 | "executable": {"kind": "toolchain", "value": "python"}, |
414 | | - "arguments": [ |
415 | | - *_string_args(launch.get("runtimeArguments")), |
416 | | - *program, |
417 | | - *_string_args(params.get("optionArguments")), |
418 | | - *_string_args(launch.get("arguments", params.get("userArguments"))), |
419 | | - ], |
| 433 | + "arguments": arguments, |
420 | 434 | "workingDirectory": working_directory, |
421 | 435 | "environment": environment, |
422 | 436 | } |
|
0 commit comments