Commit 3dae3a3
fix(cli): inline lint-staged version into staged debug bundle (#1925)
## Problem
- #1922
`vp staged --debug` crashes in the published package.
When debug logging is enabled, bundled `lint-staged` calls
`getVersion()`(`lib/version.js`), which reads its own manifest
via`readFile(new URL('../package.json', import.meta.url))`. The CLI
bundleslint-staged into `dist/staged/bin.js`, so that relative
`../package.json` does not exist at runtime and the command throws
instead of printing the debug log.
This is exactly what the pinned `lint-staged@16.4.0` does in
`lib/version.js`:
```js
import fs from 'node:fs/promises'
export const getVersion = async () => {
const packageJson = JSON.parse(await fs.readFile(new URL('../package.json', import.meta.url)))
return packageJson.version
}
```
Source:
https://github.com/lint-staged/lint-staged/blob/445f9dd042b88528c798b2e25c21c9adbc69a732/lib/version.js#L1-L6
## Fix
Add a tsdown plugin (`inline-lint-staged-version`) that rewrites the
`./version.js` import coming from `lint-staged/lib/index.js` to a
virtual module
exposing the dependency version resolved at build time:
```js
export const getVersion = async () => "16.4.0";
```
Keeps the `getVersion` named-export shape identical, so no lint-staged
internals change.
## Tests
- Extend the `command-staged-with-config` global snap test with a `vp
staged --debug` step that must succeed. A regression flips the recorded
exit code (`>` → `[1]>`), failing the snapshot.
Verified against a real build: `dist/staged/bin.js` now contains the
inlined `const version = "16.4.0"` and no longer references
`readFile(new URL('../package.json', ...))`.
```bash
vscode ➜ /workspaces/vite-plus (fix-debug-staged) $ vp staged --debug
lint-staged: Running `lint-staged@16.4.0` on Node.js v22.18.0 (linux) +126ms
lint-staged:execGit: Running git command: [ 'version', '--build-options' ] +128ms
lint-staged: git version 2.51.1
cpu: aarch64
no commit associated with this build
```
---
closes: #1922
---------
Co-authored-by: MK (fengmk2) <fengmk2@gmail.com>1 parent f1652fc commit 3dae3a3
3 files changed
Lines changed: 34 additions & 1 deletion
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
23 | 26 | | |
24 | 27 | | |
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
12 | 21 | | |
13 | 22 | | |
14 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
3 | 8 | | |
4 | 9 | | |
5 | 10 | | |
| |||
18 | 23 | | |
19 | 24 | | |
20 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
21 | 42 | | |
22 | 43 | | |
23 | 44 | | |
| |||
56 | 77 | | |
57 | 78 | | |
58 | 79 | | |
59 | | - | |
| 80 | + | |
60 | 81 | | |
61 | 82 | | |
62 | 83 | | |
| |||
0 commit comments