|
7 | 7 | input closed, then SIGTERM and SIGKILL to the group, as a cancelled run escalates; whatever the |
8 | 8 | server does not read, the stop reaches SIGTERM and SIGKILL on time. A Node server also starts with |
9 | 9 | `hide_paths.cjs` preloaded, which keeps the hidden directories from it |
10 | | -(docs/specs/web-ui.md#hidden-directories). |
| 10 | +(docs/specs/web-ui.md#hidden-directories), and rust-analyzer finds the standard library in links |
| 11 | +cstudy makes (docs/specs/web-ui.md#language-servers). |
11 | 12 | """ |
12 | 13 |
|
13 | 14 | import asyncio |
14 | 15 | import json |
15 | 16 | import os |
16 | 17 | import signal |
| 18 | +import subprocess |
17 | 19 | from collections import deque |
18 | 20 | from collections.abc import Mapping, Sequence |
19 | 21 | from contextlib import suppress |
|
22 | 24 | from typing import cast |
23 | 25 |
|
24 | 26 | from coding_study.adapters.process_environment import inherited_environment |
25 | | -from coding_study.domain.code_intelligence import LanguageServer, Launch |
| 27 | +from coding_study.application.code_intelligence import SessionTarget |
| 28 | +from coding_study.domain.code_intelligence import LanguageServer, Launch, StandardLibrary |
26 | 29 | from coding_study.entrypoints.web.children import StreamedChild, start_process |
27 | 30 | from coding_study.entrypoints.web.runs import SIGNAL_EXIT_BASE |
28 | 31 |
|
|
42 | 45 | GROUP_POLL_S = 0.02 |
43 | 46 | KILL_WAIT_S = 2.0 |
44 | 47 | """How long a group that got SIGKILL has to disappear before the stop gives up on it.""" |
| 48 | +SYSROOT_TIMEOUT_S = 30.0 |
| 49 | +"""How long `rustc --print sysroot` may take: rustup may first read the toolchain files.""" |
45 | 50 |
|
46 | 51 |
|
47 | 52 | @dataclass(frozen=True, slots=True) |
@@ -83,6 +88,59 @@ def prepare_data_directory(server: LanguageServer, launch: Launch) -> None: |
83 | 88 | path.write_text(content, encoding="utf-8") |
84 | 89 |
|
85 | 90 |
|
| 91 | +def prepare_standard_library(target: SessionTarget, environment: Mapping[str, str]) -> None: |
| 92 | + """Links the standard library of the toolchain the session's root selects (`rustc --print |
| 93 | + sysroot`, run in the root, in `environment`) for a server that reads it from links. Nothing |
| 94 | + is linked without `rustc` or the toolchain's library sources: the server then looks for the |
| 95 | + library itself.""" |
| 96 | + library = target.server.standard_library |
| 97 | + links = target.standard_library_links |
| 98 | + if library is None or links is None: |
| 99 | + return |
| 100 | + sysroot = _sysroot(library, target.directory, environment) |
| 101 | + source = None if sysroot is None else sysroot / library.library |
| 102 | + if source is not None and source.is_dir(): |
| 103 | + link_standard_library(library, source, links) |
| 104 | + |
| 105 | + |
| 106 | +def link_standard_library(library: StandardLibrary, source: Path, links: Path) -> None: |
| 107 | + """`links` holds a link to each entry of `source` but those `library` leaves out, its Cargo |
| 108 | + manifest and lock. A link to another toolchain's entry is replaced, each in one rename, so a |
| 109 | + session already running on the links never finds one missing; a link to an entry `source` |
| 110 | + no longer has is removed.""" |
| 111 | + links.mkdir(parents=True, exist_ok=True) |
| 112 | + wanted = {entry.name: entry for entry in source.iterdir() if entry.name not in library.left_out} |
| 113 | + for name, entry in wanted.items(): |
| 114 | + link = links / name |
| 115 | + if link.is_symlink() and link.readlink() == entry: |
| 116 | + continue |
| 117 | + fresh = links / f".{name}.{os.getpid()}" |
| 118 | + fresh.unlink(missing_ok=True) |
| 119 | + fresh.symlink_to(entry, target_is_directory=entry.is_dir()) |
| 120 | + fresh.replace(link) |
| 121 | + for link in links.iterdir(): |
| 122 | + if link.is_symlink() and link.name not in wanted: |
| 123 | + link.unlink() |
| 124 | + |
| 125 | + |
| 126 | +def _sysroot( |
| 127 | + library: StandardLibrary, directory: Path, environment: Mapping[str, str] |
| 128 | +) -> Path | None: |
| 129 | + try: |
| 130 | + printed = subprocess.run( |
| 131 | + library.sysroot_command, |
| 132 | + cwd=directory, |
| 133 | + env=dict(environment), |
| 134 | + capture_output=True, |
| 135 | + text=True, |
| 136 | + timeout=SYSROOT_TIMEOUT_S, |
| 137 | + check=True, |
| 138 | + ).stdout.strip() |
| 139 | + except (OSError, subprocess.SubprocessError): |
| 140 | + return None |
| 141 | + return Path(printed) if printed else None |
| 142 | + |
| 143 | + |
86 | 144 | class ServerStartError(Exception): |
87 | 145 | """The server could not be started; the text is why, as the 4002 reason says it.""" |
88 | 146 |
|
|
0 commit comments