feat: Refactor and Enhance Ripgrep Tool (#930)

This commit is contained in:
tanzhenxin
2025-10-31 10:53:13 +08:00
committed by GitHub
parent 7843de882a
commit 817218f1cf
16 changed files with 648 additions and 479 deletions

View File

@@ -22,12 +22,22 @@ vi.mock('os', async (importOriginal) => {
describe('getUserStartupWarnings', () => {
let testRootDir: string;
let homeDir: string;
let startupOptions: {
workspaceRoot: string;
useRipgrep: boolean;
useBuiltinRipgrep: boolean;
};
beforeEach(async () => {
testRootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'warnings-test-'));
homeDir = path.join(testRootDir, 'home');
await fs.mkdir(homeDir, { recursive: true });
vi.mocked(os.homedir).mockReturnValue(homeDir);
startupOptions = {
workspaceRoot: testRootDir,
useRipgrep: true,
useBuiltinRipgrep: true,
};
});
afterEach(async () => {
@@ -37,7 +47,10 @@ describe('getUserStartupWarnings', () => {
describe('home directory check', () => {
it('should return a warning when running in home directory', async () => {
const warnings = await getUserStartupWarnings(homeDir);
const warnings = await getUserStartupWarnings({
...startupOptions,
workspaceRoot: homeDir,
});
expect(warnings).toContainEqual(
expect.stringContaining('home directory'),
);
@@ -46,7 +59,10 @@ describe('getUserStartupWarnings', () => {
it('should not return a warning when running in a project directory', async () => {
const projectDir = path.join(testRootDir, 'project');
await fs.mkdir(projectDir);
const warnings = await getUserStartupWarnings(projectDir);
const warnings = await getUserStartupWarnings({
...startupOptions,
workspaceRoot: projectDir,
});
expect(warnings).not.toContainEqual(
expect.stringContaining('home directory'),
);
@@ -56,7 +72,10 @@ describe('getUserStartupWarnings', () => {
describe('root directory check', () => {
it('should return a warning when running in a root directory', async () => {
const rootDir = path.parse(testRootDir).root;
const warnings = await getUserStartupWarnings(rootDir);
const warnings = await getUserStartupWarnings({
...startupOptions,
workspaceRoot: rootDir,
});
expect(warnings).toContainEqual(
expect.stringContaining('root directory'),
);
@@ -68,7 +87,10 @@ describe('getUserStartupWarnings', () => {
it('should not return a warning when running in a non-root directory', async () => {
const projectDir = path.join(testRootDir, 'project');
await fs.mkdir(projectDir);
const warnings = await getUserStartupWarnings(projectDir);
const warnings = await getUserStartupWarnings({
...startupOptions,
workspaceRoot: projectDir,
});
expect(warnings).not.toContainEqual(
expect.stringContaining('root directory'),
);
@@ -78,7 +100,10 @@ describe('getUserStartupWarnings', () => {
describe('error handling', () => {
it('should handle errors when checking directory', async () => {
const nonExistentPath = path.join(testRootDir, 'non-existent');
const warnings = await getUserStartupWarnings(nonExistentPath);
const warnings = await getUserStartupWarnings({
...startupOptions,
workspaceRoot: nonExistentPath,
});
const expectedWarning =
'Could not verify the current directory due to a file system error.';
expect(warnings).toEqual([expectedWarning, expectedWarning]);

View File

@@ -7,19 +7,26 @@
import fs from 'node:fs/promises';
import * as os from 'node:os';
import path from 'node:path';
import { canUseRipgrep } from '@qwen-code/qwen-code-core';
type WarningCheckOptions = {
workspaceRoot: string;
useRipgrep: boolean;
useBuiltinRipgrep: boolean;
};
type WarningCheck = {
id: string;
check: (workspaceRoot: string) => Promise<string | null>;
check: (options: WarningCheckOptions) => Promise<string | null>;
};
// Individual warning checks
const homeDirectoryCheck: WarningCheck = {
id: 'home-directory',
check: async (workspaceRoot: string) => {
check: async (options: WarningCheckOptions) => {
try {
const [workspaceRealPath, homeRealPath] = await Promise.all([
fs.realpath(workspaceRoot),
fs.realpath(options.workspaceRoot),
fs.realpath(os.homedir()),
]);
@@ -35,9 +42,9 @@ const homeDirectoryCheck: WarningCheck = {
const rootDirectoryCheck: WarningCheck = {
id: 'root-directory',
check: async (workspaceRoot: string) => {
check: async (options: WarningCheckOptions) => {
try {
const workspaceRealPath = await fs.realpath(workspaceRoot);
const workspaceRealPath = await fs.realpath(options.workspaceRoot);
const errorMessage =
'Warning: You are running Qwen Code in the root directory. Your entire folder structure will be used for context. It is strongly recommended to run in a project-specific directory.';
@@ -53,17 +60,33 @@ const rootDirectoryCheck: WarningCheck = {
},
};
const ripgrepAvailabilityCheck: WarningCheck = {
id: 'ripgrep-availability',
check: async (options: WarningCheckOptions) => {
if (!options.useRipgrep) {
return null;
}
const isAvailable = await canUseRipgrep(options.useBuiltinRipgrep);
if (!isAvailable) {
return 'Ripgrep not available: Please install ripgrep globally to enable faster file content search. Falling back to built-in grep.';
}
return null;
},
};
// All warning checks
const WARNING_CHECKS: readonly WarningCheck[] = [
homeDirectoryCheck,
rootDirectoryCheck,
ripgrepAvailabilityCheck,
];
export async function getUserStartupWarnings(
workspaceRoot: string = process.cwd(),
options: WarningCheckOptions,
): Promise<string[]> {
const results = await Promise.all(
WARNING_CHECKS.map((check) => check.check(workspaceRoot)),
WARNING_CHECKS.map((check) => check.check(options)),
);
return results.filter((msg) => msg !== null);
}