privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
8.61k stars 132 forks source link

tsx fails to find aliased imports from workspace dependencies #559

Open chrisandrewcl opened 1 month ago

chrisandrewcl commented 1 month ago

Acknowledgements

Minimal reproduction URL

https://github.com/chrisandrewcl/tsx-dependency-paths

Problem & expected behavior (under 200 words)

No response

Bugs are expected to be fixed by those affected by it

Compensating engineering work financially will speed up resolution

privatenumber commented 1 month ago

Thanks for the issue. I appreciate the conciseness.

This is basically a duplicate of https://github.com/privatenumber/tsx/issues/96 because the root issue is that tsx doesn't lookup tsconfig.json in imported packages.

I see this more of a feature request rather than a bug since TypeScript doesn't do this either.

chrisandrewcl commented 1 month ago

As a workaround, after experimenting with a few other options, I got a working setup by ditching tscongif.json's paths and using package.json imports instead. Demo here.

P.S.: I am in a hurry, so I didn't update the file names in the demo repo, but everything is now working for tsc, tsx and esbuild.

Infinitay commented 1 day ago

Posting here to avoid making a duplicate issue because I believe I'm facing the same issue. If you'd like I can create a new issue.

tsx version

tsx v4.16.0
node v22.3.0

Code

package.json ```json { "name": "discord-collectibles-archive", "version": "0.1.0", "private": true, "type": "module", "scripts": { "build": "next build", "db:generate": "drizzle-kit generate", "db:migrate": "drizzle-kit migrate", "db:push": "drizzle-kit push", "db:studio": "drizzle-kit studio", "dev": "next dev", "lint": "next lint", "start": "next start", "format": "prettier --check --ignore-path .gitignore .", "format:fix": "prettier --write --ignore-path .gitignore ." }, "dependencies": { "@t3-oss/env-nextjs": "^0.10.1", "drizzle-orm": "^0.30.10", "fast-equals": "^5.0.1", "geist": "^1.3.0", "next": "^14.2.1", "postgres": "^3.4.4", "react": "^18.3.0", "react-dom": "^18.3.0", "zod": "^3.23.3" }, "devDependencies": { "@types/eslint": "^8.56.2", "@types/node": "^20.14.7", "@types/react": "^18.2.57", "@types/react-dom": "^18.2.19", "@typescript-eslint/eslint-plugin": "^7.1.1", "@typescript-eslint/parser": "^7.1.1", "drizzle-kit": "^0.21.4", "eslint": "^8.57.0", "eslint-config-next": "^14.1.3", "eslint-plugin-drizzle": "^0.2.3", "postcss": "^8.4.34", "prettier": "^3.2.5", "prettier-plugin-tailwindcss": "^0.5.14", "tailwindcss": "^3.4.3", "typescript": "^5.5.2" }, "ct3aMetadata": { "initVersion": "7.34.0" }, "packageManager": "npm@9.5.1" } ```
tsconfig.json ```json { "compilerOptions": { /* Base Options: */ "esModuleInterop": true, "skipLibCheck": true, "target": "es2022", "allowJs": true, "resolveJsonModule": true, "moduleDetection": "force", "isolatedModules": true, /* Strictness */ "strict": true, "noUncheckedIndexedAccess": true, "checkJs": true, /* Bundled projects */ "lib": ["dom", "dom.iterable", "ES2022"], "noEmit": true, "module": "ESNext", "moduleResolution": "Bundler", "jsx": "preserve", "plugins": [{ "name": "next" }], "incremental": true, /* Path Aliases */ "baseUrl": ".", "paths": { "~/*": ["./src/*"], "~discord-data/*": ["./discord-data/*"], } }, "include": [".eslintrc.cjs", "next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.js", ".next/types/**/*.ts"], "exclude": ["node_modules"] } ```
src\types\CollectiblesCategories.d.ts ```ts export interface CollectiblesCategories { sku_id: string; name: string; summary: string; store_listing_id: string; banner: string; unpublished_at: string | null; styles: Styles; logo: string; mobile_bg?: string; pdp_bg?: string; mobile_banner?: string; products: Product[]; } export interface Product { sku_id: string; name: string; summary: string; store_listing_id: string; banner: string; unpublished_at: null; styles: Styles; prices: { [key: string]: PriceValue }; items: Item[]; type: ProductTypes | number; premium_type: PremiumTypes | number; category_sku_id: string; bundled_products?: BundledProduct[]; google_sku_ids: GoogleSkuIDS; } export interface BundledProduct { sku_id: string; name: string; summary: string; type: ItemTypes | number; premium_type: PremiumTypes | number; prices: { [key: string]: PriceValue }; } export interface PriceValue { country_prices: CountryPrices; } export interface CountryPrices { country_code: CountryCode; prices: PriceElement[]; } // Union to a string because it could be any country code (ISO 3166-1 alpha-2 code), but also type check was complaining export type CountryCode = "US" | string; export interface PriceElement { amount: number; currency: Currency; exponent: number; } // Union to a string because it could be any currency code (ISO 4217 code), but also type check was complaining export type Currency = "usd" | string; export interface GoogleSkuIDS {} export interface Item { type: ItemTypes | number; id: string; sku_id: string; asset?: string; label?: string; } export interface Styles { background_colors: number[]; button_colors: number[]; confetti_colors: number[]; } export enum ProductTypes { AvatarDecoration = 0, ProfileEffect = 1, Bundle = 1000 } export enum PremiumTypes { Everyone = 0, Nitro = 1 } export enum ItemTypes { AvatarDecoration = 0, ProfileEffect = 1 } ```
discord-data\scripts\previews-extractor.ts ```ts import { CollectiblesCategories, ItemTypes } from "~/types/CollectiblesCategories" import collections from "~discord-data/collections" const c: CollectiblesCategories[] = Object.values(collections); console.log(c) // No issues console.log(ItemTypes.AvatarDecoration) // Having this line or just any reference to ItemTypes makes the code crash and throw ERR_MODULE_NOT_FOUND ```

