bug: error when using glob pattern with no files found
kristiannotari opened this issue · 7 comments
Context:
- version (
md-to-pdf -v
): 5.1.0 - platform (Unix, macOS, Windows): macOS (laptop) & ubuntu 20.04 (GitHub action)
- node version: 16.5.1 & 16.x
Describe the bug:
With the following command, when more glob pattern are used to target files, but one of them doesn't match any files, the conversion works for the found files and exit with an error when it doesn't find the others:
I'm using globstar package for glob patterns, but the issue exists even when specifying non recursive glob patterns. Is there a way to have a "pass even if no files are matched" flag?
Hey I would say that the issue is with globstar in that case, which seems to pass "docs/**/*.md"
as a literal string if it doesn't find any files, i. e. doesn't expand the glob?
Looks like that's exactly what's happening 🤷🏻♂️
Just wondering, why can't you use normal shell glob expansion? Because you need it to be cross-platform?
Yeah I'm using it to ensure cross-platform correctness of npm commands. The command with globstar works correctly for other repo of mine where I do have some .md
files targeted by those glob patterns. By the way, in my second screenshot, you can find a minimal version where even without globstar the md-to-pdf command fails with no files found for given pattern. Is there a way to have a "--pass-with-no-files" option?
Let me explain it again: if globstar doesn't match any files with the given argument, instead of not running the command (i.e. running it zero times for zero results), it runs the command with the string you passed as input (the one containing the glob). This is possibly bad behavior of globstar, because a lot of CLI tools that expect a file path as input would fail if you provide an invalid/non-existent path. Changing the tools is not the way to fix this, you should report/fix it in the globstar repo (https://github.com/schnittstabil/globstar).
Edit: as for your comment about your second screenshot: "docs/*.md"
(with the quotes) will not expand the glob, but instead pass the quoted string as the value, which is then interpreted as the path. md-to-pdf
does not support expanding glob patterns like that. If you do it without the quotes, your shell will expand it before passing it to md-to-pdf
. When you try to expand and there's no actual matches, the result depends on the shell you're using though... zsh
says "no matches found" and doesn't run the command, whereas bash
has a similar behavior to globstar where it passes it as a string to the command instead (which is maybe the reasoning behind globstar doing it this way).
Edit 2: TIL about Bash's nullglob
option: http://bash.cumulonim.biz/NullGlob.html. If you enable it, you'll get md-to-pdf
to pass even if your glob pattern doesn't match any files. You can request for the globstar package to add a nullglob option as well, I guess?
Ok thanks for the detailed answer. I did got the first point already when you wrote about it the first time, it was the quoted pattern in the second screenshot that was bugging me, letting me think that was something up to md-to-pdf
.
Thanks for the hints about how to solve the bash thing.
I'm sorry for wasting your time on this.
No worries.
As a solution on your side, you can write a script and use a package like globby or glob yourself, i guess?
import { execa } from 'execa'
import { globby } from 'globby'
const files = await globby(['docs/*.md'])
if (files.length > 0) {
execa('md-to-pdf', ['--config-file', 'docs.config.js', ...files]).stdout.pipe(process.stdout)
}
(or use md-to-pdf's programmatic API but iirc it doesn't support parallelism with a shared browser instance yet, so it would be a bit slower)
Or you somehow test your glob for matches first and then conditionally call md-to-pdf only if files exist (not sure there's a good cross-platform solution for that).
Thanks for the suggestions. I don't need to overcomplicate this, I'm doing all of this for docs related purposes, so the less scripts, logics and packages are involved the better. I'm just making sure there's at least an empty .md
file as per documentation in each repo. No repo should contain 0 doc files anyway!!! 😄