microsoft / microsoft/TypeScript

Importing causes side effect of changing module resolution for transitive dependencies for other imports in 5.6

Aperta
#60,062 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs More Info
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

"module resolution cache", "5.6 cache"

🕗 Version & Regression Information
  • This changed between versions 5.5.4 and 5.6.2
⏯ Playground Link

No response

💻 Code

Importing dependency B that transitively imports dependency D can pollute the cache for dependency C which also needs dependency D. In the example I observed this caused the types used for D to be CommonJS types for D when ESM should have been used, just because C imported D as CommonJs. This behavior changed in TypeScript 5.6.


import { Aggregate } from '@convex-dev/aggregate'
import { Migrations } from '@convex-dev/migrations'

Both of these packages transitively import convex.

The @convex-dev/aggregate package is "broken:" it uses export conditions to point '.' (see https://unpkg.com/browse/@convex-dev/aggregate@0.1.6/package.json) to point to a file in a directory with a package.json with "type": "commonjs" (see https://unpkg.com/browse/@convex-dev/aggregate@0.1.6/dist/esm/package.json), when based on the directory names that doesn't really make sense.

@convex-dev/migrations works more like you'd expect, a similar layout but the package.json at dist/esm/package.json more reasonably has "type": "module".

🙁 Actual behavior

In 5.5.6, just adding the import

`import { Aggregate } from '@convex-dev/aggregate` 

is enough to change the resolution of package 'convex' used by '@convex-dev/migrations'.

tsc --traceResolution with import { Aggregate } from '@convex-dev/aggregate: cache is used to resolve 'convex/server' to /Users/tomb/memory-palace/node_modules/convex/dist/cjs-types/server/index.d.ts
(partial output)

======== Resolving module 'convex/server' from '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/index.d.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
File '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/package.json' does not exist according to earlier cached lookups.
File '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/package.json' exists according to earlier cached lookups.
Loading module 'convex/server' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/node_modules' does not exist, skipping all lookups in it.
Resolution for module 'convex/server' was found in cache from location '/Users/tomb/memory-palace/node_modules/@convex-dev'.
======== Module name 'convex/server' was successfully resolved to '/Users/tomb/memory-palace/node_modules/convex/dist/cjs-types/server/index.d.ts' with Package ID 'convex/dist/cjs-types/server/index.d.ts@1.16.2+@auth0/auth0-react@2.0.2+react@18.2.0+react-dom@18.2.0'. ========

tsc --traceResolution with import { Aggregate } from '@convex-dev/aggregate commented out: cache is not used, 'convex/server' resolved to /Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts
(partial output)

======== Resolving module 'convex/server' from '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/index.d.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
File '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/package.json' does not exist according to earlier cached lookups.
File '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/package.json' exists according to earlier cached lookups.
Loading module 'convex/server' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/node_modules' does not exist, skipping all lookups in it.
File '/Users/tomb/memory-palace/node_modules/convex/server/package.json' exists according to earlier cached lookups.
File '/Users/tomb/memory-palace/node_modules/convex/package.json' exists according to earlier cached lookups.
Entering conditional exports.
Saw non-matching condition 'require'.
Matched 'exports' condition 'import'.
Entering conditional exports.
Matched 'exports' condition 'types'.
Using 'exports' subpath './server' with target './dist/esm-types/server/index.d.ts'.
File '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts' exists - use it as a name resolution result.
Resolved under condition 'types'.
Exiting conditional exports.
Resolved under condition 'import'.
Exiting conditional exports.
Resolving real path for '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts', result '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts'.
======== Module name 'convex/server' was successfully resolved to '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts' with Package ID 'convex/dist/esm-types/server/index.d.ts@1.16.2+@auth0/auth0-react@2.0.2+react@18.2.0+react-dom@18.2.0'. ========
🙂 Expected behavior

In 5.5.4, adding

`import { Aggregate } from '@convex-dev/aggregate` 

has no effect on the resolution of package 'convex' used by '@convex-dev/migrations'.

tsc --traceResolution with import { Aggregate } from '@convex-dev/aggregate commented out
(partial output)

======== Resolving module 'convex/server' from '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/index.d.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
File '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/package.json' does not exist.
File '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/package.json' exists according to earlier cached lookups.
Loading module 'convex/server' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/client/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/esm/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/dist/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/migrations/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/tomb/memory-palace/node_modules/@convex-dev/node_modules' does not exist, skipping all lookups in it.
File '/Users/tomb/memory-palace/node_modules/convex/server/package.json' exists according to earlier cached lookups.
File '/Users/tomb/memory-palace/node_modules/convex/package.json' exists according to earlier cached lookups.
Entering conditional exports.
Saw non-matching condition 'require'.
Matched 'exports' condition 'import'.
Entering conditional exports.
Matched 'exports' condition 'types'.
Using 'exports' subpath './server' with target './dist/esm-types/server/index.d.ts'.
File '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts' exists - use it as a name resolution result.
Resolved under condition 'types'.
Exiting conditional exports.
Resolved under condition 'import'.
Exiting conditional exports.
Resolving real path for '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts', result '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts'.
======== Module name 'convex/server' was successfully resolved to '/Users/tomb/memory-palace/node_modules/convex/dist/esm-types/server/index.d.ts' with Package ID 'convex/dist/esm-types/server/index.d.ts@1.16.2+@auth0/auth0-react@2.0.2+react@18.2.0+react-dom@18.2.0'. ========
Additional information about the issue

Restating the issue: Having already resolved a module import causes important steps to be skipped:

Resolution for module 'convex/server' was found in cache from location '/Users/tomb/memory-palace/node_modules/@convex-dev'.

seems to indicate that exports condition logic is being skipped inappropriately for this cached lookup: because it had been found before from a place where exports condition "require" matched, that was used instead of re-resolving now that "require" would not have matched.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo gli import di @convex-dev/aggregate e @convex-dev/migrations con TypeScript 5.5.4 e 5.6.2, quindi confronta l'output fornito di tsc --traceResolution. Esamina il percorso della cache di risoluzione dei moduli che risolve convex/server da @convex-dev e aggiungi un test di regressione che dimostri che i risultati memorizzati nella cache non aggirano le esportazioni condizionali; il lavoro è completato quando la destinazione ESM rimane selezionata indipendentemente dall'ordine degli import.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.