Skip to content
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

Go: Update autobuilder to deal with the upcoming deprecation of the legacy GOPATH mode #15361

Merged
merged 55 commits into from
Mar 4, 2024

Conversation

mbg
Copy link
Member

@mbg mbg commented Jan 17, 2024

This PR makes changes to the Go autobuilder to account for the deprecation of the legacy GOPATH mode in Go 1.22. In the process we refactor parts of the Go autobuilder to both account for that change and be able to handle multiple Go modules as well as Go workspaces.

We are now able to successfully extract the code for the following, existing integration tests:

  • go-files-found-not-processed (Multiple go.mod files)
  • single-go-work-not-in-root (go.work file)
  • two-go-mods-nested-none-in-root (Multiple go.mod files)
  • two-go-mods-not-nested (Multiple go.mod files)

This PR also adds query predicates to the integration tests to query the list of extracted files, so that we can now verify that the files we expect to be extracted have been extracted.

Best reviewed commit-by-commit.

@mbg mbg self-assigned this Jan 17, 2024
@github-actions github-actions bot added the Go label Jan 17, 2024
@smowton
Copy link
Contributor

smowton commented Jan 18, 2024

I think we should check there aren't go.mod files inside the proposed root dir before doing this -- otherwise the strategy you described of running go get in the context of those go.mod files seems more promising

@mbg mbg force-pushed the mbg/go/legacy-gopath-mode-deprecated branch 2 times, most recently from 45c349a to 4ad0e0f Compare January 25, 2024 19:02
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would benefit from a block comment or a short doc outlining the different scenarios you hope to deal with and how you want to deal with them -- that would make it clearer if there are any cases not accounted for

go/extractor/cli/go-autobuilder/go-autobuilder.go Outdated Show resolved Hide resolved
go/extractor/cli/go-autobuilder/go-autobuilder.go Outdated Show resolved Hide resolved
for i, use := range workFile.Use {
if filepath.IsAbs(use.Path) {
// TODO: This case might be problematic for some other logic (e.g. stray file detection)
goModFilePaths[i] = filepath.Join(use.Path, "go.mod")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check is under baseDir?

// TODO: This case might be problematic for some other logic (e.g. stray file detection)
goModFilePaths[i] = filepath.Join(use.Path, "go.mod")
} else {
goModFilePaths[i] = filepath.Join(filepath.Dir(workFilePath), use.Path, "go.mod")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check is under baseDir?

@mbg mbg force-pushed the mbg/go/legacy-gopath-mode-deprecated branch 4 times, most recently from 1a2e829 to 6d4a31d Compare February 1, 2024 12:40
@mbg mbg marked this pull request as ready for review February 1, 2024 15:48
@mbg mbg requested a review from a team as a code owner February 1, 2024 15:48
@mbg mbg requested a review from smowton February 1, 2024 15:49
smowton
smowton previously approved these changes Feb 2, 2024
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks plausible. Would still benefit from writing down explicitly what you intend to happen in odd cases, like stray go files outside modules (I think you're creating a new module at the repository root in this case?)

go/extractor/project/project.go Outdated Show resolved Hide resolved
go/extractor/project/project.go Outdated Show resolved Hide resolved
@mbg mbg force-pushed the mbg/go/legacy-gopath-mode-deprecated branch from ef4c1cd to 021cf80 Compare February 13, 2024 14:16
@mbg mbg requested a review from smowton February 13, 2024 14:31
@mbg mbg force-pushed the mbg/go/legacy-gopath-mode-deprecated branch from 021cf80 to 8ef53f4 Compare February 14, 2024 17:21
@mbg mbg force-pushed the mbg/go/legacy-gopath-mode-deprecated branch from 8ef53f4 to 6267506 Compare February 14, 2024 19:12
@@ -480,9 +480,17 @@ func installDependencies(workspace project.GoWorkspace) {

// get dependencies for all modules
for _, module := range workspace.Modules {
path := filepath.Dir(module.Path)

if util.DirExists(filepath.Join(path, "vendor")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this happens on both the existing module and the temporary module for legacy project paths. What are the expected consequences in each case?

@owen-mc
Copy link
Contributor

owen-mc commented Feb 21, 2024

The point of the go-files-found-not-processed integration test is to check that the TSP message "Go files were found but not processed" works correctly. It exploited the fact that we didn't process go files in a certain project layout. You've fixed that with the PR. So we should either change the test to trigger that scenario in some other way, or we should delete the test as it isn't testing what it's meant to (the project layout that it uses is tested in the integration test ql/go/ql/integration-tests/all-platforms/go/two-go-mods-nested-one-in-root). We could even go as far as to delete the TSP diagnostic, if we're sure that it can't be triggered any more... probably best to leave it in and keep a lookout for it in the dashboard.

Comment on lines +1 to +3
extractedFiles
| work/subdir/go.mod:0:0:0:0 | work/subdir/go.mod |
| work/subdir/test.go:0:0:0:0 | work/subdir/test.go |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is work/go.mod not in this list? And is this the reason that we still produce the "Go files were found but not processed" diagnostic?

@owen-mc
Copy link
Contributor

owen-mc commented Mar 2, 2024

I vaguely thought that we might be able to get rid of the "rearrange files in $GOPATH" behaviour now (which would be nice as it's a bit brittle), but I see it's still there. Can you explain a situation when we need it?

Comment on lines +596 to 604
if semver.Compare(toolchain.GetEnvGoSemVer(), "v1.21.0") < 0 && greatestGoVersion.Found && semver.Compare("v"+greatestGoVersion.Version, toolchain.GetEnvGoSemVer()) > 0 {
diagnostics.EmitNewerGoVersionNeeded(toolchain.GetEnvGoSemVer(), "v"+greatestGoVersion.Version)
if val, _ := os.LookupEnv("GITHUB_ACTIONS"); val == "true" {
log.Printf(
"The go.mod file requires version %s of Go, but version %s is installed. Consider adding an actions/setup-go step to your workflow.\n",
"v"+goVersionInfo.Version,
getEnvGoSemVer())
"A go.mod file requires version %s of Go, but version %s is installed. Consider adding an actions/setup-go step to your workflow.\n",
"v"+greatestGoVersion.Version,
toolchain.GetEnvGoSemVer())
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason this was moved after the $GOPATH rearrangement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the $GOPATH rearrangement ahead of this to address @smowton's comment in https://github.com/github/codeql/pull/15361/files/008585eeba2c455deb85b58e608a1822bf494dea#r1476152501

Do you have a concern about something I may have missed as part of this rearrangement?

Comment on lines +152 to +153
if strings.Contains(string(out), "is relative, but relative import paths are not supported in module mode") {
diagnostics.EmitRelativeImportPaths()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! I thought relative import paths did work in module mode. As that seems to not be the case, we should update the message for this diagnostic and remove the suggestion to use a Go module.

}
}

return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This preserves the old behaviour, so it should stay like this. But I wonder if in future we might want to remove this return so that we get some other diagnostics in this case as well.

Copy link
Contributor

@owen-mc owen-mc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've finished reviewing. I think it would be okay to merge this. I've only given minor suggestions, which could be dealt with in follow-up PRs.

@mbg mbg merged commit 0c93641 into main Mar 4, 2024
12 checks passed
@mbg mbg deleted the mbg/go/legacy-gopath-mode-deprecated branch March 4, 2024 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants