Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'node:http'
import path from 'node:path'
import fs from 'node:fs'
import { afterEach, describe, expect, test } from 'vitest'
import { afterEach, describe, expect, test, vi } from 'vitest'
import type { InlineConfig, PluginOption } from '..'
import type { UserConfig, UserConfigExport } from '../config'
import { defineConfig, loadConfigFromFile, resolveConfig } from '../config'
Expand Down Expand Up @@ -632,9 +632,22 @@ describe('resolveEnvPrefix', () => {
expect(() => resolveEnvPrefix(config)).toThrow()
})

test(`show a warning message if envPrefix contains a whitespace`, () => {
const consoleWarnSpy = vi
.spyOn(console, 'warn')
.mockImplementation(() => {})
let config: UserConfig = { envPrefix: 'WITH SPACE' }
resolveEnvPrefix(config)
expect(consoleWarnSpy).toHaveBeenCalled()
config = { envPrefix: ['CUSTOM_', 'ANOTHER SPACE'] }
resolveEnvPrefix(config)
expect(consoleWarnSpy).toHaveBeenCalledTimes(2)
consoleWarnSpy.mockRestore()
})

test('should work correctly for valid envPrefix value', () => {
const config: UserConfig = { envPrefix: [' ', 'CUSTOM_'] }
expect(resolveEnvPrefix(config)).toMatchObject([' ', 'CUSTOM_'])
const config: UserConfig = { envPrefix: ['CUSTOM_'] }
expect(resolveEnvPrefix(config)).toMatchObject(['CUSTOM_'])
})
})

Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs'
import path from 'node:path'
import { parse } from 'dotenv'
import { type DotenvPopulateInput, expand } from 'dotenv-expand'
import colors from 'picocolors'
import { arraify, createDebugger, normalizePath, tryStatSync } from './utils'
import type { UserConfig } from './config'

Expand Down Expand Up @@ -99,5 +100,13 @@ export function resolveEnvPrefix({
`envPrefix option contains value '', which could lead unexpected exposure of sensitive information.`,
)
}
if (envPrefix.some((prefix) => /\s/.test(prefix))) {
// eslint-disable-next-line no-console
console.warn(
Comment thread
sapphi-red marked this conversation as resolved.
colors.yellow(
`[vite] Warning: envPrefix option contains values with whitespace, which does not work in practice.`,
),
)
}
return envPrefix
}