Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LSP not recommending shortest subpath imports from a shared package in a monorepo #59847

Closed
Frederick888 opened this issue Sep 4, 2024 · 1 comment

Comments

@Frederick888
Copy link

πŸ”Ž Search Terms

"package.json exports", "monorepo exports"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about export maps

⏯ Playground Link

https://github.com/Frederick888/playground/tree/monorepo

πŸ’» Code

I'm setting up a monorepo where there are a few 'executables' and a shared package. In the example repo, it's structured this way:

.
β”œβ”€β”€ package.json
β”œβ”€β”€ packages
β”‚  β”œβ”€β”€ bar
β”‚  β”‚  β”œβ”€β”€ package.json
β”‚  β”‚  β”œβ”€β”€ spec
β”‚  β”‚  β”œβ”€β”€ src
β”‚  β”‚  β”‚  └── index.ts                   <- playground
β”‚  β”‚  └── tsconfig.json
β”‚  └── shared
β”‚     β”œβ”€β”€ dist                          <- outDir, only *.js files listed here
β”‚     β”‚  β”œβ”€β”€ src
β”‚     β”‚  β”‚  β”œβ”€β”€ index.js
β”‚     β”‚  β”‚  └── utils
β”‚     β”‚  β”‚     β”œβ”€β”€ bonjour.js
β”‚     β”‚  β”‚     └── index.js
β”‚     β”‚  └── tsconfig.tsbuildinfo
β”‚     β”œβ”€β”€ package.json
β”‚     β”œβ”€β”€ spec                          <- tests
β”‚     β”œβ”€β”€ src
β”‚     β”‚  β”œβ”€β”€ index.ts
β”‚     β”‚  └── utils
β”‚     β”‚     β”œβ”€β”€ bonjour.ts
β”‚     β”‚     └── index.ts                <- barrel index.ts to re-export things we want to expose from utils/
β”‚     └── tsconfig.json
β”œβ”€β”€ tsconfig.base.json
└── tsconfig.json

(Please clear the playground index.ts before testing these. tsserver actually recommends "@playground/shared/utils" if it's already there.)

Now in bar/, if I import something from shared/utils/index.ts, it'll recommend import { greetings } from "@playground/shared/utils/index". I guess this part is similar to #51418.

Another issue is that if I import something from shared/utils/bonjour.ts, it'll recommend import { bonjour } from "@playground/shared/utils/bonjour".

πŸ™ Actual behavior

// import { greetings } from "@playground/shared/utils/index" <- Nope
// import { bonjour } from "@playground/shared/utils/bonjour" <- Nope
import { bonjour, greetings } from "@playground/shared/utils"

console.log(greetings())
console.log(bonjour())

Both the two 'nope' import recommendations result in runtime import errors.

πŸ™‚ Expected behavior

It recommends the re-exports from @playground/shared/utils.

Additional information about the issue

Please feel free to close this if the underlying issue is the same as ##51418's.

Also I'm not sure whether #58657 is relevant.

@Frederick888
Copy link
Author

Frederick888 commented Sep 4, 2024

Closing. should've used

    "./*": {
      "types": "./dist/src/*/index.d.ts",
      "default": "./dist/src/*/index.js"
    }

or just

    "./*": "./dist/src/*/index.js"

instead of

    "./*": {
      "types": "./dist/src/*.d.ts",
      "default": "./dist/src/*/index.js"
    }

in shared/'s package.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant