The following Vite config options will be overridden by SvelteKit: root
Describe the bug
This warning pops up when running vitest on Windows. It seems like the config() function of the Kit Vite plugin runs multiple times, and that paths are normalized (possibly since Vite 6?). The warning comes from this line:
https://github.com/sveltejs/kit/blob/01f001bd861e399027a580c9402b7b6206de3d24/packages/kit/src/exports/vite/index.js#L1061
where, for me, the configs have these values:
config.root=C:/Users/.../project-dirresolved_config.root=C:\Users\...\project-dir
So, the 2 configs have the same path, but one with forward slashes, and one with backslashes.
SvelteKit itself tries to set config.root to the version with backslashes, as it uses process.cwd() to get it. Maybe it should use posixify() on it before? Replacing this line:
https://github.com/sveltejs/kit/blob/01f001bd861e399027a580c9402b7b6206de3d24/packages/kit/src/exports/vite/index.js#L262
with this:
root: posixify(cwd),
If anyone is interested, I got AI to write a patch script to help with this until it's fixed.
Added this to package.json "scripts": { "postinstall": "node ./scripts/patch-script.js" }
and created this file.
#!/usr/bin/env node
// This will be deleted when Svelte releases this fix https://github.com/sveltejs/kit/issues/13376
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Path to the file we need to modify
const filePath = path.join(
process.cwd(),
'node_modules',
'@sveltejs',
'kit',
'src',
'exports',
'vite',
'index.js'
);
console.log(`Checking for SvelteKit file at: ${filePath}`);
// Check if the file exists
if (!fs.existsSync(filePath)) {
console.log('SvelteKit file not found. The patch was not applied.');
process.exit(0);
}
// Read the file
let content = fs.readFileSync(filePath, 'utf8');
// Look for the line we need to change
const searchString = 'root: cwd,';
const replaceString = 'root: posixify(cwd),';
// Check if the file already contains the fix
if (content.includes(replaceString)) {
console.log('The SvelteKit file is already patched.');
process.exit(0);
}
// Check if the line to change exists
if (!content.includes(searchString)) {
console.log('The line to patch was not found. Structure may have changed. Please check manually.');
process.exit(1);
}
// Apply the change
const patchedContent = content.replace(searchString, replaceString);
// Write the file back
fs.writeFileSync(filePath, patchedContent, 'utf8');
console.log('Successfully patched SvelteKit vite/index.js to use posixify(cwd).');
Any update guys?
I'm also seeing this with storybook
Seeing
The following Vite config options will be overridden by SvelteKit:
- base
since upgrading to Vitest 4. Possibly related.
The following Vite config options will be overridden by SvelteKit:
- base
- base is popping as well in my projects when I migrate from vitest v3 to v4.
Seeing
The following Vite config options will be overridden by SvelteKit: - basesince upgrading to Vitest 5. Possibly related.
Looking into this issue, it appears that Vitest >=4 sets the base path to '/' by default, which is not acceptable according to the SvelteKit docs which say:
A root-relative path that must start, but not end with / (e.g. /base-path), unless it is the empty string...
I realized this by simply appending these lines right after the console.error from the warn_overridden_config API:
console.info("config", `'${config.base}'`); // Shows '/'
console.info("resolved_config", `'${resolved_config.base}'`); // Shows ''
I foresee a few options, in case that helps future maintainers or users who stumble upon this issue:
- Wait for SvelteKit team to address the initial assumptions around Vitest
- File a PR to refine the underlying assumptions around Vitest
- Reach out to Vitest team regarding their breaking changes
- Apply a local hack to the SvelteKit logic so that it suppresses that warning only for the
basekey - Keep
vitestunder 4.x for our specific projects
An simple hack for the base issue is to reset base to '' in a plugin that runs before sveltekit. A super minimal vite.config.ts might look like:
import { defineConfig } from 'vitest/config'
import { sveltekit } from '@sveltejs/kit/vite'
export default defineConfig({
plugins: [
// fix for vitest v4 setting base to '/'
{
name: 'reset-base',
config() {
return {
base: '',
}
},
},
sveltekit(),
],
test: {
name: 'server',
environment: 'node',
include: ['src/**/*.{test,spec}.{js,ts}'],
},
})
This stops the annoying bold red message.