gradle-git-properties icon indicating copy to clipboard operation
gradle-git-properties copied to clipboard

`generateGitProperties` fails to resolve .git dir on 2.5.0

Open vpavic opened this issue 9 months ago • 22 comments

After picking up 2.5.0 generateGitProperties task fails with the following cause:

Caused by: org.gradle.api.GradleException: No Git repository found. Ensure the gitProperties.dotGitDirectory property points to the correct .git directory.
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at org.gradle.internal.classpath.intercept.DefaultCallSiteDecorator$DecoratingCallSite.lambda$callConstructor$2(DefaultCallSiteDecorator.java:248)
	at org.gradle.internal.instrumentation.api.groovybytecode.InvocationImpl.callNext(InvocationImpl.java:63)
	at org.gradle.internal.classpath.intercept.DefaultCallSiteDecorator$1.intercept(DefaultCallSiteDecorator.java:88)
	at org.gradle.internal.classpath.intercept.DefaultCallSiteDecorator$DecoratingCallSite.callConstructor(DefaultCallSiteDecorator.java:247)
	at com.gorylenko.GenerateGitPropertiesTask.generate(GenerateGitPropertiesTask.groovy:114)
	...

This is on a multi-project build, and after taking a very quick look at https://github.com/n0mer/gradle-git-properties/commit/84306004450b99aab3857f05c8f465c49bb43de0 I suspect those changes don't take such setup into account.

vpavic avatar Mar 07 '25 18:03 vpavic

I had the same issue, but setting following config:

gitProperties {
    dotGitDirectory = project.rootProject.layout.projectDirectory.dir(".git")
}

Solved the issue for me

Grandys avatar Mar 10 '25 07:03 Grandys

That's certainly possible, but rather than adding workarounds IMO it's wiser to skip the upgrade to 2.5.0 altogether and instead wait for the release that will fix this regression.

vpavic avatar Mar 10 '25 08:03 vpavic

I can see at https://github.com/n0mer/gradle-git-properties/pull/235 The git directory has to be set explicitly with dotGitDirectory property.

I think that this either should be changed, or a note about multi-project build should be added to https://github.com/n0mer/gradle-git-properties/blob/master/README.md?plain=1#L142

kdebski85 avatar Mar 10 '25 08:03 kdebski85

This workaround helped for our composite build project

fun findGitDirectory(startDir: File): File? {
    var currentDir: File? = startDir
    while (currentDir != null && !File(currentDir, ".git").exists()) {
        currentDir = currentDir.parentFile
    }
    return currentDir?.let { File(it, ".git") }
}

gitProperties {
    val gitDir = findGitDirectory(project.projectDir)
    if (gitDir != null) {
        dotGitDirectory.set(gitDir)
    } else {
        throw GradleException("Could not find .git directory")
    }
}

roman-kiselevich avatar Mar 10 '25 13:03 roman-kiselevich

build.gradle.kts multi-project workaround:

gitProperties {
    dotGitDirectory.set(file("${rootProject.rootDir}/.git"))
}

demtnt avatar Mar 11 '25 08:03 demtnt

in combination with worktrees the workaround from https://github.com/n0mer/gradle-git-properties/issues/14 does not work anymore with this change

tompson avatar Mar 11 '25 18:03 tompson

We have the same problem and don't use a multi-module build. We simply use a setup generated by https://start.spring.io.
As for 99.9% of all projects .git is the correct Git folder. I really don't wanna setup the property for all the 10 services we have, this should rather be a working default. Any ETA?

cmdjulian avatar Mar 17 '25 10:03 cmdjulian

With Gradle 8.13 you can write

gitProperties {
    dotGitDirectory = layout.settingsDirectory.dir(".git")
}

This should be compatible with the configuration cache, so maybe it can be incorporated as a default for the plugin (provided Gradle version is >= 8.13)?

ummels avatar Mar 18 '25 08:03 ummels

