vercel / turborepo

Build system optimized for JavaScriptΒ and TypeScript, written in Rust
https://turbo.build/repo/docs
MIT License
26k stars 1.79k forks source link

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

Closed pt232 closed 1 year ago

pt232 commented 1 year ago

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

mehulkar commented 1 year ago

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 commented 1 year ago

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"
  },
}
mehulkar commented 1 year ago

Great! Glad it worked out @pt232!

gyhyfj commented 1 year ago

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 commented 1 year ago

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

lbennett-stacki commented 1 year ago

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

Eliot00 commented 2 weeks ago

Added -- but still not work with bun 1.1.20