Skip to content

Commit 404b6f4

Browse files
committed
Use a more portable and clear jq regex technique
Because regular expressions in a `jq` query are strings, their `\` escapes must be written as `\\`. But the `get-metadata` recipe in the `justfile` is strangely intolerant of `\\` in a cross-platform setting. On Windows, even though `sh` is used on both, and even though the `quote()` function is supposed to work the same way on both, the `\\` gets folded into `\` in the actual query argument passed to `jq`, and `just msrv` fails with: cargo metadata --format-version 1 --no-deps | jq --exit-status --raw-output -- '.packages[] | select(.name == "gix") | .rust_version | sub("^\\d+\\.\\d+\\K$"; ".0")' jq: error: Invalid escape at line 1, column 4 (while parsing '"\d"') at <top-level>, line 1: .packages[] | select(.name == "gix") | .rust_version | sub("^\d+\.\d+\K$"; ".0") jq: error: Invalid escape at line 1, column 6 (while parsing '"\.\d"') at <top-level>, line 1: .packages[] | select(.name == "gix") | .rust_version | sub("^\d+\.\d+\K$"; ".0") jq: error: Invalid escape at line 1, column 4 (while parsing '"\K"') at <top-level>, line 1: .packages[] | select(.name == "gix") | .rust_version | sub("^\d+\.\d+\K$"; ".0") jq: 3 compile errors error: Recipe `get-metadata` failed on line 191 with exit code 3 A Windows-specific workaround is to write `\\\\` instead of `\\`. This works on Windows, but of course breaks on other platforms where it represents a subpattern of `\\` and thus matches only a literal `\` (which is not present and shouldn't be matched if it were). In this case, it so happens that the pattern was also potentially confusing for other reasons, due to the way it matched a zero-width string after a `\K`-discarded sub-match that did most of the work. Because `\\d` and `\\.` are easily replaced with character classes that use no `\`, the `\\K` was the only use of `\` in the pattern string that needed `\`. So this replaces that technique with a different more readable one that does not require writing any occurrences of `\\`. (It uses a `\`, but no `\\`, and `\` by itself seems to cause no problems.) Instead of discarding a sub-match and replacing an empty string, this matches on the entire string (if in MAJOR.MINOR form) and replaces the whole thing with itself followed by `.0`. This does make the recipe longer. The `jq` query argument is accordingly now split over multiple lines. (Although this is done using a `'''` string instead of a `'` string, that does not make a difference to the misinterpretation of `\\`, which occurs after the string has been received by the `get-metadata` recipe.)
1 parent 91f6883 commit 404b6f4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

justfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,12 @@ check-size:
242242
etc/check-package-size.sh
243243

244244
# Report the Minimum Supported Rust Version (the `rust-version` of `gix`) in X.Y.Z form
245-
msrv: (get-metadata '.packages[] | select(.name == "gix") | .rust_version | sub("^\\d+\\.\\d+\\K$"; ".0")')
245+
msrv: (get-metadata '''
246+
.packages[]
247+
| select(.name == "gix")
248+
| .rust_version
249+
| sub("(?<xy>^[0-9]+[.][0-9]+$)"; "\(.xy).0")
250+
''')
246251
247252
# Regenerate the MSRV badge SVG
248253
msrv-badge:

0 commit comments

Comments
 (0)