Lint-staged: error TS5042: Option 'project' cannot be mixed with source files on a command line.

Created on 3 Apr 2020  Â·  4Comments  Â·  Source: okonet/lint-staged

Description

I'm new to the whole lint-staged thing so please bear with me. As part of the pre-commit step, we've configured lint-staged to only lint staged files as follows:
package.json

  "scripts": {
    "typecheck": "tsc",
     "prettier:commit": "prettier --check",
      ...
   }
...
  "lint-staged": {
    "*.{ts,tsx,js}": [
      "yarn prettier:commit",
      "yarn typecheck && yarn tslint -p ./"
    ]
  },

However, when we attempt a git commit this error occurs and I'm not too sure why.
error TS5042: Option 'project' cannot be mixed with source files on a command line.

Any help on this would be very much appreciated.

We have a tsconfig.json and tslint.json file in the root directory as well but I'm not sure of any of these files could be causing us this issue.

Steps to reproduce

  • Make a change in the code base
  • run git add
  • run git commit

Acutal

  • yarn prettier:commit succeeds
  • yarn typecheck && yarn tslint -p ./ fails and logs the following message: "found some errors. Please fix them and try committing again"
  • error TS5042: Option 'project' cannot be mixed with source files on a command line.

Expected

  • Both yarn prettier:commit and yarn typecheck && yarn tslint -p ./ succeed without any errors

Debug Logs

expand to view

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/foo/Desktop/me/repos/project/package.json:
lint-staged {
lint-staged '.{ts,tsx,js}': [ 'yarn typecheck && yarn tslint -p ./', 'yarn prettier:commit' ]
lint-staged } +3ms
lint-staged:cfg Normalizing config +0ms
lint-staged:cfg Validating config +2ms
Running lint-staged with the following config:
{
linters: {
'
.{ts,tsx,js}': [
'yarn typecheck && yarn tslint -p ./',
'yarn prettier:commit'
]
},
concurrent: true,
chunkSize: 9007199254740991,
globOptions: {
matchBase: true,
dot: true
},
ignore: [],
subTaskConcurrency: 1,
renderer: 'verbose'
}
lint-staged:run Running all linter scripts +0ms
lint-staged:run Resolved git directory to be /Users/foo/Desktop/me/repos/project +1ms
lint-staged:run Loaded list of staged files in git:
lint-staged:run [
lint-staged:run 'src/client/components/Component/Component.tsx'
lint-staged:run ] +25ms
lint-staged:gen-tasks Generating linter tasks +0ms
lint-staged:cfg Normalizing config +32ms
lint-staged:gen-tasks Generated task:
lint-staged:gen-tasks {
lint-staged:gen-tasks pattern: '*.{ts,tsx,js}',
lint-staged:gen-tasks commands: [ 'yarn typecheck && yarn tslint -p ./', 'yarn prettier:commit' ],
lint-staged:gen-tasks fileList: [
lint-staged:gen-tasks '/Users/foo/Desktop/me/repos/project/src/client/components/Component/Component.tsx'
lint-staged:gen-tasks ]
lint-staged:gen-tasks } +12ms
Running tasks for *.{ts,tsx,js} [started]
lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'yarn typecheck && yarn tslint -p ./', 'yarn prettier:commit' ] +0ms
lint-staged:find-bin Resolving binary for command yarn typecheck && yarn tslint -p ./ +246ms
lint-staged:find-bin Binary for yarn typecheck && yarn tslint -p ./ resolved to /usr/local/bin/yarn +2ms
lint-staged:task ✔ OS: darwin; File path chunking unnecessary +0ms
lint-staged:find-bin Resolving binary for command yarn prettier:commit +0ms
lint-staged:find-bin Resolving binary for yarn from cache +0ms
lint-staged:task ✔ OS: darwin; File path chunking unnecessary +0ms
yarn typecheck && yarn tslint -p ./ [started]
lint-staged:task bin: /usr/local/bin/yarn +1ms
lint-staged:task args: [
lint-staged:task 'typecheck',
lint-staged:task '&&',
lint-staged:task 'yarn',
lint-staged:task 'tslint',
lint-staged:task '-p',
lint-staged:task './',
lint-staged:task '/Users/foo/Desktop/me/repos/project/src/client/components/Component/Component.tsx'
lint-staged:task ] +0ms
lint-staged:task opts: { reject: false } +0ms
yarn typecheck && yarn tslint -p ./ [failed]
→
Running tasks for *.{ts,tsx,js} [failed]
→
✖ "yarn typecheck && yarn tslint -p ./" found some errors. Please fix them and try committing again.

Environment

  • OS: macOS Mojave v10.14.6
  • Node.js: v12.16.1
  • lint-staged: v^7.3.0
  • Yarn: v1.22.4
  • tsc: v3.8.3
  • tslint: v5.11.0
  • prettier: v1.18.2
  • IDE: VSCode

Most helpful comment

I generate a temporary tsconfig.json for lint, this config file includes all staged files, don't forget ignore this file in .gitignore

// .lintstagedrc.js
const fs = require('fs');

const generateTSConfig = stagedFilenames => {
  const tsconfig = JSON.parse(fs.readFileSync('tsconfig.json', 'utf8'));
  tsconfig.include = stagedFilenames;
  fs.writeFileSync("tsconfig.lint.json", JSON.stringify(tsconfig));
  return "tsc --noEmit --project tsconfig.lint.json";
};
module.exports = {
  "*.{js,jsx,ts,tsx}": [
    "eslint --ext .js,.jsx,.ts,.tsx --fix",
    generateTSConfig
  ]
}

All 4 comments

You specify a project file with -p ./, and lint-staged appends all staged files as the input to that, so it looks like tslint -p ./ <file1> ... <fileN>. I guess the error means you can't specify both.

@OriginUnknown I ran into the same issue. The solution is to make the tsc command a function. This way, the command is only run once and the individual file names aren't appended as @iiroj alluded to.

Try creating a lint-staged.config.js file. It should look something like this:

module.exports = {
  '*.{js,ts,tsx}': 'yarn prettier:commit',
  '*.{ts,tsx}': () => 'yarn typecheck && yarn tslint'
}

I generate a temporary tsconfig.json for lint, this config file includes all staged files, don't forget ignore this file in .gitignore

// .lintstagedrc.js
const fs = require('fs');

const generateTSConfig = stagedFilenames => {
  const tsconfig = JSON.parse(fs.readFileSync('tsconfig.json', 'utf8'));
  tsconfig.include = stagedFilenames;
  fs.writeFileSync("tsconfig.lint.json", JSON.stringify(tsconfig));
  return "tsc --noEmit --project tsconfig.lint.json";
};
module.exports = {
  "*.{js,jsx,ts,tsx}": [
    "eslint --ext .js,.jsx,.ts,.tsx --fix",
    generateTSConfig
  ]
}

I'll close this issue, but it would be nice to have a PR with an example addition to the README.

Was this page helpful?
0 / 5 - 0 ratings