From 1f1ffc7ea132fc273889cde37e2f63e5a1eebc8f Mon Sep 17 00:00:00 2001 From: Andre Destro Date: Thu, 19 Nov 2026 17:30:33 +0000 Subject: [PATCH] fix: move plugin variable lookups to rootProject.hasProperty Gradle 9 deprecates project.hasProperty() finding a property that is only declared on the root project, and Gradle 10 will turn it into an error. Migrated plugins read app overrides with project.hasProperty('x') ? rootProject.ext.x : default, so apps get a warning per variable. Write the lookups as rootProject.hasProperty(). The existing matchers expect the project form, so updateBuildGradle normalizes to it after reading and switches to rootProject before writing, which keeps a second run working. --- src/index.ts | 4 +++ src/property-lookup.test.ts | 57 +++++++++++++++++++++++++++++++++++++ src/property-lookup.ts | 12 ++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/property-lookup.test.ts create mode 100644 src/property-lookup.ts diff --git a/src/index.ts b/src/index.ts index 0566d97..98a514c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,7 @@ import type { CommandRunner } from './install-scripts'; import { logger } from './log'; import { declaresOtherPackageManager } from './package-manager'; import { migratePackageSwift } from './package-swift'; +import { setPropertyLookupReceiver } from './property-lookup'; import { runCommand } from './subprocess'; const coreVersion = '9.0.0-rc.0'; @@ -520,6 +521,8 @@ async function updateBuildGradle( return; } gradleFile = gradleFile.replaceAll(' = ', ' = '); + // the matchers below expect `project.hasProperty`, also when the tool runs again on a migrated file + gradleFile = setPropertyLookupReceiver(gradleFile, 'project'); logger.info('Updating build.gradle'); gradleFile = gradleFile.replaceAll('androidxCoreKTXVersion', 'androidxCoreVersion'); @@ -614,6 +617,7 @@ async function updateBuildGradle( ); } + gradleFile = setPropertyLookupReceiver(gradleFile, 'rootProject'); writeFileSync(filename, gradleFile, 'utf-8'); } diff --git a/src/property-lookup.test.ts b/src/property-lookup.test.ts new file mode 100644 index 0000000..ce9e3f5 --- /dev/null +++ b/src/property-lookup.test.ts @@ -0,0 +1,57 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { setPropertyLookupReceiver } from './property-lookup.ts'; + +describe('setPropertyLookupReceiver', () => { + const legacy = `ext { + junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' +} + +buildscript { + ext.kotlin_version = project.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '2.2.20' +} + +android { + compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 37 + defaultConfig { + minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 26 + } +}`; + const migrated = `ext { + junitVersion = rootProject.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2' +} + +buildscript { + ext.kotlin_version = rootProject.hasProperty("kotlin_version") ? rootProject.ext.kotlin_version : '2.2.20' +} + +android { + compileSdk = rootProject.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 37 + defaultConfig { + minSdkVersion rootProject.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 26 + } +}`; + + it('moves variable lookups to rootProject', () => { + assert.equal(setPropertyLookupReceiver(legacy, 'rootProject'), migrated); + }); + + it('is idempotent', () => { + assert.equal(setPropertyLookupReceiver(migrated, 'rootProject'), migrated); + }); + + it('moves them back to project', () => { + assert.equal(setPropertyLookupReceiver(migrated, 'project'), legacy); + }); + + it('leaves other hasProperty calls alone', () => { + const other = [ + `def flag = project.hasProperty('myFlag') ? project.myFlag : false`, + `x = project.hasProperty('a') ? rootProject.ext.b : 1`, + `if (subproject.hasProperty('x') ? rootProject.ext.x : 1) {}`, + `return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""`, + ].join('\n'); + assert.equal(setPropertyLookupReceiver(other, 'rootProject'), other); + }); +}); diff --git a/src/property-lookup.ts b/src/property-lookup.ts new file mode 100644 index 0000000..0efb12d --- /dev/null +++ b/src/property-lookup.ts @@ -0,0 +1,12 @@ +// Matches `project.hasProperty('x') ? rootProject.ext.x` (or the `rootProject.hasProperty` form) with the same `x` on both sides. +const PROPERTY_LOOKUP = + /(?