Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "hooks-subdir-test",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[[case]]
name = "command_config_subdir_hook_path"
vp = "local"
steps = [
{ argv = ["git", "init"], snapshot = false },
{ argv = ["vp", "config", "--no-agent", "--hooks-dir", ".vite-hooks"], cwd = "pkg", snapshot = false },
{ argv = ["git", "config", "--local", "core.hooksPath"], comment = "the hooks path stays relative to the checkout" },
{ argv = ["vpt", "mkdir", "-p", "pkg/node_modules/.bin", "shadow/pkg"], snapshot = false },
{ argv = ["vpt", "write-file", "pkg/node_modules/.bin/test-hook-cmd", "#!/usr/bin/env sh\nprintf 'hook-path-ok\\n'\nprintf 'CDPATH=%s\\n' \"${CDPATH-unset}\"\n"], snapshot = false },
{ argv = ["vpt", "chmod", "+x", "pkg/node_modules/.bin/test-hook-cmd"], snapshot = false },
# ./pkg keeps the user hook's cd independent of CDPATH, isolating the dispatcher.
{ argv = ["vpt", "write-file", "pkg/.vite-hooks/pre-commit", "cd ./pkg\ntest-hook-cmd\n"], snapshot = false },
{ argv = ["git", "hook", "run", "pre-commit"], comment = "a package-only binary stays available after cd", continue-on-failure = true },
{ argv = ["git", "hook", "run", "pre-commit"], envs = [["CDPATH", ""]], comment = "an empty CDPATH remains empty in the user hook", continue-on-failure = true },
{ argv = ["git", "hook", "run", "pre-commit"], envs = [["CDPATH", "."]], comment = "cd must not contaminate the resolved path with extra output", continue-on-failure = true },
{ argv = ["git", "hook", "run", "pre-commit"], envs = [["CDPATH", "shadow"]], comment = "cd must not select shadow/pkg; the user hook still inherits CDPATH", continue-on-failure = true },
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# command_config_subdir_hook_path

## `git init`


## `cd pkg && vp config --no-agent --hooks-dir .vite-hooks`


## `git config --local core.hooksPath`

the hooks path stays relative to the checkout

```
pkg/.vite-hooks/_
```

## `vpt mkdir -p pkg/node_modules/.bin shadow/pkg`


## `vpt write-file pkg/node_modules/.bin/test-hook-cmd '#'\!'/usr/bin/env sh
printf '\''hook-path-ok\n'\''
printf '\''CDPATH=%s\n'\'' "${CDPATH-unset}"
'`


## `vpt chmod +x pkg/node_modules/.bin/test-hook-cmd`


## `vpt write-file pkg/.vite-hooks/pre-commit 'cd ./pkg
test-hook-cmd
'`


## `git hook run pre-commit`

a package-only binary stays available after cd

```
hook-path-ok
CDPATH=unset
```

## `CDPATH= git hook run pre-commit`

an empty CDPATH remains empty in the user hook

```
hook-path-ok
CDPATH=
```

## `CDPATH=. git hook run pre-commit`

cd must not contaminate the resolved path with extra output

```
hook-path-ok
CDPATH=.
```

## `CDPATH=shadow git hook run pre-commit`

cd must not select shadow/pkg; the user hook still inherits CDPATH

```
hook-path-ok
CDPATH=shadow
```
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ i="${XDG_CONFIG_HOME:-$HOME/.config}/vite-plus/hooks-init.sh"

{ [ "${HUSKY-}" = "0" ] || [ "${VP_GIT_HOOKS-}" = "0" ] || [ "${VITE_GIT_HOOKS-}" = "0" ]; } && exit 0

d="$(dirname "$(dirname "$(dirname "$0")")")"
# Keep the project bin path valid after hooks change directory, independent of CDPATH.
d="$(CDPATH= cd "$(dirname "$(dirname "$(dirname "$0")")")" && pwd)" || exit 1
__vp_shell=/bin/sh
[ -x "$__vp_shell" ] || __vp_shell=$(command -v sh)

Expand Down
88 changes: 86 additions & 2 deletions packages/cli/src/config/__tests__/hooks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execSync, spawn } from 'node:child_process';
import { execFileSync, execSync, spawn, spawnSync } from 'node:child_process';
import { once } from 'node:events';
import {
chmodSync,
Expand All @@ -7,6 +7,7 @@ import {
mkdirSync,
mkdtempSync,
readFileSync,
realpathSync,
rmSync,
symlinkSync,
statSync,
Expand Down Expand Up @@ -843,10 +844,93 @@ describe('hookScript', () => {
encoding: 'utf8',
});

expect(output).toBe(`./node_modules/.bin:${inheritedPath}`);
expect(output).toBe(`${join(realpathSync(tmp), 'node_modules', '.bin')}:${inheritedPath}`);
} finally {
rmSync(tmp, { recursive: true, force: true });
}
},
);

