From 612cee1af9daaef5f2d17c3c613a54e5e4c6bec6 Mon Sep 17 00:00:00 2001 From: raeperd Date: Sun, 12 Oct 2025 17:43:59 +0900 Subject: [PATCH] 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 b89c08f..7dc3c03 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 96f4ef9..3290c12 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 26939ee..9ad747f 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; }