Skip to content

Commit 9fcde3c

Browse files
bteabluwygraphite-app[bot]sapphi-red
authored
feat: warn if envPrefix contains spaces (#21292)
Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> Co-authored-by: 翠 <[email protected]>
1 parent dac242a commit 9fcde3c

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/vite/src/node/__tests__/config.spec.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import http from 'node:http'
22
import path from 'node:path'
33
import fs from 'node:fs'
4-
import { afterEach, describe, expect, test } from 'vitest'
4+
import { afterEach, describe, expect, test, vi } from 'vitest'
55
import type { InlineConfig, PluginOption } from '..'
66
import type { UserConfig, UserConfigExport } from '../config'
77
import { defineConfig, loadConfigFromFile, resolveConfig } from '../config'
@@ -632,9 +632,22 @@ describe('resolveEnvPrefix', () => {
632632
expect(() => resolveEnvPrefix(config)).toThrow()
633633
})
634634

635+
test(`show a warning message if envPrefix contains a whitespace`, () => {
636+
const consoleWarnSpy = vi
637+
.spyOn(console, 'warn')
638+
.mockImplementation(() => {})
639+
let config: UserConfig = { envPrefix: 'WITH SPACE' }
640+
resolveEnvPrefix(config)
641+
expect(consoleWarnSpy).toHaveBeenCalled()
642+
config = { envPrefix: ['CUSTOM_', 'ANOTHER SPACE'] }
643+
resolveEnvPrefix(config)
644+
expect(consoleWarnSpy).toHaveBeenCalledTimes(2)
645+
consoleWarnSpy.mockRestore()
646+
})
647+
635648
test('should work correctly for valid envPrefix value', () => {
636-
const config: UserConfig = { envPrefix: [' ', 'CUSTOM_'] }
637-
expect(resolveEnvPrefix(config)).toMatchObject([' ', 'CUSTOM_'])
649+
const config: UserConfig = { envPrefix: ['CUSTOM_'] }
650+
expect(resolveEnvPrefix(config)).toMatchObject(['CUSTOM_'])
638651
})
639652
})
640653

packages/vite/src/node/env.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import { parse } from 'dotenv'
44
import { type DotenvPopulateInput, expand } from 'dotenv-expand'
5+
import colors from 'picocolors'
56
import { arraify, createDebugger, normalizePath, tryStatSync } from './utils'
67
import type { UserConfig } from './config'
78

@@ -101,5 +102,13 @@ export function resolveEnvPrefix({
101102
`envPrefix option contains value '', which could lead unexpected exposure of sensitive information.`,
102103
)
103104
}
105+
if (envPrefix.some((prefix) => /\s/.test(prefix))) {
106+
// eslint-disable-next-line no-console
107+
console.warn(
108+
colors.yellow(
109+
`[vite] Warning: envPrefix option contains values with whitespace, which does not work in practice.`,
110+
),
111+
)
112+
}
104113
return envPrefix
105114
}

0 commit comments

Comments
 (0)