question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatic type package resolving doesn’t respect paths defined

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ryanrheecommented, Oct 1, 2020

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.

Is this still accurate as of now? Where in the source is this happening? Not being able to use @types for scoped packages is a pretty big problem.

0reactions
ryanrheecommented, Oct 11, 2020

Is resolving to third_party/node_modules a bazel-specific thing?

in bazel, node_modules is symlink’d to be inside the build directory. for example, bazel’s generated tsconfig.json might have entries that look like this:

path from repo root: ./dist/bin/myDir/mySubdir/lib_es5_tsconfig.json

{
  "compilerOptions": {
    ....
    "outDir": "../../../../../bazel-out/darwin-fastbuild/bin",
    "rootDir": "../../../../..",
    "rootDirs": [
      "../../../../..",
      "../../../../../bazel-out/darwin-fastbuild/bin",
      "../../../../../bazel-out/darwin-fastbuild/bin"
    ],
    "baseUrl": "../../../../..",
    "paths": {
      "*": [
        "external/npm/node_modules/*",
        "external/npm/node_modules/@types/*"
      ],
      "bbl/*": [
        "./*",
        "bazel-out/darwin-fastbuild/bin/*",
        "bazel-out/darwin-fastbuild/bin/*"
      ]
    },
    ...
    "declarationDir": "../../../../../bazel-out/darwin-fastbuild/bin",
    ...
    "typeRoots": [
      "../../../../../external/npm/node_modules/@types"
    ],
    "types": []
  },
  ...
}

So, to answer your question, having the node_modules directory in a non-standard place is a bazel-specific thing. It’s not specifically third_party/node_modules, though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Visual Studio Code can't resolve angular's tsconfig paths
if I specify paths in ./tsconfig. json starting from src , vscode is happy but angular is not. if I specify paths in...
Read more >
go command - cmd/go - Go Packages
Build compiles the packages named by the import paths, along with their dependencies, but it does not install the results. If the arguments...
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
Imported CommonJS modules have their URLs converted to absolute paths and are then loaded via the CommonJS module loader. package.json and file extensions#....
Read more >
Building for Production - Vite
If you are deploying your project under a nested public path, ... in your .html files are all automatically adjusted to respect this...
Read more >
sam build - AWS Serverless Application Model
You specify your AWS Lambda function's package type with the PackageType property. ... By default, relative paths are resolved with respect to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found