Skip to content

fix(core): remove dead code, fix cross-platform symlink detection, and add error logging#20162

Closed
lavjeetrai wants to merge 2 commits into
google-gemini:mainfrom
lavjeetrai:fix/code-quality-bugs
Closed

fix(core): remove dead code, fix cross-platform symlink detection, and add error logging#20162
lavjeetrai wants to merge 2 commits into
google-gemini:mainfrom
lavjeetrai:fix/code-quality-bugs

Conversation

@lavjeetrai
Copy link
Copy Markdown

@lavjeetrai lavjeetrai commented Feb 24, 2026

…d add error logging

Summary

Fix three bugs found during a code quality audit: dead code caused by variable shadowing in editCorrector.ts, a Windows-specific symlink misclassification in workspaceContext.ts, and silently swallowed errors in shellExecutionService.ts.

Details

1. Dead code in editCorrector.ts

Inside ensureCorrectEdit, the else if (occurrences > expectedReplacements) branch re-declared const expectedReplacements (shadowing the outer variable on line 188), then checked if (occurrences === expectedReplacements) — which is logically impossible inside a > branch. Removed the 22 lines of unreachable dead code.

2. Windows symlink path separator in workspaceContext.ts

isFileSymlink only checked for / as a trailing separator, but on Windows readlinkSync returns paths with \. Now checks for both separators.

3. Swallowed errors in shellExecutionService.ts

isPtyActive had two empty catch blocks. While ESRCH is expected when checking process existence, other errors like EPERM were silently ignored. Added debugLogger.warn() for unexpected error codes.

Related Issues

Fixes #20159

How to Validate

  1. Run unit tests for the modified files:
    npm test -w @google/gemini-cli-core -- src/utils/editCorrector.test.ts
    npm test -w @google/gemini-cli-core -- src/utils/workspaceContext.test.ts
    npm test -w @google/gemini-cli-core -- src/services/shellExecutionService.test.ts
    
  2. Run full typecheck and lint:
   npm run typecheck
   npm run lint

## Pre-Merge Checklist

<!-- Check all that apply before requesting review or merging. -->

- [ ] Updated relevant documentation and README (if needed)
- [ ] Added/updated tests (if needed)
- [x] Noted breaking changes (if any)
- [ ] Validated on required platforms/methods:
  - [ ] MacOS
    - [ ] npm run
    - [ ] npx
    - [ ] Docker
    - [ ] Podman
    - [ ] Seatbelt
  - [x] Windows
    - [x] npm run
    - [ ] npx
    - [ ] Docker
  - [ ] Linux
    - [ ] npm run
    - [ ] npx
    - [ ] Docker

.

@lavjeetrai lavjeetrai requested a review from a team as a code owner February 24, 2026 10:31
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Feb 24, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @lavjeetrai, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the robustness and reliability of the core codebase by addressing several identified bugs. It cleans up unreachable code, enhances cross-platform compatibility for file system operations, and prevents silent failures by introducing explicit logging for unexpected errors during process management. These changes contribute to a more stable and maintainable application.

Highlights

  • Dead Code Removal: Removed 22 lines of unreachable dead code in editCorrector.ts caused by variable shadowing and logically impossible conditions within the ensureCorrectEdit function.
  • Cross-Platform Symlink Detection Fix: Corrected symlink detection in workspaceContext.ts to properly handle Windows paths by checking for both forward (/) and backward (\) slashes as path separators.
  • Improved Error Logging: Enhanced error handling in shellExecutionService.ts by adding debugLogger.warn() for unexpected error codes (other than ESRCH) that were previously silently swallowed in isPtyActive.
Changelog
  • packages/core/src/services/shellExecutionService.ts
    • Added imports for debugLogger and isNodeError to facilitate structured error logging.
    • Updated the isPtyActive method to log unexpected errors (other than ESRCH) when checking child process existence, preventing silent failures.
  • packages/core/src/utils/editCorrector.ts
    • Removed a block of dead code within the ensureCorrectEdit function that was unreachable due to variable shadowing and incorrect logical conditions.
  • packages/core/src/utils/workspaceContext.ts
    • Modified the isFileSymlink method to correctly identify symlinks on Windows by checking for both forward slash (/) and backslash (\) as path separators.
