Skip to content

Conversation

ksprashu
Copy link

@ksprashu ksprashu commented Oct 5, 2025

This commit improves the import processor regex to avoid matching code inside markdown code blocks.

The new logic is based on the following rules:

  • A path is valid if it starts with ./, ../, or /.
  • A path is valid if it has a file extension.

This change also includes new tests to cover the following cases:

  • npm-style packages in code blocks
  • Python decorators
  • Email addresses
  • Decorators
  • Subdirectory imports with and without extensions

Fixes #2967

TLDR

The bug was caused by an overly permissive regex in the import processor, which incorrectly identified patterns in markdown code blocks as import statements. The fix refines the validation logic to correctly identify valid import paths while avoiding false positives.

Dive Deeper

The new logic ensures that a path is considered a valid import only if it starts with an explicit path prefix (./, ../, /) or contains a file extension. This approach is more robust and prevents the incorrect parsing of decorators, npm packages, and email addresses as import paths.

Reviewer Test Plan

Have a Markdown that contains '@' symbols.
Gemini tries to process the following strings and tries to import it.

Eg:

*   **Testing:**
    *   **Frontend:** `vitest` is used for testing. Run with `pnpm -F @google-cloud-pulse/frontend test`.
    *   **Backend:** `jest` is used for testing. Run with `pnpm -F @google-cloud-pulse/backend test`.

The error when running Gemini CLI is as below.

❯ gemini "How many colors are there in a rainbow?"
[ERROR] [ImportProcessor] Failed to import google-cloud-pulse/frontend: ENOENT: no such file or directory, access '/Users/ksprashanth/code/github/google-cloud-pulse/google-cloud-pulse/frontend'
[ERROR] [ImportProcessor] Failed to import google-cloud-pulse/backend: ENOENT: no such file or directory, access '/Users/ksprashanth/code/github/google-cloud-pulse/google-cloud-pulse/backend'
[ERROR] [ImportProcessor] Failed to import google-cloud-pulse/frontend: ENOENT: no such file or directory, access '/Users/ksprashanth/code/github/google-cloud-pulse/google-cloud-pulse/frontend'
There are seven colors in a rainbow: red, orange, yellow, green, blue, indigo, and violet.

After the fix, we don't see any errors.

❯ gemini "How many colors are there in a rainbow?"
There are seven colors in a rainbow: red, orange, yellow, green, blue, indigo, and violet.

Testing Matrix

🍏 🪟 🐧
npm run X
npx X
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

This commit improves the import processor regex to avoid matching code inside markdown code blocks.

The new logic is based on the following rules:
- A path is valid if it starts with `./`, `../`, or `/`.
- A path is valid if it has a file extension.

This change also includes new tests to cover the following cases:
- npm-style packages in code blocks
- Python decorators
- Email addresses
- Decorators
- Subdirectory imports with and without extensions

Fixes google-gemini#2967
@ksprashu ksprashu requested a review from a team as a code owner October 5, 2025 05:14
Copy link
Contributor

Summary of Changes

Hello @ksprashu, 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 addresses a critical bug in the import processor where its overly broad regular expression incorrectly parsed various patterns within markdown code blocks as valid import statements, leading to ENOENT errors. The fix refines the import validation logic to be more precise, ensuring that only explicitly defined relative/absolute paths or paths with file extensions are recognized as imports, thereby preventing false positives and improving the robustness of the system.

Highlights

  • Improved Import Processor Regex: The regular expression used by the import processor has been refined to prevent it from incorrectly matching code within markdown code blocks.
  • Stricter Path Validation: New rules for valid import paths have been introduced: a path is now considered valid only if it starts with './', '../', or '/', or if it includes a file extension.
  • Enhanced Test Coverage: Comprehensive new test cases have been added to cover scenarios such as npm-style packages, Python decorators, email addresses, and subdirectory imports, ensuring the fix correctly handles these edge cases.
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
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 significantly improves the import processor's logic for identifying import statements in Markdown files. The new validation is more restrictive, correctly avoiding false positives from constructs like npm package names, decorators, and email addresses. The changes in packages/core/src/utils/memoryImportProcessor.ts are clear and effectively implement the new validation rules. I'm particularly impressed with the comprehensive suite of new tests in packages/core/src/utils/memoryImportProcessor.test.ts, which cover a wide range of edge cases and ensure the new logic is robust. The updated circular import test is also a great improvement. Overall, this is a high-quality contribution that addresses the reported issue effectively. I have not found any critical or high-severity issues.

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.

Import processor regex matches code inside markdown code blocks

1 participant