it.skipIf(process.platform === 'win32').each([
{ pkg: 'pkg', dir: '.vite-hooks', cdpath: undefined, linked: false },
{ pkg: 'pkg', dir: '.vite-hooks', cdpath: '.', linked: false },
{ pkg: 'pkg', dir: '.vite-hooks', cdpath: 'shadow', linked: false },
{ pkg: 'packages/my pkg', dir: '.config/hooks', cdpath: 'shadow', linked: false },
{ pkg: 'pkg', dir: '.vite-hooks', cdpath: 'shadow', linked: true },
])(
'keeps project binaries after cd: $pkg, $dir, CDPATH=$cdpath, linked=$linked',
({ pkg, dir, cdpath, linked }) => {
const tmp = realpathSync(mkdtempSync(join(tmpdir(), 'hooks-subdir-cd-test-')));
const originalCwd = process.cwd();
try {
execSync('git init', { cwd: tmp, stdio: 'ignore' });
const roots = [tmp];
if (linked) {
execFileSync(
'git',
[
'-c',
'user.name=Test',
'-c',
'user.email=test@example.com',
'-c',
'commit.gpgsign=false',
'-c',
'core.hooksPath=/dev/null',
'commit',
'--allow-empty',
'-m',
'init',
],
{ cwd: tmp, stdio: 'ignore' },
);
const worktree = join(tmp, 'linked');
execFileSync('git', ['worktree', 'add', '--detach', worktree], {
cwd: tmp,
stdio: 'ignore',
});
roots.push(worktree);
}

for (const root of roots) {
const pkgDir = join(root, pkg);
const nodeModulesBin = join(pkgDir, 'node_modules', '.bin');
mkdirSync(nodeModulesBin, { recursive: true });
process.chdir(pkgDir);
expect(install(dir).isError).toBe(false);
expect(
execFileSync('git', ['config', '--get', 'core.hooksPath'], {
cwd: root,
encoding: 'utf8',
}).trim(),
).toBe(`${pkg}/${dir}/_`);
writeFileSync(join(pkgDir, dir, 'pre-commit'), `cd './${pkg}'\nhook-command\n`);
writeFileSync(join(nodeModulesBin, 'hook-command'), '#!/bin/sh\nprintf "%s" "$0"\n', {
mode: 0o755,
});
// A competing package must not redirect the dispatcher's cd through CDPATH.
mkdirSync(join(root, 'shadow', pkg), { recursive: true });
}

// Run both after the last install, since linked worktrees share core.hooksPath.
for (const root of roots) {
const result = spawnSync('git', ['hook', 'run', 'pre-commit'], {
cwd: root,
env: {
HOME: join(tmp, 'home'),
PATH: '/usr/bin:/bin',
CDPATH: cdpath === 'shadow' ? join(root, 'shadow') : cdpath,
},
encoding: 'utf8',
});
expect(result.status, result.stderr).toBe(0);
// Git forwards hook stdout to stderr.
expect(result.stderr).toBe(join(root, pkg, 'node_modules', '.bin', 'hook-command'));
}
} finally {
process.chdir(originalCwd);
rmSync(tmp, { recursive: true, force: true });
}
},
);
});
3 changes: 2 additions & 1 deletion packages/cli/src/config/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ i="\${XDG_CONFIG_HOME:-$HOME/.config}/vite-plus/hooks-init.sh"

{ [ "\${HUSKY-}" = "0" ] || [ "\${VP_GIT_HOOKS-}" = "0" ] || [ "\${VITE_GIT_HOOKS-}" = "0" ]; } && exit 0

d=${rootExpr}
# Keep the project bin path valid after hooks change directory, independent of CDPATH.
d="$(CDPATH= cd ${rootExpr} && pwd)" || exit 1
__vp_shell=/bin/sh
[ -x "$__vp_shell" ] || __vp_shell=$(command -v sh)

Expand Down
Loading