Zyncho
Zyncho

Reputation: 447

lint-staged custom config > error Command failed with exit code 1

I'm following the first example (copy/paste) of custom config file (lint-staged.config.js) for lint-staged packaged from its github README without success. I get error Command failed with exit code 1. always.

I tried this...

I have tried three things, for each case I had my lint-staged.config.js in the root directory.

  1. package.json: result is error Command failed with exit code 1.
"lint-staged": {
        "packages/**/*.{ts,tsx}": [
            "yarn lint-staged --config ./lint-staged.config.js"
        ]
    },
  1. husky/pre-commit: result is error Command failed with exit code 1.
npx lint-staged --config ../lint-staged.config.js
  1. cmd line: result is error Command failed with exit code 1.
yarn lint-staged --config lint-staged.config.js

The problem

Im just looking for run a custom config file.

The problem is that the execution fails, the error message its related to the command but the command itself its correct as lint-staged [options] (yarn/npx lint-staged -h) then to provide a custom config file it would as lint-staged [--config [path]] but it fails (I even provide all kind of quotes for path).

Upvotes: 5

Views: 5088

Answers (1)

Zyncho
Zyncho

Reputation: 447

The issue is that when the module doesn't provide an explicit positive answer to the validation it will always return error Command failed with exit code 1 meaning that the validation has fail.

To properly work as expected it should, in my case:

  1. first, the module had to have a return.
  2. Secondly it should be in form of string array.
  3. Third, the first string of the array had to be terminal-like response as '0' or 'true' of 'false'.

Then, the next following strings could be a message or messages like 'error some A' and 'error some B' and 'error some C' and so on...

For example: ['0', 'error some A', 'error some B', 'error some C']

const path = require("path");
module.exports = (absolutePaths) => {
  const cwd = process.cwd();
  const relativePaths = absolutePaths.map((file) => path.relative(cwd, file));
  console.log("query", relativePaths)
  return ['0', 'error some A', 'error some B', 'error some C']
};

This runs ok, but as Andrey Mikhaylov said in this post to run something like

"lint-staged": {
        "packages/**/*.{ts,tsx}": [
            "yarn lint-staged --config ./lint-staged.config.js"
        ]
    },

If the lint returns an error, It will blow away the staged files causing a regression that will drop the commit completely, which means that all the work will be lost.

I fix this not intended/desired behaviour running the same command yarn lint-staged --config ./lint-staged.config.js but from husky at the pre-commit file as

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged --config ./lint-staged.config.js`

Upvotes: 1

Related Questions