Lint-staged: Can't find staged files matching glob, but does find when not on pre-commit

Created on 26 May 2019  Β·  6Comments  Β·  Source: okonet/lint-staged

Description

I stage JavaScript files that match my lint-staged glob with git add. When I git commit I expect prettier and eslint to run. Instead I get the "No staged files match any of provided globs" message.

However if I run lint-staged directly from node-modules, without triggering a precommit, then prettier and eslint execute and find issues (as expected).

Steps to reproduce

package.json

{
  ...
  "scripts": {
    ...
    "lint": "eslint '**/*.js'",
    "lint:fix": "eslint '**/*.js' --fix",
    "pretty": "prettier --check '**/*.{js,json}'",
    "pretty:fix": "prettier --write '**/*.{js,json}'",
    "precommit": "lint-staged",
    "postcommit": "git update-index --again"
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    "husky": "^2.3.0",
    "lint-staged": "^8.1.7"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "linters": {
      "**/*.{js,json}": [
        "prettier --write",
        "git add"
      ],
      "**/*.js": [
        "eslint",
        "git add"
      ]
    }
  }
}

Debug Logs

When running on precommit:

14:43 $ git add .; git commit -m "some message"
husky > pre-commit (node v10.15.3)
No staged files match any of provided globs.
[feature/automate-lint-format 254e671] format and lint
 2 files changed, 40 insertions(+), 38 deletions(-)
 rewrite etl/test/integration/mycomponent.test.js (72%)  # the file was not changed tho πŸ€”

When running directly:

expand to view

14:55 $ ./node_modules/.bin/lint-staged --debug
  lint-staged:bin Running `[email protected]` +0ms
  lint-staged:find-bin Loaded package.json using `process.cwd()` +0ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/Users/dnunes/Git/.../etl/package.json`:
  lint-staged { linters:
  lint-staged    { '**/*.{js,json}': [ 'prettier --write', 'git add' ],
  lint-staged      '**/*.js': [ 'eslint', 'git add' ] } } +8ms
  lint-staged:cfg Normalizing config +0ms
  lint-staged:cfg Validating config +1ms
Running lint-staged with the following config:
{
  linters: {
    '**/*.{js,json}': [
      'prettier --write',
      'git add'
    ],
    '**/*.js': [
      'eslint',
      'git add'
    ]
  },
  concurrent: true,
  chunkSize: 9007199254740991,
  globOptions: {
    matchBase: true,
    dot: true
  },
  ignore: [],
  subTaskConcurrency: 1,
  renderer: 'verbose',
  relative: false
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:run Resolved git directory to be `/Users/dnunes/Git/.../myproject/` +0ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [] +19ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:cfg Normalizing config +28ms
  lint-staged:gen-tasks Generated task: 
  lint-staged:gen-tasks { pattern: '**/*.{js,json}',
  lint-staged:gen-tasks   commands: [ 'prettier --write', 'git add' ],
  lint-staged:gen-tasks   fileList: [] } +0ms
  lint-staged:gen-tasks Generated task: 
  lint-staged:gen-tasks { pattern: '**/*.js',
  lint-staged:gen-tasks   commands: [ 'eslint', 'git add' ],
  lint-staged:gen-tasks   fileList: [] } +1ms
No staged files match any of provided globs.
  lint-staged linters were executed successfully! +33ms
βœ” ~/Git/.../etl [feature/automate-lint-format ↑·11|✚ 2] 
14:56 $ git add .
βœ” ~/Git/.../etl [feature/automate-lint-format ↑·11|●1✚ 1] 
14:56 $ ./node_modules/.bin/lint-staged --debug
  lint-staged:bin Running `[email protected]` +0ms
  lint-staged:find-bin Loaded package.json using `process.cwd()` +0ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/Users/dnunes/Git/.../etl/package.json`:
  lint-staged { linters:
  lint-staged    { '**/*.{js,json}': [ 'prettier --write', 'git add' ],
  lint-staged      '**/*.js': [ 'eslint', 'git add' ] } } +8ms
  lint-staged:cfg Normalizing config +0ms
  lint-staged:cfg Validating config +1ms
Running lint-staged with the following config:
{
  linters: {
    '**/*.{js,json}': [
      'prettier --write',
      'git add'
    ],
    '**/*.js': [
      'eslint',
      'git add'
    ]
  },
  concurrent: true,
  chunkSize: 9007199254740991,
  globOptions: {
    matchBase: true,
    dot: true
  },
  ignore: [],
  subTaskConcurrency: 1,
  renderer: 'verbose',
  relative: false
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:run Resolved git directory to be `/Users/dnunes/Git/.../` +0ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'etl/src/services/common.service.js' ] +20ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:cfg Normalizing config +28ms
  lint-staged:gen-tasks Generated task: 
  lint-staged:gen-tasks { pattern: '**/*.{js,json}',
  lint-staged:gen-tasks   commands: [ 'prettier --write', 'git add' ],
  lint-staged:gen-tasks   fileList:
  lint-staged:gen-tasks    [ '/Users/dnunes/Git/.../etl/src/services/common.service.js' ] } +17ms
  lint-staged:gen-tasks Generated task: 
  lint-staged:gen-tasks { pattern: '**/*.js',
  lint-staged:gen-tasks   commands: [ 'eslint', 'git add' ],
  lint-staged:gen-tasks   fileList:
  lint-staged:gen-tasks    [ '/Users/dnunes/Git/.../etl/src/services/common.service.js' ] } +2ms
