{"record":{"id":"4766f5e9dadcdf81","repo":"grafana/k6","slug":"invalid-environment-variable-name-s","errorCode":null,"errorMessage":"invalid environment variable name '%s'","messagePattern":"invalid environment variable name '(.+?)'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/runtime_options.go","lineNumber":69,"sourceCode":"\tenvironment map[string]string,\n) (lib.RuntimeOptions, error) {\n\t// TODO: refactor with composable helpers as a part of #883, to reduce copy-paste\n\t// TODO: get these options out of the JSON config file as well?\n\topts, err := populateRuntimeOptionsFromEnv(runtimeOptionsFromFlags(flags), environment)\n\tif err != nil {\n\t\treturn opts, err\n\t}\n\n\t// Set/overwrite environment variables with custom user-supplied values\n\tenvVars, err := flags.GetStringArray(\"env\")\n\tif err != nil {\n\t\treturn opts, err\n\t}\n\tfor _, kv := range envVars {\n\t\tk, v := state.ParseEnvKeyValue(kv)\n\t\t// Allow only alphanumeric ASCII variable names for now\n\t\tif !userEnvVarName.MatchString(k) {\n\t\t\treturn opts, fmt.Errorf(\"invalid environment variable name '%s'\", k)\n\t\t}\n\t\topts.Env[k] = v\n\t}\n\n\treturn opts, nil\n}\n\nfunc runtimeOptionsFromFlags(flags *pflag.FlagSet) lib.RuntimeOptions {\n\topts := lib.RuntimeOptions{\n\t\tTestType:                  getNullString(flags, \"type\"),\n\t\tIncludeSystemEnvVars:      getNullBool(flags, \"include-system-env-vars\"),\n\t\tCompatibilityMode:         getNullString(flags, \"compatibility-mode\"),\n\t\tNoThresholds:              getNullBool(flags, \"no-thresholds\"),\n\t\tSummaryMode:               getNullString(flags, \"summary-mode\"),\n\t\tSummaryExport:             getNullString(flags, \"summary-export\"),\n\t\tNewMachineReadableSummary: getNullBool(flags, \"new-machine-readable-summary\"),\n\t\tTracesOutput:              getNullString(flags, \"traces-output\"),\n\t\tEnv:                       make(map[string]string),","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/runtime_options.go#L51-L87","documentation":"Values passed via --env / -e become script environment variables (__ENV). k6 accepts only names matching ^[a-zA-Z_][a-zA-Z0-9_]*$ (ASCII letters, digits, underscore, not starting with a digit); anything else — hyphens, dots, leading digits, unicode — is rejected before the run starts with \"invalid environment variable name '<name>'.\"","triggerScenarios":"k6 run -e 'my-var=1' script.js (hyphen), k6 run -e '2fa=on' script.js (leading digit), k6 run -e 'app.db=postgres' script.js (dot). ParseEnvKeyValue splits on the first '=' and the extracted key fails userEnvVarName.MatchString.","commonSituations":"Pasting shell-style kebab-case names into -e; splitting connection strings on '=' so the key contains symbols; migrating from tools that accept arbitrary variable names.","solutions":["Rename the variable using only letters, digits, and underscore: my-var becomes MY_VAR","Quote the whole assignment so the shell passes it intact: k6 run -e 'MY_VAR=value with spaces' script.js","If you need arbitrary keys, ship them via a JSON file loaded with open() inside the script instead of --env"],"exampleFix":"# before\nk6 run -e my-var=1 script.js\n# after\nk6 run -e MY_VAR=1 script.js","handlingStrategy":"validation","validationCode":"# Validate -e names exactly like k6 does (^[a-zA-Z_][a-zA-Z0-9_]*$)\nfor kv in \"$@\"; do\n  k=${kv%%=*}\n  echo \"$k\" | grep -Eq '^[a-zA-Z_][a-zA-Z0-9_]*$' || { echo \"invalid env name: $k\" >&2; exit 2; }\ndone\nk6 run -e \"$@\" script.js","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use SCREAMING_SNAKE_CASE for --env names — it matches the regex by construction","Quote -e arguments so the shell passes them intact","Keep a single source of truth for env names in the repo"],"tags":["cli","environment","configuration","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}