microsoft / microsoft/TypeScript

Automatic type package resolving doesn’t respect paths defined

Aperta
#34,917 11 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

TypeScript Version: 3.8.0-dev.20191105

Search Terms:
mangled scope package resovle

Code

Consider a project layout as follow:

$ tree -L 2
.
├── test.ts
├── third_party
│   ├── node_modules
│   ├── package.json
│   └── yarn.lock
└── tsconfig.json

test.ts

import * as babel from '@babel/core';

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "moduleResolution": "node",
    "outDir": "tsc-out",
    "paths": {
      "*": [
        "*",
        "third_party/node_modules/*",
      ],
    },
    "rootDir": "./",
    "strict": true,
    "traceResolution": true,
  },
  "include": [
    "*.ts",
  ],
}

package.json

{
  "devDependencies": {
    "@babel/core": "^7.7.0",
    "@types/babel__core": "^7.1.3",
    "typescript": "^3.8.0-dev.20191105"
  }
}

Expected behavior:

Code compiles without an error.

Actual behavior:

Code failed to compile.

$ ./third_party/node_modules/typescript/bin/tsc -p .
======== Resolving module '@babel/core' from '/Users/xiaoyi/Projects/tsc-mangle-resolve/test.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to '/Users/xiaoyi/Projects/tsc-mangle-resolve', using this value to resolve non-relative module name '@babel/core'.
'paths' option is specified, looking for a pattern to match module name '@babel/core'.
Module name '@babel/core', matched pattern '*'.
Trying substitution '*', candidate module location: '@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/@babel/core', target file type 'TypeScript'.
Trying substitution 'third_party/node_modules/*', candidate module location: 'third_party/node_modules/@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core', target file type 'TypeScript'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.d.ts' does not exist.
Found 'package.json' at '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/package.json'.
'package.json' does not have a 'typesVersions' field.
'package.json' does not have a 'typings' field.
'package.json' does not have a 'types' field.
'package.json' has 'main' field 'lib/index.js' that references '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' exist - use it as a name resolution result.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' has an unsupported extension, so skipping it.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js', target file type 'TypeScript'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js.d.ts' does not exist.
File name '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' has a '.js' extension - stripping it.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.d.ts' does not exist.
Directory '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' does not exist, skipping all lookups in it.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/index.ts' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/index.tsx' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/index.d.ts' does not exist.
Loading module '@babel/core' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/Users/xiaoyi/Projects/tsc-mangle-resolve/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/Users/xiaoyi/Projects/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/Users/xiaoyi/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
Directory '/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'babel__core'
'baseUrl' option is set to '/Users/xiaoyi/Projects/tsc-mangle-resolve', using this value to resolve non-relative module name '@babel/core'.
'paths' option is specified, looking for a pattern to match module name '@babel/core'.
Module name '@babel/core', matched pattern '*'.
Trying substitution '*', candidate module location: '@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/@babel/core', target file type 'JavaScript'.
Trying substitution 'third_party/node_modules/*', candidate module location: 'third_party/node_modules/@babel/core'.
Loading module as file / folder, candidate module location '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core', target file type 'JavaScript'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.js' does not exist.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core.jsx' does not exist.
Found 'package.json' at '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/package.json'.
'package.json' does not have a 'typesVersions' field.
'package.json' has 'main' field 'lib/index.js' that references '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js'.
File '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' exist - use it as a name resolution result.
======== Module name '@babel/core' was successfully resolved to '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' with Package ID '@babel/core/lib/index.js@7.7.0'. ========
test.ts:1:24 - error TS7016: Could not find a declaration file for module '@babel/core'. '/Users/xiaoyi/Projects/tsc-mangle-resolve/third_party/node_modules/@babel/core/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/babel__core` if it exists or add a new declaration (.d.ts) file containing `declare module '@babel/core';`

1 import * as babel from '@babel/core';
                         ~~~~~~~~~~~~~


Found 1 error.

@babel/core doesn't ship with typedefs, so tsc tries to find the typedef in @types. After trying @types/@babel/core, tsc decides to try the mangled scope package name @types/babel__core. However, the second attempt no longer follows the paths defined in tsconfig.json.

Playground Link:

N/A

Related Issues:

#19104, similar, but adding node_modules/@types/* won't work for mangled scope package name case.

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 da test.ts e tsconfig.json, quindi esegui il comando mostrato tsc -p . con traceResolution abilitato per riprodurre la ricerca del pacchetto con scope. Traccia il modo in cui viene applicato il mapping paths durante la risoluzione di @types/babel__core; il lavoro è completato quando il pacchetto delle dichiarazioni viene trovato tramite il percorso configurato e il progetto viene compilato senza TS7016.

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

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.