Repository navigation
refactor(lsp): share GitHub-release and dotnet-tool install logic across downloaded servers #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b90002a
refactor(lsp): share GitHub-release and dotnet-tool install logic acr…
amondnet ca179cf
fix(lsp): make downloaded server binaries executable by their owner only
amondnet df6a56c
chore(lsp): apply AI code review suggestions
amondnet c9372e9
chore(lsp): apply AI code review suggestions
amondnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import type { LSPServerInfo } from './types' | ||
| import { spawn } from 'node:child_process' | ||
| import fs from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import process from 'node:process' | ||
| import { findCachedBinary, serverCacheDir } from './github-release' | ||
| import { attachLSPProcessHandlers, autoInstallDisabled, log, nearestRoot } from './utils' | ||
|
|
||
| export interface DotnetToolServerSpec { | ||
| id: string | ||
| /** NuGet tool package, also the executable name. */ | ||
| tool: string | ||
| extensions: string[] | ||
| rootMarkers: string[] | ||
| } | ||
|
|
||
| async function installDotnetTool(serverId: string, tool: string, toolPath: string): Promise<string | undefined> { | ||
| if (!Bun.which('dotnet')) { | ||
| log.warn(`.NET SDK is required for ${tool}. Install .NET SDK first.`) | ||
| return undefined | ||
| } | ||
| if (autoInstallDisabled(serverId)) { | ||
| return undefined | ||
| } | ||
|
|
||
| log.info({ tool }, 'Installing language server via dotnet tool') | ||
| try { | ||
| await fs.mkdir(toolPath, { recursive: true }) | ||
| const proc = Bun.spawn(['dotnet', 'tool', 'install', tool, '--tool-path', toolPath], { | ||
| stdout: 'ignore', | ||
| stderr: 'pipe', | ||
| }) | ||
| const [exitCode, stderr] = await Promise.all([proc.exited, new Response(proc.stderr).text()]) | ||
| if (exitCode !== 0) { | ||
| log.error({ tool, exitCode, stderr }, 'dotnet tool install failed') | ||
| return undefined | ||
| } | ||
| } | ||
| catch (err) { | ||
| log.error({ tool, err }, 'Failed to run dotnet tool install') | ||
| return undefined | ||
| } | ||
|
|
||
| const bin = path.join(toolPath, `${tool}${process.platform === 'win32' ? '.exe' : ''}`) | ||
| log.info({ bin }, 'Language server installed via dotnet tool') | ||
| return bin | ||
| } | ||
|
|
||
| /** A server run from PATH, or installed with `dotnet tool install` into `~/.cache/dora/<id>-lsp`. */ | ||
| export function dotnetToolServer(spec: DotnetToolServerSpec): LSPServerInfo { | ||
| return { | ||
| id: spec.id, | ||
| extensions: spec.extensions, | ||
| root: nearestRoot(spec.rootMarkers), | ||
| async spawn(root) { | ||
| const toolPath = serverCacheDir(`${spec.id}-lsp`) | ||
| const bin = findCachedBinary(spec.tool, toolPath) ?? await installDotnetTool(spec.id, spec.tool, toolPath) | ||
| if (!bin) { | ||
| return undefined | ||
| } | ||
| const proc = spawn(bin, [], { cwd: root }) | ||
| attachLSPProcessHandlers(proc, spec.id) | ||
| return { process: proc } | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import fs from 'node:fs/promises' | ||
| import os from 'node:os' | ||
| import path from 'node:path' | ||
| import process from 'node:process' | ||
| import { downloadFile, extractZip, log } from './utils' | ||
|
|
||
| const RELEASE_INFO_TIMEOUT_MS = 30_000 | ||
|
|
||
| export interface GithubRelease { | ||
| tag_name: string | ||
| assets: Array<{ name: string, browser_download_url: string }> | ||
| } | ||
|
|
||
| export interface ReleaseBinarySpec { | ||
| serverId: string | ||
| /** GitHub `owner/name` whose latest release ships the server. */ | ||
| repo: string | ||
| /** Name of this platform's asset; `.zip` assets are unzipped, all others untarred. */ | ||
| assetName: (release: GithubRelease) => string | ||
| } | ||
|
|
||
| export function serverCacheDir(name: string): string { | ||
| return path.join(os.homedir(), '.cache', 'dora', name) | ||
| } | ||
|
|
||
| /** Looks up `name` on PATH, then in `dirs`. */ | ||
| export function findCachedBinary(name: string, ...dirs: string[]): string | null { | ||
| return Bun.which(name, { | ||
| PATH: [process.env.PATH, ...dirs].filter(Boolean).join(path.delimiter), | ||
| }) | ||
| } | ||
|
|
||
| async function findReleaseAsset(spec: ReleaseBinarySpec): Promise<GithubRelease['assets'][number] | undefined> { | ||
| const { serverId, repo } = spec | ||
| try { | ||
| const response = await fetch(`https://api.github.com/repos/${repo}/releases/latest`, { | ||
| signal: AbortSignal.timeout(RELEASE_INFO_TIMEOUT_MS), | ||
| }) | ||
| if (!response.ok) { | ||
| log.error({ serverId, repo, status: response.status, statusText: response.statusText }, 'Failed to fetch release info from GitHub') | ||
| return undefined | ||
| } | ||
| const release = await response.json() as GithubRelease | ||
| const assetName = spec.assetName(release) | ||
| const asset = release.assets.find(a => a.name === assetName) | ||
| if (!asset) { | ||
| log.error({ serverId, assetName, availableAssets: release.assets.map(a => a.name).slice(0, 10) }, 'Release asset not found for this platform') | ||
| } | ||
| return asset | ||
| } | ||
| catch (err) { | ||
| log.error({ serverId, repo, err }, 'Failed to fetch release info from GitHub') | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts this platform's asset from the latest release of `spec.repo` into `destDir` | ||
| * and makes `bin` executable. Returns false, after logging, when any step fails. | ||
|
amondnet marked this conversation as resolved.
Resolved
|
||
| */ | ||
| export async function downloadReleaseBinary(spec: ReleaseBinarySpec, destDir: string, bin: string): Promise<boolean> { | ||
| const { serverId } = spec | ||
| const asset = await findReleaseAsset(spec) | ||
| if (!asset) { | ||
| return false | ||
| } | ||
| const assetName = asset.name | ||
|
|
||
| let downloadDir: string | undefined | ||
| try { | ||
| downloadDir = await fs.mkdtemp(path.join(os.tmpdir(), `dora-${serverId}-`)) | ||
| const archivePath = path.join(downloadDir, assetName) | ||
| await downloadFile(asset.browser_download_url, archivePath) | ||
| await extractArchive(archivePath, destDir) | ||
| await fs.access(bin) | ||
| if (process.platform !== 'win32') { | ||
| await fs.chmod(bin, 0o700) | ||
| } | ||
| } | ||
| catch (err) { | ||
| log.error({ serverId, assetName, bin, err }, 'Failed to install language server from GitHub release') | ||
| return false | ||
| } | ||
| finally { | ||
| if (downloadDir) { | ||
| await fs.rm(downloadDir, { force: true, recursive: true }).catch((err) => { | ||
| log.warn({ serverId, downloadDir, err }, 'Failed to clean up release download') | ||
| }) | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
amondnet marked this conversation as resolved.
Resolved
|
||
|
|
||
| async function extractArchive(archivePath: string, destDir: string): Promise<void> { | ||
| if (archivePath.endsWith('.zip')) { | ||
| await extractZip(archivePath, destDir) | ||
| return | ||
| } | ||
| await fs.mkdir(destDir, { recursive: true }) | ||
| const proc = Bun.spawn(['tar', '-xzf', archivePath, '-C', destDir], { stdout: 'ignore', stderr: 'pipe' }) | ||
|
amondnet marked this conversation as resolved.
Resolved
|
||
| const [exitCode, stderr] = await Promise.all([proc.exited, new Response(proc.stderr).text()]) | ||
| if (exitCode !== 0) { | ||
| throw new Error(`tar exited ${exitCode}: ${stderr}`) | ||
| } | ||
|
amondnet marked this conversation as resolved.
Resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.