From 612cee1af9daaef5f2d17c3c613a54e5e4c6bec6 Mon Sep 17 00:00:00 2001 From: raeperd Date: Sun, 12 Oct 2025 17:43:59 +0900 Subject: [PATCH 1/4] feat: auto-detect go.mod when no version inputs specified - Add unit tests for auto-detection behavior - Implement go.mod auto-detection in resolveVersionInput() - Explicit inputs still take precedence over auto-detection Related issue: #523 --- __tests__/setup-go.test.ts | 25 +++++++++++++++++++++++++ dist/setup/index.js | 3 +++ src/main.ts | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts index b89c08fc3..7dc3c0354 100644 --- a/__tests__/setup-go.test.ts +++ b/__tests__/setup-go.test.ts @@ -1090,4 +1090,29 @@ use . expect(vars).toStrictEqual({GOTOOLCHAIN: 'local'}); expect(process.env).toHaveProperty('GOTOOLCHAIN', 'local'); }); + + describe('auto-detect go.mod', () => { + it('uses go.mod from workspace root when no inputs provided', async () => { + existsSpy.mockImplementation((filePath: string) => { + return filePath === 'go.mod'; + }); + readFileSpy.mockImplementation(() => + Buffer.from('module test\n\ngo 1.20') + ); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.20'); + }); + + it('uses pre-installed Go when no inputs and no go.mod exists', async () => { + existsSpy.mockImplementation(() => false); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith( + '[warning]go-version input was not specified. The action will try to use pre-installed version.' + ); + }); + }); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index 96f4ef9e0..3290c1223 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -94902,6 +94902,9 @@ function resolveVersionInput() { } version = installer.parseGoVersionFile(versionFilePath); } + if (!version && fs_1.default.existsSync('go.mod')) { + version = installer.parseGoVersionFile('go.mod'); + } return version; } function setGoToolchain() { diff --git a/src/main.ts b/src/main.ts index 26939ee6a..9ad747f94 100644 --- a/src/main.ts +++ b/src/main.ts @@ -160,6 +160,10 @@ function resolveVersionInput(): string { version = installer.parseGoVersionFile(versionFilePath); } + if (!version && fs.existsSync('go.mod')) { + version = installer.parseGoVersionFile('go.mod'); + } + return version; } From ec93c403628f141750ca277def7d4ff0d7b54a97 Mon Sep 17 00:00:00 2001 From: raeperd Date: Sun, 12 Oct 2025 17:44:55 +0900 Subject: [PATCH 2/4] test: add E2E test for auto go.mod detection Related issue: #523 --- .github/workflows/versions.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 89f4c4240..07589048a 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -142,6 +142,28 @@ jobs: run: __tests__/verify-go.sh 1.21 shell: bash + auto-detect-go-mod: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest, macos-13] + steps: + - uses: actions/checkout@v5 + - name: Create go.mod in workspace root + run: | + cat > go.mod << 'EOF' + module github.com/actions/setup-go + + go 1.21 + EOF + shell: bash + - name: Setup Go with auto-detection + uses: ./ + - name: Verify go version + run: __tests__/verify-go.sh 1.21 + shell: bash + setup-versions-from-manifest: runs-on: ${{ matrix.os }} strategy: From a16a57d83db79c851a6de04c1450b275ce7ab5b3 Mon Sep 17 00:00:00 2001 From: raeperd Date: Sun, 12 Oct 2025 17:56:24 +0900 Subject: [PATCH 3/4] docs: document automatic go.mod detection --- README.md | 16 +++++++++++++++- action.yml | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 564d55f71..4d9996d83 100644 --- a/README.md +++ b/README.md @@ -187,11 +187,25 @@ from thils file will be: The version can specify a patch version or omit it altogether (e.g., `go 1.22.0` or `go 1.22`). -If a patch version is specified, that specific patch version will be used. +If a patch version is specified, that specific patch version will be used. If no patch version is specified, it will search for the latest available patch version in the cache, [versions-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json), and the [official Go language website](https://golang.org/dl/?mode=json&include=all), in that order. +### Automatic go.mod detection + +**New in v6**: If neither `go-version` nor `go-version-file` is specified, the action will automatically look for a `go.mod` file in the repository root and use the Go version specified in it. This simplifies workflows for projects that already have a `go.mod` file. + +```yaml +steps: + - uses: actions/checkout@v5 + - uses: actions/setup-go@v6 + # Automatically uses go.mod from repository root + - run: go version +``` + +### Specifying a custom path + If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used. > The action will search for the `go.mod` file relative to the repository root diff --git a/action.yml b/action.yml index 9946e476e..5edb460f7 100644 --- a/action.yml +++ b/action.yml @@ -5,7 +5,7 @@ inputs: go-version: description: 'The Go version to download (if necessary) and use. Supports semver spec and ranges. Be sure to enclose this option in single quotation marks.' go-version-file: - description: 'Path to the go.mod or go.work file.' + description: 'Path to the go.mod or go.work file. If not specified, the action will automatically use go.mod from the workspace root if it exists.' check-latest: description: 'Set this option to true if you want the action to always check for the latest available version that satisfies the version spec' default: false From 16c5d19d3eecb44c5842fac392329b22d6300b80 Mon Sep 17 00:00:00 2001 From: raeperd Date: Thu, 26 Mar 2026 02:09:59 +0900 Subject: [PATCH 4/4] docs: move auto-detect go.mod docs to advanced-usage.md Follows the pattern from #724 where detailed usage docs live in docs/advanced-usage.md rather than README.md. --- README.md | 12 ------------ docs/advanced-usage.md | 12 ++++++++++++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f05315d0b..32c86d709 100644 --- a/README.md +++ b/README.md @@ -102,18 +102,6 @@ For details on Semantic Versioning, see [the semver package documentation](https > > The recommendation is based on the YAML parser's behavior, which interprets non-wrapped values as numbers and, in the case of version `1.20`, trims it down to `1.2`, which may not be very obvious. -### Automatic go.mod detection - -**New in v6**: If neither `go-version` nor `go-version-file` is specified, the action will automatically look for a `go.mod` file in the repository root and use the Go version specified in it. This simplifies workflows for projects that already have a `go.mod` file. - -```yaml -steps: - - uses: actions/checkout@v6 - - uses: actions/setup-go@v6 - # Automatically uses go.mod from repository root - - run: go version -``` - For more usage examples, please refer to the section: [Using go-version input](docs/advanced-usage.md#using-the-go-version-input) of the [Advanced usage](docs/advanced-usage.md) guide. ## Recommended permissions diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 91e8ffa9d..1a4196249 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -168,6 +168,18 @@ jobs: If both the `go-version` and the `go-version-file` inputs are provided then the `go-version` input is used. The `.tool-versions` file supports version specifications in accordance with asdf standards, adhering to Semantic Versioning ([semver](https://semver.org)). +### Automatic go.mod detection + +If neither `go-version` nor `go-version-file` is specified, the action will automatically look for a `go.mod` file in the repository root and use the Go version specified in it. This simplifies workflows for projects that already have a `go.mod` file. + +```yaml +steps: + - uses: actions/checkout@v6 + - uses: actions/setup-go@v6 + # Automatically uses go.mod from repository root + - run: go version +``` + ```yaml steps: - uses: actions/checkout@v6