ui icon indicating copy to clipboard operation
ui copied to clipboard

[bug]: Components refer to wrong "utils.ts" file after "utils.ts" file renamed and moved to different folder [Remix]

Open vanirvan opened this issue 1 year ago • 7 comments

Describe the bug

when adding a new components for example npx shadcn@latest add button. usually the components importing the generated "utils" file, however, i rename the file to "cn.ts" and move it from ~/lib/utils to ~/lib/utils/cn. I also change the generated components.json file and refer the moved utils file to correct path.

here's my components.json file:

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "default",
  "rsc": false,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.ts",
    "css": "app/tailwind.css",
    "baseColor": "neutral",
    "cssVariables": false,
    "prefix": ""
  },
  "aliases": {
    "components": "~/components",
    "utils": "~/lib/utils/cn", // before was ~/lib/utils
    "ui": "~/components/ui",
    "lib": "~/lib",
    "hooks": "~/hooks"
  }
}

also my tsconfig.json file (i'm not changing anything):

{
  "include": [
    "**/*.ts",
    "**/*.tsx",
    "**/.server/**/*.ts",
    "**/.server/**/*.tsx",
    "**/.client/**/*.ts",
    "**/.client/**/*.tsx"
  ],
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ES2022"],
    "types": ["@remix-run/node", "vite/client"],
    "isolatedModules": true,
    "esModuleInterop": true,
    "jsx": "react-jsx",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "resolveJsonModule": true,
    "target": "ES2022",
    "strict": true,
    "allowJs": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./app/*"]
    },

    // Vite takes care of building everything, not tsc.
    "noEmit": true
  }
}

I do the same thing when working with Nextjs project, and everything works fine, but in Remix, this happened.

Affected component/components

Probably all components, but i only try it on Button, Label, and Form

How to reproduce

  1. install shadcn ui in Remix project and follow all instruction.
  2. create new folder inside lib folder named utils
  3. move utils.ts to inside utils folder
  4. rename utils.ts into cn.ts
  5. in components.json, replace "utils": "~/lib/utils", to "utils": "~/lib/utils/cn",
  6. add components like usual, for example npx shadcn@latest add button
  7. after component installed, open button.ts
  8. see that the components have an error cannot find module ~/lib/utils

Codesandbox/StackBlitz link

No response

Logs

No response

System Info

Nothing relevant

Before submitting

  • [X] I've made research efforts and searched the documentation
  • [X] I've searched for existing issues

vanirvan avatar Sep 10 '24 13:09 vanirvan

What exactly is going wrong? Is the CLI not picking up your new utils file path?

imMatheus avatar Sep 13 '24 21:09 imMatheus

@imMatheus I guess the button.ts still contains import { cn } from "~/lib/utils" instead of import { cn } from "~/lib/utils/cn".

denis-shvets avatar Sep 16 '24 19:09 denis-shvets

What exactly is going wrong? Is the CLI not picking up your new utils file path?

Yes, sorry for not replying as soon as possible, the button component is importing the utils.ts file from ~/lib/utils not cn.ts file from ~lib/utils/cn.

Expected Output:

...

import { cn } from "~/lib/utils/cn";

// the rest of button component

Intead of :

...

import { cn } from "~/lib/utils";

// the rest of button component

vanirvan avatar Sep 17 '24 04:09 vanirvan

@vanirvan I am not a maintainer or contributor to this project, but I ran into this on my own project and did some investigation/testing and it seems as though the issue is that you're using something other than @ as the start of your path alias for the components key in your components.json. To confirm, temporarily change your components key to @/components and give a try to npx shadcn@latest add button (or any other component) and I suspect it will then respect your custom path alias to the utils. I'm fairly certain this is a bug in the new shadcn cli because trying with the old cli (aka npx [email protected] add button) seems to just take the value of the utils alias verbatim and doesn't try to do any calculation to determine the correct path to use for the utils file.

andrewbrey avatar Sep 23 '24 00:09 andrewbrey

I'll take a look.

shadcn avatar Sep 25 '24 12:09 shadcn

@vanirvan I am not a maintainer or contributor to this project, but I ran into this on my own project and did some investigation/testing and it seems as though the issue is that you're using something other than @ as the start of your path alias for the components key in your components.json. To confirm, temporarily change your components key to @/components and give a try to npx shadcn@latest add button (or any other component) and I suspect it will then respect your custom path alias to the utils. I'm fairly certain this is a bug in the new shadcn cli because trying with the old cli (aka npx [email protected] add button) seems to just take the value of the utils alias verbatim and doesn't try to do any calculation to determine the correct path to use for the utils file.

@andrewbrey Hello sir, sorry for not replying as soon as possible, and seems like your advice is indeed works, i changed the path alias from ~/app into @/app and it works very well, thankyou, maybe the downside is i only need change the path alias. But i hope in the future, shadcn-ui team would fix this.

once again, thank you

vanirvan avatar Sep 26 '24 11:09 vanirvan

I encountered the same problem This worked before but it seems something was broken.

farwayer avatar Oct 02 '24 13:10 farwayer

Seeing this issue also. It likely may be due to this related bug: https://github.com/shadcn-ui/ui/issues/5433#issuecomment-2425131609

mindofjonas avatar Oct 21 '24 02:10 mindofjonas

I'm encountering same issue. lib or utils config aliases seems to be ignored.

tomas223 avatar Nov 19 '24 19:11 tomas223

Yes, same here

MoSattler avatar Dec 26 '24 08:12 MoSattler

Hi everyone,

I’ve been testing this issue myself since I encountered it as well. With the current support for ShadCN in an NX monorepo, having to manually correct the imports for each component became unmanageable.

As @andrewbrey pointed out, older versions seem to work fine, but version 2.1.8 introduces the problem. I’ve reviewed recent versions to pinpoint where the functionality started breaking.

It appears that [email protected] works correctly and generates the appropriate imports. However, starting from 2.1.7, the behaviour changes unexpectedly: the @ namespace is ignored, and the imports default to @namespace/lib/utils, which is quite frustrating.

As a temporary workaround, you can downgrade the CLI to version 2.1.6, which resolves the issue for now.

For example:
CLI: npx [email protected] add button

Button.tsx (Correct):

import * as React from "react";  
import { Slot } from "@radix-ui/react-slot";  
import { cva, type VariantProps } from "class-variance-authority";  

// Correct  
import { cn } from "@example-mono/shadcn-ui/utils";  

//...  

CLI: npx [email protected] add button

Button.tsx (Incorrect):

import * as React from "react";  
import { Slot } from "@radix-ui/react-slot";  
import { cva, type VariantProps } from "class-variance-authority";  

// Incorrect  
import { cn } from "@example-mono/lib/utils";  

//...  

For reference, here are the relevant configurations:

components.json:

{  
  "$schema": "https://ui.shadcn.com/schema.json",  
  "style": "new-york",  
  "rsc": false,  
  "tsx": true,  
  "tailwind": {  
    "config": "apps/**/tailwind.config.ts",  
    "css": "apps/**/src/app/global.css",  
    "baseColor": "stone",  
    "cssVariables": false  
  },  
  "aliases": {  
    "components": "@example-mono/shadcn-ui/components",  
    "utils": "@example-mono/shadcn-ui/utils"  
  }  
}  

