Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gradle): add support for java.toolchain.languageVersion detection in build.gradle(.kts) files #32461

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/modules/manager/gradle-wrapper/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fs, partial } from '../../../../test/util';
import {
extractGradleVersion,
getJavaConstraint,
getJavaLanguageVersion,
getJvmConfiguration,
gradleWrapperFileName,
prepareGradleCommand,
Expand Down Expand Up @@ -45,6 +46,15 @@ describe('modules/manager/gradle-wrapper/util', () => {
fs.readLocalFile.mockResolvedValue(daemonJvm);
expect(await getJavaConstraint('8.8', './gradlew')).toBe('^999.0.0');
});

it('returns languageVersion constraint if found', async () => {
const buildGradle = codeBlock`
java { toolchain { languageVersion = JavaLanguageVersion.of(456) } }
`;
fs.localPathExists.mockResolvedValueOnce(true);
fs.readLocalFile.mockResolvedValue(buildGradle);
expect(await getJavaConstraint('6.7', './gradlew')).toBe('^456.0.0');
});
});

describe('getJvmConfiguration', () => {
Expand All @@ -67,6 +77,36 @@ describe('modules/manager/gradle-wrapper/util', () => {
});
});

describe('getJavaLanguageVersion', () => {
it('extract languageVersion value', async () => {
const buildGradle = codeBlock`
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
`;
fs.localPathExists.mockResolvedValue(true);
fs.readLocalFile.mockResolvedValue(buildGradle);
expect(await getJavaLanguageVersion('')).toBe('21');
});

it('returns null if build.gradle or build.gradle.kts file not found', async () => {
fs.localPathExists.mockResolvedValue(false);
fs.readLocalFile.mockResolvedValue(null);
expect(await getJavaLanguageVersion('sub/gradlew')).toBeNull();
expect(fs.readLocalFile).toHaveBeenCalledWith(
'sub/build.gradle.kts',
'utf8',
);
});

it('returns null if build.gradle does not include languageVersion', async () => {
const buildGradle = codeBlock`
dependencies { implementation "com.google.protobuf:protobuf-java:2.17.0" }
`;
fs.localPathExists.mockResolvedValue(true);
fs.readLocalFile.mockResolvedValue(buildGradle);
expect(await getJavaLanguageVersion('')).toBeNull();
});
});

describe('extractGradleVersion()', () => {
it('returns null', () => {
const properties = codeBlock`
Expand Down
36 changes: 35 additions & 1 deletion lib/modules/manager/gradle-wrapper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import os from 'node:os';
import { dirname, join } from 'upath';
import { GlobalConfig } from '../../../config/global';
import { logger } from '../../../logger';
import { chmodLocalFile, readLocalFile, statLocalFile } from '../../../util/fs';
import {
chmodLocalFile,
localPathExists,
readLocalFile,
statLocalFile,
} from '../../../util/fs';
import { regEx } from '../../../util/regex';
import gradleVersioning from '../../versioning/gradle';
import { parseJavaToolchainVersion } from '../gradle/parser';
import type { GradleVersionExtract } from './types';

export const extraEnv = {
Expand Down Expand Up @@ -60,6 +66,13 @@ export async function getJavaConstraint(
return `^${toolChainVersion}.0.0`;
}
}
// https://docs.gradle.org/6.7/release-notes.html#new-jvm-ecosystem-features
if (major > 6 || (major === 6 && minor && minor >= 7)) {
const languageVersion = await getJavaLanguageVersion(gradlewFile);
if (languageVersion) {
return `^${languageVersion}.0.0`;
}
}
if (major > 8 || (major === 8 && minor && minor >= 5)) {
return '^21.0.0';
}
Expand Down Expand Up @@ -103,6 +116,27 @@ export async function getJvmConfiguration(
return null;
}

/**
* https://docs.gradle.org/current/userguide/toolchains.html#sec:consuming
*/
export async function getJavaLanguageVersion(
gradlewFile: string,
): Promise<string | null> {
const localGradleDir = dirname(gradlewFile);
let buildFileName = join(localGradleDir, 'build.gradle');
if (!(await localPathExists(buildFileName))) {
buildFileName = join(localGradleDir, 'build.gradle.kts');
}

const buildFileContent = await readLocalFile(buildFileName, 'utf8');
if (!buildFileContent) {
logger.debug('build.gradle or build.gradle.kts not found');
return null;
}

return parseJavaToolchainVersion(buildFileContent);
}

// https://regex101.com/r/IcOs7P/1
const DISTRIBUTION_URL_REGEX = regEx(
'^(?:distributionUrl\\s*=\\s*)(?<url>\\S*-(?<version>\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)-(?<type>bin|all)\\.zip)\\s*$',
Expand Down
26 changes: 25 additions & 1 deletion lib/modules/manager/gradle/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import is from '@sindresorhus/is';
import { codeBlock } from 'common-tags';
import { Fixtures } from '../../../../test/fixtures';
import { fs, logger } from '../../../../test/util';
import { parseGradle, parseKotlinSource, parseProps } from './parser';
import {
parseGradle,
parseJavaToolchainVersion,
parseKotlinSource,
parseProps,
} from './parser';
import { GRADLE_PLUGINS, REGISTRY_URLS } from './parser/common';

jest.mock('../../../util/fs');
Expand Down Expand Up @@ -1108,4 +1113,23 @@ describe('modules/manager/gradle/parser', () => {
});
});
});

describe('Java language version', () => {
it.each`
input | output
${'java { toolchain { languageVersion = JavaLanguageVersion.of(22) } }'} | ${'22'}
${'java { toolchain.languageVersion.set(JavaLanguageVersion.of(16)) }'} | ${'16'}
${'java.toolchain { languageVersion.set(JavaLanguageVersion.of(17)) }'} | ${'17'}
${'java.toolchain.languageVersion = JavaLanguageVersion.of(21)'} | ${'21'}
${'kotlin { jvmToolchain { languageVersion = JavaLanguageVersion.of(17) } }'} | ${'17'}
${'kotlin { jvmToolchain { languageVersion.set(JavaLanguageVersion.of(17)) } }'} | ${'17'}
${'kotlin.jvmToolchain { languageVersion.set(JavaLanguageVersion.of(8)) }'} | ${'8'}
${'kotlin { jvmToolchain(11) }'} | ${'11'}
${'kotlin.jvmToolchain(16)'} | ${'16'}
${'dependencies { implementation "com.google.protobuf:protobuf-java:2.17.0" }'} | ${null}
`('$input', ({ input, output }) => {
const javaLanguageVersion = parseJavaToolchainVersion(input);
expect(javaLanguageVersion).toBe(output);
});
});
});
8 changes: 8 additions & 0 deletions lib/modules/manager/gradle/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { qAssignments } from './parser/assignments';
import { qKotlinImport } from './parser/common';
import { qDependencies, qLongFormDep } from './parser/dependencies';
import { setParseGradleFunc } from './parser/handlers';
import { qToolchainVersion } from './parser/language-version';
import { qKotlinMultiObjectVarAssignment } from './parser/objects';
import { qPlugins } from './parser/plugins';
import { qRegistryUrls } from './parser/registry-urls';
Expand Down Expand Up @@ -108,6 +109,13 @@ export function parseKotlinSource(
return { deps, vars };
}

export function parseJavaToolchainVersion(input: string): string | null {
const ctx: Partial<Ctx> = {};
const parsedResult = groovy.query(input, qToolchainVersion, ctx);

return parsedResult?.javaLanguageVersion ?? null;
}

const propWord = '[a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*';
const propRegex = regEx(
`^(?<leftPart>\\s*(?<key>${propWord})\\s*[= :]\\s*['"]?)(?<value>[^\\s'"]+)['"]?\\s*$`,
Expand Down
19 changes: 19 additions & 0 deletions lib/modules/manager/gradle/parser/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,22 @@ export const qKotlinImport = q
return ctx;
})
.handler(cleanupTempVars);

// foo { bar { baz } }
// foo.bar { baz }
export const qDotOrBraceExpr = (
symValue: q.SymMatcherValue,
matcher: q.QueryBuilder<Ctx, parser.Node>,
): q.QueryBuilder<Ctx, parser.Node> =>
q.sym<Ctx>(symValue).alt(
q.alt<Ctx>(
q.op<Ctx>('.').join(matcher),
q.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '{',
endsWith: '}',
search: matcher,
}),
),
);
59 changes: 59 additions & 0 deletions lib/modules/manager/gradle/parser/language-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { lexer } from 'good-enough-parser';
import { query as q } from 'good-enough-parser';
import { regEx } from '../../../../util/regex';
import type { Ctx } from '../types';
import { qDotOrBraceExpr } from './common';

// (21)
const qVersionNumber = q.tree({
type: 'wrapped-tree',
maxDepth: 1,
maxMatches: 1,
startsWith: '(',
endsWith: ')',
search: q.num((ctx: Ctx, node: lexer.Token) => {
ctx.javaLanguageVersion = node.value;
return ctx;
}),
});

// kotlin { jvmToolchain(17) }
// kotlin.jvmToolchain(17)
const qKotlinShortNotationToolchain = qDotOrBraceExpr(
'kotlin',
q.sym<Ctx>('jvmToolchain').join(qVersionNumber),
);

// JavaLanguageVersion.of(21)
const qJavaLanguageVersion = q
.sym<Ctx>('JavaLanguageVersion')
.op('.')
.sym('of')
.join(qVersionNumber);

// java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
// kotlin { jvmToolchain { languageVersion.set(JavaLanguageVersion.of(17)) } }
const qLongFormToolchainVersion = qDotOrBraceExpr(
regEx(/^(?:java|kotlin)$/),
qDotOrBraceExpr(
regEx(/^(?:toolchain|jvmToolchain)$/),
q.sym<Ctx>('languageVersion').alt(
q.op<Ctx>('=').join(qJavaLanguageVersion),
q
.op<Ctx>('.')
.sym('set')
.tree({
type: 'wrapped-tree',
maxDepth: 1,
startsWith: '(',
endsWith: ')',
search: q.begin<Ctx>().join(qJavaLanguageVersion).end(),
}),
),
),
);

export const qToolchainVersion = q.alt(
qKotlinShortNotationToolchain,
qLongFormToolchainVersion,
);
2 changes: 2 additions & 0 deletions lib/modules/manager/gradle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface ParseGradleResult {
deps: PackageDependency<GradleManagerData>[];
urls: PackageRegistry[];
vars: PackageVariables;
javaLanguageVersion?: string;
}

export interface GradleCatalog {
Expand Down Expand Up @@ -80,6 +81,7 @@ export interface Ctx {
globalVars: PackageVariables;
deps: PackageDependency<GradleManagerData>[];
registryUrls: PackageRegistry[];
javaLanguageVersion?: string;

varTokens: lexer.Token[];
tmpKotlinImportStore: lexer.Token[][];
Expand Down