joho / godotenv

A Go port of Ruby's dotenv library (Loads environment variables from .env files)
http://godoc.org/github.com/joho/godotenv
MIT License
8.49k stars 405 forks source link

(possible) bug in parser right-trimming escaped quotes? #226

Open monerowner opened 9 months ago

monerowner commented 9 months ago

The call to bytes.TrimRightFunc(src[0:i], trimFunc) in parser.go's extractVarValue removes a single escaped quote off the right side of the var value (if such a quote exists and it is the same as the quotes used to enclose the value). Is this intended behavior or a bug?

package main

import (
    "fmt"
    "github.com/joho/godotenv"
)

func main() {
    m := map[string]string{"foo": `""`}
    // Shows up in .env as foo="\"\""
    godotenv.Write(m, ".env")

    env, _ := godotenv.Read(".env")
    // Expecting ""
        // Instead prints "\
        // This is because the escaping \ remains after the quote's removed
    fmt.Println(env["foo"])
}