{"record":{"id":"2c73085ae2fa5eda","repo":"siyuan-note/siyuan","slug":"duplicate-variable-q","errorCode":null,"errorMessage":"duplicate variable %q","messagePattern":"duplicate variable %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/client/mcp.go","lineNumber":568,"sourceCode":"\t\t\treturn err\n\t\t}\n\t\tkey := environmentKey(name, goos)\n\t\tif inherited[key] {\n\t\t\treturn fmt.Errorf(\"duplicate inherited variable %q\", name)\n\t\t}\n\t\tinherited[key] = true\n\t}\n\texplicit := map[string]bool{}\n\tfor name, value := range server.Env {\n\t\tif err := validateEnvironmentName(name); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif strings.ContainsRune(value, '\\x00') {\n\t\t\treturn fmt.Errorf(\"variable %q contains NUL\", name)\n\t\t}\n\t\tkey := environmentKey(name, goos)\n\t\tif explicit[key] {\n\t\t\treturn fmt.Errorf(\"duplicate variable %q\", name)\n\t\t}\n\t\texplicit[key] = true\n\t}\n\treturn nil\n}\n\n// ValidateMCPServerEnvironment 校验当前平台上的 stdio 环境变量配置。\nfunc ValidateMCPServerEnvironment(server conf.MCPServer) error {\n\treturn validateMCPServerEnvironment(server, runtime.GOOS)\n}\n\nfunc defaultMCPEnvironmentNames(goos string) []string {\n\tif goos == \"windows\" {\n\t\treturn []string{\"APPDATA\", \"HOMEDRIVE\", \"HOMEPATH\", \"LOCALAPPDATA\", \"PATH\", \"PATHEXT\",\n\t\t\t\"PROCESSOR_ARCHITECTURE\", \"PROGRAMFILES\", \"SYSTEMDRIVE\", \"SYSTEMROOT\", \"TEMP\", \"USERNAME\", \"USERPROFILE\"}\n\t}\n\treturn []string{\"HOME\", \"LOGNAME\", \"PATH\", \"SHELL\", \"TERM\"}\n}","sourceCodeStart":550,"sourceCodeEnd":586,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/client/mcp.go#L550-L586","documentation":"validateMCPServerEnvironment rejects a server whose explicit Env map contains the same variable name twice. Keys in the Go map are already unique, so this triggers when two names normalize to the same environmentKey — i.e. on Windows, names differing only by case (PATH vs Path). Explicitly setting the same variable twice is ambiguous, so it fails fast.","triggerScenarios":"conf.MCPServer.Env has two keys that map to the same environmentKey (case-insensitive match on Windows) when ValidateMCPServerEnvironment runs or a stdio server is launched.","commonSituations":"On Windows, adding both 'Path' and 'PATH' as explicit env entries; a config merge/import producing near-duplicate keys; hand-editing conf.json with case-variant duplicates.","solutions":["Open the MCP server settings and remove one of the duplicated environment entries","On Windows, check for case-only variants (PATH vs Path) and keep a single entry","If the duplicate originates from an imported config, deduplicate the env object keys in conf.json and restart the server connection"],"exampleFix":"// before (Windows)\nserver.Env[\"PATH\"] = \"C:\\\\tools\"\nserver.Env[\"Path\"] = \"C:\\\\other\"\n// after\nserver.Env[\"PATH\"] = \"C:\\\\tools;C:\\\\other\"","handlingStrategy":"validation","validationCode":"// normalize keys case-insensitively on Windows before building Env\nkey := name\nif runtime.GOOS == \"windows\" { key = strings.ToUpper(name) }\nif _, dup := envByKey[key]; dup { return fmt.Errorf(\"duplicate env %q\", name) }\nenvByKey[key] = value","typeGuard":null,"tryCatchPattern":null,"preventionTips":["On Windows, canonicalize env key casing (e.g. always uppercase) when creating entries","Deduplicate env keys after config merge/import","Validate with ValidateMCPServerEnvironment before saving"],"tags":["mcp","environment-variables","duplicate-config"],"backgroundTag":"conflicting-config-options","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}