Stacktrace

[Running] npx tsx "e:\Documents\Code\NextJS\discord-collectibles-archive\discord-data\scripts\previews-extractor.ts"

node:internal/modules/run_main:115 triggerUncaughtException( ^ Error [ERR_MODULE_NOT_FOUND]: Cannot find package '~' imported from e:\Documents\Code\NextJS\discord-collectibles-archive\discord-data\scripts\previews-extractor.ts at packageResolve (node:internal/modules/esm/resolve:841:9) at moduleResolve (node:internal/modules/esm/resolve:914:18) at defaultResolve (node:internal/modules/esm/resolve:1119:11) at nextResolve (node:internal/modules/esm/hooks:750:28) at resolveBase (file:///C:/Users/admin/AppData/Roaming/nvm/v22.3.0/node_modules/tsx/dist/esm/index.mjs?1719846903563:2:3233) at async resolveDirectory (file:///C:/Users/admin/AppData/Roaming/nvm/v22.3.0/node_modules/tsx/dist/esm/index.mjs?1719846903563:2:3519) at async resolve (file:///C:/Users/admin/AppData/Roaming/nvm/v22.3.0/node_modules/tsx/dist/esm/index.mjs?1719846903563:2:4036) at async nextResolve (node:internal/modules/esm/hooks:750:22) at async Hooks.resolve (node:internal/modules/esm/hooks:238:24) at async handleMessage (node:internal/modules/esm/worker:199:18) { code: 'ERR_MODULE_NOT_FOUND' }

Node.js v22.3.0

Issue

For some reason, I can import an interface and assign it as a type with no problem. However, whenever I try to import an enum within the same type file that I imported for the previously mentioned interface I get an ERR_MODULE_NOT_FOUND issue. Why is it that one import works but another doesn't? It's the exact same type declaration being imported.