diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 178e73b8137428..2b0f6d28360987 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -45,6 +45,7 @@ import { isFilePathESM, isNodeBuiltin, isObject, + isParentDirectory, mergeAlias, mergeConfig, normalizeAlias, @@ -914,6 +915,20 @@ assetFileNames isn't equal for every build.rollupOptions.output. A single patter ) } + const resolvedBuildOutDir = normalizePath( + path.resolve(resolved.root, resolved.build.outDir), + ) + if ( + isParentDirectory(resolvedBuildOutDir, resolved.root) || + resolvedBuildOutDir === resolved.root + ) { + resolved.logger.warn( + colors.yellow(` +(!) build.outDir must not be the same directory of root or a parent directory of root as this could cause Vite to overwriting source files with build outputs. +`), + ) + } + return resolved }