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

Task working directoy when multiple configs #1090

Closed
chengcyber opened this issue Jan 19, 2022 · 7 comments · Fixed by #1091
Closed

Task working directoy when multiple configs #1090

chengcyber opened this issue Jan 19, 2022 · 7 comments · Fixed by #1091

Comments

@chengcyber
Copy link

Description

For now, the cwd for each task is process.cwd() in most cases. but ideally the working directory should be the directory of configFilePath.

Expecting behaviour

For example,

- monorepo
  |- packageA
    |- index.js
    |- .eslintrc.json
    |- .lintstagedrc.json

and packageA/.lintstagedrc.json

{
  "*.js": "eslint --fix"
}

Assuming packageA/index.js is stage file. In this case, what i really want is run eslint --fix packageA/index.js under packageA instead of monorepo root folder. Because eslint need to load packageA/.eslintrc.json

Code detail

https://github.com/okonet/lint-staged/blob/36b9546dda5ca24174b519ce6d132f31077b093b/lib/runAll.js#L148-L151

may change to

          makeCmdTasks({
            commands: task.commands,
            cwd: path.dirname(configPath),  // <---- each cmd task runs under the same folder of the related config file

Environment

  • OS:
  • Node.js:
  • lint-staged: 12.2.1
@iiroj
Copy link
Member

iiroj commented Jan 19, 2022

I'm not sure what the use-case has been for the current logic, but this would be a breaking change if cwd is not the same as where the (most typical, single) lint-staged config file is.

@iiroj
Copy link
Member

iiroj commented Jan 19, 2022

More specifically, it breaks this test, because even if cwd is test/, the file path will still be test/foo.js instead of foo.js:

https://github.com/okonet/lint-staged/blob/36b9546dda5ca24174b519ce6d132f31077b093b/test/runAll.spec.js#L262-L295

@iiroj
Copy link
Member

iiroj commented Jan 19, 2022

Further, it's possible to supply lint-staged with a path to a config, eg. lint-staged --config /some/totally/different/lint-staged.config.json, which would then also break.

@iiroj
Copy link
Member

iiroj commented Jan 19, 2022

Maybe a new setting like --cwd-from-config could handle this, PR is welcome!

@iiroj
Copy link
Member

iiroj commented Jan 19, 2022

I figured it might not have to be a breaking change if it only applies when multiple config files are detected: #1091

@chengcyber
Copy link
Author

chengcyber commented Jan 20, 2022

Hi @iiroj , thanks for your quick reply! The idea i wanna show is the cwd property for each task is critical in monorepo use cases. I am open if other args should be passed like cwdFromConfig, or it is easier to use when it is a default behavior.

@pkerschbaum
Copy link

Until #1091 is merged, this workaround can help (seems to work in my case):

packageA/lint-staged.config.js:

module.exports = {
  '**/*.{ts,tsx}': [`yarn --cwd "${__dirname}" eslint --max-warnings 0`],
};

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

Successfully merging a pull request may close this issue.

3 participants