Activity
  • The author, lavjeetrai, created this pull request to fix three bugs identified during a code quality audit.
  • The pull request includes a detailed summary and specific details for each fix.
  • Validation instructions are provided, including running unit tests for modified files and full typecheck/lint.
  • The pre-merge checklist indicates validation on Windows (npm run) has been completed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request addresses three bugs: dead code removal, cross-platform symlink detection, and improved error logging. The changes enhance code robustness and maintainability. The shellExecutionService.ts file now includes more explicit error handling for process.kill calls, preventing silent swallowing of unexpected errors. The editCorrector.ts file has unreachable code removed, improving clarity. Lastly, workspaceContext.ts now correctly handles Windows symlink paths. All changes are well-justified and improve the overall quality of the codebase.

Comment on lines +942 to +947
} catch (error: unknown) {
if (isNodeError(error) && error.code !== 'ESRCH') {
debugLogger.warn(
`Unexpected error checking child process ${pid}: ${error.code}`,
);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This change correctly addresses the issue of silently swallowed errors. By explicitly checking for ESRCH and logging other unexpected errors, the code becomes more robust and easier to debug. This is a critical improvement for production stability.

Comment on lines +956 to +961
} catch (error: unknown) {
if (isNodeError(error) && error.code !== 'ESRCH') {
debugLogger.warn(
`Unexpected error checking PTY process ${pid}: ${error.code}`,
);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Similar to the previous comment, this change prevents silent error swallowing when checking PTY processes. Logging unexpected errors is crucial for identifying and resolving issues that might otherwise go unnoticed.

Comment on lines +31 to +32
import { debugLogger } from '../utils/debugLogger.js';
import { isNodeError } from '../utils/errors.js';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The addition of debugLogger and isNodeError imports is good for improving error visibility and handling. This aligns with the pull request's goal of adding error logging.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request delivers several valuable bug fixes. It correctly removes dead code in editCorrector.ts and improves cross-platform symlink detection in workspaceContext.ts. The addition of error logging in shellExecutionService.ts is a good step towards more robust process management. However, I've identified a logical issue in the updated isPtyActive function where it incorrectly handles EPERM errors, potentially misreporting an active process as inactive. My review includes suggestions to address this.

Comment on lines +942 to 949
} catch (error: unknown) {
if (isNodeError(error) && error.code !== 'ESRCH') {
debugLogger.warn(
`Unexpected error checking child process ${pid}: ${error.code}`,
);
}
return false;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While adding logging for unexpected errors is a great improvement, there's a subtle logic issue here. An EPERM error from process.kill(pid, 0) indicates that the process exists, but the current user lacks permission to send it a signal. In this case, isPtyActive should return true, not false. The current implementation incorrectly reports the process as inactive on EPERM errors.

Suggested change
} catch (error: unknown) {
if (isNodeError(error) && error.code !== 'ESRCH') {
debugLogger.warn(
`Unexpected error checking child process ${pid}: ${error.code}`,
);
}
return false;
}
} catch (error: unknown) {
if (isNodeError(error)) {
if (error.code === 'EPERM') {
// We don't have permission to signal the process, but it does exist.
return true;
}
if (error.code !== 'ESRCH') {
debugLogger.warn(
'Unexpected error checking child process ' + pid + ': ' + error.code
);
}
}
// For ESRCH or other errors, assume it's not active.
return false;
}

Comment on lines +956 to 963
} catch (error: unknown) {
if (isNodeError(error) && error.code !== 'ESRCH') {
debugLogger.warn(
`Unexpected error checking PTY process ${pid}: ${error.code}`,
);
}
return false;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the previous comment, this catch block should handle the EPERM error code. If process.kill(pid, 0) throws EPERM, it means the process exists, and therefore isPtyActive should return true.

    } catch (error: unknown) {
      if (isNodeError(error)) {
        if (error.code === 'EPERM') {
          // We don't have permission to signal the process, but it does exist.
          return true;
        }
        if (error.code !== 'ESRCH') {
          debugLogger.warn(
            'Unexpected error checking PTY process ' + pid + ': ' + error.code
          );
        }
      }
      // For ESRCH or other errors, assume it's not active.
      return false;
    }

@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli Bot commented Mar 15, 2026

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383).

We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'.

This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community!

@gemini-cli gemini-cli Bot closed this Mar 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

During a code quality audit, I found three bugs in packages/core/src/:

1 participant