8000 fix: include options hash in cache key for options normalization by anomiex · Pull Request #466 · import-js/eslint-import-resolver-typescript · GitHub
[go: up one dir, main page]

Skip to content

fix: include options hash in cache key for options normalization #466

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

Merged

Conversation

anomiex
Copy link
Contributor
@anomiex anomiex commented Jun 25, 2025

Presumably to avoid excessive stat calls, the return value of normalizeOptions() is cached for each input tsconfig file. But the return value also depends on the input options: eslint configuration may pass different options for the same tsconfig file.

To fix it, include a hash of the options in the cache key too.

Fixes #465


Important

Modify normalizeOptions() to cache only filename lookup, ensuring correct handling of different options for the same tsconfig file, and add tests for verification.

  • Behavior:
    • Modify normalizeOptions() in normalize-options.ts to cache only the filename lookup, not the entire options object.
    • Ensures different options for the same tsconfig file are handled correctly.
  • Tests:
    • Add filesWithDifferentOptions test case in e2e.spec.ts.snap to verify behavior with different file extensions.
    • New test files eslint.config.js, a.bar.js, a.foo.js, x.foo.js, y.bar.js added under tests/e2e/filesWithDifferentOptions/ to support the new test case.

This description was created by Ellipsis for 614f3b2. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced caching to accurately differentiate option sets, improving import resolution reliability.
  • Tests

    • Added test files and ESLint configurations to validate import resolution for .foo.js and .bar.js extensions.
    • Included sample modules to support these new test scenarios.
  • Chores

    • Added a changeset documenting the patch update and its impact.

Copy link
changeset-bot bot commented Jun 25, 2025

🦋 Changeset detected

Latest commit: 769f6a6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-import-resolver-typescript Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
codesandbox-ci bot commented Jun 25, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link
@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 614f3b2 in 38 seconds. Click for details.
  • Reviewed 146 lines of code in 7 files
  • Skipped 1 files when reviewing.
  • Skipped posting 2 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. src/normalize-options.ts:19
  • Draft comment:
    Good change: updating configFileMapping to Map<string, string> ensures only the resolved filename is cached, avoiding reuse of stale options.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
2. src/normalize-options.ts:79
  • Draft comment:
    The caching block now correctly checks for a cached resolved config file and re-resolves using tryFile when needed. This addresses issue Caching results in wrong configuration being used #465 by decoupling caching from provided options.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None

Workflow ID: wflow_FAEdDSxudN6mmQHT

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link
pkg-pr-new bot commented Jun 25, 2025

Open in StackBlitz

npm i https://pkg.pr.new/eslint-import-resolver-typescript@466

commit: 769f6a6

Presumably to avoid excessive stat calls, the return value of
`normalizeOptions()` is cached for each input tsconfig file. But the
return value also depends on the input `options`: eslint configuration
may pass different options for the same tsconfig file.

Instead, cache only the result of the `tryFile()` call.
@anomiex anomiex force-pushed the fix/excessive-caching-in-normalize-options branch from 614f3b2 to aba5ddf Compare June 25, 2025 15:15
Copy link
coderabbitai bot commented Jun 25, 2025

"""

Walkthrough

The changes enhance the caching mechanism in the TypeScript resolver by including a stable hash of the entire options object in the cache key, ensuring distinct caching for different option sets. New end-to-end tests and supporting files verify correct resolution behavior for files with differing resolver options and extensions.

Changes

File(s) Change Summary
src/normalize-options.ts Enhanced caching by using a combined cache key of config file path and options hash; updated cache lookup and storage accordingly.
tests/e2e/filesWithDifferentOptions/eslint.config.js
tests/e2e/filesWithDifferentOptions/tsconfig.json
Added ESLint config with overrides for .foo.js and .bar.js extensions and an empty tsconfig for test scenario.
tests/e2e/filesWithDifferentOptions/src/a.foo.js
tests/e2e/filesWithDifferentOptions/src/a.bar.js
Added test files importing modules with .foo.js and .bar.js extensions respectively.
tests/e2e/filesWithDifferentOptions/src/x.foo.js
tests/e2e/filesWithDifferentOptions/src/y.bar.js
Added modules exporting constants x and y for import resolution testing.
.changeset/slick-breads-feel.md Added changeset documenting the patch and rationale for caching fix.

Sequence Diagram(s)

sequenceDiagram
    participant ESLint
    participant Resolver
    participant Cache

    ESLint->>Resolver: Resolve module with options (per file)
    Resolver->>Cache: Lookup cache with key: configFile + optionsHash
    alt Cache hit
        Cache-->>Resolver: Return cached normalized options
    else Cache miss
        Resolver->>Resolver: Normalize options
        Resolver->>Cache: Store normalized options with key: configFile + optionsHash
    end
    Resolver-->>ESLint: Return resolution result
Loading

Assessment against linked issues

Objective Addressed Explanation
Prevent caching of full options objects so that per-file resolver options are respected (#465)
Ensure correct config is used for files with different extensions and resolver options (#465)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Suggested reviewers

  • SukkaW

Poem

🐇 A hop, a skip, a cache refined,
Each option set now well-defined.
.foo.js and .bar.js play,
Resolved correctly every day!
With hashes hashed and keys aligned,
Bugs are gone, no more confined.
Hooray for fixes, swift and kind! 🎉
"""

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
warning Resolution field "workspace:" has an invalid version entry and may be ignored
(node:25321) [DEP0169] DeprecationWarning: url.parse() behavior is not standardized and prone to errors that have security implications. Use the WHATWG URL API instead. CVEs are not issued for url.parse() vulnerabilities.
(Use node --trace-deprecation ... to show where the warning was created)
[1/4] Resolving packages...
error Couldn't find any versions for "eslint-import-resolver-typescript" that matches "workspace:
"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 50bd16e and 769f6a6.

