Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/amazonq/src/lsp/activation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
*/

import vscode from 'vscode'
import path from 'path'
import { AmazonQLSPDownloader } from './download'
import { startLanguageServer } from './client'
import { AmazonQLSPInstaller } from './lspInstaller'
import { ToolkitError } from 'aws-core-vscode/shared'

export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
const serverPath = ctx.asAbsolutePath('resources/qdeveloperserver')
const clientPath = ctx.asAbsolutePath('resources/qdeveloperclient')
await new AmazonQLSPDownloader(serverPath, clientPath).tryInstallLsp()
await startLanguageServer(ctx, path.join(serverPath, 'aws-lsp-codewhisperer.js'))
try {
const result = await new AmazonQLSPInstaller().install()
await startLanguageServer(ctx, result.location)
} catch (err) {
const e = err as ToolkitError
void vscode.window.showInformationMessage(`Unable to launch amazonq language server: ${e.message}`)
}
}
77 changes: 0 additions & 77 deletions packages/amazonq/src/lsp/download.ts

This file was deleted.

32 changes: 32 additions & 0 deletions packages/amazonq/src/lsp/lspInstaller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import * as vscode from 'vscode'
import { ManifestManager, LspManager, LspInstaller, LspResult } from 'aws-core-vscode/shared'

const manifestURL = 'https://aws-toolkit-language-servers.amazonaws.com/codewhisperer/0/manifest.json'

export class AmazonQLSPInstaller implements LspInstaller {
async install(): Promise<LspResult> {
const overrideLocation = process.env.AWS_LANGUAGE_SERVER_OVERRIDE
if (overrideLocation) {
void vscode.window.showInformationMessage(`Using language server override location: ${overrideLocation}`)
return {
assetDirectory: overrideLocation,
location: 'override',
version: '0.0.0',
}
}

const manifestManager = new ManifestManager(manifestURL, 'amazonq')
const manifest = await manifestManager.getManifest()

const lspManager = new LspManager(manifest, '', '')
const downloadResult = lspManager.download()

// TODO Cleanup old versions of language servers
return downloadResult
}
}
90 changes: 46 additions & 44 deletions packages/amazonq/test/unit/amazonq/lsp/lspController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,52 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'assert'
import sinon from 'sinon'
import { LspController } from 'aws-core-vscode/amazonq'
import { createTestFile } from 'aws-core-vscode/test'
import { fs, Content } from 'aws-core-vscode/shared'

describe('Amazon Q LSP controller', function () {
it('Download mechanism checks against hash, when hash matches', async function () {
const content = {
filename: 'qserver-linux-x64.zip',
url: 'https://x/0.0.6/qserver-linux-x64.zip',
hashes: [
'sha384:768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9',
],
bytes: 512,
} as Content
const lspController = new LspController()
sinon.stub(lspController, '_download')
const mockFileName = 'test_case_1.zip'
const mockDownloadFile = await createTestFile(mockFileName)
await fs.writeFile(mockDownloadFile.fsPath, 'test')
const result = await lspController.downloadAndCheckHash(mockDownloadFile.fsPath, content)
assert.strictEqual(result, true)
})
// TODO re-enable this file once amazon q context server is migrated to the new interface
// import assert from 'assert'
// import sinon from 'sinon'
// import { LspController } from 'aws-core-vscode/amazonq'
// import { createTestFile } from 'aws-core-vscode/test'
// import { fs, Content } from 'aws-core-vscode/shared'

it('Download mechanism checks against hash, when hash does not match', async function () {
const content = {
filename: 'qserver-linux-x64.zip',
url: 'https://x/0.0.6/qserver-linux-x64.zip',
hashes: [
'sha384:38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b',
],
bytes: 512,
} as Content
const lspController = new LspController()
sinon.stub(lspController, '_download')
const mockFileName = 'test_case_2.zip'
const mockDownloadFile = await createTestFile(mockFileName)
await fs.writeFile(mockDownloadFile.fsPath, 'file_content')
const result = await lspController.downloadAndCheckHash(mockDownloadFile.fsPath, content)
assert.strictEqual(result, false)
})
// describe('Amazon Q LSP controller', function () {
// it('Download mechanism checks against hash, when hash matches', async function () {
// const content = {
// filename: 'qserver-linux-x64.zip',
// url: 'https://x/0.0.6/qserver-linux-x64.zip',
// hashes: [
// 'sha384:768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9',
// ],
// bytes: 512,
// } as Content
// const lspController = new LspController()
// sinon.stub(lspController, '_download')
// const mockFileName = 'test_case_1.zip'
// const mockDownloadFile = await createTestFile(mockFileName)
// await fs.writeFile(mockDownloadFile.fsPath, 'test')
// const result = await lspController.downloadAndCheckHash(mockDownloadFile.fsPath, content)
// assert.strictEqual(result, true)
// })

afterEach(() => {
sinon.restore()
})
})
// it('Download mechanism checks against hash, when hash does not match', async function () {
// const content = {
// filename: 'qserver-linux-x64.zip',
// url: 'https://x/0.0.6/qserver-linux-x64.zip',
// hashes: [
// 'sha384:38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b',
// ],
// bytes: 512,
// } as Content
// const lspController = new LspController()
// sinon.stub(lspController, '_download')
// const mockFileName = 'test_case_2.zip'
// const mockDownloadFile = await createTestFile(mockFileName)
// await fs.writeFile(mockDownloadFile.fsPath, 'file_content')
// const result = await lspController.downloadAndCheckHash(mockDownloadFile.fsPath, content)
// assert.strictEqual(result, false)
// })

