From e79813d32efe5487eb7793e06f90e8b0e94cc9bf Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Thu, 5 May 2022 11:33:50 +0200 Subject: [PATCH] feat: gomodNoMassage (#15462) --- docs/usage/configuration-options.md | 1 + docs/usage/golang.md | 9 +++++++++ lib/config/options/index.ts | 1 + lib/modules/manager/gomod/artifacts.ts | 8 ++++++-- lib/modules/manager/gomod/readme.md | 1 + 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 07a55593f2..9ad76e41e7 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -1881,6 +1881,7 @@ This way Renovate can use GitHub's [Commit signing support for bots and other Gi ## postUpdateOptions +- `gomodNoMassage`: Skip massaging `replace` directives before calling `go` commands - `gomodTidy`: Run `go mod tidy` after Go module updates. This is implicitly enabled for major module updates when `gomodUpdateImportPaths` is enabled - `gomodTidy1.17`: Run `go mod tidy -compat=1.17` after Go module updates. - `gomodUpdateImportPaths`: Update source import paths on major module updates, using [mod](https://github.com/marwan-at-work/mod) diff --git a/docs/usage/golang.md b/docs/usage/golang.md index f450e05c1f..9519ab4822 100644 --- a/docs/usage/golang.md +++ b/docs/usage/golang.md @@ -28,6 +28,15 @@ To install Renovate Bot itself, either enable the [Renovate App](https://github. ## Technical Details +### Replace massaging + +Renovate's default behavior is to massage any `replace` statements it finds prior to running `go` commands, and then massage them back afterwards. +This was originally done because relative `replace` statements outside of the current repo will not work when Renovate clones the repo locally. + +On the other hand, this massaging of `replace` statements may lead to unexpected results, especially because `go mod tidy` may not fully tidy the `go.sum` if it is missing the `replace` directives in `go.mod`. + +To disable this default behavior, and retain all `replace` statements when running `go` commands, add `gomodNoMassage` to your `postUpdateOptions` array. + ### Module Tidying Go Modules tidying is not enabled by default, and is opt-in via the [`postUpdateOptions`](https://docs.renovatebot.com/configuration-options/#postupdateoptions) config option. diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts index 6948d502b5..623b11fd5d 100644 --- a/lib/config/options/index.ts +++ b/lib/config/options/index.ts @@ -1820,6 +1820,7 @@ const options: RenovateOptions[] = [ type: 'array', default: [], allowedValues: [ + 'gomodNoMassage', 'gomodUpdateImportPaths', 'gomodTidy', 'gomodTidy1.17', diff --git a/lib/modules/manager/gomod/artifacts.ts b/lib/modules/manager/gomod/artifacts.ts index 0063080871..00722db7e8 100644 --- a/lib/modules/manager/gomod/artifacts.ts +++ b/lib/modules/manager/gomod/artifacts.ts @@ -158,7 +158,9 @@ export async function updateArtifacts({ const vendorModulesFileName = upath.join(vendorDir, 'modules.txt'); const useVendor = (await readLocalFile(vendorModulesFileName)) !== null; - try { + let massagedGoMod = newGoModContent; + + if (!config.postUpdateOptions?.includes('gomodNoMassage')) { // Regex match inline replace directive, example: // replace golang.org/x/net v1.2.3 => example.com/fork/net v1.4.5 // https://go.dev/ref/mod#go-mod-file-replace @@ -186,13 +188,15 @@ export async function updateArtifacts({ match.replace(/(\r?\n)/g, '$1// renovate-replace '); // Comment out golang replace directives - const massagedGoMod = newGoModContent + massagedGoMod = newGoModContent .replace(inlineReplaceRegEx, inlineCommentOut) .replace(blockReplaceRegEx, blockCommentOut); if (massagedGoMod !== newGoModContent) { logger.debug('Removed some relative replace statements from go.mod'); } + } + try { await writeLocalFile(goModFileName, massagedGoMod); const cmd = 'go'; diff --git a/lib/modules/manager/gomod/readme.md b/lib/modules/manager/gomod/readme.md index b749e7bba5..0610e6bdf7 100644 --- a/lib/modules/manager/gomod/readme.md +++ b/lib/modules/manager/gomod/readme.md @@ -4,6 +4,7 @@ You might be interested in the following `postUpdateOptions`: 1. This is implicitly enabled for major updates if the user has enabled the option `gomodUpdateImportPaths` 1. `gomodTidy1.17` - if you'd like Renovate to run `go mod tidy -compat=1.17` after every update before raising the PR. 1. `gomodUpdateImportPaths` - if you'd like Renovate to update your source import paths on major updates before raising the PR. +1. `gomodNoMassage` - to skip massaging of `replace` statements. When Renovate is running using `binarySource=docker` (such as in the hosted WhiteSource Renovate app) then it will pick the latest compatible version of Go to run, i.e. the latest `1.x` release. Therefore even if the `go.mod` has a version like `go 1.14`, you will see Renovate treating that as a `^1.14` constraint and not `=1.14`. -- GitLab