Skip to content

The agent that grows with you

refactor(lsp): declare svelte's untrusted switch on the registry and share the path-list parser

kshitijk4poor8ee5305fdb82
diff --git a/agent/lsp/manager.py b/agent/lsp/manager.py
index 0b1d08eb716..e32ebf7a5fd 100644
--- a/agent/lsp/manager.py
+++ b/agent/lsp/manager.py
@@ -44,32 +44,27 @@ def _float_or(value: Any, default: float) -> float:
return default
-def _parse_exclude_roots(value: Any) -> Optional[List[str]]:
- """Normalise ``lsp.exclude_roots``; ``None`` (fail closed) with a WARNING for a non-list value."""
+def _parse_path_list(value: Any, key: str, expected: str, consequence: str, invalid: Optional[List[str]]):
+ """Normalise a ``~``-expanded list of paths; ``invalid`` (with a WARNING) for a non-list value."""
if value is None:
return []
if not isinstance(value, list) or not all(isinstance(p, str) for p in value):
- eventlog.event_log.warning(
- "lsp.exclude_roots must be a list of glob patterns, e.g. ['~/big-monorepo', '/srv/*/vendor'] "
- "(got %s); LSP is skipped for every workspace until the key is fixed",
- type(value).__name__,
- )
- return None
+ eventlog.event_log.warning("lsp.%s must be %s (got %s); %s until the key is fixed",
+ key, expected, type(value).__name__, consequence)
+ return invalid
return [os.path.expanduser(p) for p in value if p]
+def _parse_exclude_roots(value: Any) -> Optional[List[str]]:
+ """``lsp.exclude_roots``; ``None`` (fail closed: every root excluded) for a malformed value."""
+ return _parse_path_list(value, "exclude_roots", "a list of glob patterns, e.g. ['~/big-monorepo', '/srv/*/vendor']",
+ "LSP is skipped for every workspace", None)
+
+
def parse_trusted_workspaces(value: Any) -> List[str]:
- """Normalise ``lsp.trusted_workspaces``; a malformed value trusts nothing extra (WARNING)."""
- if value is None:
- return []
- if not isinstance(value, list) or not all(isinstance(p, str) for p in value):
- eventlog.event_log.warning(
- "lsp.trusted_workspaces must be a list of directories, e.g. ['~/code/my-app'] (got %s); "
- "only the workspaces you launched Hermes or opened the session in are trusted until the key is fixed",
- type(value).__name__,
- )
- return []
- return [os.path.expanduser(p) for p in value if p]
+ """``lsp.trusted_workspaces``; a malformed value trusts nothing extra."""
+ return _parse_path_list(value, "trusted_workspaces", "a list of directories, e.g. ['~/code/my-app']",
+ "only the workspaces you launched Hermes or opened the session in are trusted", [])
def _client_key(srv: ServerDef, root: str, trusted: bool) -> _Key:
diff --git a/agent/lsp/servers.py b/agent/lsp/servers.py
index 50140e36a4a..60b153267e0 100644
--- a/agent/lsp/servers.py
+++ b/agent/lsp/servers.py
@@ -155,11 +155,13 @@ def _make_spec(root: str, ctx: ServerContext, server_id: str, command: List[str]
def _simple_spawn(server_id: str, which: Sequence[str], args: Sequence[str] = (),
install_pkg: Optional[str] = None, base_init: Optional[Dict[str, Any]] = None,
- seed: bool = False) -> _SpawnFn:
- """Build a spawn function for the common single-binary server shape."""
+ seed: bool = False, untrusted_init: Optional[Dict[str, Any]] = None) -> _SpawnFn:
+ """Build a spawn function for the common single-binary server shape; ``untrusted_init`` replaces
+ ``base_init`` in an untrusted workspace (the server's own switch for not loading project code)."""
def build(root: str, ctx: ServerContext) -> Optional[SpawnSpec]:
bin_path = _find_binary(ctx, server_id, which, install_pkg)
- return None if bin_path is None else _make_spec(root, ctx, server_id, [bin_path, *args], base_init, seed)
+ init = base_init if ctx.trusted or untrusted_init is None else untrusted_init
+ return None if bin_path is None else _make_spec(root, ctx, server_id, [bin_path, *args], init, seed)
return build
@@ -304,16 +306,6 @@ def _spawn_typescript(root: str, ctx: ServerContext) -> Optional[SpawnSpec]:
return _make_spec(root, ctx, "typescript", [bin_path, "--stdio"], base, seed=True)
-def _untrusted_init(server_id: str, init: Dict[str, Any], which: Sequence[str], args: Sequence[str],
- install_pkg: str) -> _SpawnFn:
- """A single-binary server whose own option switches off project-code loading when untrusted."""
- def build(root: str, ctx: ServerContext) -> Optional[SpawnSpec]:
- bin_path = _find_binary(ctx, server_id, which, install_pkg)
- return None if bin_path is None else _make_spec(root, ctx, server_id, [bin_path, *args],
- None if ctx.trusted else init)
- return build
-
-
def _find_pses_bundle(ctx: ServerContext) -> Optional[str]:
"""Locate the PowerShellEditorServices bundle dir (release zip, manual install). Resolution order:
``lsp.servers.powershell.command[0]`` when a directory, ``init_overrides["powershell"]["bundlePath"]``,
@@ -390,7 +382,7 @@ UNTRUSTED_SAFE_SERVERS = frozenset({
"typescript", # tsserver pinned to Hermes's SDK; plugins then resolve beside it (_spawn_typescript)
"svelte-language-server", # isTrusted: false — no svelte.config.js, no project svelte/prettier
"bash-language-server", # parses scripts; diagnostics from shellcheck on PATH
- "yaml-language-server", # parses YAML against JSON schemas; no project code
+ "yaml-language-server", # validates against JSON schemas (may fetch them); no project code
"dockerfile-ls", # parses the Dockerfile; never builds it
"intelephense", # static PHP indexer; never runs php or composer
"clangd", # no --query-driver, so it never runs a project compiler
@@ -409,12 +401,12 @@ def _server(server_id: str, extensions: Tuple[str, ...], description: str, *,
resolve_root: Optional[_RootFn] = None, build_spawn: Optional[_SpawnFn] = None,
which: Sequence[str] = (), args: Sequence[str] = (), install_pkg: Optional[str] = None,
base_init: Optional[Dict[str, Any]] = None, seed: bool = False,
- multi_root: bool = False) -> ServerDef:
+ untrusted_init: Optional[Dict[str, Any]] = None, multi_root: bool = False) -> ServerDef:
"""Registry entry factory: defaults to marker-based root + single-binary spawn."""
return ServerDef(
server_id, extensions,
resolve_root or _markers_root(markers, excludes),
- build_spawn or _simple_spawn(server_id, which or (server_id,), args, install_pkg, base_init, seed),
+ build_spawn or _simple_spawn(server_id, which or (server_id,), args, install_pkg, base_init, seed, untrusted_init),
seed_first_push=seed, description=description, multi_root=multi_root,
)
@@ -428,10 +420,9 @@ SERVERS: List[ServerDef] = [
build_spawn=_spawn_typescript, seed=True),
_server("vue-language-server", (".vue",), "Vue.js — @vue/language-server", resolve_root=_root_typescript,
build_spawn=_spawn_vue),
- # Untrusted: no svelte.config.js and no project svelte/prettier (the server's own isTrusted switch).
_server("svelte-language-server", (".svelte",), "Svelte — svelte-language-server", resolve_root=_root_typescript,
- build_spawn=_untrusted_init("svelte-language-server", {"isTrusted": False},
- ("svelteserver", "svelte-language-server"), ("--stdio",), "svelte-language-server")),
+ which=("svelteserver", "svelte-language-server"), args=("--stdio",), install_pkg="svelte-language-server",
+ untrusted_init={"isTrusted": False}),
_server("astro-language-server", (".astro",), "Astro — @astrojs/language-server", resolve_root=_root_typescript,
which=("astro-ls", "astro-language-server"), args=("--stdio",), install_pkg="@astrojs/language-server"),
_server("gopls", (".go",), "Go — gopls", markers=["go.work", "go.mod", "go.sum"], install_pkg="gopls"),
diff --git a/website/docs/user-guide/features/lsp.md b/website/docs/user-guide/features/lsp.md
index 5aa89bbd66f..8ec1219928b 100644
--- a/website/docs/user-guide/features/lsp.md
+++ b/website/docs/user-guide/features/lsp.md
@@ -100,7 +100,7 @@ lsp.trusted_workspaces`, and `hermes lsp status` marks those servers
| pyright | `VIRTUAL_ENV` or the Hermes-managed Python, never the project's `.venv`/`venv` |
| typescript-language-server | `tsserver.path` pinned to the TypeScript next to the server; skipped if there is none |
| svelte-language-server | `isTrusted: false` (no `svelte.config.js`, no project `svelte`/`prettier`) |
-| bash-language-server, yaml-language-server, dockerfile-ls, intelephense | unchanged: they only parse files |
+| bash-language-server, yaml-language-server, dockerfile-ls, intelephense | unchanged: they run no project code (yaml-language-server may fetch the JSON schemas a file names) |
| clangd | unchanged: Hermes never passes `--query-driver`, so no project compiler runs |
On a local backend, the post-write shell linters that would use the
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/lsp.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/lsp.md
index b4fcf044131..72a31095edd 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/lsp.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/lsp.md
@@ -82,7 +82,7 @@ prisma、astro、vue-language-server(它会加载项目 `tsconfig.json` 中
| pyright | 使用 `VIRTUAL_ENV` 或 Hermes 管理的 Python,绝不使用项目的 `.venv`/`venv` |
| typescript-language-server | `tsserver.path` 固定为服务器旁边的 TypeScript;若没有则跳过 |
| svelte-language-server | `isTrusted: false`(不加载 `svelte.config.js`,不加载项目的 `svelte`/`prettier`) |
-| bash-language-server、yaml-language-server、dockerfile-ls、intelephense | 不变:它们只解析文件 |
+| bash-language-server、yaml-language-server、dockerfile-ls、intelephense | 不变:它们不运行项目代码(yaml-language-server 可能会下载文件指定的 JSON schema) |
| clangd | 不变:Hermes 从不传入 `--query-driver`,因此不会运行项目的编译器 |
在本地后端上,当终端的当前目录不受信任时,会使用检出自带工具链的写入后 shell 检查器也会以同样方式跳过: