Skip to content

Commit

Permalink
feat(manager/gomod): update indirect dependencies for Go Modules (#19431
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Jamie Tanna committed Dec 15, 2022
1 parent ff2a15d commit d19effc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/modules/manager/gomod/__snapshots__/extract.spec.ts.snap
Expand Up @@ -659,6 +659,16 @@ exports[`modules/manager/gomod/extract extractPackageFile() extracts single-line
"lineNumber": 3,
},
},
{
"currentValue": "v1.0.0",
"datasource": "go",
"depName": "github.com/davecgh/go-spew",
"depType": "indirect",
"enabled": false,
"managerData": {
"lineNumber": 4,
},
},
{
"currentValue": "v1.0.0",
"datasource": "go",
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/manager/gomod/extract.spec.ts
Expand Up @@ -13,7 +13,9 @@ describe('modules/manager/gomod/extract', () => {
it('extracts single-line requires', () => {
const res = extractPackageFile(gomod1)?.deps;
expect(res).toMatchSnapshot();
expect(res).toHaveLength(8);
expect(res).toHaveLength(9);
expect(res?.filter((e) => e.depType === 'require')).toHaveLength(7);
expect(res?.filter((e) => e.depType === 'indirect')).toHaveLength(1);
expect(res?.filter((e) => e.skipReason)).toHaveLength(1);
expect(res?.filter((e) => e.depType === 'replace')).toHaveLength(1);
});
Expand Down
15 changes: 11 additions & 4 deletions lib/modules/manager/gomod/extract.ts
Expand Up @@ -70,10 +70,17 @@ export function extractPackageFile(content: string): PackageFile | null {
deps.push(dep);
}
const requireMatch = regEx(/^require\s+([^\s]+)\s+([^\s]+)/).exec(line);
if (requireMatch && !line.endsWith('// indirect')) {
logger.trace({ lineNumber }, `require line: "${line}"`);
const dep = getDep(lineNumber, requireMatch, 'require');
deps.push(dep);
if (requireMatch) {
if (line.endsWith('// indirect')) {
logger.trace({ lineNumber }, `indirect line: "${line}"`);
const dep = getDep(lineNumber, requireMatch, 'indirect');
dep.enabled = false;
deps.push(dep);
} else {
logger.trace({ lineNumber }, `require line: "${line}"`);
const dep = getDep(lineNumber, requireMatch, 'require');
deps.push(dep);
}
}
if (line.trim() === 'require (') {
logger.trace(`Matched multi-line require on line ${lineNumber}`);
Expand Down
14 changes: 14 additions & 0 deletions lib/modules/manager/gomod/readme.md
Expand Up @@ -8,3 +8,17 @@ You might be interested in the following `postUpdateOptions`:

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.
Even if the `go.mod` has a version like `go 1.14`, Renovate will treat it as a `^1.14` constraint and not `=1.14`.

Indirect updates are disabled by default. To enable them, add a package rule such as:

```json
{
"packageRules": [
{
"matchManagers": ["gomod"],
"matchDepTypes": ["indirect"],
"enabled": true
}
]
}
```
24 changes: 24 additions & 0 deletions lib/modules/manager/gomod/update.spec.ts
Expand Up @@ -362,5 +362,29 @@ describe('modules/manager/gomod/update', () => {
});
expect(res).toBeNull();
});

it('should perform indirect upgrades when top-level', () => {
const upgrade = {
depName: 'github.com/davecgh/go-spew',
managerData: { lineNumber: 4 },
newValue: 'v1.1.1',
depType: 'indirect',
};
const res = updateDependency({ fileContent: gomod1, upgrade });
expect(res).not.toEqual(gomod1);
expect(res).toContain(`${upgrade.newValue} // indirect`);
});

it('should perform indirect upgrades when in require blocks', () => {
const upgrade = {
depName: 'github.com/go-ole/go-ole',
managerData: { lineNumber: 23, multiLine: true },
newValue: 'v1.5.0',
depType: 'indirect',
};
const res = updateDependency({ fileContent: gomod3, upgrade });
expect(res).not.toEqual(gomod2);
expect(res).toContain(`${upgrade.newValue} // indirect`);
});
});
});
7 changes: 6 additions & 1 deletion lib/modules/manager/gomod/update.ts
Expand Up @@ -55,7 +55,7 @@ export function updateDependency({
/^(?<depPart>replace\s+[^\s]+[\s]+[=][>]+\s+)(?<divider>[^\s]+\s+)[^\s]+/
);
}
} else if (depType === 'require') {
} else if (depType === 'require' || depType === 'indirect') {
if (upgrade.managerData.multiLine) {
updateLineExp = regEx(/^(?<depPart>\s+[^\s]+)(?<divider>\s+)[^\s]+/);
} else {
Expand Down Expand Up @@ -135,6 +135,11 @@ export function updateDependency({
logger.debug('No changes necessary');
return fileContent;
}

if (depType === 'indirect') {
newLine += ' // indirect';
}

lines[upgrade.managerData.lineNumber] = newLine;
return lines.join('\n');
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions lib/modules/manager/types.ts
Expand Up @@ -97,6 +97,7 @@ export interface Package<T> extends ManagerData<T> {
target?: string;
versioning?: string;
dataType?: string;
enabled?: boolean;

// npm manager
bumpVersion?: ReleaseType | string;
Expand Down

0 comments on commit d19effc

Please sign in to comment.