Giter Site home page Giter Site logo

Comments (9)

imhoffd avatar imhoffd commented on June 7, 2024 1

I was able to get the automatic cwd stuff working by having a config in the root of the monorepo and a config for each package as described in the readme.

However, the config in the root actually needs rules in it, otherwise lint-staged exits silently with exit code 1.

In other words, it appears you need something like this if you plan to use lint-staged in a monorepo but don't want any base rules for the monorepo root itself:

./lint-staged.config.cjs

module.exports = {
  '*': 'true'
}

./packages/thing-1/lint-staged.config.cjs

module.exports = {
  '*.{cjs,mjs,js,jsx,ts,tsx}': ['eslint --fix', 'prettier --write'],
}

from lint-staged.

phuczeero avatar phuczeero commented on June 7, 2024

Updated. Because I’m using shared eslint config which resolves tsconfig relative to process.cwd() (details here).
However, I'm still looking for a way to specify the working dir per .lintstagedrc.js in each package.

Feel free to close this thread if it does not make sense, thanks.

from lint-staged.

iiroj avatar iiroj commented on June 7, 2024

By default monorepo tasks run with the specific config file directory as the root. The npx lint-staged --cwd <some-directory> can be used to override this for all tasks. Would that work for you?

For example, to run all tasks from the monorepo root, probably:

npx lint-staged --cwd $PWD

from lint-staged.

phuczeero avatar phuczeero commented on June 7, 2024

Yup, that works but if using lint-staged with husky, will I have to supply multiple lint-staged commands, so any process.cwd() used inside each package will result in the correct value?

// pre-commit
npx lint-staged --cwd packages/frontend
npx lint-staged --cwd packages/backend

from lint-staged.

iiroj avatar iiroj commented on June 7, 2024

If you have multiple configs, in packages/frontend and packages/backend for example, the commands in those should already use those directories as CWD for the tasks. Is this not the case?

from lint-staged.

phuczeero avatar phuczeero commented on June 7, 2024

You are correct.
I previously added an empty config at root, follow the monorepo setup document "you can either add an "empty" lint-staged configuration to the root of the repo". When running lint-staged at root, I found process.cwd() resulting the root dir, though there are lint-staged config in each package.
Then update the root config from empty to @imhoffd suggestion and now it works correctly.

from lint-staged.

iiroj avatar iiroj commented on June 7, 2024

Is there something you think could be fixed, or at least the README updated?

from lint-staged.

imhoffd avatar imhoffd commented on June 7, 2024

Hi @iiroj, thanks for your work on lint-staged. I don't think there are any active bugs in this issue. Here are a few takeaways, if it helps:

  1. With an empty root config (e.g. module.exports = {}), lint-staged exits silently with exit code 1. This could probably be improved with an error message if the intention is to require at least one rule.
  2. If the intention is to require at least one rule, I would question that. If a root config is necessary, then an empty root config should indicate to lint-staged that no linters need to run. Right now, I have the rule '*': 'true' in my root config because I don't need to run any linters on the monorepo root (yet), so it literally runs the true built-in bash command for all files--I don't see a better way to do this currently.
  3. If a root config is necessary, I would further question that. lint-staged appears to correctly identify the root of the monorepo as well as the closest config file. Therefore, a missing root config should indicate to lint-staged that no linters need to run.

Basically, I expected to be able to install lint-staged in the root of my monorepo and add a single lint-staged config file in a package's subdirectory, but that ended up not working without doing some investigation. I was surprised that the root config file was necessary at all.

Thanks again!

from lint-staged.

iiroj avatar iiroj commented on June 7, 2024

Thanks! The root config itself is not strictly necessary, but in order to activate the "monorepo features", you need at least two valid configuration files. This is because of backwards-compatibility reasons, as lint-staged has supported the (single) configuration file being in a subdirectory of a git repo for a longer time.

Thus, having an "empty config" in the root is a workaround for enabling monorepo features with a single configuration file in one of monorepo's subdirectories.

from lint-staged.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.