// afterEach(() => {
// sinon.restore()
// })
// })
48 changes: 10 additions & 38 deletions packages/core/src/amazonq/lsp/lspController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { telemetry } from '../../shared/telemetry'
import { isCloud9 } from '../../shared/extensionUtilities'
import globals, { isWeb } from '../../shared/extensionGlobals'
import { isAmazonInternalOs } from '../../shared/vscode/env'
import { LspDownloader, Manifest } from '../../shared/fetchLsp'
import { fs } from '../../shared/fs/fs'
import { makeTemporaryToolkitFolder, tryRemoveFolder } from '../../shared/filesystemUtilities'
import { tryRemoveFolder } from '../../shared/filesystemUtilities'
import { LspInstaller, LspResult } from '../../shared/languageServer/types'

export interface Chunk {
readonly filePath: string
Expand All @@ -26,9 +26,9 @@ export interface Chunk {
readonly programmingLanguage?: string
}

const manifestUrl = 'https://aws-toolkit-language-servers.amazonaws.com/q-context/manifest.json'
// this LSP client in Q extension is only going to work with these LSP server versions
const supportedLspServerVersions = ['0.1.32']
// const manifestUrl = 'https://aws-toolkit-language-servers.amazonaws.com/q-context/manifest.json'
// // this LSP client in Q extension is only going to work with these LSP server versions
// const supportedLspServerVersions = ['0.1.32']

const nodeBinName = process.platform === 'win32' ? 'node.exe' : 'node'

Expand All @@ -49,7 +49,7 @@ export interface BuildIndexConfig {
* Pre-process the input to Index Files API
* Post-process the output from Query API
*/
export class LspController extends LspDownloader {
export class LspController implements LspInstaller {
static #instance: LspController
private _isIndexingInProgress = false
private serverPath: string
Expand All @@ -60,7 +60,6 @@ export class LspController extends LspDownloader {
}

constructor() {
super(manifestUrl, supportedLspServerVersions)
this.serverPath = globals.context.asAbsolutePath(path.join('resources', 'qserver'))
this.nodePath = globals.context.asAbsolutePath(path.join('resources', nodeBinName))
}
Expand Down Expand Up @@ -186,36 +185,9 @@ export class LspController extends LspDownloader {
return true
}

async install(manifest: Manifest) {
const server = this.getDependency(manifest, 'qserver')
const runtime = this.getDependency(manifest, 'node')
if (!server || !runtime) {
getLogger('lsp').info(`Did not find LSP URL for ${process.platform} ${process.arch}`)
return false
}

let tempFolder = undefined

try {
tempFolder = await makeTemporaryToolkitFolder()
await this.downloadAndExtractServer({
content: server,
installLocation: this.serverPath,
name: 'qserver',
tempFolder,
extractToTempFolder: true,
})

const runtimeTempPath = path.join(tempFolder, nodeBinName)
await this.installRuntime(runtime, this.nodePath, runtimeTempPath)
} finally {
// clean up temp folder
if (tempFolder) {
await tryRemoveFolder(tempFolder)
}
}

return true
install(): Promise<LspResult> {
// TODO
throw new Error('Method not implemented.')
}

async trySetupLsp(context: vscode.ExtensionContext, buildIndexConfig: BuildIndexConfig) {
Expand All @@ -225,7 +197,7 @@ export class LspController extends LspDownloader {
return
}
setImmediate(async () => {
const ok = await LspController.instance.tryInstallLsp()
const ok = await LspController.instance.install()
if (!ok) {
return
}
Expand Down
Loading
Loading