Skip to content

Commit

Permalink
fix(config): improved warning when root path includes bad characters (#…
Browse files Browse the repository at this point in the history
…15761)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
poppa and bluwy committed Feb 7, 2024
1 parent 3ee4e7b commit 1c0dc3d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
if: runner.os == 'Windows' && steps.changed-files.outputs.only_changed != 'true'
run: |
echo "PLAYWRIGHT_BROWSERS_PATH=$HOME\.cache\playwright-bin" >> $env:GITHUB_ENV
$env:PLAYWRIGHT_VERSION="$(pnpm ls --depth 0 --json -w playwright-chromium | jq --raw-output '.[0].devDependencies[\"playwright-chromium\"].version')"
$env:PLAYWRIGHT_VERSION="$(pnpm ls --depth 0 --json -w playwright-chromium | jq --raw-output '.[0].devDependencies["playwright-chromium"].version')"
echo "PLAYWRIGHT_VERSION=$env:PLAYWRIGHT_VERSION" >> $env:GITHUB_ENV
- name: Cache Playwright's binary
Expand Down
15 changes: 14 additions & 1 deletion packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { InlineConfig } from '..'
import type { PluginOption, UserConfig, UserConfigExport } from '../config'
import { defineConfig, resolveConfig } from '../config'
import { resolveEnvPrefix } from '../env'
import { mergeConfig } from '../publicUtils'
import { createLogger, mergeConfig } from '../publicUtils'

describe('mergeConfig', () => {
test('handles configs with different alias schemas', () => {
Expand Down Expand Up @@ -332,4 +332,17 @@ describe('resolveConfig', () => {
expect(results1.clearScreen).toBe(false)
expect(results2.clearScreen).toBe(false)
})

test('resolveConfig with root path including "#" and "?" should warn ', async () => {
expect.assertions(1)

const logger = createLogger('info')
logger.warn = (str) => {
expect(str).to.include(
'Consider renaming the directory to remove the characters',
)
}

await resolveConfig({ root: './inc?ud#s', customLogger: logger }, 'build')
})
})
41 changes: 31 additions & 10 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,34 @@ export type ResolveFn = (
ssr?: boolean,
) => Promise<string | undefined>

/**
* Check and warn if `path` includes characters that don't work well in Vite,
* such as `#` and `?`.
*/
function checkBadCharactersInPath(path: string, logger: Logger): void {
const badChars = []

if (path.includes('#')) {
badChars.push('#')
}
if (path.includes('?')) {
badChars.push('?')
}

if (badChars.length > 0) {
const charString = badChars.map((c) => `"${c}"`).join(' and ')
const inflectedChars = badChars.length > 1 ? 'characters' : 'character'

logger.warn(
colors.yellow(
`The project root contains the ${charString} ${inflectedChars} (${colors.cyan(
path,
)}), which may not work when running Vite. Consider renaming the directory to remove the characters.`,
),
)
}
}

export async function resolveConfig(
inlineConfig: InlineConfig,
command: 'build' | 'serve',
Expand Down Expand Up @@ -477,15 +505,8 @@ export async function resolveConfig(
const resolvedRoot = normalizePath(
config.root ? path.resolve(config.root) : process.cwd(),
)
if (resolvedRoot.includes('#')) {
logger.warn(
colors.yellow(
`The project root contains the "#" character (${colors.cyan(
resolvedRoot,
)}), which may not work when running Vite. Consider renaming the directory to remove the "#".`,
),
)
}

checkBadCharactersInPath(resolvedRoot, logger)

const clientAlias = [
{
Expand Down Expand Up @@ -1258,7 +1279,7 @@ function optimizeDepsDisabledBackwardCompatibility(
}
resolved.logger.warn(
colors.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
To disable the deps optimizer, set ${optimizeDepsPath}optimizeDeps.noDiscovery to true and ${optimizeDepsPath}optimizeDeps.include as undefined or empty.
To disable the deps optimizer, set ${optimizeDepsPath}optimizeDeps.noDiscovery to true and ${optimizeDepsPath}optimizeDeps.include as undefined or empty.
Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
${
commonjsPluginDisabled
Expand Down

0 comments on commit 1c0dc3d

Please sign in to comment.