Skip to content

Commit f7ccfa2

Browse files
authored
feat: Set CODER=true in workspaces (#3637)
Fixes #2340
1 parent 8343a4f commit f7ccfa2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

agent/agent.go

+4
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
394394
if err != nil {
395395
return nil, xerrors.Errorf("getting os executable: %w", err)
396396
}
397+
// Set environment variables reliable detection of being inside a
398+
// Coder workspace.
399+
cmd.Env = append(cmd.Env, "CODER=true")
400+
397401
cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", username))
398402
// Git on Windows resolves with UNIX-style paths.
399403
// If using backslashes, it's unable to find the executable.

agent/agent_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,26 @@ func TestAgent(t *testing.T) {
232232
require.Equal(t, expect, strings.TrimSpace(string(output)))
233233
})
234234

235+
t.Run("Coder env vars", func(t *testing.T) {
236+
t.Parallel()
237+
238+
for _, key := range []string{"CODER"} {
239+
key := key
240+
t.Run(key, func(t *testing.T) {
241+
t.Parallel()
242+
243+
session := setupSSHSession(t, agent.Metadata{})
244+
command := "sh -c 'echo $" + key + "'"
245+
if runtime.GOOS == "windows" {
246+
command = "cmd.exe /c echo %" + key + "%"
247+
}
248+
output, err := session.Output(command)
249+
require.NoError(t, err)
250+
require.NotEmpty(t, strings.TrimSpace(string(output)))
251+
})
252+
}
253+
})
254+
235255
t.Run("StartupScript", func(t *testing.T) {
236256
t.Parallel()
237257
tempPath := filepath.Join(t.TempDir(), "content.txt")

0 commit comments

Comments
 (0)