mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-29 06:25:16 -07:00
fix(core): auto-correct file paths in smart edit where possible (#9526)
This commit is contained in:
@@ -755,6 +755,58 @@ A good instruction should concisely answer:
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Quickly checks if the file path can be resolved directly against the workspace root.
|
||||
* @param filePath The relative file path to check.
|
||||
* @returns The absolute path if the file exists, otherwise null.
|
||||
*/
|
||||
private findDirectPath(filePath: string): string | null {
|
||||
const directPath = path.join(this.config.getTargetDir(), filePath);
|
||||
return fs.existsSync(directPath) ? directPath : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for a file across all configured workspace directories.
|
||||
* @param filePath The file path (can be partial) to search for.
|
||||
* @returns A list of absolute paths for all matching files found.
|
||||
*/
|
||||
private findAmbiguousPaths(filePath: string): string[] {
|
||||
const workspaceContext = this.config.getWorkspaceContext();
|
||||
const fileSystem = this.config.getFileSystemService();
|
||||
const searchPaths = workspaceContext.getDirectories();
|
||||
return fileSystem.findFiles(filePath, searchPaths);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to correct a relative file path to an absolute path.
|
||||
* This function modifies `params.file_path` in place if successful.
|
||||
* @param params The tool parameters containing the file_path to correct.
|
||||
* @returns An error message string if correction fails, otherwise null.
|
||||
*/
|
||||
private correctPath(params: EditToolParams): string | null {
|
||||
const directPath = this.findDirectPath(params.file_path);
|
||||
if (directPath) {
|
||||
params.file_path = directPath;
|
||||
return null;
|
||||
}
|
||||
|
||||
const foundFiles = this.findAmbiguousPaths(params.file_path);
|
||||
|
||||
if (foundFiles.length === 0) {
|
||||
return `File not found for '${params.file_path}' and path is not absolute.`;
|
||||
}
|
||||
|
||||
if (foundFiles.length > 1) {
|
||||
return (
|
||||
`The file path '${params.file_path}' is too ambiguous and matches multiple files. ` +
|
||||
`Please provide a more specific path. Matches: ${foundFiles.join(', ')}`
|
||||
);
|
||||
}
|
||||
|
||||
params.file_path = foundFiles[0];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the parameters for the Edit tool
|
||||
* @param params Parameters to validate
|
||||
@@ -768,7 +820,9 @@ A good instruction should concisely answer:
|
||||
}
|
||||
|
||||
if (!path.isAbsolute(params.file_path)) {
|
||||
return `File path must be absolute: ${params.file_path}`;
|
||||
// Attempt to auto-correct to an absolute path
|
||||
const error = this.correctPath(params);
|
||||
if (error) return error;
|
||||
}
|
||||
|
||||
const workspaceContext = this.config.getWorkspaceContext();
|
||||
@@ -776,7 +830,6 @@ A good instruction should concisely answer:
|
||||
const directories = workspaceContext.getDirectories();
|
||||
return `File path must be within one of the workspace directories: ${directories.join(', ')}`;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user