From 213bb63776e921cf97ece3cc3420fac3e6b0df2f Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Sun, 5 Jun 2022 19:03:43 -0600 Subject: [PATCH] Use java to determine user.home dir Although convenient, the os.homedir() function can return a different value that the 'user.home' SystemProperty in Java. The latter is used to locate the Gradle User Home directory. By switching to use Java to determine the value for 'user.home', we can use the same process as Gradle to determine Gradle User Home. Fixes #207 --- src/setup-gradle.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/setup-gradle.ts b/src/setup-gradle.ts index 1170015..fc316c7 100644 --- a/src/setup-gradle.ts +++ b/src/setup-gradle.ts @@ -13,7 +13,7 @@ const GRADLE_USER_HOME = 'GRADLE_USER_HOME' const CACHE_LISTENER = 'CACHE_LISTENER' export async function setup(buildRootDirectory: string): Promise { - const gradleUserHome = determineGradleUserHome(buildRootDirectory) + const gradleUserHome = await determineGradleUserHome(buildRootDirectory) // Bypass setup on all but first action step in workflow. if (process.env[GRADLE_SETUP_VAR]) { @@ -55,13 +55,30 @@ export async function complete(): Promise { writeJobSummary(buildResults, cacheListener) } -function determineGradleUserHome(rootDir: string): string { +async function determineGradleUserHome(rootDir: string): Promise { const customGradleUserHome = process.env['GRADLE_USER_HOME'] if (customGradleUserHome) { return path.resolve(rootDir, customGradleUserHome) } - return path.resolve(os.homedir(), '.gradle') + return path.resolve(await determineUserHome(), '.gradle') +} + +/** + * Different values can be returned by os.homedir() in Javascript and System.getProperty('user.home') in Java. + * In order to determine the correct Gradle User Home, we ask Java for the user home instead of using os.homedir(). + */ +async function determineUserHome(): Promise { + const output = await exec.getExecOutput('java', ['-XshowSettings:properties', '-version'], {silent: true}) + const regex = /user\.home = (\S*)/i + const found = output.stderr.match(regex) + if (found == null || found.length <= 1) { + core.info('Could not determine user.home from java -version output. Using os.homedir().') + return os.homedir() + } + const userHome = found[1] + core.debug(`Determined user.home from java -version output: '${userHome}'`) + return userHome } function getUniqueGradleHomes(buildResults: BuildResult[]): string[] {