Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions core/aws-lsp-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * as textUtils from './util/text'
export * as timeoutUtils from './util/timeoutUtils'
export * from './util/awsError'
export * from './base/index'
export * as testFolder from './test/testFolder'
60 changes: 60 additions & 0 deletions core/aws-lsp-core/src/test/testFolder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as path from 'path'
import * as fs from 'fs'
import * as os from 'os'
import * as crypto from 'crypto'

/**
* Interface for working with temporary files.
* Simplified port of https://github.com/aws/aws-toolkit-vscode/blob/16477869525fb79f8dc82cb22e301aaea9c5e0c6/packages/core/src/test/testUtil.ts#L77
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 for now, this kind of precise cross-reference will help a lot, in each of these util files.

*
* Proper usage requires adding the proper logic into the hooks. See example below:
*
* before(async () => {
* ...
* testFolder = await TestFolder.create()
* ...
* }
* // Only necessary if test state should not bleed through.
* afterEach(async () => {
* ...
* await testFolder.clear()
* ...
* })
*
* after(async () => {
* ...
* await testFolder.delete()
* ...
* })
*/
export class TestFolder {
private constructor(public readonly path: string) {}

async write(fileName: string, content: string): Promise<string> {
const filePath = path.join(this.path, fileName)
fs.writeFileSync(filePath, content)
return filePath
}

static async create() {
const tempDir = path.join(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: A future optimization you could do is delete the root folder at the start/end of the entire test run (if easily doable). This will handle any mistakes from tests failing to clean up properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree. I noticed the VS Code implementation does this, and it would be a great feature. We do this by wrapping all the tests with our own test runner in VSC, but I don't see similar logic here yet, so this change would require some ground work.

os.type() === 'Darwin' ? '/tmp' : os.tmpdir(),
'aws-language-servers',
'test',
crypto.randomBytes(4).toString('hex')
)
await fs.promises.mkdir(tempDir, { recursive: true })
return new TestFolder(tempDir)
}

async delete() {
fs.rmSync(this.path, { recursive: true, force: true })
}

async clear() {
const files = await fs.readdirSync(this.path)
for (const f of files) {
await fs.rmSync(path.join(this.path, f), { recursive: true, force: true })
}
}
}
39 changes: 38 additions & 1 deletion core/aws-lsp-core/src/util/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import * as assert from 'assert'
import * as path from 'path'
import * as os from 'os'
import { normalizeSeparator, normalize, isInDirectory } from './path'
import { normalizeSeparator, normalize, isInDirectory, sanitize } from './path'
import sinon from 'ts-sinon'

describe('pathUtils', async function () {
it('normalizeSeparator()', function () {
Expand Down Expand Up @@ -76,4 +77,40 @@ describe('pathUtils', async function () {
assert.ok(!isInDirectory('/foo/bar/baz/', '/FOO/BAR/BAZ/A.TXT'))
}
})

describe('sanitizePath', function () {
let sandbox: sinon.SinonSandbox

beforeEach(function () {
sandbox = sinon.createSandbox()
})

afterEach(function () {
sandbox.restore()
})

it('trims whitespace from input path', function () {
const result = sanitize(' /test/path ')
assert.strictEqual(result, '/test/path')
})

it('expands tilde to user home directory', function () {
const homeDir = '/Users/testuser'
sandbox.stub(os, 'homedir').returns(homeDir)

const result = sanitize('~/documents/file.txt')
assert.strictEqual(result, path.join(homeDir, 'documents/file.txt'))
})

it('converts relative paths to absolute paths', function () {
const result = sanitize('relative/path')
assert.strictEqual(result, path.resolve('relative/path'))
})

it('leaves absolute paths unchanged', function () {
const absolutePath = path.resolve('/absolute/path')
const result = sanitize(absolutePath)
assert.strictEqual(result, absolutePath)
})
})
})
13 changes: 13 additions & 0 deletions core/aws-lsp-core/src/util/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,16 @@ export function normalize(p: string): string {
}
return normalizeSeparator(path.normalize(p))
}