📒 Files selected for processing (1)
  • src/normalize-options.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/normalize-options.ts
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Lint and Test with Node.js 24 on windows-latest
  • GitHub Check: Lint and Test with Node.js 20 on windows-latest
  • GitHub Check: Lint and Test with Node.js 22 on windows-latest
  • GitHub Check: Lint and Test with Node.js 18 on windows-latest
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

8000 Copy link
Collaborator
@JounQin JounQin left a comment

Choose a reason for hiding this comment

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

Thanks for working on this first, but the better fix would be move the optionsHash check from

const optionsHash = stableHash(options)
to normalizeOptions instead, makes the cache key into configFile + '\0' + optionsHash.

@anomiex
Copy link
Contributor Author
anomiex commented Jun 25, 2025

Ok, we can do it that way. 🤷

I didn't move the check though, because the separate cache in src/index.ts still needs it too.

@anomiex anomiex changed the title fix: only cache filename lookup, not options fix: include options hash in cache key for options normalization Jun 25, 2025
@JounQin JounQin requested a review from Copilot June 25, 2025 17:06
Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue with caching options in normalizeOptions by incorporating an options hash into the cache key. It also adds tests to ensure that different option sets for the same tsconfig file are correctly handled.

  • Updates the caching mechanism in src/normalize-options.ts by computing a stable hash for the options.
  • Introduces new test files and adjustments in the ESLint configuration to validate the behavior with different file extensions.
  • Adds a minimal tsconfig.json and enhanced snapshots for the new test scenarios.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/e2e/filesWithDifferentOptions/tsconfig.json Added minimal config file for tests
tests/e2e/filesWithDifferentOptions/src/y.bar.js Added test file exporting a constant
tests/e2e/filesWithDifferentOptions/src/x.foo.js Added test file exporting a constant
tests/e2e/filesWithDifferentOptions/src/a.foo.js Uses default import for x despite x.foo.js exporting a named export
tests/e2e/filesWithDifferentOptions/src/a.bar.js Uses default import for y despite y.bar.js exporting a named export
tests/e2e/filesWithDifferentOptions/eslint.config.js Configured extension preferences for .foo.js and .bar.js files
tests/e2e/snapshots/e2e.spec.ts.snap Updated snapshot for new test scenarios
src/normalize-options.ts Updated caching implementation to include options hash in the cache key
.changeset/slick-breads-feel.md Documented the patch update
Comments suppressed due to low confidence (2)

tests/e2e/filesWithDifferentOptions/src/a.foo.js:1

  • The file is importing the default export from './x', but x.foo.js exports x as a named export. Consider changing the import to use named import syntax (e.g., import { x } from './x') or updating x.foo.js to export a default.
import x from './x'

tests/e2e/filesWithDifferentOptions/src/a.bar.js:1

  • The file is importing the default export from './y', but y.bar.js exports y as a named export. Consider changing the import to use named import syntax (e.g., import { y } from './y') or updating y.bar.js to export a default.
import y from './y'

@JounQin JounQin added the bug label Jun 25, 2025
Copy link
Collaborator
@JounQin JounQin left a comment

Choose a reason for hiding this comment

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

LGTM

@JounQin JounQin merged commit 799f1ce into import-js:master Jun 25, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

Caching results in wrong configuration being used
2 participants
0