esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

`[email protected]` uses `compilerOptions.baseUrl` when resolving transitive dependencies

Open merceyz opened this issue 3 years ago • 2 comments

Describe the bug

When resolving transitive dependencies [email protected] tries to resolve them from compilerOptions.baseUrl which doesn't match what tsc does.

Resolving import "react" in directory "/tmp/tmp.1Iu4gSxpc7/node_modules/react-dom/cjs" of type "require-call"

  Read 13 entries for directory "/tmp/tmp.1Iu4gSxpc7/node_modules/react-dom/cjs"
  Checking for "react" in the "browser" map in "/tmp/tmp.1Iu4gSxpc7/node_modules/react-dom/package.json"
    Checking for "react"
    Checking for "react.tsx"
    Checking for "react.ts"
    Checking for "react.jsx"
    Checking for "react.js"
    Checking for "react.css"
    Checking for "react.json"
    Checking for "react/index"
    Checking for "react/index.tsx"
    Checking for "react/index.ts"
    Checking for "react/index.jsx"
    Checking for "react/index.js"
    Checking for "react/index.css"
    Checking for "react/index.json"
  Checking for "./cjs/react" in the "browser" map in "/tmp/tmp.1Iu4gSxpc7/node_modules/react-dom/package.json"
    Checking for "./cjs/react"
    Checking for "./cjs/react/index"
  Failed to find "react"
  Searching for "react" in "node_modules" directories starting from "/tmp/tmp.1Iu4gSxpc7/node_modules/react-dom/cjs"
    Attempting to load "/tmp/tmp.1Iu4gSxpc7/react" as a file
      Checking for file "react"
      Checking for file "react.tsx"
      Checking for file "react.ts"
      Checking for file "react.jsx"
      Checking for file "react.js"
      Found file "react.js"
  Read 12 entries for directory "/tmp/tmp.1Iu4gSxpc7"
  This import is under the effect of "/tmp/tmp.1Iu4gSxpc7/tsconfig.json"
  Primary path is "/tmp/tmp.1Iu4gSxpc7/react.js" in namespace "file"

Ref https://github.com/yarnpkg/berry/pull/4732 Ref https://github.com/evanw/esbuild/issues/2473#issuecomment-1217106596

To Reproduce

docker run --rm -it node:16.16.0 bash
cd $(mktemp -d)
yarn init -2
yarn set version 3.2.2
yarn config set nodeLinker node-modules
yarn add [email protected] [email protected] [email protected]
printf '{"compilerOptions": {"baseUrl": "."}}' > tsconfig.json
printf "import 'react-dom'" > index.ts
printf "this should not match" > react.js
yarn esbuild --log-level="verbose" --bundle ./index.ts

merceyz avatar Aug 17 '22 20:08 merceyz

FWIW this is not specific to Yarn. This happens when npm is used instead of Yarn. It looks like this has been esbuild's behavior for quite a while.

evanw avatar Aug 19 '22 23:08 evanw

Indeed, that's why the reproduction uses the node-modules linker (node_modules folder)

merceyz avatar Aug 20 '22 14:08 merceyz

I'm still not totally sure what to do about this. One reason why TypeScript doesn't do this is that it doesn't check JavaScript files. Another reason is that (I think?) it doesn't type packages, which is basically the equivalent of esbuild's --packages=external setting, which would also work here. So I'm not sure if esbuild's behavior should be that path mappings in tsconfig files shouldn't apply to JavaScript files, or that they shouldn't apply to node_modules files, or something else.

evanw avatar Mar 24 '23 19:03 evanw