diff --git a/packages/vite/src/node/baseEnvironment.ts b/packages/vite/src/node/baseEnvironment.ts index e02b1a39e0c374..730eb26e9e6d7b 100644 --- a/packages/vite/src/node/baseEnvironment.ts +++ b/packages/vite/src/node/baseEnvironment.ts @@ -54,6 +54,13 @@ export class PartialEnvironment { topLevelConfig: ResolvedConfig, options: ResolvedEnvironmentOptions = topLevelConfig.environments[name], ) { + // only allow some characters so that we can use name without escaping for directory names + // and make users easier to access with `environments.*` + if (!/^[\w$]+$/.test(name)) { + throw new Error( + `Invalid environment name "${name}". Environment names must only contain alphanumeric characters and "$", "_".`, + ) + } this.name = name this._topLevelConfig = topLevelConfig this._options = options