export function sanitize(inputPath: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

In aws-toolkit-vscode this santize() was added recently, but I don't get why it was added: aws/aws-toolkit-vscode#6840 (comment)

Why is this needed instead of our normalize() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like they do slightly different things that definitely overlap with normalize. For example, normalize doesn't remove white space or expand ~. I agree they definitely overlap and could potentially be refactored to make this more clear.

let sanitized = inputPath.trim()

if (sanitized.startsWith('~')) {
sanitized = path.join(os.homedir(), sanitized.slice(1))
}

if (!path.isAbsolute(sanitized)) {
sanitized = path.resolve(sanitized)
}
return sanitized
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as assert from 'assert'
import { FsRead } from './fsRead'
import * as path from 'path'
import * as fs from 'fs/promises'
import { TestFeatures } from '@aws/language-server-runtimes/testing'
import { Workspace } from '@aws/language-server-runtimes/server-interface'
import { testFolder } from '@aws/lsp-core'
import { StubbedInstance } from 'ts-sinon'

describe('FsRead Tool', () => {
let features: TestFeatures
let tempFolder: testFolder.TestFolder

const stdout = new WritableStream({
write(chunk) {
process.stdout.write(chunk)
},
})

before(async () => {
features = new TestFeatures()
features.workspace = {
// @ts-ignore reading a file does not require all of fs to be implemented.
fs: {
readFile: (path, options?) =>
fs.readFile(path, { encoding: (options?.encoding || 'utf-8') as BufferEncoding }),
readdir: path => fs.readdir(path, { withFileTypes: true }),
exists: path =>
fs
.access(path)
.then(() => true)
.catch(() => false),
} as Workspace['fs'],
} as StubbedInstance<Workspace>
tempFolder = await testFolder.TestFolder.create()
})

after(async () => {
tempFolder.delete()
})

afterEach(async () => {
tempFolder.clear()
})

it('throws if path is empty', async () => {
const fsRead = new FsRead(features)
await assert.rejects(() => fsRead.invoke({ path: '' }))
})

it('reads entire file', async () => {
const fileContent = 'Line 1\nLine 2\nLine 3'
const filePath = await tempFolder.write('fullFile.txt', fileContent)

const fsRead = new FsRead(features)
const result = await fsRead.invoke({ path: filePath })

assert.strictEqual(result.output.kind, 'text', 'Output kind should be "text"')
assert.strictEqual(result.output.content, fileContent, 'File content should match exactly')
})

it('reads partial lines of a file', async () => {
const fileContent = 'A\nB\nC\nD\nE\nF'
const filePath = await tempFolder.write('partialFile.txt', fileContent)

const fsRead = new FsRead(features)
const result = await fsRead.invoke({ path: filePath, readRange: [2, 4] })

assert.strictEqual(result.output.kind, 'text')
assert.strictEqual(result.output.content, 'B\nC\nD')
})

it('throws error if path does not exist', async () => {
const filePath = path.join(tempFolder.path, 'no_such_file.txt')
const fsRead = new FsRead(features)

await assert.rejects(fsRead.invoke({ path: filePath }))
})

it('throws error if content exceeds 30KB', async function () {
const bigContent = 'x'.repeat(35_000)

const filePath = await tempFolder.write('bigFile.txt', bigContent)

const fsRead = new FsRead(features)

await assert.rejects(
fsRead.invoke({ path: filePath }),
/This tool only supports reading \d+ bytes at a time/i,
'Expected a size-limit error'
)
})

it('invalid line range', async () => {
const filePath = await tempFolder.write('rangeTest.txt', '1\n2\n3')
const fsRead = new FsRead(features)

await fsRead.invoke({ path: filePath, readRange: [3, 2] })
const result = await fsRead.invoke({ path: filePath, readRange: [3, 2] })
assert.strictEqual(result.output.kind, 'text')
assert.strictEqual(result.output.content, '')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import { sanitize } from '@aws/lsp-core/out/util/path'
import { InvokeOutput, maxToolResponseSize } from './toolShared'
import { Features } from '@aws/language-server-runtimes/server-interface/server'

// Port of https://github.com/aws/aws-toolkit-vscode/blob/10bb1c7dc45f128df14d749d95905c0e9b808096/packages/core/src/codewhispererChat/tools/fsRead.ts#L17

export interface FsReadParams {
path: string
readRange?: number[]
}

export class FsRead {
private readonly logging: Features['logging']
private readonly workspace: Features['workspace']

constructor(features: Pick<Features, 'workspace' | 'logging'> & Partial<Features>) {
this.logging = features.logging
this.workspace = features.workspace
}

public async invoke(params: FsReadParams): Promise<InvokeOutput> {
const path = sanitize(params.path)
const fileContents = await this.readFile(path)
this.logging.info(`Read file: ${path}, size: ${fileContents.length}`)
return this.handleFileRange(params, fileContents)
}

private async readFile(filePath: string): Promise<string> {
this.logging.info(`Reading file: ${filePath}`)
return await this.workspace.fs.readFile(filePath)
}

private handleFileRange(params: FsReadParams, fullText: string): InvokeOutput {
if (!params.readRange || params.readRange.length === 0) {
this.logging.log('No range provided. returning entire file.')
return this.createOutput(this.enforceMaxSize(fullText))
}

const lines = fullText.split('\n')
const [start, end] = this.parseLineRange(lines.length, params.readRange)
if (start > end) {
this.logging.error(`Invalid range: ${params.readRange.join('-')}`)
return this.createOutput('')
}

this.logging.log(`Reading file: ${params.path}, lines ${start + 1}-${end + 1}`)
const slice = lines.slice(start, end + 1).join('\n')
return this.createOutput(this.enforceMaxSize(slice))
}

private parseLineRange(lineCount: number, range: number[]): [number, number] {
const startIdx = range[0]
let endIdx = range.length >= 2 ? range[1] : undefined

if (endIdx === undefined) {
endIdx = -1
}

const convert = (i: number): number => {
return i < 0 ? lineCount + i : i - 1
}

const finalStart = Math.max(0, Math.min(lineCount - 1, convert(startIdx)))
const finalEnd = Math.max(0, Math.min(lineCount - 1, convert(endIdx)))
return [finalStart, finalEnd]
}

private enforceMaxSize(content: string): string {
const byteCount = Buffer.byteLength(content, 'utf8')
if (byteCount > maxToolResponseSize) {
throw new Error(
`This tool only supports reading ${maxToolResponseSize} bytes at a time.
You tried to read ${byteCount} bytes. Try executing with fewer lines specified.`
)
}
return content
}

private createOutput(content: string): InvokeOutput {
return {
output: {
kind: 'text',
content: content,
},
}
}

public getSpec() {
return {
name: 'fsRead',
description:
'A tool for reading a file. \n* This tool returns the contents of a file, and the optional `readRange` determines what range of lines will be read from the specified file.',
inputSchema: {
type: 'object',
parameters: {
path: {
description: 'Absolute path to a file, e.g. `/repo/file.py`.',
type: 'string',
},
readRange: {
description:
'Optional parameter when reading files.\n* If none is given, the full file is shown. If provided, the file will be shown in the indicated line number range, e.g. [11, 12] will show lines 11 and 12. Indexing at 1 to start. Setting `[startLine, -1]` shows all lines from `startLine` to the end of the file.',
items: {
type: 'integer',
},
type: 'array',
},
},
required: ['path'],
},
} as const
}
}
Loading
Loading