Skip to content

Go: Add diagnostic for 1.21 toolchain error #14161

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 1 commit into from
Sep 8, 2023

Conversation

mbg
Copy link
Member

@mbg mbg commented Sep 7, 2023

Summary

We keep seeing customers using advanced workflows who run into problems with their Go 1.21 projects and CodeQL in GHA environments:

  • The current default version of Go on GHA images is still 1.20.
  • We use the system Go toolchain under the hood to perform the build.
  • Consequently, Go 1.21 projects fail to build if Go 1.21 is not explicitly installed.

This PR introduces a new diagnostic which is emitted if the unknown directive: toolchain error is detected to advise users that a new system version of Go is required along with a link to instructions on how to do that in a GHA workflow.

@mbg mbg added the Go label Sep 7, 2023
@mbg mbg requested a review from smowton September 7, 2023 10:55
@mbg mbg self-assigned this Sep 7, 2023
@mbg mbg requested a review from a team as a code owner September 7, 2023 10:55
@mbg
Copy link
Member Author

mbg commented Sep 7, 2023

It's tricky to build an integration test for this, since it depends on having a particular Go version in the environment, but I have tested this locally with PATH="/opt/homebrew/opt/go@1.20/bin:$PATH" codeql database create ./codeql-db --language=go --search-path=[path to]/semmle-code/target/intree/codeql-go/go in a folder with a go.mod that contains a toolchain directive and the diagnostic is emitted as expected:

{
  "timestamp": "2023-09-07T11:08:57.439Z",
  "source": {
    "id": "go/autobuilder/newer-system-go-version-required",
    "name": "The Go version installed on the system is too old to support this project",
    "extractorName": "go"
  },
  "markdownMessage": "At least Go version `1.21.0` is required to build this project, but the version installed on the system is older. [Install a newer version](https://github.com/actions/setup-go#basic).",
  "severity": "error",
  "visibility": {
    "statusPage": true,
    "cliSummaryTable": true,
    "telemetry": true
  }
}

As with other diagnostics, this will only currently show up for CodeQL runs on the default repository branch. I think it would probably be useful to emit something to the log as well.

Furthermore, there are multiple places where this error occurs and we could catch it when parsing go.mod files too. We don't currently scan the output of the go command invocations, but we could search the go.mod file for a toolchain directive manually and check that the Go version is at least 1.21.

Full build log
Initializing database at .../go-test/codeql-db.
Running build command: [.../codeql/semmle-code/target/intree/codeql-go/go/tools/autobuild.sh]
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Autobuilder was built with go1.21.0, environment has go1.20.8
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 LGTM_SRC is .../go-test
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Found go.mod, enabling go modules
[2023-09-07 12:08:57] [build-stderr] go: errors parsing go.mod:
[2023-09-07 12:08:57] [build-stderr] .../go-test/go.mod:5: unknown directive: toolchain
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Running /opt/homebrew/opt/go@1.20/bin/go failed, continuing anyway: exit status 1
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Failed to run `go mod tidy -e`
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Unable to determine import path, as neither LGTM_INDEX_IMPORT_PATH nor GITHUB_REPOSITORY is set
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Build failed, continuing to install dependencies.
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Installing dependencies using `go get -v ./...` in `.`.
[2023-09-07 12:08:57] [build-stderr] go: errors parsing go.mod:
[2023-09-07 12:08:57] [build-stderr] .../go-test/go.mod:5: unknown directive: toolchain
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Running /opt/homebrew/opt/go@1.20/bin/go failed, continuing anyway: exit status 1
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Running extractor command '.../semmle-code/target/intree/codeql-go/go/tools/osx64/go-extractor [./...]' from directory '.'.
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Build flags: ''; patterns: './...'
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Running packages.Load.
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Error running go tooling: err: exit status 1: stderr: go: errors parsing go.mod:
[2023-09-07 12:08:57] [build-stderr] .../go-test/go.mod:5: unknown directive: toolchain
[2023-09-07 12:08:57] [build-stderr] 2023/09/07 12:08:57 Extraction failed: exit status 1
[2023-09-07 12:08:57] [ERROR] Spawned process exited abnormally (code 1; tried to run: [.../.local/share/gh/extensions/gh-codeql/dist/nightly/codeql-bundle-20230728/tools/osx64/preload_tracer, .../semmle-code/target/intree/codeql-go/go/tools/autobuild.sh])
A fatal error occurred: Exit status 1 from command: [.../semmle-code/target/intree/codeql-go/go/tools/autobuild.sh]

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 good to me. Local testing is sufficient IMO.

@mbg mbg merged commit 109bd90 into main Sep 8, 2023
@mbg mbg deleted the mbg/go/add-toolchain-directive-diagnostic branch September 8, 2023 13:55
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.

2 participants