Stashing changes... [started]
Stashing changes... [skipped]
β†’ No partially staged files found...
Running linters... [started]
Running tasks for **/*.{js,json} [started]
Running tasks for **/*.js [started]
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'prettier --write', 'git add' ] +0ms
  lint-staged:find-bin Resolving binary for command `prettier --write` +176ms
  lint-staged:find-bin Binary for `prettier --write` resolved to `/Users/dnunes/Git/.../etl/node_modules/.bin/prettier` +3ms
  lint-staged:task βœ”  OS: darwin; File path chunking unnecessary +0ms
  lint-staged:find-bin Resolving binary for command `git add` +0ms
  lint-staged:find-bin Binary for `git add` resolved to `/usr/local/bin/git` +1ms
  lint-staged:task βœ”  OS: darwin; File path chunking unnecessary +2ms
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'eslint', 'git add' ] +6ms
  lint-staged:find-bin Resolving binary for command `eslint` +1ms
  lint-staged:find-bin Binary for `eslint` resolved to `/Users/dnunes/Git/.../etl/node_modules/.bin/eslint` +1ms
  lint-staged:task βœ”  OS: darwin; File path chunking unnecessary +1ms
  lint-staged:find-bin Resolving binary for command `git add` +0ms
  lint-staged:find-bin Resolving binary for `git` from cache +0ms
  lint-staged:task βœ”  OS: darwin; File path chunking unnecessary +0ms
prettier --write [started]
eslint [started]
  lint-staged:task bin: /Users/dnunes/Git/.../etl/node_modules/.bin/prettier +0ms
  lint-staged:task args: [ '--write',
  lint-staged:task   '/Users/dnunes/Git/.../etl/src/services/common.service.js' ] +0ms
  lint-staged:task opts: { reject: false } +6ms
  lint-staged:task bin: /Users/dnunes/Git/.../etl/node_modules/.bin/eslint +9ms
  lint-staged:task args: [ '/Users/dnunes/Git/.../etl/src/services/common.service.js' ] +0ms
  lint-staged:task opts: { reject: false } +0ms
prettier --write [completed]
git add [started]
  lint-staged:task bin: /usr/local/bin/git +296ms
  lint-staged:task args: [ 'add',
  lint-staged:task   '/Users/dnunes/Git/.../etl/src/services/common.service.js' ] +0ms
  lint-staged:task opts: { reject: false, cwd: '/Users/dnunes/Git/.../' } +0ms
git add [completed]
Running tasks for **/*.{js,json} [completed]
eslint [failed]
β†’ 
Running tasks for **/*.js [failed]
β†’ 
Running linters... [failed]

βœ– eslint found some errors. Please fix them and try committing again.

/Users/dnunes/Git/.../etl/src/services/common.service.js
10:3  error  'dasddasdas' is not defined  no-undef
13:3  error  'dadsaad' is not defined     no-undef

βœ– 2 problems (2 errors, 0 warnings)

Environment

  • OS: macOS 10.14.5
  • Node.js: 10.15.3
  • lint-staged: 8.1.7

Most helpful comment

All 6 comments

Try removing **/ from your globs since you’re running in the same directory. There is note about this in the readme.

I also tried that (and it didn't work):

"lint-staged": {
    "linters": {
      "*.{js,json}": [
        "prettier --write",
        "git add"
      ],
      "*.js": [
        "eslint",
        "git add"
      ]
    }
  }

This is really weird because I have the same config on another project/repo, in the same machine, and it works fine.

Please find the difference between 2 projects

I've compared both project's package.json and the lint-staged config is the same and there are no major differences in the rest of the file. Is the problem the glob?

I have a monorepo, with a folder for each component with their own package.json. Below is the folder structure of the project that doesn't work:

.
β”œβ”€β”€ README.md
β”œβ”€β”€ backend
β”‚Β Β  β”œβ”€β”€ package.json <--- I'm here
β”‚Β Β  β”œβ”€β”€ src
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ *.js
β”‚Β Β  β”œβ”€β”€ test
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ integration
β”‚Β Β  β”‚Β Β  β”‚Β Β  β”œβ”€β”€ *.test.js
β”‚Β Β  β”‚Β Β  └── sanity
β”‚Β Β  β”‚Β Β      └── *.test.js
β”œβ”€β”€ frontend
β”‚Β Β  β”œβ”€β”€ package.json  <--- another lint-staged config will be here
β”‚Β Β  β”œβ”€β”€ src
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ *.js
β”‚Β Β  β”œβ”€β”€ ...
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .gitlab-ci.yml

I'm staging and committing files inside the backend folder. Am I using the right glob **/*.{js,json}? I tried etl/**/*.{js,json} but failed as well.

Have you read https://github.com/okonet/lint-staged/blob/master/README.md#filtering-files? It says globs should be relative to package.json. If this isn’t working when I’d need help resolving this. There is also an issue reported recently that might affect you about absolute paths but it doesn’t look like it.

Was this page helpful?
0 / 5 - 0 ratings