From 58aa2a27871d58c7a865311a7b6b23cfd7bb0c70 Mon Sep 17 00:00:00 2001
From: Dan Buch <dan@meatballhat.com>
Date: Sat, 4 May 2024 21:41:13 -0400
Subject: [PATCH 1/3] Run go mod tidy

---
 go.mod | 2 --
 go.sum | 4 ----
 2 files changed, 6 deletions(-)

diff --git a/go.mod b/go.mod
index 2425a9b7fc..0c7067b227 100644
--- a/go.mod
+++ b/go.mod
@@ -5,9 +5,7 @@ go 1.18
 require github.com/stretchr/testify v1.8.4
 
 require (
-	github.com/BurntSushi/toml v1.3.2 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
-	github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )
diff --git a/go.sum b/go.sum
index 1ce67a49f3..fa4b6e6825 100644
--- a/go.sum
+++ b/go.sum
@@ -1,13 +1,9 @@
-github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
-github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
 github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
-github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2 h1:j4SaBpPB8++L0c0KuTnz/Yus3UQoWJ54hQjhIMW8rCM=
-github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2/go.mod h1:Q79oyIY/z4jtzIrKEK6MUeWC7/szGr46x4QdOaOAIWc=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

From 3a5f3d3801a382d3b76131e32a86c04588fa470f Mon Sep 17 00:00:00 2001
From: Dan Buch <dan@meatballhat.com>
Date: Sun, 5 May 2024 12:27:33 -0400
Subject: [PATCH 2/3] Introduce docs-specific go.mod file

with `replace` to point at top-level package
---
 .github/workflows/test.yml |  2 ++
 docs/go.mod                | 16 +++++++++++++
 docs/go.sum                | 14 +++++++++++
 docs/package.go            |  9 +++++++
 docs/v3/examples/flags.md  |  9 ++++---
 internal/build/build.go    | 48 +++++++++++++++++++-------------------
 6 files changed, 69 insertions(+), 29 deletions(-)
 create mode 100644 docs/go.mod
 create mode 100644 docs/go.sum
 create mode 100644 docs/package.go

diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index dff31eefc5..444d144771 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -79,9 +79,11 @@ jobs:
         uses: actions/checkout@v4
 
       - run: make ensure-gfmrun
+
       - run: make gfmrun
         env:
           FLAGS: --walk docs/v3/
+
       - run: make diffcheck
 
   publish:
