sindresorhus / globby

User-friendly glob matching
MIT License
2.51k stars 130 forks source link

Relative paths don't work with `gitignore` option #168

Open NickHeiner opened 3 years ago

NickHeiner commented 3 years ago

When I pass a relative path, and gitignore: true, I get an error.

Demo repo

const path = require('path');
const globby = require('globby');

console.log(globby.sync(
  ['..'],
  {cwd: path.join(__dirname, 'node_modules'), gitignore: true}
))

When I run this, I get:

/Users/nheiner/code/globby-relative-path-demo/node_modules/ignore/index.js:366
  throw new Ctor(message)
  ^

RangeError: path should be a `path.relative()`d string, but got "../index.js"
    at throwError (/Users/nheiner/code/globby-relative-path-demo/node_modules/ignore/index.js:366:9)
    at checkPath (/Users/nheiner/code/globby-relative-path-demo/node_modules/ignore/index.js:385:12)
    at Ignore._test (/Users/nheiner/code/globby-relative-path-demo/node_modules/ignore/index.js:502:5)
    at Ignore.ignores (/Users/nheiner/code/globby-relative-path-demo/node_modules/ignore/index.js:541:17)
    at /Users/nheiner/code/globby-relative-path-demo/node_modules/globby/gitignore.js:62:22
    at /Users/nheiner/code/globby-relative-path-demo/node_modules/globby/index.js:154:34
    at Array.filter (<anonymous>)
    at AsyncFunction.module.exports.sync (/Users/nheiner/code/globby-relative-path-demo/node_modules/globby/index.js:154:17)
    at Object.<anonymous> (/Users/nheiner/code/globby-relative-path-demo/index.js:4:20)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
cboden commented 3 years ago

+1

pinuke commented 2 years ago

+1

A simpler way to demonstrate the bug:

globbySync([ "file.ext" ], { gitignore:true }) //works
globbySync([ "./file.ext" ], { gitignore:false }) //works

globbySync([ "./file.ext" ], { gitignore:true }) //throws an error

globby needs to get the relative path to the .gitignore file before passing it to ignore.js.

globby should also detect if a file with no relative path to the gitignore file was accidentally passed.

vdawg-git commented 11 months ago

Any updates on this?