Repository navigation
Conversation
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.
|
There was a problem hiding this comment.
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.
| const proc = Bun.spawn(['npm', 'install', '--prefix', resourcesDir, ...specs], { | ||
| cwd: resourcesDir, | ||
| stdout: 'pipe', | ||
| stderr: 'pipe', | ||
| }) |
There was a problem hiding this comment.
[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
- Always wrap process spawning calls (such as
Bun.spawn) in atry/catchblock when probing system binaries on the PATH. OS-level errors (e.g.,ENOEXECdue 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.
| if (!await isInstalled(resourcesDir, spec, expectedVersion) | ||
| && !autoInstallDisabled(spec.serverId) | ||
| && !await install(resourcesDir, spec, expectedVersion)) { | ||
| return undefined | ||
| } |
There was a problem hiding this comment.
[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
- The Organization Style Guide (§12.2, R1) flags boolean expressions with 3 or more conditions as a symptom of Cognitive Overload. (link)
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |



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.packages/lsp/src/server/npm-cache.tsexportsensureNpmCache(spec),npmBin(name)andnpmCacheDir(dir).ensureNpmCachechecks the required paths and the.installed_versionmarker, runsnpm install --prefix $HOME/.cache/dora/<cacheDir>when the install is missing or outdated, honors theCODE_LSP_AUTO_INSTALLgate (autoInstallDisabled), and verifies the required paths afterwards.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
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 typecheckand eslint are clean.Related issue
None.
Checklist
bun run test)bun run lint)BREAKING CHANGE:note is includedSummary by cubic
Consolidates the duplicated npm install logic shared by eight LSP servers into one
ensureNpmCachehelper inpackages/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, theCODE_LSP_AUTO_INSTALLgate, and required-path verification. That logic is now shared, with each server declaring anNpmCacheSpecand keeping its own spawn logic. Cache directories, the.installed_versionmarker format, and overall behavior are preserved, so existing caches stay valid.Behavior change
Written for commit 22193f6. Summary will update on new commits.