{"record":{"id":"c921c97505952f14","repo":"golang/go","slug":"argument-q-contains-both-single-and-double-quotes","errorCode":null,"errorMessage":"argument %q contains both single and double quotes and cannot be quoted","messagePattern":"argument %q contains both single and double quotes and cannot be quoted","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/internal/quoted/quoted.go","lineNumber":99,"sourceCode":"\t\t\t\tsawDoubleQuote = true\n\t\t\t}\n\t\t}\n\t\tswitch {\n\t\tcase !sawSpace && !sawSingleQuote && !sawDoubleQuote:\n\t\t\tbuf = append(buf, arg...)\n\n\t\tcase !sawSingleQuote:\n\t\t\tbuf = append(buf, '\\'')\n\t\t\tbuf = append(buf, arg...)\n\t\t\tbuf = append(buf, '\\'')\n\n\t\tcase !sawDoubleQuote:\n\t\t\tbuf = append(buf, '\"')\n\t\t\tbuf = append(buf, arg...)\n\t\t\tbuf = append(buf, '\"')\n\n\t\tdefault:\n\t\t\treturn \"\", fmt.Errorf(\"argument %q contains both single and double quotes and cannot be quoted\", arg)\n\t\t}\n\t}\n\treturn string(buf), nil\n}\n\n// A Flag parses a list of string arguments encoded with Join.\n// It is useful for flags like cmd/link's -extldflags.\ntype Flag []string\n\nvar _ flag.Value = (*Flag)(nil)\n\nfunc (f *Flag) Set(v string) error {\n\tfs, err := Split(v)\n\tif err != nil {\n\t\treturn err\n\t}\n\t*f = fs[:len(fs):len(fs)]\n\treturn nil","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/internal/quoted/quoted.go#L81-L117","documentation":"Thrown by the Join function when an argument contains both single-quote (') and double-quote (\") characters. Join tries to wrap the argument in single quotes if it contains a double quote, or double quotes if it contains a single quote. If both are present, neither quoting strategy can produce a reversible result, so the function refuses to encode it.","triggerScenarios":"An argument string in the slice passed to Join contains at least one ' character AND at least one \" character. The function checks sawSingleQuote and sawDoubleQuote flags; when both are true and neither single-quote-only nor double-quote-only branches apply, the default case fires.","commonSituations":"Building linker flags or shell command strings where an argument legitimately contains mixed quote characters — e.g. a path or value with embedded SQL-like strings, JSON snippets, or localized text. Rare in practice but unfixable without escaping support.","solutions":["Remove one type of quote character from the argument before calling Join.","Replace embedded quotes with escaped alternatives or use a different delimiter strategy.","If both quote types are essential, avoid quoted.Join and use a custom serialization with proper escaping."],"exampleFix":"// Before (broken): quoted.Join([]string{`it's a \"test\"`})\n// After (fixed):  quoted.Join([]string{`it\\'s a test`})\n// (remove one quote type so Join can wrap the other)","handlingStrategy":"validation","validationCode":"// Check for mixed quotes before calling Join:\nfunc canQuote(arg string) bool {\n    hasSingle := strings.Contains(arg, \"'\")\n    hasDouble := strings.Contains(arg, \"\\\"\")\n    return !(hasSingle && hasDouble)\n}\n\n// Usage:\n// for _, arg := range args {\n//     if !canQuote(arg) {\n//         return fmt.Errorf(\"cannot quote argument with mixed quotes: %q\", arg)\n//     }\n// }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid embedding both single and double quotes in a single argument.","Sanitize or replace one quote type before encoding if both are present.","Consider a custom serialization format for arguments containing mixed quotes."],"tags":["quoted","encoding","mixed-quotes","go-compiler-internal"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}