diff --git a/docs/go.mod b/docs/go.mod
new file mode 100644
index 0000000000..ff0299fb17
--- /dev/null
+++ b/docs/go.mod
@@ -0,0 +1,16 @@
+module github.com/urfave/cli/docs/v3
+
+go 1.22.2
+
+replace github.com/urfave/cli/v3 => ../
+
+require (
+	github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2
+	github.com/urfave/cli/v3 v3.0.0-alpha9
+)
+
+require (
+	github.com/BurntSushi/toml v1.3.2 // indirect
+	github.com/stretchr/testify v1.9.0 // indirect
+	gopkg.in/yaml.v3 v3.0.1 // indirect
+)
diff --git a/docs/go.sum b/docs/go.sum
new file mode 100644
index 0000000000..7c5c22143a
--- /dev/null
+++ b/docs/go.sum
@@ -0,0 +1,14 @@
+github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
+github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
+github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2 h1:j4SaBpPB8++L0c0KuTnz/Yus3UQoWJ54hQjhIMW8rCM=
+github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2/go.mod h1:Q79oyIY/z4jtzIrKEK6MUeWC7/szGr46x4QdOaOAIWc=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/docs/package.go b/docs/package.go
new file mode 100644
index 0000000000..89d8cc99a2
--- /dev/null
+++ b/docs/package.go
@@ -0,0 +1,9 @@
+// Package docs is an empty shell! This file is *only* meant to capture the dependencies
+// required by the `gfmrun` documentation tests.
+
+package docs
+
+import (
+	_ "github.com/urfave/cli-altsrc/v3"
+	_ "github.com/urfave/cli/v3"
+)
diff --git a/docs/v3/examples/flags.md b/docs/v3/examples/flags.md
index 0781fb19d1..1344c55182 100644
--- a/docs/v3/examples/flags.md
+++ b/docs/v3/examples/flags.md
@@ -520,18 +520,18 @@ Here is a more complete sample of a command using YAML support:
 package main
 
 import (
+	"context"
 	"fmt"
 	"os"
-	"context"
 
+	altsrc "github.com/urfave/cli-altsrc/v3"
 	"github.com/urfave/cli/v3"
-	"github.com/urfave/cli-altsrc/v3"
 )
 
 func main() {
 	flags := []cli.Flag{
 		&cli.IntFlag{
-			Name: "test",
+			Name:    "test",
 			Sources: altsrc.YAML("key", "/path/to/file"),
 		},
 		&cli.StringFlag{Name: "load"},
@@ -542,8 +542,7 @@ func main() {
 			fmt.Println("--test value.*default: 0")
 			return nil
 		},
-		//Before: altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("load")),
-		Flags:  flags,
+		Flags: flags,
 	}
 
 	cmd.Run(context.Background(), os.Args)
diff --git a/internal/build/build.go b/internal/build/build.go
index f3f9a6d491..763adac1e6 100644
--- a/internal/build/build.go
+++ b/internal/build/build.go
@@ -42,7 +42,7 @@ const (
 )
 
 func main() {
-	top, err := func() (string, error) {
+	topDir, err := func() (string, error) {
 		ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
 		defer cancel()
 
@@ -148,8 +148,8 @@ func main() {
 				Usage: "set build tags",
 			},
 			&cli.StringFlag{
-				Name:  "top",
-				Value: top,
+				Name:  "top-dir",
+				Value: topDir,
 			},
 			&cli.StringSliceFlag{
 				Name:  "packages",
@@ -175,7 +175,7 @@ func sh(ctx context.Context, exe string, args ...string) (string, error) {
 
 func topRunAction(arg string, args ...string) cli.ActionFunc {
 	return func(ctx context.Context, cmd *cli.Command) error {
-		if err := os.Chdir(cmd.String("top")); err != nil {
+		if err := os.Chdir(cmd.String("top-dir")); err != nil {
 			return err
 		}
 
@@ -232,7 +232,7 @@ func downloadFile(src, dest string, dirPerm, perm os.FileMode) error {
 }
 
 func VetActionFunc(ctx context.Context, cmd *cli.Command) error {
-	return runCmd(ctx, "go", "vet", cmd.String("top")+"/...")
+	return runCmd(ctx, "go", "vet", cmd.String("top-dir")+"/...")
 }
 
 func TestActionFunc(ctx context.Context, cmd *cli.Command) error {
@@ -293,7 +293,7 @@ func testCleanup(packages []string) error {
 }
 
 func GfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {
-	top := cmd.String("top")
+	docsDir := filepath.Join(cmd.String("top-dir"), "docs")
 
 	bash, err := exec.LookPath("bash")
 	if err != nil {
@@ -318,7 +318,7 @@ func GfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {
 
 	fmt.Fprintf(cmd.ErrWriter, "# ---> workspace/TMPDIR is %q\n", tmpDir)
 
-	if err := runCmd(ctx, "go", "work", "init", top); err != nil {
+	if err := runCmd(ctx, "go", "work", "init", docsDir); err != nil {
 		return err
 	}
 
@@ -489,22 +489,22 @@ func checkBinarySizeActionFunc(ctx context.Context, cmd *cli.Command) (err error
 }
 
 func GenerateActionFunc(ctx context.Context, cmd *cli.Command) error {
-	top := cmd.String("top")
+	topDir := cmd.String("top-dir")
 
-	cliDocs, err := sh(ctx, "go", "doc", "-all", top)
+	cliDocs, err := sh(ctx, "go", "doc", "-all", topDir)
 	if err != nil {
 		return err
 	}
 
 	return os.WriteFile(
-		filepath.Join(top, "godoc-current.txt"),
+		filepath.Join(topDir, "godoc-current.txt"),
 		[]byte(cliDocs),
 		0o644,
 	)
 }
 
 func DiffCheckActionFunc(ctx context.Context, cmd *cli.Command) error {
-	if err := os.Chdir(cmd.String("top")); err != nil {
+	if err := os.Chdir(cmd.String("top-dir")); err != nil {
 		return err
 	}
 
@@ -516,8 +516,8 @@ func DiffCheckActionFunc(ctx context.Context, cmd *cli.Command) error {
 }
 
 func EnsureGoimportsActionFunc(ctx context.Context, cmd *cli.Command) error {
-	top := cmd.String("top")
-	if err := os.Chdir(top); err != nil {
+	topDir := cmd.String("top-dir")
+	if err := os.Chdir(topDir); err != nil {
 		return err
 	}
 
@@ -525,21 +525,21 @@ func EnsureGoimportsActionFunc(ctx context.Context, cmd *cli.Command) error {
 		ctx,
 		"goimports",
 		"-d",
-		filepath.Join(top, "internal/build/build.go"),
+		filepath.Join(topDir, "internal/build/build.go"),
 	); err == nil {
 		return nil
 	}
 
-	os.Setenv("GOBIN", filepath.Join(top, ".local/bin"))
+	os.Setenv("GOBIN", filepath.Join(topDir, ".local/bin"))
 
 	return runCmd(ctx, "go", "install", "golang.org/x/tools/cmd/goimports@latest")
 }
 
 func EnsureGfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {
-	top := cmd.String("top")
-	gfmrunExe := filepath.Join(top, ".local/bin/gfmrun")
+	topDir := cmd.String("top-dir")
+	gfmrunExe := filepath.Join(topDir, ".local/bin/gfmrun")
 
-	if err := os.Chdir(top); err != nil {
+	if err := os.Chdir(topDir); err != nil {
 		return err
 	}
 
@@ -561,7 +561,7 @@ func EnsureGfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {
 }
 
 func EnsureMkdocsActionFunc(ctx context.Context, cmd *cli.Command) error {
-	if err := os.Chdir(cmd.String("top")); err != nil {
+	if err := os.Chdir(cmd.String("top-dir")); err != nil {
 		return err
 	}
 
@@ -584,7 +584,7 @@ func SetMkdocsRemoteActionFunc(ctx context.Context, cmd *cli.Command) error {
 		return errors.New("empty github token")
 	}
 
-	if err := os.Chdir(cmd.String("top")); err != nil {
+	if err := os.Chdir(cmd.String("top-dir")); err != nil {
 		return err
 	}
 
@@ -600,12 +600,12 @@ func SetMkdocsRemoteActionFunc(ctx context.Context, cmd *cli.Command) error {
 }
 
 func LintActionFunc(ctx context.Context, cmd *cli.Command) error {
-	top := cmd.String("top")
-	if err := os.Chdir(top); err != nil {
+	topDir := cmd.String("top-dir")
+	if err := os.Chdir(topDir); err != nil {
 		return err
 	}
 
-	out, err := sh(ctx, filepath.Join(top, ".local/bin/goimports"), "-l", ".")
+	out, err := sh(ctx, filepath.Join(topDir, ".local/bin/goimports"), "-l", ".")
 	if err != nil {
 		return err
 	}
@@ -621,7 +621,7 @@ func LintActionFunc(ctx context.Context, cmd *cli.Command) error {
 }
 
 func V3Diff(ctx context.Context, cmd *cli.Command) error {
-	if err := os.Chdir(cmd.String("top")); err != nil {
+	if err := os.Chdir(cmd.String("top-dir")); err != nil {
 		return err
 	}
 

From 9ee5837d0d45f322ec6ece1cee1380c51de90268 Mon Sep 17 00:00:00 2001
From: Dan Buch <dan@meatballhat.com>
Date: Sun, 5 May 2024 12:30:40 -0400
Subject: [PATCH 3/3] Declare same go version in docs go.mod

---
 docs/go.mod | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docs/go.mod b/docs/go.mod
index ff0299fb17..b53a2b002e 100644
--- a/docs/go.mod
+++ b/docs/go.mod
@@ -1,6 +1,6 @@
 module github.com/urfave/cli/docs/v3
 
-go 1.22.2
+go 1.18
 
 replace github.com/urfave/cli/v3 => ../