Skip to content

Commit f4cacb6

Browse files
committed
fix: find and source ~/.jq file on windows (fixes #3104)
1 parent c127616 commit f4cacb6

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

docs/content/manual/manual.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3662,7 +3662,8 @@ sections:
36623662
For example, with `-L$HOME/.jq` a module `foo` can be found in
36633663
`$HOME/.jq/foo.jq` and `$HOME/.jq/foo/foo.jq`.
36643664
3665-
If `$HOME/.jq` is a file, it is sourced into the main program.
3665+
If `.jq` exists in the user's home directory, and is a file (not a
3666+
directory), it is automatically sourced into the main program.
36663667
36673668
entries:
36683669
- title: "`import RelativePathString as NAME [<metadata>];`"

jq.1.prebuilt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/linker.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,16 @@ int load_program(jq_state *jq, struct locfile* src, block *out_block) {
420420
return 1;
421421
}
422422

423-
char* home = getenv("HOME");
424-
if (home) { // silently ignore no $HOME
425-
/* Import ~/.jq as a library named "" found in $HOME */
423+
jv home = get_home();
424+
if (jv_is_valid(home)) {
425+
/* Import ~/.jq as a library named "" found in $HOME or %USERPROFILE% */
426426
block import = gen_import_meta(gen_import("", NULL, 0),
427427
gen_const(JV_OBJECT(
428428
jv_string("optional"), jv_true(),
429-
jv_string("search"), jv_string(home))));
429+
jv_string("search"), home)));
430430
program = BLOCK(import, program);
431+
} else { // silently ignore if home dir not determined
432+
jv_free(home);
431433
}
432434

433435
nerrors = process_dependencies(jq, jq_get_jq_origin(jq), jq_get_prog_origin(jq), &program, &lib_state);

tests/shtest

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,17 @@ if [ "$(HOME="$mods/home1" $VALGRIND $Q $JQ -nr fg)" != foobar ]; then
359359
exit 1
360360
fi
361361

362+
if $msys || $mingw; then
363+
HOME_BAK=$HOME
364+
unset HOME
365+
if [ "$(USERPROFILE="$mods/home1" $VALGRIND $Q $JQ -nr foo)" != baz ]; then
366+
echo "Bug #3104 regressed (sourcing %USERPROFILE%/.jq on Windows)" 1>&2
367+
exit 1
368+
fi
369+
export HOME=$HOME_BAK
370+
unset HOME_BAK
371+
fi
372+
362373
if [ $(HOME="$mods/home1" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l) -ne 3 ]; then
363374
echo "Binding too many defs into program" 1>&2
364375
exit 1

0 commit comments

Comments
 (0)