From 7678c83214c8f844360ea3f399163b0010119bf9 Mon Sep 17 00:00:00 2001
From: koba1t <kobdotsh@gmail.com>
Date: Tue, 1 Nov 2022 19:13:57 +0900
Subject: [PATCH] add support gowork for go-version-file

---
 .github/workflows/versions.yml | 18 +++++++++++++++++-
 README.md                      |  2 +-
 __tests__/data/go.work         |  3 +++
 __tests__/setup-go.test.ts     | 18 ++++++++++++++++++
 action.yml                     |  6 +++---
 dist/setup/index.js            |  3 ++-
 src/installer.ts               |  5 ++++-
 7 files changed, 48 insertions(+), 7 deletions(-)
 create mode 100644 __tests__/data/go.work

diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml
index 3c74bd4..faaa439 100644
--- a/.github/workflows/versions.yml
+++ b/.github/workflows/versions.yml
@@ -66,6 +66,22 @@ jobs:
         run: __tests__/verify-go.sh 1.14
         shell: bash
 
+  go-version-file-with-gowork:
+    runs-on: ${{ matrix.os }}
+    strategy:
+      fail-fast: false
+      matrix:
+        os: [ubuntu-latest, windows-latest, macos-latest]
+    steps:
+      - uses: actions/checkout@v3
+      - name: Setup Go and check latest
+        uses: ./
+        with:
+          go-version-file: __tests__/data/go.work
+      - name: verify go
+        run: __tests__/verify-go.sh 1.19
+        shell: bash
+
   setup-versions-from-manifest:
     name: Setup ${{ matrix.go }} ${{ matrix.os }}
     runs-on: ${{ matrix.os }}
@@ -123,4 +139,4 @@ jobs:
           go-version: ${{ matrix.go-version }}
           architecture: x64
       - name: Verify Go
-        run: go version
\ No newline at end of file
+        run: go version
diff --git a/README.md b/README.md
index 9feb0db..f2c10ac 100644
--- a/README.md
+++ b/README.md
@@ -127,7 +127,7 @@ steps:
   ```
 ## Getting go version from the go.mod file
 
-The `go-version-file` input accepts a path to a `go.mod` file containing the version of Go to be used by a project. As the `go.mod` file contains only major and minor (e.g. 1.18) tags, the action will search for the latest available patch version sequentially in the runner's directory with the cached tools, in the [version-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go servers.
+The `go-version-file` input accepts a path to a `go.mod` file or a `go.work` file that contains the version of Go to be used by a project. As the `go.mod` file contains only major and minor (e.g. 1.18) tags, the action will search for the latest available patch version sequentially in the runner's directory with the cached tools, in the [version-manifest.json](https://github.com/actions/go-versions/blob/main/versions-manifest.json) file or at the go servers.
 
 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/__tests__/data/go.work b/__tests__/data/go.work
new file mode 100644
index 0000000..52a3b7c
--- /dev/null
+++ b/__tests__/data/go.work
@@ -0,0 +1,3 @@
+go 1.19
+
+use .
diff --git a/__tests__/setup-go.test.ts b/__tests__/setup-go.test.ts
index 2232f91..c7b3a22 100644
--- a/__tests__/setup-go.test.ts
+++ b/__tests__/setup-go.test.ts
@@ -824,6 +824,12 @@ require (
 
 replace example.com/thatmodule => ../thatmodule
 exclude example.com/thismodule v1.3.0
+`;
+
+    const goWorkContents = `go 1.19
+
+use .
+
 `;
 
     it('reads version from go.mod', async () => {
@@ -838,6 +844,18 @@ exclude example.com/thismodule v1.3.0
       expect(logSpy).toHaveBeenCalledWith('matching 1.14...');
     });
 
+    it('reads version from go.work', async () => {
+      inputs['go-version-file'] = 'go.work';
+      existsSpy.mockImplementation(() => true);
+      readFileSpy.mockImplementation(() => Buffer.from(goWorkContents));
+
+      await main.run();
+
+      expect(logSpy).toHaveBeenCalledWith('Setup go version spec 1.19');
+      expect(logSpy).toHaveBeenCalledWith('Attempting to download 1.19...');
+      expect(logSpy).toHaveBeenCalledWith('matching 1.19...');
+    });
+
     it('reads version from .go-version', async () => {
       inputs['go-version-file'] = '.go-version';
       existsSpy.mockImplementation(() => true);
diff --git a/action.yml b/action.yml
index 5310a11..4f23f79 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.'
   go-version-file:
-    description: 'Path to the go.mod file.'
+    description: 'Path to the go.mod or go.work file.'
   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
@@ -21,8 +21,8 @@ inputs:
     description: 'Target architecture for Go to use. Examples: x86, x64. Will use system architecture by default.'
 outputs:
   go-version:
-    description: 'The installed Go version. Useful when given a version range as input.'  
-  cache-hit: 
+    description: 'The installed Go version. Useful when given a version range as input.'
+  cache-hit:
     description: 'A boolean value to indicate if a cache was hit'
 runs:
   using: 'node16'
diff --git a/dist/setup/index.js b/dist/setup/index.js
index 9c80baa..9577409 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -63444,7 +63444,8 @@ function makeSemver(version) {
 exports.makeSemver = makeSemver;
 function parseGoVersionFile(versionFilePath) {
     const contents = fs_1.default.readFileSync(versionFilePath).toString();
-    if (path.basename(versionFilePath) === 'go.mod') {
+    if (path.basename(versionFilePath) === 'go.mod' ||
+        path.basename(versionFilePath) === 'go.work') {
         const match = contents.match(/^go (\d+(\.\d+)*)/m);
         return match ? match[1] : '';
     }
diff --git a/src/installer.ts b/src/installer.ts
index 6335dbd..94c5411 100644
--- a/src/installer.ts
+++ b/src/installer.ts
@@ -316,7 +316,10 @@ export function makeSemver(version: string): string {
 export function parseGoVersionFile(versionFilePath: string): string {
   const contents = fs.readFileSync(versionFilePath).toString();
 
-  if (path.basename(versionFilePath) === 'go.mod') {
+  if (
+    path.basename(versionFilePath) === 'go.mod' ||
+    path.basename(versionFilePath) === 'go.work'
+  ) {
     const match = contents.match(/^go (\d+(\.\d+)*)/m);
     return match ? match[1] : '';
   }