From b643231fc7dfa7a7e1c83c01310fa5745e59c948 Mon Sep 17 00:00:00 2001 From: Charan Rathore <180254320+charan-rathore@users.noreply.github.com> Date: Thu, 1 Oct 2026 22:27:41 +0530 Subject: [PATCH] edit, provider: stop inline comments leaking into dotenv values (#439) * edit,provider: stop reading inline comments as part of dotenv values * provider: read Google env values through edit.GetEnvFile --- internal/edit/env.go | 6 +++- internal/edit/env_comment_test.go | 13 ++++++++ internal/provider/google.go | 25 ++------------- internal/provider/google_env_comment_test.go | 32 ++++++++++++++++++++ 4 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 internal/edit/env_comment_test.go create mode 100644 internal/provider/google_env_comment_test.go diff --git a/internal/edit/env.go b/internal/edit/env.go index 9d5d0c47f..bbe5f2f88 100644 --- a/internal/edit/env.go +++ b/internal/edit/env.go @@ -20,7 +20,11 @@ func GetEnvFile(path, key string) (string, bool) { if v, ok := unquote(m[2]); ok { return v, true } - return m[2], true + v := m[2] + if i := strings.IndexByte(v, '#'); i >= 0 { + v = strings.TrimSpace(v[:i]) + } + return v, true } } return "", false diff --git a/internal/edit/env_comment_test.go b/internal/edit/env_comment_test.go new file mode 100644 index 000000000..afd0e5573 --- /dev/null +++ b/internal/edit/env_comment_test.go @@ -0,0 +1,13 @@ +package edit + +import "testing" + +func TestEnvInlineComments(t *testing.T) { + for _, tc := range []struct{ line, want string }{{"GEMINI_API_KEY=abc123 # my key\n", "abc123"}, {"GEMINI_API_KEY=abc123#comment\n", "abc123"}, {"export GEMINI_API_KEY='abc#123' # my key\n", "abc#123"}, {"GEMINI_API_KEY=\"abc#123\" # my key\n", "abc#123"}, {"GEMINI_API_KEY= # none\n", ""}} { + p := tmpFile(t, ".env", tc.line) + got, ok := GetEnvFile(p, "GEMINI_API_KEY") + if !ok || got != tc.want { + t.Fatalf("line %q: got %q %v, want %q", tc.line, got, ok, tc.want) + } + } +} diff --git a/internal/provider/google.go b/internal/provider/google.go index de4d49470..6d7d675d2 100644 --- a/internal/provider/google.go +++ b/internal/provider/google.go @@ -35,6 +35,7 @@ import ( "time" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/edit" ) // CodeAssist is Google's Code Assist API, which magpie only speaks @@ -181,28 +182,8 @@ func googleClientOf(idToken string) string { // envFileValue is one KEY=value of a dotenv file. func envFileValue(path, key string) string { - f, err := os.Open(path) - if err != nil { - return "" - } - defer f.Close() - sc := bufio.NewScanner(f) - for sc.Scan() { - line := strings.TrimSpace(sc.Text()) - line = strings.TrimPrefix(line, "export ") - k, v, ok := strings.Cut(line, "=") - if !ok || strings.TrimSpace(k) != key { - continue - } - v = strings.TrimSpace(v) - if uq, err := strconv.Unquote(v); err == nil { - v = uq - } else { - v = strings.Trim(v, `'`) - } - return v - } - return "" + v, _ := edit.GetEnvFile(path, key) + return v } // savedGoogleProject is the project the user named for the agent's own diff --git a/internal/provider/google_env_comment_test.go b/internal/provider/google_env_comment_test.go new file mode 100644 index 000000000..0bc957ce3 --- /dev/null +++ b/internal/provider/google_env_comment_test.go @@ -0,0 +1,32 @@ +package provider + +import ( + "os" + "path/filepath" + "testing" +) + +func TestGoogleEnvInlineComment(t *testing.T) { + p := filepath.Join(t.TempDir(), ".env") + os.WriteFile(p, []byte("GOOGLE_CLOUD_PROJECT=my-project-123 # work project\n"), 0o600) + if got := envFileValue(p, "GOOGLE_CLOUD_PROJECT"); got != "my-project-123" { + t.Fatalf("got %q", got) + } +} + +func TestEnvFileValueQuotedInlineComment(t *testing.T) { + for _, tc := range []struct{ name, line, want string }{ + {"double quoted", `GOOGLE_CLOUD_PROJECT="my-proj" # work`, "my-proj"}, + {"single quoted hash", `GOOGLE_CLOUD_PROJECT='a#b' # c`, "a#b"}, + } { + t.Run(tc.name, func(t *testing.T) { + p := filepath.Join(t.TempDir(), ".env") + if err := os.WriteFile(p, []byte(tc.line+"\n"), 0o600); err != nil { + t.Fatal(err) + } + if got := envFileValue(p, "GOOGLE_CLOUD_PROJECT"); got != tc.want { + t.Fatalf("envFileValue = %q, want %q", got, tc.want) + } + }) + } +}