Use staged filenames as git references them
See original GitHub issueDescription
git standard for filename conventions is to always provide the git-aware path, and both options of true
and false
for relative
config result in neither of that.
Given the following folder as an example use-case:
| project/
| .git
| server/
| - package.json
| - a.js
When lint-staged
is configured with relative: true
and is triggered from inner server directory, then while the linting command it calls will get the relative a.js
file, when the next spawned command of git add
gets called it has no notion of a.js
and so it fails.
As a solution I’m proposing to add a new config directive, perhaps something like gitOverrideFilename: true
or alike, in order to retain the git standard for filename conventions. I’m happy to consider other alternatives for providing this mechanism which complies with git expectations.
Steps to reproduce
Reproduce the example directory structure provided in the description and run a git commit
from the inner server/
directory.
lint-staged config reference:
"lint-staged": {
"relative": true,
"linters": {
"**/*.js": [
"some-command",
"git add"
]
}
}
Debug Logs
expand to view
husky > pre-commit (node v10.13.0)
lint-staged:bin Running `lint-staged@8.1.6` +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 `/REDACTED/server/package.json`:
lint-staged { relative: true,
lint-staged linters:
lint-staged { '**/*.js':
lint-staged [ 'some-command',
lint-staged 'git add' ] } } +6ms
lint-staged:cfg Normalizing config +0ms
lint-staged:cfg Validating config +1ms
Running lint-staged with the following config:
{
relative: true,
linters: {
'**/*.js': [
'some-command',
'git add'
]
},
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 `/REDACTED/` +1ms
lint-staged:run Loaded list of staged files in git:
lint-staged:run [ 'server/a.js' ] +26ms
lint-staged:gen-tasks Generating linter tasks +0ms
lint-staged:cfg Normalizing config +36ms
lint-staged:gen-tasks Generated task:
lint-staged:gen-tasks { pattern: '**/*.js',
lint-staged:gen-tasks commands:
lint-staged:gen-tasks [ 'some-command',
lint-staged:gen-tasks 'git add' ],
lint-staged:gen-tasks fileList: [ 'a.js' ] } +8ms
Stashing changes... [started]
Stashing changes... [skipped]
→ No partially staged files found...
Running linters... [started]
Running tasks for **/*.js [started]
lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'detect-secrets-hook --baseline .secrets-baseline', 'git add server/' ] +0ms
lint-staged:find-bin Resolving binary for command `some-command` +160ms
lint-staged:find-bin Binary for `some-command` resolved to `/Users/lirantal/Library/Python/2.7/bin/some-command` +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/Cellar/git/2.19.1/libexec/git-core/git` +1ms
lint-staged:task ✔ OS: darwin; File path chunking unnecessary +1ms
some-command [started]
lint-staged:task bin: /Users/lirantal/Library/Python/2.7/bin/some-command +0ms
lint-staged:task args: [ 'a.js' ] +0ms
lint-staged:task opts: { reject: false } +0ms
some-command [completed]
git add [started]
lint-staged:task bin: /usr/local/Cellar/git/2.19.1/libexec/git-core/git +111ms
lint-staged:task args: [ 'add', 'a.js' ] +0ms
lint-staged:task opts: { reject: false, cwd: '/REDACTED/' } +1ms
git add [failed]
→
Running tasks for **/*.js [failed]
→
Running linters... [failed]
✖ git add found some errors. Please fix them and try committing again.
fatal: pathspec 'a.js' did not match any files
husky > pre-commit hook failed (add --no-verify to bypass)
Environment
- OS: macOS High Sierra
- Node.js: 10.13.0
lint-staged
: 2.2.0
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
We dropped
relative
option with #639 so you’d need to change your config before you upgrade. On the bright side, you now can write custom resolving to files yourself.What kind of confusion? Any examples you can share? I can imagine how this flag is actually useful. Are you perhaps suggesting we should drop it completely and then provide the file path the way git provides it?
Currently the default relative flag is
false
, so it is defaulting to provide the absolute path as an argument. I believe the best option of tackling this is to fix thetrue
flag so that it uses git’s relative path but we’d be required to release a major version