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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slick-breads-feel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": patch
---

fix: only cache filename lookup, not options
22 changes: 9 additions & 13 deletions src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { TypeScriptResolverOptions } from './types.js'

export let defaultConfigFile: string

const configFileMapping = new Map<string, TypeScriptResolverOptions>()
const configFileMapping = new Map<string, string>()

let warned: boolean | undefined

Expand Down Expand Up @@ -77,17 +77,17 @@ export function normalizeOptions(
}

if (configFile) {
const cachedOptions = configFileMapping.get(configFile)
if (cachedOptions) {
log('using cached options for', configFile)
return cachedOptions
let cachedConfigFile: string | undefined = configFileMapping.get(configFile)
if (cachedConfigFile) {
log('using cached config file for', configFile)
configFile = cachedConfigFile
} else if (!ensured && configFile !== defaultConfigFile) {
cachedConfigFile = tryFile(DEFAULT_TRY_PATHS, false, configFile)
configFileMapping.set(configFile, cachedConfigFile)
configFile = cachedConfigFile
}
}

if (!ensured && configFile && configFile !== defaultConfigFile) {
configFile = tryFile(DEFAULT_TRY_PATHS, false, configFile)
}

options = {
conditionNames: defaultConditionNames,
extensions: defaultExtensions,
Expand All @@ -100,9 +100,5 @@ export function normalizeOptions(
: undefined,
}

if (configFile) {
configFileMapping.set(configFile, options)
}

return options
}
8 changes: 8 additions & 0 deletions tests/e2e/__snapshots__/e2e.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ exports[`e2e cases > should exec eslint successfully > dotProject 1`] = `
}
`;

exports[`e2e cases > should exec eslint successfully > filesWithDifferentOptions 1`] = `
{
"exitCode": 0,
"stderr": "",
"stdout": "",
}
`;

exports[`e2e cases > should exec eslint successfully > importXResolverV3 1`] = `
{
"exitCode": 0,
Expand Down
44 changes: 44 additions & 0 deletions tests/e2e/filesWithDifferentOptions/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { defineConfig, globalIgnores } from 'eslint/config'
import { defaultExtensions } from 'eslint-import-resolver-typescript'
import importX, { flatConfigs } from 'eslint-plugin-import-x'

export default defineConfig(
globalIgnores(['eslint.config.js']),
{
files: ['**/*.foo.js', '**/*.bar.js'],
plugins: {
'import-x': importX,
},
settings: {
...flatConfigs.typescript.settings,
'import-x/resolver': {
typescript: {},
},
},
rules: {
'import-x/no-unresolved': 'error',
},
},
// .foo.js files should prefer importing other .foo.js files.
{
files: ['**/*.foo.js'],
settings: {
'import-x/resolver': {
typescript: {
extensions: ['.foo.js', ...defaultExtensions],
},
},
},
},
// .bar.js files should prefer importing other .bar.js files.
{
files: ['**/*.bar.js'],
settings: {
'import-x/resolver': {
typescript: {
extensions: ['.bar.js', ...defaultExtensions],
},
},
},
},
)
1 change: 1 addition & 0 deletions tests/e2e/filesWithDifferentOptions/src/a.bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import y from './y'
1 change: 1 addition & 0 deletions tests/e2e/filesWithDifferentOptions/src/a.foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import x from './x'
1 change: 1 addition & 0 deletions tests/e2e/filesWithDifferentOptions/src/x.foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const x = 'x'
1 change: 1 addition & 0 deletions tests/e2e/filesWithDifferentOptions/src/y.bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const y = 'y'
1 change: 1 addition & 0 deletions tests/e2e/filesWithDifferentOptions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}