fix: enhance path handling in handleAtCommand to support relative paths (#9065)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
lifefloating
2025-10-29 10:13:04 +08:00
committed by GitHub
parent cca41edc6e
commit 706834ecd3
2 changed files with 235 additions and 70 deletions

View File

@@ -145,6 +145,7 @@ export async function handleAtCommand({
const pathSpecsToRead: string[] = [];
const atPathToResolvedSpecMap = new Map<string, string>();
const contentLabelsForDisplay: string[] = [];
const absoluteToRelativePathMap = new Map<string, string>();
const ignoredByReason: Record<string, string[]> = {
git: [],
gemini: [],
@@ -229,17 +230,30 @@ export async function handleAtCommand({
for (const dir of config.getWorkspaceContext().getDirectories()) {
let currentPathSpec = pathName;
let resolvedSuccessfully = false;
let relativePath = pathName;
try {
const absolutePath = path.resolve(dir, pathName);
const absolutePath = path.isAbsolute(pathName)
? pathName
: path.resolve(dir, pathName);
const stats = await fs.stat(absolutePath);
// Convert absolute path to relative path
relativePath = path.isAbsolute(pathName)
? path.relative(dir, absolutePath)
: pathName;
if (stats.isDirectory()) {
currentPathSpec =
pathName + (pathName.endsWith(path.sep) ? `**` : `/**`);
relativePath + (relativePath.endsWith(path.sep) ? `**` : `/**`);
onDebugMessage(
`Path ${pathName} resolved to directory, using glob: ${currentPathSpec}`,
);
} else {
onDebugMessage(`Path ${pathName} resolved to file: ${absolutePath}`);
currentPathSpec = relativePath;
absoluteToRelativePathMap.set(absolutePath, relativePath);
onDebugMessage(
`Path ${pathName} resolved to file: ${absolutePath}, using relative path: ${relativePath}`,
);
}
resolvedSuccessfully = true;
} catch (error) {
@@ -266,6 +280,10 @@ export async function handleAtCommand({
if (lines.length > 1 && lines[1]) {
const firstMatchAbsolute = lines[1].trim();
currentPathSpec = path.relative(dir, firstMatchAbsolute);
absoluteToRelativePathMap.set(
firstMatchAbsolute,
currentPathSpec,
);
onDebugMessage(
`Glob search for ${pathName} found ${firstMatchAbsolute}, using relative path: ${currentPathSpec}`,
);
@@ -305,7 +323,8 @@ export async function handleAtCommand({
if (resolvedSuccessfully) {
pathSpecsToRead.push(currentPathSpec);
atPathToResolvedSpecMap.set(originalAtPath, currentPathSpec);
contentLabelsForDisplay.push(pathName);
const displayPath = path.isAbsolute(pathName) ? relativePath : pathName;
contentLabelsForDisplay.push(displayPath);
break;
}
}
@@ -430,10 +449,27 @@ export async function handleAtCommand({
if (typeof part === 'string') {
const match = fileContentRegex.exec(part);
if (match) {
const filePathSpecInContent = match[1]; // This is a resolved pathSpec
const filePathSpecInContent = match[1];
const fileActualContent = match[2].trim();
let displayPath = absoluteToRelativePathMap.get(
filePathSpecInContent,
);
// Fallback: if no mapping found, try to convert absolute path to relative
if (!displayPath) {
for (const dir of config.getWorkspaceContext().getDirectories()) {
if (filePathSpecInContent.startsWith(dir)) {
displayPath = path.relative(dir, filePathSpecInContent);
break;
}
}
}
displayPath = displayPath || filePathSpecInContent;
processedQueryParts.push({
text: `\nContent from @${filePathSpecInContent}:\n`,
text: `\nContent from @${displayPath}:\n`,
});
processedQueryParts.push({ text: fileActualContent });
} else {