Skip to content

Commit 780f4ce

Browse files
fix(cli): avoid hook path shadowing
1 parent 5e850c0 commit 780f4ce

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

packages/cli/src/config/__tests__/hooks.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('hookScript', () => {
7575
});
7676

7777
it.skipIf(process.platform === 'win32')(
78-
'should add Vite+ managed bin to PATH before running user hook',
78+
'should add Vite+ managed bin to PATH as a fallback before running user hook',
7979
() => {
8080
const tmp = mkdtempSync(join(tmpdir(), 'hooks-path-test-'));
8181
try {
@@ -108,6 +108,16 @@ describe('hookScript', () => {
108108
'#!/bin/sh\necho "fake-node $*" > "$VP_HOME/node-used"\n',
109109
{ mode: 0o755 },
110110
);
111+
writeFileSync(
112+
join(vpHomeBin, 'dirname'),
113+
'#!/bin/sh\necho "wrong dirname" > "$VP_HOME/dirname-used"\nexit 1\n',
114+
{ mode: 0o755 },
115+
);
116+
writeFileSync(
117+
join(vpHomeBin, 'sh'),
118+
'#!/bin/sh\necho "wrong sh" > "$VP_HOME/sh-used"\nexit 1\n',
119+
{ mode: 0o755 },
120+
);
111121

112122
writeFileSync(join(systemBin, 'sh'), '#!/bin/sh\nexec /bin/sh "$@"\n', {
113123
mode: 0o755,
@@ -129,9 +139,20 @@ describe('hookScript', () => {
129139
});
130140

131141
expect(existsSync(join(tmp, 'vp-home', 'node-used'))).toBe(true);
142+
expect(existsSync(join(tmp, 'vp-home', 'dirname-used'))).toBe(false);
143+
expect(existsSync(join(tmp, 'vp-home', 'sh-used'))).toBe(false);
132144
} finally {
133145
rmSync(tmp, { recursive: true, force: true });
134146
}
135147
},
136148
);
149+
150+
it('should compute root and shell before appending Vite+ managed bin', () => {
151+
const script = hookScript('.vite-hooks');
152+
expect(script.indexOf('d=')).toBeLessThan(script.indexOf('export PATH="$PATH:$__vp_bin"'));
153+
expect(script.indexOf('__vp_shell=')).toBeLessThan(
154+
script.indexOf('export PATH="$PATH:$__vp_bin"'),
155+
);
156+
expect(script).toContain('"$__vp_shell" -e "$s" "$@"');
157+
});
137158
});

packages/cli/src/config/hooks.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,21 @@ i="\${XDG_CONFIG_HOME:-$HOME/.config}/vite-plus/hooks-init.sh"
4949
5050
{ [ "\${HUSKY-}" = "0" ] || [ "\${VITE_GIT_HOOKS-}" = "0" ]; } && exit 0
5151
52+
d=${rootExpr}
53+
__vp_shell=/bin/sh
54+
[ -x "$__vp_shell" ] || __vp_shell=$(command -v sh)
55+
5256
if [ -n "\${VP_HOME-}" ]; then
5357
__vp_bin="$VP_HOME/bin"
5458
elif [ -n "\${HOME-}" ]; then
5559
__vp_bin="$HOME/.vite-plus/bin"
5660
else
5761
__vp_bin=""
5862
fi
59-
[ -n "$__vp_bin" ] && [ -d "$__vp_bin" ] && export PATH="$__vp_bin:$PATH"
63+
[ -n "$__vp_bin" ] && [ -d "$__vp_bin" ] && export PATH="$PATH:$__vp_bin"
6064
61-
d=${rootExpr}
6265
export PATH="$d/node_modules/.bin:$PATH"
63-
sh -e "$s" "$@"
66+
"$__vp_shell" -e "$s" "$@"
6467
c=$?
6568
6669
[ $c != 0 ] && echo "VITE+ - $n script failed (code $c)"

0 commit comments

Comments
 (0)