pulumi/sdk/go/auto/git.go

189 lines
6.7 KiB
Go
Raw Permalink Normal View History

2020-08-27 17:43:23 +00:00
// Copyright 2016-2020, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auto
import (
"context"
"errors"
"fmt"
"path/filepath"
"strings"
Fix datarace in setupGitRepo (#14643) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes a datarace in setupGitRepo seen in CI: ``` === FAIL: go/auto TestNewStackRemoteSource (44.69s) ================== WARNING: DATA RACE Read at 0x000002d35650 by goroutine 103: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:125 +0xf78 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.NewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1114 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestNewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:465 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Previous write at 0x000002d35650 by goroutine 104: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:139 +0x11d1 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.UpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1133 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestUpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:559 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Goroutine 103 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 Goroutine 104 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 ================== testing.go:1465: race detected during execution of test ``` Pretty clear culprit of mutating a global variable in the go-git module. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works - This was caught be existing tests <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-24 14:34:43 +00:00
"sync"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)
Fix datarace in setupGitRepo (#14643) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes a datarace in setupGitRepo seen in CI: ``` === FAIL: go/auto TestNewStackRemoteSource (44.69s) ================== WARNING: DATA RACE Read at 0x000002d35650 by goroutine 103: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:125 +0xf78 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.NewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1114 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestNewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:465 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Previous write at 0x000002d35650 by goroutine 104: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:139 +0x11d1 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.UpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1133 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestUpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:559 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Goroutine 103 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 Goroutine 104 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 ================== testing.go:1465: race detected during execution of test ``` Pretty clear culprit of mutating a global variable in the go-git module. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works - This was caught be existing tests <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-24 14:34:43 +00:00
var transportMutex sync.Mutex
func setupGitRepo(ctx context.Context, workDir string, repoArgs *GitRepo) (string, error) {
cloneOptions := &git.CloneOptions{
RemoteName: "origin", // be explicit so we can require it in remote refs
URL: repoArgs.URL,
}
if repoArgs.Shallow {
cloneOptions.Depth = 1
cloneOptions.SingleBranch = true
}
if repoArgs.Auth != nil {
authDetails := repoArgs.Auth
// Each of the authentication options are mutually exclusive so let's check that only 1 is specified
if authDetails.SSHPrivateKeyPath != "" && authDetails.Username != "" ||
authDetails.PersonalAccessToken != "" && authDetails.Username != "" ||
authDetails.PersonalAccessToken != "" && authDetails.SSHPrivateKeyPath != "" ||
authDetails.Username != "" && authDetails.SSHPrivateKey != "" {
return "", errors.New("please specify one authentication option of `Personal Access Token`, " +
"`Username\\Password`, `SSH Private Key Path` or `SSH Private Key`")
}
// Firstly we will try to check that an SSH Private Key Path has been specified
if authDetails.SSHPrivateKeyPath != "" {
publicKeys, err := ssh.NewPublicKeysFromFile("git", repoArgs.Auth.SSHPrivateKeyPath, repoArgs.Auth.Password)
if err != nil {
return "", fmt.Errorf("unable to use SSH Private Key Path: %w", err)
}
cloneOptions.Auth = publicKeys
}
// Then we check if the details of a SSH Private Key as passed
if authDetails.SSHPrivateKey != "" {
publicKeys, err := ssh.NewPublicKeys("git", []byte(repoArgs.Auth.SSHPrivateKey), repoArgs.Auth.Password)
if err != nil {
return "", fmt.Errorf("unable to use SSH Private Key: %w", err)
}
cloneOptions.Auth = publicKeys
}
// Then we check to see if a Personal Access Token has been specified
// the username for use with a PAT can be *anything* but an empty string
// so we are setting this to `git`
if authDetails.PersonalAccessToken != "" {
cloneOptions.Auth = &http.BasicAuth{
Username: "git",
Password: repoArgs.Auth.PersonalAccessToken,
}
}
// then we check to see if a username and a password has been specified
if authDetails.Password != "" && authDetails.Username != "" {
cloneOptions.Auth = &http.BasicAuth{
Username: repoArgs.Auth.Username,
Password: repoArgs.Auth.Password,
}
}
}
// *Repository.Clone() will do appropriate fetching given a branch name. We must deal with
// different varieties, since people have been advised to use these as a workaround while only
// "refs/heads/<default>" worked.
//
// If a reference name is not supplied, then .Clone will fetch all refs (and all objects
// referenced by those), and checking out a commit later will work as expected.
if repoArgs.Branch != "" {
refName := plumbing.ReferenceName(repoArgs.Branch)
switch {
case refName.IsRemote(): // e.g., refs/remotes/origin/branch
shorter := refName.Short() // this gives "origin/branch"
parts := strings.SplitN(shorter, "/", 2)
if len(parts) == 2 && parts[0] == "origin" {
refName = plumbing.NewBranchReferenceName(parts[1])
} else {
return "", fmt.Errorf("a remote ref must begin with 'refs/remote/origin/', but got %q", repoArgs.Branch)
}
case refName.IsTag(): // looks like `refs/tags/v1.0.0` -- respect this even though the field is `.Branch`
// nothing to do
case !refName.IsBranch(): // not a remote, not refs/heads/branch; treat as a simple branch name
refName = plumbing.NewBranchReferenceName(repoArgs.Branch)
default:
// already looks like a full branch name, so use as is
}
cloneOptions.ReferenceName = refName
}
Fix datarace in setupGitRepo (#14643) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes a datarace in setupGitRepo seen in CI: ``` === FAIL: go/auto TestNewStackRemoteSource (44.69s) ================== WARNING: DATA RACE Read at 0x000002d35650 by goroutine 103: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:125 +0xf78 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.NewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1114 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestNewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:465 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Previous write at 0x000002d35650 by goroutine 104: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:139 +0x11d1 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.UpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1133 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestUpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:559 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Goroutine 103 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 Goroutine 104 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 ================== testing.go:1465: race detected during execution of test ``` Pretty clear culprit of mutating a global variable in the go-git module. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works - This was caught be existing tests <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-24 14:34:43 +00:00
// Azure DevOps requires multi_ack and multi_ack_detailed capabilities, which go-git doesn't implement.
// But: it's possible to do a full clone by saying it's _not_ _un_supported, in which case the library
// happily functions so long as it doesn't _actually_ get a multi_ack packet. See
// https://github.com/go-git/go-git/blob/v5.5.1/_examples/azure_devops/main.go.
Fix datarace in setupGitRepo (#14643) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes a datarace in setupGitRepo seen in CI: ``` === FAIL: go/auto TestNewStackRemoteSource (44.69s) ================== WARNING: DATA RACE Read at 0x000002d35650 by goroutine 103: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:125 +0xf78 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.NewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1114 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestNewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:465 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Previous write at 0x000002d35650 by goroutine 104: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:139 +0x11d1 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.UpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1133 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestUpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:559 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Goroutine 103 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 Goroutine 104 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 ================== testing.go:1465: race detected during execution of test ``` Pretty clear culprit of mutating a global variable in the go-git module. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works - This was caught be existing tests <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-24 14:34:43 +00:00
repo, err := func() (*git.Repository, error) {
// Because transport.UnsupportedCapabilities is a global variable, we need a global lock around the
// use of this.
transportMutex.Lock()
defer transportMutex.Unlock()
oldUnsupportedCaps := transport.UnsupportedCapabilities
// This check is crude, but avoids having another dependency to parse the git URL.
if strings.Contains(repoArgs.URL, "dev.azure.com") {
transport.UnsupportedCapabilities = []capability.Capability{
capability.ThinPack,
}
}
Fix datarace in setupGitRepo (#14643) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes a datarace in setupGitRepo seen in CI: ``` === FAIL: go/auto TestNewStackRemoteSource (44.69s) ================== WARNING: DATA RACE Read at 0x000002d35650 by goroutine 103: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:125 +0xf78 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.NewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1114 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestNewStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:465 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Previous write at 0x000002d35650 by goroutine 104: github.com/pulumi/pulumi/sdk/v3/go/auto.setupGitRepo() /Users/runner/work/pulumi/pulumi/sdk/go/auto/git.go:139 +0x11d1 github.com/pulumi/pulumi/sdk/v3/go/auto.NewLocalWorkspace() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:778 +0x435 github.com/pulumi/pulumi/sdk/v3/go/auto.UpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace.go:1133 +0x2c4 github.com/pulumi/pulumi/sdk/v3/go/auto.TestUpsertStackRemoteSource() /Users/runner/work/pulumi/pulumi/sdk/go/auto/local_workspace_test.go:559 +0x326 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.(*T).Run.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x44 Goroutine 103 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 Goroutine 104 (running) created at: testing.(*T).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1648 +0x82a testing.runTests.func1() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2054 +0x84 testing.tRunner() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1595 +0x238 testing.runTests() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:2052 +0x896 testing.(*M).Run() /Users/runner/hostedtoolcache/go/1.21.1/x64/src/testing/testing.go:1925 +0xb57 main.main() _testmain.go:159 +0x2e4 ================== testing.go:1465: race detected during execution of test ``` Pretty clear culprit of mutating a global variable in the go-git module. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works - This was caught be existing tests <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-24 14:34:43 +00:00
// clone
repo, err := git.PlainCloneContext(ctx, workDir, false, cloneOptions)
// Regardless of error we need to restore the UnsupportedCapabilities
transport.UnsupportedCapabilities = oldUnsupportedCaps
return repo, err
}()
if err != nil {
return "", fmt.Errorf("unable to clone repo: %w", err)
}
2020-08-19 18:13:42 +00:00
if repoArgs.CommitHash != "" {
// ensure that the commit has been fetched
err = repo.FetchContext(ctx, &git.FetchOptions{
RemoteName: "origin",
Auth: cloneOptions.Auth,
Depth: cloneOptions.Depth,
RefSpecs: []config.RefSpec{config.RefSpec(repoArgs.CommitHash + ":" + repoArgs.CommitHash)},
})
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) && !errors.Is(err, git.ErrExactSHA1NotSupported) {
return "", fmt.Errorf("fetching commit: %w", err)
}
// checkout commit if specified
w, err := repo.Worktree()
if err != nil {
return "", err
}
hash := repoArgs.CommitHash
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(hash),
Force: true,
})
if err != nil {
return "", fmt.Errorf("unable to checkout commit: %w", err)
}
}
var relPath string
2020-08-19 18:13:42 +00:00
if repoArgs.ProjectPath != "" {
relPath = repoArgs.ProjectPath
}
2020-08-19 18:13:42 +00:00
workDir = filepath.Join(workDir, relPath)
return workDir, nil
}