Skip to content

Commit

Permalink
Add XCADDY_GO_MOD_CACHE_RW env var (caddyserver#102)
Browse files Browse the repository at this point in the history
Commit 47f9ded is not sufficient alone to
ensure that all go modules are installed with write bit set because go mod
and go get installs modules of other modules with read-only access.

The environment variable sets -modcacherw to both go get and go mod commands
to ensure that the build/pkg/mod directory does not have sub directories
that are read-only for the current user.
  • Loading branch information
aabelix committed Aug 13, 2022
1 parent 47f9ded commit 16d470c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Because the subcommands and flags are constrained to benefit rapid plugin protot
- `XCADDY_SKIP_CLEANUP=1` causes xcaddy to leave build artifacts on disk after exiting.
- `XCADDY_WHICH_GO` sets the go command to use when for example more then 1 version of go is installed.
- `XCADDY_GO_BUILD_FLAGS` overrides default build arguments. Supports Unix-style shell quoting, for example: XCADDY_GO_BUILD_FLAGS="-ldflags '-w s'".
- `XCADDY_GO_MOD_CACHE_RW` sets flag `-modcacherw` before installing go modules. This sets write bit instead of read-only which is the default. See [Go 1.14 Release Notes](https://go.dev/doc/go1.14) for more information.
---

© 2020 Matthew Holt
8 changes: 8 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Builder struct {
SkipBuild bool `json:"skip_build,omitempty"`
Debug bool `json:"debug,omitempty"`
BuildFlags string `json:"build_flags,omitempty"`
ModCacheRw bool `json:"mod_cache_rw,omitempty"`
}

// Build builds Caddy at the configured version with the
Expand Down Expand Up @@ -105,6 +106,9 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {

// tidy the module to ensure go.mod and go.sum are consistent with the module prereq
tidyCmd := buildEnv.newCommand(utils.GetGo(), "mod", "tidy")
if b.ModCacheRw {
tidyCmd.Args = append(tidyCmd.Args, "-modcacherw")
}
if err := buildEnv.runCommand(ctx, tidyCmd, b.TimeoutGet); err != nil {
return err
}
Expand Down Expand Up @@ -132,6 +136,10 @@ func (b Builder) Build(ctx context.Context, outputFile string) error {
}
}

if b.ModCacheRw {
cmd.Args = append(cmd.Args, "-modcacherw")
}

if b.RaceDetector {
cmd.Args = append(cmd.Args, "-race")
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1" || skipBuild
buildDebugOutput = os.Getenv("XCADDY_DEBUG") == "1"
buildFlags = os.Getenv("XCADDY_GO_BUILD_FLAGS")
modCacheRw = os.Getenv("XCADDY_GO_MOD_CACHE_RW") == "1"
)

func Main() {
Expand Down Expand Up @@ -136,6 +137,7 @@ func runBuild(ctx context.Context, args []string) error {
SkipCleanup: skipCleanup,
Debug: buildDebugOutput,
BuildFlags: buildFlags,
ModCacheRw: modCacheRw,
}
err := builder.Build(ctx, output)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
timeoutGoGet: b.TimeoutGet,
skipCleanup: b.SkipCleanup,
buildFlags: b.BuildFlags,
modCacheRw: b.ModCacheRw,
}

// initialize the go module
Expand Down Expand Up @@ -179,6 +180,7 @@ type environment struct {
timeoutGoGet time.Duration
skipCleanup bool
buildFlags string
modCacheRw bool
}

// Close cleans up the build environment, including deleting
Expand Down Expand Up @@ -266,11 +268,17 @@ func (env environment) execGoGet(ctx context.Context, modulePath, moduleVersion,
// breaks the command since it treats the empty string as a
// distinct argument, so we're using an if statement to avoid it.
var cmd *exec.Cmd
var args []string
args = append(args, "get", "-d", "-v")
if env.modCacheRw {
args = append(args, "-modcacherw")
}
if caddy != "" {
cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod, caddy)
args = append(args, mod, caddy)
} else {
cmd = env.newCommand(utils.GetGo(), "get", "-d", "-v", mod)
args = append(args, mod)
}
cmd = env.newCommand(utils.GetGo(), args...)

return env.runCommand(ctx, cmd, env.timeoutGoGet)
}
Expand Down

0 comments on commit 16d470c

Please sign in to comment.