Repository navigation
Description
Description
While I was reading the code for the remoteResolver, which is intended to call git to get the list of remotes, and then do some filtering and sorting, I noticed this line:
cli/pkg/cmd/factory/remote_resolver.go
Line 72 in 5402e20
| cachedRemotes := resolvedRemotes.FilterByHosts(hosts) |
I don't believe this works as intended, because the walrus operator results in cachedRemotes being shadowed since it is in an inner scope. See https://go.dev/play/p/YIENXfxFfbP for an example of shadowing.
I do believe on first read that errors are cached correctly:
cli/pkg/cmd/factory/remote_resolver.go
Line 41 in 5402e20
| remotesError = errors.New("no git remotes found") |
I also believe the code before this was caching correctly:
Here's a minimal test to demonstrate that the cache branch isn't being hit in the happy path. If made into a real test, it should probably check that the correct remotes are returned as well.
func TestRemoteResolverCachesRemotes(t *testing.T) {
var readRemotesCalled bool
rr := &remoteResolver{
readRemotes: func() (git.RemoteSet, error) {
if readRemotesCalled {
return git.RemoteSet{}, errors.New("readRemotes should only be called once")
}
readRemotesCalled = true
return git.RemoteSet{
git.NewRemote("origin", "https://github.com/owner/repo.git"),
}, nil
},
getConfig: func() (gh.Config, error) {
cfg := &ghmock.ConfigMock{}
cfg.AuthenticationFunc = func() gh.AuthConfig {
authCfg := &config.AuthConfig{}
authCfg.SetHosts([]string{"github.com"})
authCfg.SetDefaultHost("github.com", "default")
return authCfg
}
return cfg, nil
},
urlTranslator: identityTranslator{},
}
resolver := rr.Resolver()
_, err := resolver()
require.NoError(t, err)
_, err = resolver()
require.NoError(t, err)
}Expected Output
The expected output for this issue is that:
- A comment is left confirming or refuting my statements above
- A comment that indicates the author has confirmed that caching is in fact safe (this code has been this way for 4 years)
- The code is fixed, if necessary
- There are tests
Activity
BLOCkCHAINMASTERINC commentedon Dec 19, 2024
iamazeem commentedon Feb 16, 2025
Hey @williammartin,
Verified that caching isn't working as intended.
Checked with copilot also. Submitted a PR with tests.