Skip to content

Fix Vite plugin to respect root option for index.html entries#1561

Merged
webpro merged 1 commit intowebpro-nl:mainfrom
azat-io:fix/vite-root
Feb 23, 2026
Merged

Fix Vite plugin to respect root option for index.html entries#1561
webpro merged 1 commit intowebpro-nl:mainfrom
azat-io:fix/vite-root

Conversation

@azat-io
Copy link
Copy Markdown
Contributor

@azat-io azat-io commented Feb 23, 2026

In one of my projects, Vite uses the root option, so index.html is not located at the project root. Because of this, Knip did not detect entries referenced from index.html and incorrectly reported related files/dependencies as unused.

This PR makes Knip respect Vite’s root option when resolving index.html entries, and adds a test case for a custom root setup.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Feb 23, 2026

Open in StackBlitz

npm i https://pkg.pr.new/knip@1561
npm i https://pkg.pr.new/@knip/language-server@1561
npm i https://pkg.pr.new/@knip/mcp@1561

commit: e0f0dc0

Copy link
Copy Markdown
Member

@webpro webpro left a comment

Choose a reason for hiding this comment

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

Thanks, keep it coming buddy 😃 One comment I have.

const viteRoot = toAbsolute(cfg.root ?? '.', options.cwd);
if (!seenRoots.has(viteRoot)) {
seenRoots.add(viteRoot);
for (const entry of await getIndexHtmlEntries(viteRoot)) inputs.add(entry);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we do getIndexHtmlEntries here, can we remove resolve from the Vite plugin? As we're executing resolveConfig from both plugins already.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

resolveConfig only runs when a config file exists. Without resolve, config-less Vite projects (like the vite3 fixture) would lose index.html entry detection.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good point! Thanks.

@webpro webpro merged commit 67a5647 into webpro-nl:main Feb 23, 2026
30 checks passed
@webpro
Copy link
Copy Markdown
Member

webpro commented Feb 23, 2026

Thanks Azat! ✨

@webpro
Copy link
Copy Markdown
Member

webpro commented Mar 7, 2026

🚀 This pull request is included in v5.86.0. See Release 5.86.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

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.

2 participants