Skip to content

refactor(lsp): share npm-cache install logic across npm-based servers - #159

Draft
amondnet wants to merge 1 commit into
mainfrom
amondnet/lsp-npm-cache-helper
Draft

amondnet wants to merge 1 commit into
mainfrom
amondnet/lsp-npm-cache-helper

Conversation

@amondnet

@amondnet amondnet commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

SonarCloud flagged new-code duplication at 5.3% against a 3% limit on #156. Eight npm-installed LSP servers (astro, bash, dockerfile, php, prisma, svelte, vue, yaml in packages/lsp/src/server) each carried about 70 lines of near-identical install logic. This PR moves that logic into one shared helper.

  • New packages/lsp/src/server/npm-cache.ts exports ensureNpmCache(spec), npmBin(name) and npmCacheDir(dir). ensureNpmCache checks the required paths and the .installed_version marker, runs npm install --prefix $HOME/.cache/dora/<cacheDir> when the install is missing or outdated, honors the CODE_LSP_AUTO_INSTALL gate (autoInstallDisabled), and verifies the required paths afterwards.
  • Each server keeps its own spawn logic and declares an NpmCacheSpec.

Scope

Out of scope: duplication in csharp, fsharp, lua, terraform and texlab is also flagged and is not touched here.

Blast radius

Behavior is preserved: same cache dirs and the same marker format (versions joined with _), so existing caches stay valid. With auto_install off, a stale install is still used, and nothing is installed when the cache is missing.

One small difference: the pre-install check now requires all required paths. For astro this includes the TypeScript lib dir, so an astro cache missing it now triggers a reinstall.

Verification

  • New unit tests in packages/lsp/test/unit/auto-install.test.ts: gate off with no cache returns undefined and creates nothing; a cache with a matching or outdated marker is used.
  • bun test packages/lsp/test/unit: 196 pass.
  • bun run typecheck and eslint are clean.

Related issue

None.

Checklist

  • PR title follows Conventional Commits
  • Tests added or updated, and the suite passes (bun run test)
  • Lint/format pass (bun run lint)
  • Documentation updated if behavior changed (no user-facing behavior change)
  • No breaking change, or a BREAKING CHANGE: note is included

Summary by cubic

Consolidates the duplicated npm install logic shared by eight LSP servers into one ensureNpmCache helper in packages/lsp/src/server/npm-cache.ts.

The eight npm-installed servers (astro, bash, dockerfile, php, prisma, svelte, vue, yaml) each carried ~70 lines of near-identical logic for the version-marker check, npm install --prefix, the CODE_LSP_AUTO_INSTALL gate, and required-path verification. That logic is now shared, with each server declaring an NpmCacheSpec and keeping its own spawn logic. Cache directories, the .installed_version marker format, and overall behavior are preserved, so existing caches stay valid.

Behavior change

  • Pre-install checks now require all paths listed in the spec, so an astro cache missing the TypeScript lib dir now triggers a reinstall where it previously didn't.

Written for commit 22193f6. Summary will update on new commits.

Turn on auto-fix

The eight npm-installed servers (astro, bash, dockerfile, php, prisma, svelte, vue, yaml)
each had about 70 lines of their own install logic, and SonarCloud flagged it as
duplication. That logic now lives in `ensureNpmCache` in npm-cache.ts:
- version-marker check
- `npm install --prefix`
- auto_install gate
- required-path check

The cache dirs and the marker format are unchanged, so existing installs stay valid.
Tests are added for the gate-off/no-cache case and the stale-but-usable-cache case.
@sonarqubecloud

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the dependency management of various language servers (Astro, Bash, Dockerfile, PHP, Prisma, Svelte, Vue, and YAML) by consolidating their installation and caching logic into a new shared utility module, npm-cache.ts. Feedback on these changes highlights a high-severity issue where an unconsumed stdout pipe in Bun.spawn could lead to a deadlock, along with a lack of error handling for synchronous spawning errors. Additionally, a medium-severity cognitive overload issue was identified in the installation control flow due to a complex three-part negated boolean expression.

Comment on lines +52 to +56
const proc = Bun.spawn(['npm', 'install', '--prefix', resourcesDir, ...specs], {
cwd: resourcesDir,
stdout: 'pipe',
stderr: 'pipe',
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

[HIGH] Potential deadlock due to unconsumed stdout pipe and missing try/catch for Bun.spawn

Problem 1: The stdout of the spawned npm install process is piped (stdout: 'pipe') but never consumed. If the installation produces more output than the OS pipe buffer capacity, the child process will block on writing to stdout, causing await proc.exited to hang indefinitely.

Problem 2: Bun.spawn is called to execute npm (a system binary on the PATH) without being wrapped in a try/catch block. OS-level errors (such as permission issues or invalid binary headers) can cause Bun.spawn to throw synchronous errors, which will crash the application.

Suggestion: Set stdout: 'ignore' to prevent blocking, and wrap the Bun.spawn call in a try/catch block to handle synchronous spawning errors gracefully.

    let proc;
    try {
      proc = Bun.spawn(['npm', 'install', '--prefix', resourcesDir, ...specs], {
        cwd: resourcesDir,
        stdout: 'ignore',
        stderr: 'pipe',
      });
    } catch (err) {
      // Handle spawn error gracefully
    }
References
  1. Always wrap process spawning calls (such as Bun.spawn) in a try/catch block when probing system binaries on the PATH. OS-level errors (e.g., ENOEXEC due to invalid ELF headers, or permission issues) can cause the spawn call to throw synchronous errors, which must be caught to prevent application crashes and allow graceful fallbacks.

Comment on lines +82 to +86
if (!await isInstalled(resourcesDir, spec, expectedVersion)
&& !autoInstallDisabled(spec.serverId)
&& !await install(resourcesDir, spec, expectedVersion)) {
return undefined
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

[MEDIUM] Cognitive Overload in installation control flow

Symptom: A complex, three-part negated boolean expression with an asynchronous side-effecting function call (!await isInstalled(...) && !autoInstallDisabled(...) && !await install(...)) is used to control the installation flow.

Source: Fowler — Refactoring (Long Method / Cognitive Overload / Side Effects in Expressions); Ousterhout — A Philosophy of Software Design (Cognitive Load).

Consequence: Future maintainers will find it harder to reason about the control flow, especially since the third condition (install) has side effects and is only executed if the first two conditions are met. This increases the risk of introducing bugs during future modifications.

Remedy: Refactor the condition into explicit, nested if statements to separate the checks from the action and make the control flow obvious.

  if (!await isInstalled(resourcesDir, spec, expectedVersion)) {
    if (autoInstallDisabled(spec.serverId)) {
      // Auto-install is disabled; proceed to check if required files exist
    } else if (!await install(resourcesDir, spec, expectedVersion)) {
      return undefined
    }
  }
References
  1. The Organization Style Guide (§12.2, R1) flags boolean expressions with 3 or more conditions as a symptom of Cognitive Overload. (link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant