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

[BUG] The plugin does not pick up tailwind configs in projects nested within monorepos #241

Open
quesabe opened this issue May 4, 2023 · 16 comments · May be fixed by #380
Open

[BUG] The plugin does not pick up tailwind configs in projects nested within monorepos #241

quesabe opened this issue May 4, 2023 · 16 comments · May be fixed by #380
Labels
bug Something isn't working

Comments

@quesabe
Copy link
Contributor

quesabe commented May 4, 2023

Describe the bug
When a frontend project is nested within a monorepo, any linting process run not from the frontend folder does not see the tailwind config. This is especially apparent with VSCode intellisense - the eslint process is run at the project root level, while the tailwind config resides somewhere nested. Since the config is not found the plugin des not pick up any custom colours and other stuff.

To Reproduce
Assumed folder structure:

/rootProject
├ package.json
├ /frontend
│  ├ .eslintrc.js 
│  ├ package.json
│  ├ tailwind.config.js
  1. When eslint is run from rootProject folder the tailwind.config.js is not found.
  2. When eslint is run from frontend folder the tailwind.config.js is found.
  3. eslint process in VSCode is run from rootProject and thus does not find the tailwind.config.js.

BTW, the eslint config is picked up and the rules from eslint-plugin-tailwindcss apply - they just don't use the custom theme and other settings.

Expected behavior
Tailwind config is discoverable as long as it is in the same folder with the eslint config.

Environment (please complete the following information):

Additional context
Seems like this code is the reason for the behaviour:
https://github.com/francoismassart/eslint-plugin-tailwindcss/blob/master/lib/util/customConfig.js#L26

The path.resolve() resolves to the rootProject folder, while the config is at rootProject/frontend.

PS. I can make it work if I open the frontend folder in a separate editor window. However it is very inconvenient in a large project.

@quesabe quesabe added the bug Something isn't working label May 4, 2023
@Gomah
Copy link

Gomah commented May 8, 2023

Do you mind sharing your current .eslintrc.js file?

I just fixed a similar issue and this worked for me:

// .eslintrc.js
settings: {
  tailwindcss: {
    config: path.join(__dirname, './tailwind.config.js'),
  },
},

@av-erencelik
Copy link

I can confirm @Gomah 's solution works.

@quesabe
Copy link
Contributor Author

quesabe commented May 13, 2023

Do you mind sharing your current .eslintrc.js file?

I just fixed a similar issue and this worked for me:

// .eslintrc.js
settings: {
  tailwindcss: {
    config: path.join(__dirname, './tailwind.config.js'),
  },
},

It's fairly simple:

{
  "env": {
    "browser": true,
    "jest": true,
    "node": false
  },
  "plugins": [
    "tailwindcss"
  ],
  "rules": {
    "tailwindcss/classnames-order": [
      "error"
    ],
    "tailwindcss/enforces-negative-arbitrary-values": [
      "error"
    ],
    "tailwindcss/enforces-shorthand": [
      "error"
    ],
    "tailwindcss/migration-from-tailwind-2": [
      "error"
    ],
    "tailwindcss/no-arbitrary-value": [
      "off"
    ],
    "tailwindcss/no-contradicting-classname": [
      "error"
    ],
    "tailwindcss/no-custom-classname": [
      "error",
    ]
  },
  "settings": {
    "tailwindcss": {
      "callees": [
        "cn"
      ],
      "config": "tailwind-config.js"
    }
  }
}

In fact I can't use the workaround, since I use a JSON config.

@francoismassart
Copy link
Owner

@quesabe did your commit provide a solution for your issue with JSON config ?
@Gomah , @av-erencelik & @quesabe Can we close this issue ? It looks like you found a workaround...

@quesabe
Copy link
Contributor Author

quesabe commented Jun 7, 2023

@quesabe did your commit provide a solution for your issue with JSON config ?
@Gomah , @av-erencelik & @quesabe Can we close this issue ? It looks like you found a workaround...

@francoismassart
Actually no, the commit does not solve the current issue completely. It helps in my case, where I can get rid of the custom config. But if anybody needs to use a json eslint config, refer to a custom tailwind config, and all this done not in the root folder of a project, then it still won't load the tailwind config properly.

However I'm not sure if it has a solution within the plugin repo - maybe it's a problem with eslint itself.

@lachieh
Copy link

