lhapaipai / vite-plugin-symfony

Vite plugin to use your bundler with Symfony
https://symfony-vite.pentatrion.com
Other
43 stars 11 forks source link

Fix TypeError when handling Vite user config with no root option #26

Closed andyexeter closed 12 months ago

andyexeter commented 12 months ago

The root option in Vite's user config is optional however the change made in https://github.com/lhapaipai/vite-plugin-symfony/commit/b6bdc633d32bcd45a31878db4a8695f356067652 assumes it will always be set.

When it is not set this plugin is crashing the dev server and throwing the following TypeError:

error when starting dev server:
TypeError [ERR_INVALID_ARG_TYPE]: The "paths[0]" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:405:5)
    at validateString (node:internal/validators:162:11)
    at resolve (node:path:1097:7)
    at config (/app/node_modules/vite-plugin-symfony/dist/index.js:5996:50)
    at runConfigHook (file:///app/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:66346:31)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async resolveConfig (file:///app/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:65787:14)
    at async _createServer (file:///app/node_modules/vite/dist/node/chunks/dep-2b82a1ce.js:65031:20)
    at async CAC.<anonymous> (file:///app/node_modules/vite/dist/node/cli.js:763:24)

This PR fixes the issue by checking whether userConfig.root is set before resolving it, falling back to cwd() otherwise.

I've also added a test in a separate commit which fails without my fix and succeeds with it.

lhapaipai commented 12 months ago

Thanks very much @andyexeter for your review and PR !!