-
Notifications
You must be signed in to change notification settings - Fork 433
fix import parser bug #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @tastelikefeet, 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 and resolves a significant bug in the import parser that caused incorrect path resolution, particularly in complex file system environments involving relative output directories or macOS symlinks. The core fix involves introducing a more robust path resolution utility, which is thoroughly validated by new, comprehensive test cases. Additionally, the PR includes updates to code documentation guidelines, promoting better clarity and information capture within code comments. Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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 addresses a bug in the import parser concerning path resolution, particularly when dealing with relative output directories and symlinks. The introduction of the safe_relpath function for Python path resolution is a solid fix, and it's well-supported by new regression tests for both Python and JavaScript/TypeScript.
My main feedback is that the fix is incomplete. The robust path resolution logic using os.path.realpath has been applied to the Python parser, but a similar function in the JavaScript/TypeScript parser was not updated. This leads to code duplication and leaves the symlink-related bug unresolved for JS/TS files. I've left a specific comment with a suggestion to refactor this into a shared method to ensure consistency and fully resolve the issue.
| # Helper function to safely convert to relative path | ||
| def safe_relpath(path): | ||
| """Convert path to relative from output_dir, handling mixed abs/rel paths. | ||
| IMPORTANT: In Python imports, paths should always be absolute (from current_dir). | ||
| Always convert output_dir to absolute to avoid relpath bugs. | ||
| Use realpath to resolve symlinks (e.g., /var vs /private/var on macOS). | ||
| """ | ||
| # Use realpath to resolve symlinks and get canonical paths | ||
| abs_output_dir = os.path.realpath(os.path.abspath(self.output_dir)) | ||
|
|
||
| # Path should be absolute (constructed from absolute current_dir) | ||
| # If somehow it's relative, make it absolute from output_dir | ||
| if os.path.isabs(path): | ||
| abs_path = os.path.realpath(path) | ||
| else: | ||
| # This shouldn't happen in Python imports, but handle it anyway | ||
| abs_path = os.path.realpath(os.path.join(abs_output_dir, path)) | ||
|
|
||
| return os.path.relpath(abs_path, abs_output_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new helper function safe_relpath is a great addition and correctly solves the path resolution issue, including symlink problems on macOS by using os.path.realpath.
However, I noticed two issues:
- Incomplete Fix: A similar, but less robust, inner function
to_relativeexists inside_resolve_js_path(lines 562-579) which was not updated to useos.path.realpath. This means the symlink resolution bug is fixed for Python imports but still exists for JavaScript/TypeScript imports. The new JS test (test_relative_output_dir_js_no_excessive_parent_dirs) might be passing coincidentally in an environment without symlinked temp directories. - Code Duplication: Defining
safe_relpathas an inner function prevents its reuse.
To improve maintainability and ensure the bug is fixed consistently across all parsers, I recommend refactoring this. You could move safe_relpath to BaseImportParser as a private helper method (e.g., _safe_relpath) and then call it from both PythonImportParser and JavaScriptImportParser, removing the duplicated logic from _resolve_js_path.
(cherry picked from commit fa0f33866054a7018885cf8f0d7026f5417788ed)
Change Summary
Related issue number
Checklist
pre-commit installandpre-commit run --all-filesbefore git commit, and passed lint check.