diff options
| author | TIRUMALASETTI PRANITH <[email protected]> | 2025-08-02 03:52:17 +0530 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-01 22:22:17 +0000 |
| commit | 15a1f1af9d0e4628e9e82f81d384d614899770e3 (patch) | |
| tree | 19c1c76b177faa5c774f06de2f150a13a03fc57b /packages/core | |
| parent | 387706607dfa372f4f0c6fee14286bf4a290b258 (diff) | |
fix(config): Resolve duplicate config loading from home directory (#5090)
Co-authored-by: Allen Hutchison <[email protected]>
Co-authored-by: Allen Hutchison <[email protected]>
Diffstat (limited to 'packages/core')
| -rw-r--r-- | packages/core/src/utils/memoryDiscovery.ts | 133 |
1 files changed, 48 insertions, 85 deletions
diff --git a/packages/core/src/utils/memoryDiscovery.ts b/packages/core/src/utils/memoryDiscovery.ts index a673a75e..323b13c5 100644 --- a/packages/core/src/utils/memoryDiscovery.ts +++ b/packages/core/src/utils/memoryDiscovery.ts @@ -94,7 +94,6 @@ async function getGeminiMdFilePathsInternal( const geminiMdFilenames = getAllGeminiMdFilenames(); for (const geminiMdFilename of geminiMdFilenames) { - const resolvedCwd = path.resolve(currentWorkingDirectory); const resolvedHome = path.resolve(userHomePath); const globalMemoryPath = path.join( resolvedHome, @@ -102,12 +101,7 @@ async function getGeminiMdFilePathsInternal( geminiMdFilename, ); - if (debugMode) - logger.debug( - `Searching for ${geminiMdFilename} starting from CWD: ${resolvedCwd}`, - ); - if (debugMode) logger.debug(`User home directory: ${resolvedHome}`); - + // This part that finds the global file always runs. try { await fs.access(globalMemoryPath, fsSync.constants.R_OK); allPaths.add(globalMemoryPath); @@ -116,102 +110,71 @@ async function getGeminiMdFilePathsInternal( `Found readable global ${geminiMdFilename}: ${globalMemoryPath}`, ); } catch { + // It's okay if it's not found. + } + + // FIX: Only perform the workspace search (upward and downward scans) + // if a valid currentWorkingDirectory is provided. + if (currentWorkingDirectory) { + const resolvedCwd = path.resolve(currentWorkingDirectory); if (debugMode) logger.debug( - `Global ${geminiMdFilename} not found or not readable: ${globalMemoryPath}`, + `Searching for ${geminiMdFilename} starting from CWD: ${resolvedCwd}`, ); - } - const projectRoot = await findProjectRoot(resolvedCwd); - if (debugMode) - logger.debug(`Determined project root: ${projectRoot ?? 'None'}`); - - const upwardPaths: string[] = []; - let currentDir = resolvedCwd; - // Determine the directory that signifies the top of the project or user-specific space. - const ultimateStopDir = projectRoot - ? path.dirname(projectRoot) - : path.dirname(resolvedHome); + const projectRoot = await findProjectRoot(resolvedCwd); + if (debugMode) + logger.debug(`Determined project root: ${projectRoot ?? 'None'}`); - while (currentDir && currentDir !== path.dirname(currentDir)) { - // Loop until filesystem root or currentDir is empty - if (debugMode) { - logger.debug( - `Checking for ${geminiMdFilename} in (upward scan): ${currentDir}`, - ); - } + const upwardPaths: string[] = []; + let currentDir = resolvedCwd; + const ultimateStopDir = projectRoot + ? path.dirname(projectRoot) + : path.dirname(resolvedHome); - // Skip the global .gemini directory itself during upward scan from CWD, - // as global is handled separately and explicitly first. - if (currentDir === path.join(resolvedHome, GEMINI_CONFIG_DIR)) { - if (debugMode) { - logger.debug( - `Upward scan reached global config dir path, stopping upward search here: ${currentDir}`, - ); + while (currentDir && currentDir !== path.dirname(currentDir)) { + if (currentDir === path.join(resolvedHome, GEMINI_CONFIG_DIR)) { + break; } - break; - } - const potentialPath = path.join(currentDir, geminiMdFilename); - try { - await fs.access(potentialPath, fsSync.constants.R_OK); - // Add to upwardPaths only if it's not the already added globalMemoryPath - if (potentialPath !== globalMemoryPath) { - upwardPaths.unshift(potentialPath); - if (debugMode) { - logger.debug( - `Found readable upward ${geminiMdFilename}: ${potentialPath}`, - ); + const potentialPath = path.join(currentDir, geminiMdFilename); + try { + await fs.access(potentialPath, fsSync.constants.R_OK); + if (potentialPath !== globalMemoryPath) { + upwardPaths.unshift(potentialPath); } + } catch { + // Not found, continue. } - } catch { - if (debugMode) { - logger.debug( - `Upward ${geminiMdFilename} not found or not readable in: ${currentDir}`, - ); + + if (currentDir === ultimateStopDir) { + break; } - } - // Stop condition: if currentDir is the ultimateStopDir, break after this iteration. - if (currentDir === ultimateStopDir) { - if (debugMode) - logger.debug( - `Reached ultimate stop directory for upward scan: ${currentDir}`, - ); - break; + currentDir = path.dirname(currentDir); } + upwardPaths.forEach((p) => allPaths.add(p)); - currentDir = path.dirname(currentDir); - } - upwardPaths.forEach((p) => allPaths.add(p)); - - // Merge options with memory defaults, with options taking precedence - const mergedOptions = { - ...DEFAULT_MEMORY_FILE_FILTERING_OPTIONS, - ...fileFilteringOptions, - }; + const mergedOptions = { + ...DEFAULT_MEMORY_FILE_FILTERING_OPTIONS, + ...fileFilteringOptions, + }; - const downwardPaths = await bfsFileSearch(resolvedCwd, { - fileName: geminiMdFilename, - maxDirs, - debug: debugMode, - fileService, - fileFilteringOptions: mergedOptions, // Pass merged options as fileFilter - }); - downwardPaths.sort(); // Sort for consistent ordering, though hierarchy might be more complex - if (debugMode && downwardPaths.length > 0) - logger.debug( - `Found downward ${geminiMdFilename} files (sorted): ${JSON.stringify( - downwardPaths, - )}`, - ); - // Add downward paths only if they haven't been included already (e.g. from upward scan) - for (const dPath of downwardPaths) { - allPaths.add(dPath); + const downwardPaths = await bfsFileSearch(resolvedCwd, { + fileName: geminiMdFilename, + maxDirs, + debug: debugMode, + fileService, + fileFilteringOptions: mergedOptions, + }); + downwardPaths.sort(); + for (const dPath of downwardPaths) { + allPaths.add(dPath); + } } } - // Add extension context file paths + // Add extension context file paths. for (const extensionPath of extensionContextFilePaths) { allPaths.add(extensionPath); } |
