question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use staged filenames as git references them

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
okonetcommented, Jul 1, 2019

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.

0reactions
lirantalcommented, May 19, 2019

I think there is a lot of confusion coming from that

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 the true flag so that it uses git’s relative path but we’d be required to release a major version

Read more comments on GitHub >

github_iconTop Results From Across the Web

2.2 Git Basics - Recording Changes to the Repository
To stage it, you run the git add command. git add is a multipurpose command — you use it to begin tracking new...
Read more >
How to make git status show only staged files - Stack Overflow
Use git diff --name-only (with --cached to get the staged files).
Read more >
Curious git - Matthew Brett on github
In git, the staging area is a single file called .git/index . This file contains a directory listing that is the equivalent of...
Read more >
Chapter 6 Using Git at the Command Line
Once Git is configured, we can use it to track work on our Zipf's Law project. ... On branch master Changes not staged...
Read more >
.gitignore file - ignoring files in Git | Atlassian Git Tutorial
Prepending an exclamation mark to a pattern negates it. If a file matches a pattern, but also matches a negating pattern defined later...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found