Skip to content

Commit

Permalink
source ~/.jq file on windows (fixes jqlang#3104)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkoman committed May 9, 2024
1 parent c127616 commit 4396b78
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3662,7 +3662,8 @@ sections:
For example, with `-L$HOME/.jq` a module `foo` can be found in
`$HOME/.jq/foo.jq` and `$HOME/.jq/foo/foo.jq`.
If `$HOME/.jq` is a file, it is sourced into the main program.
If `.jq` exists in the user's home directory, and is a file (not a
directory), it is automatically sourced into the main program.
entries:
- title: "`import RelativePathString as NAME [<metadata>];`"
Expand Down
2 changes: 1 addition & 1 deletion jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ int load_program(jq_state *jq, struct locfile* src, block *out_block) {
return 1;
}

char* home = getenv("HOME");
if (home) { // silently ignore no $HOME
/* Import ~/.jq as a library named "" found in $HOME */
jv home = get_home();
if (jv_is_valid(home)) { // silently ignore if home dir not determined
/* Import ~/.jq as a library named "" found in $HOME or %USERPROFILE% */
block import = gen_import_meta(gen_import("", NULL, 0),
gen_const(JV_OBJECT(
jv_string("optional"), jv_true(),
jv_string("search"), jv_string(home))));
jv_string("search"), home)));
program = BLOCK(import, program);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,24 @@ clean=true

# Check handling of ~/.jq; these can't move into jq_test.c yet because
# they depend on $HOME
HOME_BAK=$HOME
unset HOME

if [ "$(HOME="$mods/home1" $VALGRIND $Q $JQ -nr fg)" != foobar ]; then
echo "Bug #479 appears to be back" 1>&2
exit 1
fi

test_win_home_dir=false
$msys && test_win_home_dir=true
$mingw && test_win_home_dir=true
if $test_win_home_dir; then
if [ "$(USERPROFILE="$mods/home1" $VALGRIND $Q $JQ -nr foo)" != baz ]; then
echo "Bug #3104 regressed (sourcing %USERPROFILE%/.jq on Windows)" 1>&2
exit 1
fi
fi

if [ $(HOME="$mods/home1" $VALGRIND $Q $JQ --debug-dump-disasm -n fg | grep '^[a-z]' | wc -l) -ne 3 ]; then
echo "Binding too many defs into program" 1>&2
exit 1
Expand All @@ -369,6 +382,9 @@ if ! HOME="$mods/home2" $VALGRIND $Q $JQ -n 'include "g"; empty'; then
exit 1
fi

HOME=$HOME_BAK
unset HOME_BAK

cd "$JQBASEDIR" # so that relative library paths are guaranteed correct
if ! $VALGRIND $Q $JQ -L ./tests/modules -ne 'import "test_bind_order" as check; check::check==true'; then
echo "Issue #817 regression?" 1>&2
Expand Down

0 comments on commit 4396b78

Please sign in to comment.