Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/envbuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (

func main() {
cmd := envbuilderCmd()
err := cmd.Invoke().WithOS().Run()
inv := cmd.Invoke().WithOS()
inv.Environ = options.EnvironWithoutEmptyValues(os.Environ())
err := inv.Run()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
Expand Down
287 changes: 158 additions & 129 deletions go.mod

Large diffs are not rendered by default.

964 changes: 532 additions & 432 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions log/coder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"sync"
"time"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"cdr.dev/slog/v3"
"cdr.dev/slog/v3/sloggers/sloghuman"
"github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
Expand Down
2 changes: 1 addition & 1 deletion log/coder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"cdr.dev/slog/sloggers/slogtest"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
Expand Down
21 changes: 21 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,22 @@ func skipDeprecatedOptions(options []serpent.Option) []serpent.Option {
return activeOptions
}

// EnvironWithoutEmptyValues converts environ into a serpent.Environ for
// option parsing, dropping variables with empty values. Since serpent v0.15.0, an empty
// value overrides an option's default instead of being ignored. Envbuilder
// keeps treating empty values as unset so that existing templates that pass
// empty strings keep their defaults.
func EnvironWithoutEmptyValues(environ []string) serpent.Environ {
envs := serpent.ParseEnviron(environ, "")
filtered := make(serpent.Environ, 0, len(envs))
for _, env := range envs {
if env.Value != "" {
filtered = append(filtered, env)
}
}
return filtered
}

// UnsetEnv unsets all environment variables that are used
// to configure the options.
func UnsetEnv() {
Expand All @@ -693,4 +709,9 @@ func UnsetEnv() {
// Unset the Kaniko environment variable which we set it in the
// Dockerfile to ensure correct behavior during building.
_ = os.Unsetenv("KANIKO_DIR")

// dd-trace-go (imported transitively via codersdk) sets this at init
// to propagate a per-process session ID to child processes. It must not
// leak into the workspace environment.
_ = os.Unsetenv("_DD_ROOT_GO_SESSION_ID")
}
7 changes: 7 additions & 0 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func TestEnvOptionParsing(t *testing.T) {
require.Equal(t, o.SetupScript, val)
})

t.Run("empty string keeps default", func(t *testing.T) {
t.Setenv(options.WithEnvPrefix("WORKSPACE_BASE_DIR"), "")
o := runCLI()
require.Equal(t, "/workspaces", o.WorkspaceBaseDir)
})

t.Run("int", func(t *testing.T) {
t.Setenv(options.WithEnvPrefix("CACHE_TTL_DAYS"), "7")
o := runCLI()
Expand Down Expand Up @@ -229,6 +235,7 @@ func runCLI() options.Options {
}

i := cmd.Invoke().WithOS()
i.Environ = options.EnvironWithoutEmptyValues(os.Environ())
i.Args = []string{"--help"}
fakeIO(i)
err := i.Run()
Expand Down
Loading