Skip to content

fix(agent/agentcontainers): respect ignore files #19016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: respect ~/.gitconfig's core.excludesFile option
  • Loading branch information
DanielleMaywood committed Jul 24, 2025
commit 0a438a87ce7192b68bbd1773090a67a5b42a3ba1
4 changes: 3 additions & 1 deletion agent/agentcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,14 @@
}

func (api *API) discoverDevcontainersInProject(projectPath string) error {
globalPatterns, err := ignore.LoadGlobalPatterns(api.fs)

Check failure on line 474 in agent/agentcontainers/api.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to err (ineffassign)

patterns, err := ignore.ReadPatterns(api.fs, projectPath)
if err != nil {
return xerrors.Errorf("read git ignore patterns: %w", err)
}

matcher := gitignore.NewMatcher(patterns)
matcher := gitignore.NewMatcher(append(globalPatterns, patterns...))

devcontainerConfigPaths := []string{
"/.devcontainer/devcontainer.json",
Expand Down
28 changes: 27 additions & 1 deletion agent/agentcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
Expand Down Expand Up @@ -3211,6 +3212,9 @@ func TestDevcontainerDiscovery(t *testing.T) {
// repositories to find any `.devcontainer/devcontainer.json`
// files. These tests are to validate that behavior.

homeDir, err := os.UserHomeDir()
require.NoError(t, err)

tests := []struct {
name string
agentDir string
Expand Down Expand Up @@ -3402,6 +3406,28 @@ func TestDevcontainerDiscovery(t *testing.T) {
},
},
},
{
name: "RespectHomeGitConfig",
agentDir: homeDir,
fs: map[string]string{
"/tmp/.gitignore": "node_modules/",
filepath.Join(homeDir, ".gitconfig"): `
[core]
excludesFile = /tmp/.gitignore
`,

filepath.Join(homeDir, ".git/HEAD"): "",
filepath.Join(homeDir, ".devcontainer.json"): "",
filepath.Join(homeDir, "node_modules/y/.devcontainer.json"): "",
},
expected: []codersdk.WorkspaceAgentDevcontainer{
{
WorkspaceFolder: homeDir,
ConfigPath: filepath.Join(homeDir, ".devcontainer.json"),
Status: codersdk.WorkspaceAgentDevcontainerStatusStopped,
},
},
},
}

initFS := func(t *testing.T, files map[string]string) afero.Fs {
Expand Down Expand Up @@ -3454,7 +3480,7 @@ func TestDevcontainerDiscovery(t *testing.T) {
err := json.NewDecoder(rec.Body).Decode(&got)
require.NoError(t, err)

return len(got.Devcontainers) == len(tt.expected)
return len(got.Devcontainers) >= len(tt.expected)
}, testutil.WaitShort, testutil.IntervalFast, "dev containers never found")

// Now projects have been discovered, we'll allow the updater loop
Expand Down
34 changes: 34 additions & 0 deletions agent/agentcontainers/ignore/dir.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package ignore

import (
"bytes"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/go-git/go-git/v5/plumbing/format/config"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/spf13/afero"
"golang.org/x/xerrors"
)

const (
gitconfigFile = ".gitconfig"
gitignoreFile = ".gitignore"
gitInfoExcludeFile = ".git/info/exclude"
)
Expand Down Expand Up @@ -78,3 +82,33 @@ func ReadPatterns(fileSystem afero.Fs, path string) ([]gitignore.Pattern, error)

return ps, nil
}

func loadPatterns(fileSystem afero.Fs, path string) ([]gitignore.Pattern, error) {
data, err := afero.ReadFile(fileSystem, path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}

decoder := config.NewDecoder(bytes.NewBuffer(data))

conf := config.New()
if err := decoder.Decode(conf); err != nil {
return nil, xerrors.Errorf("decode config: %w", err)
}

excludes := conf.Section("core").Options.Get("excludesfile")
if excludes == "" {
return nil, nil
}

return readIgnoreFile(fileSystem, "", excludes)
}

func LoadGlobalPatterns(fileSystem afero.Fs) ([]gitignore.Pattern, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}

return loadPatterns(fileSystem, filepath.Join(home, gitconfigFile))
}
Loading