microsoft / microsoft/TypeScript

`importModuleSpecifierPreference: "non-relative"` should use self-name package imports

Offen
#59,620 3 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@andrewbranch arbeitet bereits daran.

Seit 13.8.2024.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms
  • relative imports
  • workspaces
  • monorepo
  • subpath export
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about auto imports and subpath exports.
⏯ Playground Link

No response

💻 Code

Full reduced test case: https://github.com/OliverJAsh/typescript-auto-imports-exports

Contents inlined below. TLDR:

  • Two workspaces, packages/a and packages/b.
  • packages/b depends on packages/a.

Root files:

tsconfig.json:

{
  "compilerOptions": {
    "strict": true,
    "noEmit": true,
    "target": "ESNext",
    "module": "Node16",
    "moduleResolution": "Node16"
  }
}

package.json:

{
  "private": true,
  "dependencies": {
    "typescript": "^5.5.4"
  },
  "workspaces": [
    "packages/a",
    "packages/b"
  ],
  "packageManager": "yarn@4.4.0"
}

Package a:

packages/a/package.json:

{
  "name": "a",
  "private": true,
  "exports": {
    "./test": "./module.ts"
  }
}

packages/a/module.ts:

export const a = 1;

packages/a/other-module.ts:

// Try to import this
// Auto import uses a relative path here: `./module` ❌
a;

Package b:

packages/b/package.json:

{
  "name": "b",
  "private": true,
  "dependencies": {
    "a": "workspace:^"
  }
}

packages/b/module.ts:

// Try to import this
// Auto import works correctly here: `a/test` ✅
a;

🙁 Actual behavior
  • The auto import suggestion in packages/b/other-module.ts uses the subpath export defined in packages/a/package.json. The suggested import path is a/test. ✅
  • The auto import suggestion in packages/a/other-module.ts does not use the subpath export defined in packages/a/package.json. Instead it uses a relative import. The suggested import path is ./module.

It seems that TypeScript is not using the subpath export information for self-referencing imports.

🙂 Expected behavior

In both cases I would expect the subpath export to be used (rather than relative imports), otherwise you can end up with some verbose relative paths in the case of projects with deeply nested folders.

I would expect TypeScript to only use a relative import if a subpath export is not available.

Additional information about the issue

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.