lachieh commented Jun 30, 2023

After doing some digging, it looks like the vscode-eslint extension is not passing the context object to the rules when running from a higher level folder. You can confirm this yourself by being in your root project folder and running npx eslint ./your-package where your-package is where the eslint and tailwind config files are and it will lint the whole project without issue.

Apparently the solution is the eslint.workingDirectories setting in the workspace .vscode/settings.json. The following config should fix the problem.

// .vscode/settings.json
{
  "eslint.workingDirectories": ["./your-package"],
}

@maslennikov
Copy link

@lachieh Your suggested workaround with custom .vscode/settings.json wouldn't be practical for teams working on same monorepo, since custom vscode settings do not normally belong to codebase committed to git.

@lachieh
Copy link

lachieh commented Jul 18, 2023

@lachieh Your suggested workaround with custom .vscode/settings.json wouldn't be practical for teams working on same monorepo, since custom vscode settings do not normally belong to codebase committed to git.

@maslennikov I didn't actually say that this file should be committed. I just provided a solution for correctly configuring the eslint extension.

Whether it is practical or not is subjective and committing this file is a team decision. If your team has decided that shared workspace settings should actually not be shared, then perhaps you can find and suggest a different solution instead of just spreading your (unsolicited) opinion.

For example, a common pattern is to add the .vscode/settings.json file in .gitignore but include shared settings in a .vscode/settings.default.json so that developers can copy required settings and customize the settings themselves.

@jrozbicki
Copy link

Gomah thanks for the suggestion! Works 🚀

@nodegin
Copy link

nodegin commented Sep 12, 2023

@quesabe Thank you for the solution! It was working for me

@yossydev
Copy link

yossydev commented Nov 8, 2023

I was also facing this problem in a Monorepo environment using Turborepo, but I tried the contents of microsoft/vscode-eslint#1706 (comment) I tried the contents of and it worked fine.

@joshuajaco
Copy link

joshuajaco commented Dec 12, 2023

I think the only way to solve this reliably is by searching for the config based on the location of the file that is being linted.
Similar to what typescript-eslint is doing now, see: https://typescript-eslint.io/blog/parser-options-project-true/#introducing-true

@mamlzy
Copy link

mamlzy commented Jan 16, 2024

Do you mind sharing your current .eslintrc.js file?

I just fixed a similar issue and this worked for me:

// .eslintrc.js
settings: {
  tailwindcss: {
    config: path.join(__dirname, './tailwind.config.js'),
  },
},

it works in monorepo thank you soo much!!

@DigitalNaut
Copy link

DigitalNaut commented Aug 13, 2024

Do you mind sharing your current .eslintrc.js file?

I just fixed a similar issue and this worked for me:

// .eslintrc.js
settings: {
  tailwindcss: {
    config: path.join(__dirname, './tailwind.config.js'),
  },
},

This works but I'm not using a monorepo. I'm using the base Vite template from pnpm create vite.

This worked for me when my pnpm lint script was complaining that __dirname is not defined in ES module scope

  tailwindcss: {
    config: path.join(import.meta.dirname, 'tailwind.config.js'),
  }

@jtwozniak
Copy link

jtwozniak commented Oct 27, 2024

After trying some suggestions I found that vs code eslint run can be detected by checking if it is started from root dir

const isVsEslintRun = !process.env.PWD.includes("/apps/");

Then in specific package setup after adding below it works for VsEslint and when running script command turbo lint as turbo runs it from specific apps directories

{
    files: ["apps/web/**/*.{ts,tsx,mts,cts,js,jsx}"],
    settings: {
      tailwindcss: {
        config: isVsEslintRun ? "./apps/web/tailwind.config.ts" : undefined,
      },
    },
  },

@colinscz
Copy link

Just for people landing here who have a *.json eslint configuration and are working with a monorepo tool like Nx and wonder how to get this working.

In my case I resolved it by referencing the tailwind-preset library in the monorepo top-level file .eslintrc.json.
I still have an issue with the configuration of the IntelliJ EsLint plugin but for the command execution it looks fine.
This section in my top-level file looks like this:

{
  "root": true,
  "ignorePatterns": ["**/*"],
  "plugins": ["@nx"],
  "settings": {
    "tailwindcss": {
      "config": "./libs/tailwind-preset/tailwind.config.js"
    }
  },
...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet