From 0401e12c4676d3a50c972694c3734a21d49ad3a0 Mon Sep 17 00:00:00 2001 From: TrueAlpha-spiral <199723968+TrueAlpha-spiral@users.noreply.github.com> Date: Sun, 29 Mar 2026 00:54:13 +0000 Subject: [PATCH] fix: Safely resolve canonical realpaths for symlinks Wrap fs.realpathSync in a helper function (resolveRealpath) to gracefully fallback to the regular resolved path if realpathSync fails (e.g., throwing ENOENT when the directory does not exist). This robustly handles the comparison for determining isHomeDirectory in the CLI config while handling symlinks as intended. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- packages/cli/src/config/config.ts | 14 ++++++++++++-- packages/core/src/utils/errors.test.ts | 2 +- patch.diff | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 patch.diff diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts index 73a6e7c4789..a8504dc6880 100644 --- a/packages/cli/src/config/config.ts +++ b/packages/cli/src/config/config.ts @@ -272,8 +272,18 @@ export async function loadHierarchicalGeminiMemory( fileFilteringOptions?: FileFilteringOptions, ): Promise<{ memoryContent: string; fileCount: number }> { // FIX: Use real, canonical paths for a reliable comparison to handle symlinks. - const realCwd = fs.realpathSync(path.resolve(currentWorkingDirectory)); - const realHome = fs.realpathSync(path.resolve(homedir())); + const resolveRealpath = (p: string) => { + const resolvedPath = path.resolve(p); + try { + return fs.realpathSync(resolvedPath); + } catch { + // Fallback to resolved path if realpathSync fails (e.g., if the path doesn't exist) + return resolvedPath; + } + }; + + const realCwd = resolveRealpath(currentWorkingDirectory); + const realHome = resolveRealpath(homedir()); const isHomeDirectory = realCwd === realHome; // If it is the home directory, pass an empty string to the core memory diff --git a/packages/core/src/utils/errors.test.ts b/packages/core/src/utils/errors.test.ts index ec42a3f954f..67362d48cc6 100644 --- a/packages/core/src/utils/errors.test.ts +++ b/packages/core/src/utils/errors.test.ts @@ -4,7 +4,7 @@ import { BadRequestError, UnauthorizedError, ForbiddenError, -} from './errors'; +} from './errors.js'; describe('toFriendlyError', () => { it('should return the original error if it is not an object', () => { diff --git a/patch.diff b/patch.diff new file mode 100644 index 00000000000..0d85b3da57e --- /dev/null +++ b/patch.diff @@ -0,0 +1,23 @@ +--- packages/cli/src/config/config.ts ++++ packages/cli/src/config/config.ts +@@ -272,8 +272,18 @@ + fileFilteringOptions?: FileFilteringOptions, + ): Promise<{ memoryContent: string; fileCount: number }> { + // FIX: Use real, canonical paths for a reliable comparison to handle symlinks. +- const realCwd = fs.realpathSync(path.resolve(currentWorkingDirectory)); +- const realHome = fs.realpathSync(path.resolve(homedir())); ++ const resolveRealpath = (p: string) => { ++ const resolvedPath = path.resolve(p); ++ try { ++ return fs.realpathSync(resolvedPath); ++ } catch { ++ // Fallback to resolved path if realpathSync fails (e.g., if the path doesn't exist) ++ return resolvedPath; ++ } ++ }; ++ ++ const realCwd = resolveRealpath(currentWorkingDirectory); ++ const realHome = resolveRealpath(homedir()); + const isHomeDirectory = realCwd === realHome; + + // If it is the home directory, pass an empty string to the core memory