tsconfig.json:

{  
  "compilerOptions": {  
    "baseUrl": ".",  
    "paths": {  
      "@example-mono/shadcn-ui": ["libs/shadcn-ui/src/index.ts"],  
      "@example-mono/shadcn-ui/*": ["libs/shadcn-ui/src/*"]  
    }  
  }  
}  

I hope this helps others encountering the same issue. I’ll try to find some time to review the code and create a PR to address this.

RestfuI avatar Dec 31 '24 03:12 RestfuI

Not sure if this relates, but noticed that the utils path also is wrong when changing the @workspace name. If I spin up a new monorepo using pnpm dlx shadcn@canary init and choose Next.js (Monorepo) - then when I run an install of a component like: pnpm dlx shadcn@canary add dialog -c apps/web, it works perfectly fine, and imports cn correctly from import { cn } from "@workspace/ui/lib/utils".

Screenshot 2025-01-09 at 10 09 26

However, if I replace every instance of @workspace with something else like @repo before running the add command, the import now omits the /ui from the path.

Screenshot 2025-01-09 at 10 12 28

Mind you - the components.json files haven't changed aside from the @repo prefix:

// apps/web/components.json
{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "new-york",
  "rsc": true,
  "tsx": true,
  "tailwind": {
    "config": "../../packages/ui/tailwind.config.ts",
    "css": "../../packages/ui/src/styles/globals.css",
    "baseColor": "zinc",
    "cssVariables": true
  },
  "iconLibrary": "lucide",
  "aliases": {
    "components": "@/components",
    "hooks": "@/hooks",
    "lib": "@/lib",
    "utils": "@repo/ui/lib/utils",
    "ui": "@repo/ui/components"
  }
}

// ---

// packages/ui/components.json
{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "new-york",
  "rsc": true,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.ts",
    "css": "src/styles/globals.css",
    "baseColor": "zinc",
    "cssVariables": true
  },
  "iconLibrary": "lucide",
  "aliases": {
    "components": "@repo/ui/components",
    "utils": "@repo/ui/lib/utils",
    "hooks": "@repo/ui/hooks",
    "lib": "@repo/ui/lib",
    "ui": "@repo/ui/components"
  }
}

Even by changing the aliases.utils to something else like "utils": "Hello World!", it is ignored and remains import { cn } from "@repo/lib/utils". However - if the ui alias is different in apps/web/components.json (for example: "ui": "@foo/ui/components") the utils alias is preserved (despite of course, now the package is being created in a sub directory in the web directory).

Screenshot 2025-01-09 at 10 25 01

imCorfitz avatar Jan 09 '25 09:01 imCorfitz

+1

Use the downgraded version (2.1.6) fixed my issue

dnntung avatar Jan 20 '25 18:01 dnntung

+1 @shadcn

ScreamZ avatar Feb 02 '25 03:02 ScreamZ

+1

HarlonGarcia avatar Feb 05 '25 17:02 HarlonGarcia

+1

3pleFly avatar Feb 12 '25 08:02 3pleFly

Same bug

maelp avatar Mar 09 '25 18:03 maelp

Encountered this same issue. Switching to @ alias fixes it (as opposed to my app default ~)

Is the plan perchance still to address this @shadcn ? 🙏 🍒 🍰

garretteklof avatar Apr 03 '25 23:04 garretteklof

same issue, I configured this as :

{ 
"aliases": {
    "components": "~/components",
    "utils": "@rd/shared",
    "ui": "~/components/ui",
    "hooks": "~/hooks",
    "lib": "@rd/shared"
}

in any component it imported from import { cn } from "~/lib/utils" instead it should imported from import { cn } from "@rd/shared"

sakib412 avatar Apr 13 '25 12:04 sakib412

Been banging my head against this issue too. Just adding a / to the components alias fixes the utils import. Hope this gets fixed.

This creates "import { cn } from "@components/lib/utils"

"aliases": {
    "components": "@components",
    "utils": "@utils/cn",
  },

This creates "import { cn } from "@utils/cn"

  "aliases": {
    "components": "@/components",
    "utils": "@utils/cn",
  },

infectiousstupidity avatar May 23 '25 05:05 infectiousstupidity