Skip to content

fix(cli): inline lint-staged version into staged debug bundle#1925

Merged
fengmk2 merged 7 commits into
voidzero-dev:mainfrom
rokuosan:fix-debug-staged
Jun 24, 2026
Merged

fix(cli): inline lint-staged version into staged debug bundle#1925
fengmk2 merged 7 commits into
voidzero-dev:mainfrom
rokuosan:fix-debug-staged

Conversation

@rokuosan

@rokuosan rokuosan commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Problem

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 viareadFile(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:

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:

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', ...)).

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

@netlify

netlify Bot commented Jun 23, 2026

Copy link
Copy Markdown

Deploy Preview for viteplus-preview canceled.

Name Link
🔨 Latest commit aa99589
🔍 Latest deploy log https://app.netlify.com/projects/viteplus-preview/deploys/6a3be0517b94310008435fcc

@jong-kyung jong-kyung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that matches the direction I had in mind.

One small alternative: how about replacing lint-staged/lib/version.js directly?

It could make the change a bit shorter, since the debug path calls getVersion() here:
https://github.com/lint-staged/lint-staged/blob/5f3b8f28e895972bd5a2cdba733327b49859b91f/lib/index.js#L106-L115

and getVersion() is isolated in lib/version.js:
https://github.com/lint-staged/lint-staged/blob/5f3b8f28e895972bd5a2cdba733327b49859b91f/lib/version.js#L3-L6

Not blocking though — the current approach also looks clear to me.

@jong-kyung

Copy link
Copy Markdown
Collaborator

Also, could you please look into the CI failure?

@rokuosan

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense.
Replacing /lint-staged/lib/version.js directly is simpler here, so I'll update it.

I'll also investigate the CI failure.

rokuosan added 2 commits June 24, 2026 00:27
… hook

Drop the resolveId + virtual-module indirection and replace
lint-staged's lib/version.js directly in a single load hook.
The ./version.js import is relative, so the bundler resolves
it to the real file and the load hook short-circuits the
read — no virtual id or importer matching needed.
@rokuosan rokuosan requested a review from jong-kyung June 23, 2026 16:15
Comment thread packages/cli/snap-tests-global/command-staged-with-config/steps.json Outdated
Switch the `vp staged --debug` step from `> /dev/null 2>&1` to
`ignoreOutput: true`. The output is still suppressed on success, so the
snapshot stays stable, but it is preserved on failure, making a
regression easier to debug.

@jong-kyung jong-kyung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

@fengmk2 fengmk2 self-assigned this Jun 24, 2026
@fengmk2

fengmk2 commented Jun 24, 2026

Copy link
Copy Markdown
Member

@rokuosan @jong-kyung Thanks!

@fengmk2 fengmk2 merged commit 3dae3a3 into voidzero-dev:main Jun 24, 2026
40 checks passed
@rokuosan rokuosan deleted the fix-debug-staged branch June 24, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vp staged --debug crashes by reading vite-plus/dist/package.json instead of lint-staged's package.json

3 participants