microsoft / microsoft/TypeScript

Automatic type package resolving doesn’t respect paths defined

Open
#34,917 11 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with test.ts and tsconfig.json, then run the shown tsc -p . command with traceResolution enabled to reproduce the scoped-package lookup. Trace how the paths mapping is applied when resolving @types/babel__core; done means the declaration package is found through the configured path and the project compiles without TS7016.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.