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

[turborepo] Using turbo scripts with lint-staged, .husky and ESLint #3364

Closed
pt232 opened this issue Jan 18, 2023 · 6 comments
Closed

[turborepo] Using turbo scripts with lint-staged, .husky and ESLint #3364

pt232 opened this issue Jan 18, 2023 · 6 comments
Labels
kind: bug Something isn't working

Comments

@pt232
Copy link

pt232 commented Jan 18, 2023

What version of Turborepo are you using?

1.7.0

What package manager are you using / does the bug impact?

npm

What operating system are you using?

Windows

Describe the Bug

I would like to use turbo scripts (like turbo run lint) in combination with lint-staged. However, I get the following error message when linting:

 npm run lint:
 ERROR run failed: task `{path-to-file}/apps/web/pages/index.tsx` not found in `pipeline` in "turbo.json". Are you sure you added it?

index.tsx in this case is the file where I made changes for the commit.

If I add the changed file to the turbo.json file, then it works. But I guess that's not the point. 😀 Maybe this problem is also related to lint-staged rather than turborepo.

By the way: other npm scripts (like npm run format) run without errors.

Thanks for your help!

Expected Behavior

I would have expected to be able to run simple turbo scripts. But maybe the whole thing is not possible at all.

To Reproduce

Run the following commands to reproduce this issue:

  1. npx create-turbo@latest
Where would you like to create your turborepo? ./my-turborepo
Which package manager do you want to use? npm
  1. Open the project in VS Code
  2. npx husky-init && npm install
  3. npm install --save-dev lint-staged
  4. Add npx lint-staged --concurrent false to the .husky/pre-commit hook
  5. touch .lintstagedrc
{
  "*.{js,ts,tsx}": ["npm run format", "npm run lint"],
  "*.json": ["npm run format"]
}
  1. Make some changes inside a workspace
  2. Create a commit
  3. Get the following error:
✔ Preparing lint-staged...
❯ Running tasks for staged files...
  ❯ .lintstagedrc — 2 files

✖ npm run lint:
 ERROR  run failed: task `{path-to-file}/apps/web/pages/index.tsx` not found in `pipeline` in "turbo.json". Are you sure you added it?

> my-turborepo@0.0.0 lint
> turbo run lint {path-to-file}/apps/web/pages/index.tsx

Turbo error: task `{path-to-file}/apps/web/pages/index.tsx` not found in `pipeline` in "turbo.json". Are you sure you added it?
❯ *.{js,ts,tsx} — 1 file
      ✔ npm run format
      ✖ npm run lint [FAILED]
    ◼ *.json — 0 files
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...
✔ Preparing lint-staged...
❯ Running tasks for staged files...
  ❯ .lintstagedrc — 2 files
    ❯ *.{js,ts,tsx} — 1 file
      ✔ npm run format
      ✖ npm run lint [FAILED]
    ◼ *.json — 0 files
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...
husky - pre-commit hook exited with code 1 (error)

I have the following package.json:

{
  "name": "my-turborepo",
  "version": "0.0.0",
  "private": true,
  "workspaces": [
    "apps/*",
    "packages/*"
  ],
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev --parallel",
    "lint": "turbo run lint",
    "format": "prettier --write \"**/*.{ts,tsx,md}\"",
    "prepare": "husky install"
  },
  "devDependencies": {
    "eslint-config-custom": "*",
    "husky": "^8.0.0",
    "lint-staged": "^13.1.0",
    "prettier": "latest",
    "turbo": "latest"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "packageManager": "npm@9.2.0"
}

And the following turbo.json:

{
  "$schema": "https://turbo.build/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false
    }
  }
}

Reproduction Repo

No response

@pt232 pt232 added area: turborepo kind: bug Something isn't working labels Jan 18, 2023
@mehulkar
Copy link
Contributor

mehulkar commented Jan 19, 2023

I'm guessing that lint-staged is calling npm run lint and passes it some file arguments. Since npm run lint calls turbo run lint, this argument is getting passed directly, becoming:

turbo run lint {path-to-file}/apps/web/pages/index.tsx`

turbo interprets that last argument as a task name, which it isn't.

Try changing your .lintstagedrc file to:

{
  "*.{js,ts,tsx}": ["npm run format", "npm run lint --"],
  "*.json": ["npm run format"]
}

(notice the extra -- after npm run lint)

Ultimately you're trying to produce this command: turbo run lint -- <path>

https://turbo.build/repo/docs/reference/command-line-reference#turbo-run-task

@pt232
Copy link
Author

pt232 commented Jan 19, 2023

Great, now it works. Adding the -- directly to the lint-staged config file didn't work out, but I added it to the package.json as follows:

{
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev --parallel",
-    "lint": "turbo run lint",
+    "lint": "turbo run lint --",
    "format": "prettier --write \"**/*.{ts,tsx,md}\"",
    "prepare": "husky install"
  },
}

@pt232 pt232 closed this as completed Jan 19, 2023
@mehulkar
Copy link
Contributor

Great! Glad it worked out @pt232!

@gyhyfj
Copy link

gyhyfj commented Apr 9, 2023

Are you all sure it work? It does not throw error now, BUT the range is not scoped as lint-staged, it's command effect on ALL files in you moorepo!
I just modified 3 files in my monorepo project, but the lint-staged with prettier cost almost 10 mins!

@gyhyfj
Copy link

gyhyfj commented Apr 9, 2023

And what's worse, these config and ignore files in every project of monorepo cannot effect on their own project now

@lbennett-stacki
Copy link

Can confirm this wont fix, will behave the same as calling your lint script directly from the git hook without lint-staged

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

No branches or pull requests

4 participants