Skip to content

Auto-import ignores barrel (index.ts) when the imported folder name is a prefix of the importing file name #64029

Description

🔎 Search Terms

auto-import index label:"Domain: LS: Auto-import"

🕗 Version & Regression Information

  • This changed between versions 6.0.3 and 7.0.2

VSCode version: 1.134.0
Windows 11

⏯ Playground Link

Example

💻 Code

Project structure example:

utils/
  sum/
    index.ts    // re‑exports sum from sum.ts
    sum.ts
  sumA/
    index.ts    // re‑exports sumA from sumA.ts
    sumA.ts
  sumABNums/
    index.ts    // re‑exports sumABNums from sumABNums.ts
    sumABNums.ts
  sumAB.ts      // importing file (the issue occurs here)
  index.ts      // importing file (works correctly)
// src/utils/sumAB.ts
import { sum } from "./sum/sum.js";
import { sumA } from "./sumA/sumA.js";
import { sumABNums } from "./sumABNums/index.js";

console.log(sum, sumA, sumABNums)

// src/utils/index.ts
import { sum } from "./sum/index.js";
import { sumA } from "./sumA/index.js";
import { sumABNums } from "./sumABNums/index.js";

console.log(sum, sumA, sumABNums)

🙁 Actual behavior

In utils/sumAB.ts:
for modules sum and sumA (whose folder names are prefixes of the importing file name sumAB.ts), the suggestion points to the direct file: './sum/sum.js' and './sumA/sumA.js'.
The barrel path './sum/index.js' and './sumA/index.js' is not offered.

For sumABNums (folder name is longer than the importing file name, so it is not a prefix), the correct barrel path './sumABNums/index.js' is suggested.

In utils/index.ts (also at the same level), imports for all modules correctly resolve via their barrel files.

🙂 Expected behavior

The barrel path ('./sum/index.js', './sumA/index.js') should always be the preferred suggestion when it exists, regardless of how the folder name relates to the importing file name.

Additional information about the issue

The issue seems to occur specifically when the imported folder name is a prefix of the importing file name. If the names differ by at least one character in a way that breaks the prefix relationship (e.g., sum and sumB or sam), the barrel path is correctly suggested.

In 6.0.3 barrel files are always ignored (in both files: utils/sumAB.ts and utils/index.ts).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions