Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Globstar pattern returns an empty string as an element of the results array #519

Closed
mrmlnc opened this issue Apr 10, 2023 · 6 comments · Fixed by #520
Closed

Globstar pattern returns an empty string as an element of the results array #519

mrmlnc opened this issue Apr 10, 2023 · 6 comments · Fixed by #520

Comments

@mrmlnc
Copy link

mrmlnc commented Apr 10, 2023

What the problem?

The ** pattern returns an empty string as an element of the results array.

Steps to reproduce

const ng = require('glob');

// MaxDepth option only for shortening the list of results
const entries = ng.globSync('**', { maxDepth: 1 });

console.dir(entries, { colors: true });

Actual behaviour

[
  '',
  'tsconfig.json',
  'src',
  'package.json',
  'out',
  'node_modules',
  'index.js',
  'herebyfile.mjs',
  'fixtures',
  'README.md',
  'LICENSE'
]

Expected behavioud

[
- '',
  'tsconfig.json',
  'src',
  'package.json',
  'out',
  'node_modules',
  'index.js',
  'herebyfile.mjs',
  'fixtures',
  'README.md',
  'LICENSE'
]
@isaacs
Copy link
Owner

isaacs commented Apr 10, 2023

This is by design. That empty string is a valid result. Globstar matches directories as well as files, and '' is "the current directory". If you set dotRelative: true or absolute: true it's more apparent what's going on.

You could argue, I guess, that the first globstar portion should not match the cwd, but that's a quirk of bash that I didn't copy, since node-glob normalizes all "empty" portions at the start of the pattern, so ./x/y is equivalent to x/y.

const ng = require('glob');

// MaxDepth option only for shortening the list of results
const entries = ng.globSync('**', { maxDepth: 1, dotRelative: true });

console.dir(entries, { colors: true });
$ node ng.js
[ './', './y', './ng.js', './d', './c', './b', './a' ]

@isaacs isaacs closed this as completed Apr 10, 2023
@mrmlnc
Copy link
Author

mrmlnc commented Apr 11, 2023

I think this is a very inconvenient solution for the end user. An empty string cannot be used in many standard methods for working with the file system.

> fs.readdir('')
Uncaught:
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at __node_internal_captureLargerStackTrace (node:internal/errors:465:5)
    at new NodeError (node:internal/errors:372:5)
    at __node_internal_ (node:internal/validators:224:11)
    at makeCallback (node:fs:186:3)
    at Object.readdir (node:fs:1369:14) {
  code: 'ERR_INVALID_CALLBACK'
}

> fs.lstatSync('')
Uncaught Error: ENOENT: no such file or directory, lstat
    at Object.lstatSync (node:fs:1529:3) {
  errno: -2,
  syscall: 'lstat',
  code: 'ENOENT'
}

And every time the user will have to exclude this element from the results array.

And, yeap, makes sense with the options that you mentioned above.

@isaacs
Copy link
Owner

isaacs commented Apr 11, 2023

Hm. Maybe I could have dotRelative default to true when the result is an empty string?

@isaacs
Copy link
Owner

isaacs commented Apr 11, 2023

@mrmlnc Can you check out #520 and see how that works for you?

@isaacs isaacs closed this as completed in d6e8645 Apr 14, 2023
@isaacs
Copy link
Owner

isaacs commented Apr 14, 2023

Someone else pinged me about the same thing, so just further validation that this is legitimately confusing.

Landed in 10.1.0, '' will never be returned now, only '.'.

@mrmlnc
Copy link
Author

mrmlnc commented Apr 15, 2023

Sorry to keep you waiting. I keep thinking that in most cases this is extra information for the user, but I have a similar request in fast-glob (mrmlnc/fast-glob#47) and I am also going to implement it in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants