From 4b0102f08b197c2e1e1afd6f0ab8d8dc72909c14 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 28 May 2022 10:19:36 +0200 Subject: [PATCH] fix(gomod): default to not massaging replace statements (#15767) --- docs/usage/configuration-options.md | 2 +- docs/usage/golang.md | 7 ++++--- .../custom/post-update-options-migration.spec.ts | 14 ++++++++++++++ .../custom/post-update-options-migration.ts | 16 ++++++++++++++++ lib/config/migrations/migrations-service.ts | 2 ++ lib/config/options/index.ts | 2 +- lib/modules/manager/gomod/artifacts.spec.ts | 1 + lib/modules/manager/gomod/artifacts.ts | 2 +- lib/modules/manager/gomod/readme.md | 2 +- 9 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 lib/config/migrations/custom/post-update-options-migration.spec.ts create mode 100644 lib/config/migrations/custom/post-update-options-migration.ts diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index d8d58c26b09f6e..1df68058b92ab2 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -1881,7 +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 +- `gomodMassage`: Enable 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 9519ab48225ec3..7671f0038b64e9 100644 --- a/docs/usage/golang.md +++ b/docs/usage/golang.md @@ -30,12 +30,13 @@ To install Renovate Bot itself, either enable the [Renovate App](https://github. ### 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. +Renovate can massage `replace` statements it finds prior to running `go` commands, and then massage them back afterwards. +This capability was added - and originally default behavior - 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`. +It has therefore been disabled by default. -To disable this default behavior, and retain all `replace` statements when running `go` commands, add `gomodNoMassage` to your `postUpdateOptions` array. +To enable this replace massaging behavior, add `gomodMassage` to your `postUpdateOptions` array. ### Module Tidying diff --git a/lib/config/migrations/custom/post-update-options-migration.spec.ts b/lib/config/migrations/custom/post-update-options-migration.spec.ts new file mode 100644 index 00000000000000..87f59b813e225f --- /dev/null +++ b/lib/config/migrations/custom/post-update-options-migration.spec.ts @@ -0,0 +1,14 @@ +import { PostUpdateOptionsMigration } from './post-update-options-migration'; + +describe('config/migrations/custom/post-update-options-migration', () => { + it('should migrate properly', () => { + expect(PostUpdateOptionsMigration).toMigrate( + { + postUpdateOptions: ['gomodTidy', 'gomodNoMassage'], + }, + { + postUpdateOptions: ['gomodTidy'], + } + ); + }); +}); diff --git a/lib/config/migrations/custom/post-update-options-migration.ts b/lib/config/migrations/custom/post-update-options-migration.ts new file mode 100644 index 00000000000000..e5f0c3d750e9df --- /dev/null +++ b/lib/config/migrations/custom/post-update-options-migration.ts @@ -0,0 +1,16 @@ +import is from '@sindresorhus/is'; +import { AbstractMigration } from '../base/abstract-migration'; + +export class PostUpdateOptionsMigration extends AbstractMigration { + override readonly propertyName = 'postUpdateOptions'; + + override run(value: unknown): void { + if (Array.isArray(value)) { + const newValue = value + .filter(is.nonEmptyString) + .filter((option) => option !== 'gomodNoMassage'); + + this.rewrite(newValue); + } + } +} diff --git a/lib/config/migrations/migrations-service.ts b/lib/config/migrations/migrations-service.ts index fe456ec4c96be6..91535db83c62ed 100644 --- a/lib/config/migrations/migrations-service.ts +++ b/lib/config/migrations/migrations-service.ts @@ -28,6 +28,7 @@ import { PackagePatternMigration } from './custom/package-pattern-migration'; import { PackagesMigration } from './custom/packages-migration'; import { PathRulesMigration } from './custom/path-rules-migration'; import { PinVersionsMigration } from './custom/pin-versions-migration'; +import { PostUpdateOptionsMigration } from './custom/post-update-options-migration'; import { RaiseDeprecationWarningsMigration } from './custom/raise-deprecation-warnings-migration'; import { RebaseConflictedPrs } from './custom/rebase-conflicted-prs-migration'; import { RebaseStalePrsMigration } from './custom/rebase-stale-prs-migration'; @@ -100,6 +101,7 @@ export class MigrationsService { PackagesMigration, PathRulesMigration, PinVersionsMigration, + PostUpdateOptionsMigration, RaiseDeprecationWarningsMigration, RebaseConflictedPrs, RebaseStalePrsMigration, diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts index d6358a2486b570..d5d3d5b6e5abe1 100644 --- a/lib/config/options/index.ts +++ b/lib/config/options/index.ts @@ -1820,7 +1820,7 @@ const options: RenovateOptions[] = [ type: 'array', default: [], allowedValues: [ - 'gomodNoMassage', + 'gomodMassage', 'gomodUpdateImportPaths', 'gomodTidy', 'gomodTidy1.17', diff --git a/lib/modules/manager/gomod/artifacts.spec.ts b/lib/modules/manager/gomod/artifacts.spec.ts index d32eabb6bcd16b..22dda91fcb7c7d 100644 --- a/lib/modules/manager/gomod/artifacts.spec.ts +++ b/lib/modules/manager/gomod/artifacts.spec.ts @@ -42,6 +42,7 @@ const adminConfig: RepoGlobalConfig = { const config: UpdateArtifactsConfig = { constraints: { go: '1.14' }, + postUpdateOptions: ['gomodMassage'], }; const goEnv = { diff --git a/lib/modules/manager/gomod/artifacts.ts b/lib/modules/manager/gomod/artifacts.ts index 00722db7e8385b..cc8bee8437dd67 100644 --- a/lib/modules/manager/gomod/artifacts.ts +++ b/lib/modules/manager/gomod/artifacts.ts @@ -160,7 +160,7 @@ export async function updateArtifacts({ let massagedGoMod = newGoModContent; - if (!config.postUpdateOptions?.includes('gomodNoMassage')) { + if (config.postUpdateOptions?.includes('gomodMassage')) { // 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 diff --git a/lib/modules/manager/gomod/readme.md b/lib/modules/manager/gomod/readme.md index 4071564367a385..f41d8b140fb2fd 100644 --- a/lib/modules/manager/gomod/readme.md +++ b/lib/modules/manager/gomod/readme.md @@ -4,7 +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. +1. `gomodMassage` - to enable massaging of all `replace` statements prior to running `go` so that they will be ignored. When Renovate is running using `binarySource=docker` (such as in the hosted Mend 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`.