vercel / turborepo

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

[turborepo] Not able to use lint-staged properly #4445

Closed PixeledCode closed 1 year ago

PixeledCode commented 1 year ago

What version of Turborepo are you using?

1.8.8

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

Yarn v1

What operating system are you using?

Mac

Describe the Bug

The bug is related to #3364. Although it worked there, in my case, I am getting some other error after following the steps. error An unexpected error occurred: "Cannot create property '-node-bit-dev' on string '{\"-node-bit-dev\":\"\"}'".

Repo: https://github.com/CivicDataLab/opub-mono.git

Full Error:

➜  opub-mono git:(dev) ✗ git commit -m "lint test"
yarn run v1.22.19
$ /Users/pixeled/dev/opub-mono/node_modules/.bin/lint-staged
✔ Preparing lint-staged...
✔ Hiding unstaged changes to partially staged files...
❯ Running tasks for staged files...
  ❯ package.json — 2 files
    ❯ *.{js,jsx,ts,tsx} — 1 file
      ✖ yarn lint [FAILED]
      ◼ yarn format
↓ Skipped because of errors from tasks. [SKIPPED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...

✖ yarn lint:
web:lint: ERROR: command finished with error: command (/Users/pixeled/dev/opub-mono/apps/web) yarn run lint -- /Users/pixeled/dev/opub-mono/apps/web/pages/_app.tsx exited (1)
command (/Users/pixeled/dev/opub-mono/apps/web) yarn run lint -- /Users/pixeled/dev/opub-mono/apps/web/pages/_app.tsx exited (1)
 ERROR  run failed: command  exited (1)
error Command failed with exit code 1.
$ turbo run lint -- /Users/pixeled/dev/opub-mono/apps/web/pages/_app.tsx
• Packages in scope: @opub-cdl/ui, docs, eslint-config-custom, tsconfig, web
• Running lint in 5 packages
• Remote caching disabled
docs:lint: cache miss, executing c1335871bee84900
@opub-cdl/ui:lint: cache miss, executing 10e8c911e177e152
web:lint: cache miss, executing 381c409bd867d3a1
error An unexpected error occurred: "Cannot create property '-node-bit-dev' on string '{\"-node-bit-dev\":\"\"}'".
info If you think this is a bug, please open a bug report with the information provided in "/Users/pixeled/dev/opub-mono/apps/web/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error An unexpected error occurred: "Cannot create property '-node-bit-dev' on string '{\"-node-bit-dev\":\"\"}'".
info If you think this is a bug, please open a bug report with the information provided in "/Users/pixeled/dev/opub-mono/apps/docs/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error An unexpected error occurred: "Cannot create property '-node-bit-dev' on string '{\"-node-bit-dev\":\"\"}'".
info If you think this is a bug, please open a bug report with the information provided in "/Users/pixeled/dev/opub-mono/packages/opub-ui/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

 Tasks:    0 successful, 3 total
Cached:    0 cached, 3 total
  Time:    1.223s 

info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - pre-commit hook exited with code 1 (error)
// package.json
{
  "name": "turbo",
  "version": "1.7.4",
  "private": true,
  "workspaces": [
    "apps/*",
    "packages/*"
  ],
  "scripts": {
    "build": "turbo run build",
    "postbuild": "turbo run postbuild",
    "dev": "turbo run dev",
    "lint": "turbo run lint --",
    "lint:fix": "turbo run lint:fix",
    "format": "prettier --write",
    "test": "turbo run test --filter @opub-cdl/ui",
    "test:watch": "turbo run test:watch",
    "prepare": "husky install"
  },
  "devDependencies": {
    "@changesets/cli": "^2.26.1",
    "@typescript-eslint/eslint-plugin": "^5.57.1",
    "@typescript-eslint/parser": "^5.57.1",
    "classnames": "^2.3.2",
    "eslint-config-custom": "*",
    "husky": "^8.0.3",
    "lint-staged": "^13.2.0",
    "prettier": "^2.8.4",
    "sass": "^1.58.3",
    "tsup": "^6.7.0",
    "turbo": "^1.8.8"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "packageManager": "yarn@1.22.17",
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "yarn lint",
      "yarn format"
    ]
  },
  "dependencies": {}
}
// turbo.json
{
  "$schema": "https://turbo.build/schema.json",
  "globalDependencies": ["**/.env.*local"],
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**", ".next/**", "storybook-static/**"]
    },
    "postbuild": {
      "dependsOn": ["^build"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false
    },
    "test": { "dependsOn": ["^build"] },
    "test:watch": {
      "cache": false
    }
  }
}

Expected Behavior

I should be able to use husky with lint-staged

To Reproduce

  1. Clone: https://github.com/CivicDataLab/opub-mono.git
  2. cd opub-mono && yarn
  3. Add "yarn lint" in lint-staged in package.json <- This will break commit
  4. Change some file code
  5. Try to commit the changes

Reproduction Repo

https://github.com/CivicDataLab/opub-mono.git

gsoltis commented 1 year ago

I was not able to reproduce this with turbo@1.9.1 (latest as of today).

Can you try with that version and we can reopen if it's still broken?

gsoltis commented 1 year ago

I think I know what's going on here: lint-staged is passing the list of changed files to the yarn lint script. This script is not configured in such a way to handle individual files, as it is fanning out to running lint in each workspace. In this case I would configure lint-staged to use eslint directly, which can accept individual file paths.

Kureyko commented 1 year ago

I have same issue with Jest. Root package.json:

"lint-staged": {
    "*.{js,ts,tsx}": [
      "eslint --fix",
      "turbo run test"
    ]
  },

And have packages/ui/package.json:

"test": "jest",

But "lint-staged" passes modified file name as an argument and tries to run task "test" in that file:

turbo run test:
 ERROR  run failed: error preparing engine: Could not find the following tasks in project: /dev/turborepo-demo/packages/ui/src/Button/Button.tsx
Turbo error: error preparing engine: Could not find the following tasks in project: /dev/turborepo-demo/packages/ui/src/Button/Button.tsx

My turbo.json:

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

Current fix is adding --cwd:

"turbo run test --cwd=./packages/ui -- --findRelatedTests --bail"