From b49f156e41058199ace6632cec61ca613c685571 Mon Sep 17 00:00:00 2001 From: Laxman Reddy Aileni Date: Tue, 17 Nov 2026 01:40:00 +0000 Subject: [PATCH 1/2] fix(amazonq): fix C# name matching and nested .gitignore handling in the security scan The C# dependency graph now matches the dot between two names as a literal dot. GitIgnoreFilter now applies the patterns of each .gitignore file relative to its own folder, as Git does. Adds tests. --- .../src/util/gitIgnoreFilter.test.ts | 84 +++++++++++++++++++ core/aws-lsp-core/src/util/gitIgnoreFilter.ts | 8 +- .../csharpDependencyGraph.test.ts | 56 +++++++++++++ .../dependencyGraph/csharpDependencyGraph.ts | 11 +-- 4 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 core/aws-lsp-core/src/util/gitIgnoreFilter.test.ts diff --git a/core/aws-lsp-core/src/util/gitIgnoreFilter.test.ts b/core/aws-lsp-core/src/util/gitIgnoreFilter.test.ts new file mode 100644 index 0000000000..b4c0109373 --- /dev/null +++ b/core/aws-lsp-core/src/util/gitIgnoreFilter.test.ts @@ -0,0 +1,84 @@ +import * as assert from 'assert' +import * as path from 'path' +import { Workspace } from '@aws/language-server-runtimes/server-interface' +import { GitIgnoreFilter } from './gitIgnoreFilter' + +describe('GitIgnoreFilter', () => { + const rootPath = path.resolve('gitignore-filter-root') + const inRoot = (...segments: string[]) => path.join(rootPath, ...segments) + + async function filter(gitIgnoreFiles: Record, files: string[]): Promise { + const workspace = { + fs: { + readFile: async (filePath: string) => { + const content = gitIgnoreFiles[filePath] + if (content === undefined) { + throw new Error(`unexpected read of ${filePath}`) + } + return content + }, + }, + } as unknown as Workspace + const gitIgnoreFilter = await GitIgnoreFilter.build(rootPath, Object.keys(gitIgnoreFiles), workspace) + return gitIgnoreFilter.filterFiles(files) + } + + it('applies the patterns of the root .gitignore file to the whole tree', async () => { + const kept = await filter({ [inRoot('.gitignore')]: 'build/\n*.log\n' }, [ + inRoot('src', 'a.cs'), + inRoot('build', 'a.cs'), + inRoot('a.log'), + inRoot('sub', 'b.log'), + ]) + + assert.deepStrictEqual(kept, [inRoot('src', 'a.cs')]) + }) + + it('applies an anchored pattern relative to the folder of its .gitignore file', async () => { + const kept = await filter({ [inRoot('sub', '.gitignore')]: '/x.cs\n' }, [ + inRoot('x.cs'), + inRoot('sub', 'x.cs'), + inRoot('sub', 'deeper', 'x.cs'), + ]) + + assert.deepStrictEqual(kept, [inRoot('x.cs'), inRoot('sub', 'deeper', 'x.cs')]) + }) + + it('applies the patterns of a nested .gitignore file only inside its folder', async () => { + const kept = await filter({ [inRoot('sub', '.gitignore')]: '*.tmp\nout/\n' }, [ + inRoot('a.tmp'), + inRoot('other', 'a.tmp'), + inRoot('sub2', 'a.tmp'), + inRoot('out', 'a.cs'), + inRoot('sub', 'a.tmp'), + inRoot('sub', 'deeper', 'b.tmp'), + inRoot('sub', 'out', 'a.cs'), + inRoot('sub', 'a.cs'), + ]) + + assert.deepStrictEqual(kept, [ + inRoot('a.tmp'), + inRoot('other', 'a.tmp'), + inRoot('sub2', 'a.tmp'), + inRoot('out', 'a.cs'), + inRoot('sub', 'a.cs'), + ]) + }) + + it('keeps a file only when every .gitignore file that covers it accepts it', async () => { + const kept = await filter( + { + [inRoot('.gitignore')]: '*.log\n', + [inRoot('sub', '.gitignore')]: '/generated/\n', + }, + [ + inRoot('generated', 'a.cs'), + inRoot('sub', 'generated', 'a.cs'), + inRoot('sub', 'a.log'), + inRoot('sub', 'a.cs'), + ] + ) + + assert.deepStrictEqual(kept, [inRoot('generated', 'a.cs'), inRoot('sub', 'a.cs')]) + }) +}) diff --git a/core/aws-lsp-core/src/util/gitIgnoreFilter.ts b/core/aws-lsp-core/src/util/gitIgnoreFilter.ts index fdd60fed18..66be27e324 100644 --- a/core/aws-lsp-core/src/util/gitIgnoreFilter.ts +++ b/core/aws-lsp-core/src/util/gitIgnoreFilter.ts @@ -16,6 +16,11 @@ export class GitIgnoreFilter { this.acceptors = acceptors } + /** + * @param rootPath Root folder of the scan. Not used: each .gitignore file applies to the folder that contains + * it. The parameter stays so that existing callers keep working. + * @param gitIgnoreFiles Paths of the .gitignore files to apply. + */ public static async build( rootPath: string, gitIgnoreFiles: string[], @@ -27,8 +32,9 @@ export class GitIgnoreFilter { const fileContent = await workspace.fs.readFile(file) const gitIgnoreAcceptor = parser.compile(fileContent) + // As in Git, the patterns of a .gitignore file apply relative to the folder that contains it. acceptors.push({ - folderPath: rootPath, + folderPath: path.dirname(file), acceptor: gitIgnoreAcceptor, }) } diff --git a/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts b/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts index 56af5341fd..4fd8961264 100644 --- a/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts +++ b/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts @@ -175,6 +175,25 @@ describe('Test CsharpDependencyGraph', () => { await csharpDependencyGraph.createNamespaceFilenameMapper(projectPathUri) assert.deepStrictEqual(csharpDependencyGraph.namespaceToFilepathDirectory, new Map([])) }) + it('should treat the dot between namespace names as a literal dot', async () => { + csharpDependencyGraph.namespaceToFilepathDirectory = new Map>() + mockedFs.readFile.callsFake(async filePath => { + if (filePath === path.join(projectPathUri, 'src', 'sample.cs')) { + return `namespace Foo.Bar {}` + } + if (filePath === path.join(projectPathUri, 'src', 'model.cs')) { + return `namespace Foo Bar {}` + } + }) + await csharpDependencyGraph.createNamespaceFilenameMapper(projectPathUri) + assert.deepStrictEqual( + csharpDependencyGraph.namespaceToFilepathDirectory, + new Map([ + ['Foo.Bar', new Set([path.join(projectPathUri, 'src', 'sample.cs')])], + ['Foo', new Set([path.join(projectPathUri, 'src', 'model.cs')])], + ]) + ) + }) }) describe('Test searchDependency', () => { beforeEach(() => { @@ -382,6 +401,43 @@ namespace Amazon.Toolkit.Demo { const response = csharpDependencyGraph.readImports(content) assert.deepStrictEqual(response, []) }) + it('should read indented, aliased and static imports', () => { + const content = [ + ' using System.Collections.Generic;', + '\tglobal using static System.Math;', + 'using Alias = Foo.Bar.Baz;', + 'using X;', + ].join('\r\n') + assert.deepStrictEqual(csharpDependencyGraph.readImports(content), [ + 'System.Collections.Generic', + 'System.Math', + 'Foo.Bar.Baz', + 'X', + ]) + }) + it('should treat the dot between names as a literal dot', () => { + const content = ['using Foo Bar;', 'using Foo,Bar;', 'using Foo-Bar;', 'using Foo.Bar;'].join('\n') + assert.deepStrictEqual(csharpDependencyGraph.readImports(content), ['Foo.Bar']) + }) + + // None of these lines has a ';', so the pattern must fail on each of them in linear time. Each line takes + // well under a millisecond, so the limit only has to catch a regression, not measure speed. + const slowInputs: { name: string; content: string }[] = [ + { name: 'a long name without a semicolon', content: `using ${'A'.repeat(40)}` }, + { name: 'a long aliased name without a semicolon', content: `using Alias = ${'A'.repeat(40)}` }, + { name: 'a long dotted name without a semicolon', content: `using ${Array(5000).fill('Ab').join('.')}` }, + { name: 'a long run of whitespace', content: `${' '.repeat(100000)}x` }, + ] + for (const { name, content } of slowInputs) { + it(`should read ${name} in linear time`, () => { + const start = process.hrtime.bigint() + const response = csharpDependencyGraph.readImports(content) + const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6 + + assert.deepStrictEqual(response, []) + assert.ok(elapsedMs < 500, `readImports took ${elapsedMs.toFixed(1)} ms`) + }) + } }) describe('Test generateTruncation', () => { before(() => { diff --git a/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.ts b/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.ts index 2ed2936de6..62e9e84b03 100644 --- a/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.ts +++ b/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.ts @@ -4,7 +4,8 @@ import * as CodeWhispererConstants from './constants' import { DependencyGraph, Truncation } from './dependencyGraph' import path = require('path') -export const importRegex = /((global\s+)?using\s+(static\s+)?)([A-Z]\w*\s*?=\s*)?([A-Z]\w*(.[A-Z]\w*)*);/gm +// A '.' between two names is matched as a literal dot, not as any character. +export const importRegex = /((global\s+)?using\s+(static\s+)?)([A-Z]\w*\s*?=\s*)?([A-Z]\w*(\.[A-Z]\w*)*);/gm export class CsharpDependencyGraph extends DependencyGraph { // This contains a dictionary of namespace name and the set of filepaths, where namespace has been defined. @@ -24,7 +25,7 @@ export class CsharpDependencyGraph extends DependencyGraph { const csharpFiles = await this.filterFiles(workspacePath, files) - const searchRegEx = new RegExp('namespace ([A-Z]\\w*(.[A-Z]\\w*)*)', 'g') + const searchRegEx = new RegExp('namespace ([A-Z]\\w*(\\.[A-Z]\\w*)*)', 'g') for (const filePath of csharpFiles) { const content = await this.workspace.fs.readFile(filePath) if (!content) { @@ -113,12 +114,8 @@ export class CsharpDependencyGraph extends DependencyGraph { readImports(content: string) { const lineBreakRegex = new RegExp('\\r?\\n', 'g') this._totalLines += content.split(lineBreakRegex).length - const importNamespaceRegex = new RegExp( - '(\\s*(global\\s+)?using\\s+(static\\s+)?)([A-Z]\\w*\\s*?=\\s*)?([A-Z]\\w*(.[A-Z]\\w*)*);', - 'g' - ) const imports: string[] = [] - for (const matchedStr of content.matchAll(importNamespaceRegex)) { + for (const matchedStr of content.matchAll(importRegex)) { imports.push(matchedStr[5]) } return imports From 8db24015bcd6e121929bfd3a8f98f998f76c3cd1 Mon Sep 17 00:00:00 2001 From: Laxman Reddy Aileni Date: Wed, 18 Nov 2026 03:26:42 +0000 Subject: [PATCH 2/2] test(amazonq): name the timing tests of the C# using line pattern after their inputs Also notes in GitIgnoreFilter that, unlike in Git, a pattern in a nested .gitignore file cannot include again a file that a .gitignore file above it excludes. --- core/aws-lsp-core/src/util/gitIgnoreFilter.ts | 3 ++- .../csharpDependencyGraph.test.ts | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/core/aws-lsp-core/src/util/gitIgnoreFilter.ts b/core/aws-lsp-core/src/util/gitIgnoreFilter.ts index 66be27e324..dc63ce3aaa 100644 --- a/core/aws-lsp-core/src/util/gitIgnoreFilter.ts +++ b/core/aws-lsp-core/src/util/gitIgnoreFilter.ts @@ -32,7 +32,8 @@ export class GitIgnoreFilter { const fileContent = await workspace.fs.readFile(file) const gitIgnoreAcceptor = parser.compile(fileContent) - // As in Git, the patterns of a .gitignore file apply relative to the folder that contains it. + // As in Git, the patterns of a .gitignore file apply relative to the folder that contains it. Unlike Git, a + // pattern in a nested file cannot include again a file that a .gitignore file above it excludes. acceptors.push({ folderPath: path.dirname(file), acceptor: gitIgnoreAcceptor, diff --git a/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts b/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts index 4fd8961264..aaf32cee3b 100644 --- a/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts +++ b/server/aws-lsp-codewhisperer/src/language-server/securityScan/dependencyGraph/csharpDependencyGraph.test.ts @@ -420,16 +420,16 @@ namespace Amazon.Toolkit.Demo { assert.deepStrictEqual(csharpDependencyGraph.readImports(content), ['Foo.Bar']) }) - // None of these lines has a ';', so the pattern must fail on each of them in linear time. Each line takes - // well under a millisecond, so the limit only has to catch a regression, not measure speed. - const slowInputs: { name: string; content: string }[] = [ - { name: 'a long name without a semicolon', content: `using ${'A'.repeat(40)}` }, - { name: 'a long aliased name without a semicolon', content: `using Alias = ${'A'.repeat(40)}` }, - { name: 'a long dotted name without a semicolon', content: `using ${Array(5000).fill('Ab').join('.')}` }, + // Each case must finish within the limit. Each line takes well under a millisecond, so the limit only has to + // catch a regression, not measure speed. + const largeInputs: { name: string; content: string }[] = [ + { name: 'a long name', content: `using ${'A'.repeat(40)}` }, + { name: 'a long aliased name', content: `using Alias = ${'A'.repeat(40)}` }, + { name: 'a long dotted name', content: `using ${Array(5000).fill('Ab').join('.')}` }, { name: 'a long run of whitespace', content: `${' '.repeat(100000)}x` }, ] - for (const { name, content } of slowInputs) { - it(`should read ${name} in linear time`, () => { + for (const { name, content } of largeInputs) { + it(`should read ${name} within the time limit`, () => { const start = process.hrtime.bigint() const response = csharpDependencyGraph.readImports(content) const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6