So much for proper semantic versioning this was a breaking change. As others have already stated this simply should be the default or if it wasn't possible, this should have been properly referenced as a breaking change and semantic version updated accordingly.

jackson-chris avatar Mar 21 '25 16:03 jackson-chris

Since this breaks multi-project builds (which are very common) would it make sense to set this as the default which I think would work for the majority of the users?

gitProperties.dotGitDirectory = project.rootProject.layout.projectDirectory.dir('.git')

Edit: well maybe it should also handle worktrees: https://github.com/n0mer/gradle-git-properties/issues/242#issuecomment-2756001703

jonatan-ivanov avatar Mar 26 '25 23:03 jonatan-ivanov

This breaks the configuration cache again due to the usage of rootProject. See https://github.com/n0mer/gradle-git-properties/issues/240#issuecomment-2732069210 for a solution which is compatible with the configuration cache.

ummels avatar Mar 27 '25 07:03 ummels

Any progress or eta here? It fails most of our pipelines still

cmdjulian avatar Apr 22 '25 15:04 cmdjulian

Any progress or eta here? It fails most of our pipelines still

@cmdjulian I guess it's "resolved" by this section in the README:

gitProperties {
    // if .git directory is on the same level as the root project
    dotGitDirectory = project.rootProject.layout.projectDirectory.dir(".git")
    
    // if .git directory is in a different location
    dotGitDirectory = "${project.rootDir}/../somefolder/.git"
}

BTW, I hate these backward incompatible changes in non-major version upgrades. @tha2015 next time, could you please raise the major version for such changes?

liry avatar May 20 '25 10:05 liry

Why am I as a user are forced to set those settings in the first place though?
I'm not too deep into Gradle plugin development, but can't this be set as a default within the plugin itself? This is not convenient for the user at all. I don't want to maintain these settings for all of our projects now

cmdjulian avatar May 20 '25 10:05 cmdjulian

I don't want to maintain these settings for all of our projects now

There should be no need to manually configure the defaults. When you set up your git repository you also don't have to specify manually that it should use the .git folder. The plugin should have reasonable defaults so it works out of box with zero configuration, which would also maintain backwards compatibility and avoid forcing the whole userbase to add completely unnecessary and avoidable configuration options. We are also ignoring this update, it's not worth the hassle.

mihalyr avatar May 20 '25 12:05 mihalyr

Please try v.2.5.2 to see if the issue is fixed. Thanks.

tha2015 avatar Jul 12 '25 01:07 tha2015

@tha2015 didn't work for our project after upgrading to 2.5.2. This workaround still helps https://github.com/n0mer/gradle-git-properties/issues/240#issuecomment-2710689037

roman-kiselevich avatar Jul 17 '25 07:07 roman-kiselevich

Reopening for fixing again

tha2015 avatar Jul 17 '25 17:07 tha2015

please check if issue still exists with version 2.5.3

tompson avatar Sep 03 '25 11:09 tompson

I get the same issue with jdk23, gradle 8.14.2 and the git-props plugin version 2.5.3.

gitProperties { extProperty = 'gitProps' dotGitDirectory = project.rootProject.layout.projectDirectory.dir(".git") } generateGitProperties.outputs.upToDateWhen { false }

Task :generateGitProperties FAILED [Incubating] Problems report is available at: ****/build/reports/problems/problems-report.html FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':generateGitProperties'.

No Git repository found. Ensure the gitProperties.dotGitDirectory property points to the correct .git directory.

vroyer avatar Sep 06 '25 06:09 vroyer

FYI I was stuck for a while with this error message because I got confused by a different issue: if you're in a monorepo where the settings.gradle or rootProject are not in the git root of the repo, you need something like

gitProperties {
	dotGitDirectory = layout.settingsDirectory.dir("../.git")
}

(notice the ../)

schnapster avatar Sep 09 '25 10:09 schnapster