JavierSegoviaCordoba / semver-gradle-plugin

Set projects versions based on git tags and following semantic versioning
https://semver-gradle-plugin.javiersc.com
Other
25 stars 3 forks source link

Apply from kotlin Precompiled script plugin fails #108

Closed sureshg closed 1 year ago

sureshg commented 1 year ago

I have a pre-compiled Gradle script plugin maintained in build-logic project, which is getting applied to all root and sub projects.

// publishing.gradle.kts
package plugins

plugins {
    `maven-publish`
    signing
    id("com.javiersc.semver.gradle.plugin")
}
semver { tagPrefix = "v" }

The thing is, when com.javiersc.semver.gradle.plugin is getting added, the following is error is thrown

//in root and sub projects 
plugins {
  `java`
  id("plugins.publishing")
}

ERROR

> Task :build-logic:common-plugins:generatePrecompiledScriptPluginAccessors FAILED
semver plugin can't work if there are no commits

FAILURE: Build failed with an exception.

* Where:
Precompiled script plugin '~/code/xxxx/gradle/build-logic/common-plugins/src/main/kotlin/plugins/publishing.gradle.kts' line: 1

* What went wrong:
An exception occurred applying plugin request [id: 'com.javiersc.semver.gradle.plugin']
> Failed to apply plugin 'com.javiersc.semver.gradle.plugin'.
> Failed to create service 'gitTagBuildService'.
> Could not create an instance of type com.javiersc.semver.gradle.plugin.services.GitBuildService.
> Cannot invoke "org.eclipse.jgit.lib.AnyObjectId.hashCode()" because "id" is null
JavierSegoviaCordoba commented 1 year ago

Hi! Is it your project open source?

Your project is already a git repository and .git is located at the root directory of the project?

sureshg commented 1 year ago

Yes, it's a git repo and has .git in the root directory. If i move the plugin declaration form pre-compiled script to project specific build file , things will work fine. But i want to avoid that as it needs to duplicate the same config in root and sub-project build files.

sureshg commented 1 year ago

Here is the full stacktrace (configuration phase).

        at org.gradle.api.internal.provider.AbstractMinimalProvider.get(AbstractMinimalProvider.java:93)
        at com.javiersc.semver.gradle.plugin.valuesources.VersionValueSource$Companion$register$1.invoke(VersionValueSource.kt:93)
        at com.javiersc.semver.gradle.plugin.valuesources.VersionValueSource$Companion$register$1.invoke(VersionValueSource.kt:92)
        at com.javiersc.semver.gradle.plugin.valuesources.VersionValueSource$Companion.register$lambda$0(VersionValueSource.kt:92)
        at org.gradle.api.internal.provider.DefaultValueSourceProviderFactory.configureParameters(DefaultValueSourceProviderFactory.java:155)
        at org.gradle.api.internal.provider.DefaultValueSourceProviderFactory.createProviderOf(DefaultValueSourceProviderFactory.java:84)
        at org.gradle.api.internal.provider.DefaultProviderFactory.of(DefaultProviderFactory.java:212)
        at com.javiersc.semver.gradle.plugin.valuesources.VersionValueSource$Companion.register$semver_gradle_plugin(VersionValueSource.kt:128)
        at com.javiersc.semver.gradle.plugin.SemverPlugin.configureLazyVersion(SemverPlugin.kt:39)
        at com.javiersc.semver.gradle.plugin.SemverPlugin.apply(SemverPlugin.kt:25)
        at com.javiersc.semver.gradle.plugin.SemverPlugin.apply(SemverPlugin.kt:16)
        at org.gradle.api.internal.plugins.ImperativeOnlyPluginTarget.applyImperative(ImperativeOnlyPluginTarget.java:43)
        at org.gradle.api.internal.plugins.RuleBasedPluginTarget.applyImperative(RuleBasedPluginTarget.java:51)

.....
Caused by: java.lang.NullPointerException: Cannot invoke "org.eclipse.jgit.lib.AnyObjectId.hashCode()" because "id" is null
        at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache$Table.index(UnpackedObjectCache.java:115)
        at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache$Table.contains(UnpackedObjectCache.java:76)
        at org.eclipse.jgit.internal.storage.file.UnpackedObjectCache.isUnpacked(UnpackedObjectCache.java:31)
        at org.eclipse.jgit.internal.storage.file.LooseObjects.hasCached(LooseObjects.java:90)
        at org.eclipse.jgit.internal.storage.file.ObjectDirectory.openObjectWithoutRestoring(ObjectDirectory.java:354)
        at org.eclipse.jgit.internal.storage.file.ObjectDirectory.openObject(ObjectDirectory.java:345)
        at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:132)
        at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:214)
        at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:1081)
        at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:992)
        at com.javiersc.semver.gradle.plugin.internal.git.GitCache.<init>(GitCache.kt:19)
        at com.javiersc.semver.gradle.plugin.services.GitBuildService.<init>(GitBuildService.kt:43)
        at com.javiersc.semver.gradle.plugin.services.GitBuildService$Inject.<init>(Unknown Source)
sureshg commented 1 year ago

I am not sure, but with my limited knowledge, i think https://github.com/JavierSegoviaCordoba/semver-gradle-plugin/blob/main/semver-gradle-plugin/main/kotlin/com/javiersc/semver/gradle/plugin/SemverPlugin.kt#L22 eagerly set the root project dir as the one with pre-compiled script repo (another composite build) which is not correct.

JavierSegoviaCordoba commented 1 year ago

Can you check if, with the latest snapshot, you have this issue? I tried to reproduce it with it and it is working for me.

https://github.com/Reproducers/semver-repro

I will do a proper release with all changes if it works for you.

sureshg commented 1 year ago

Awesome..thanks for the quick fix. It seems working fine now.


> Task :playground-bom:printSemver
semver for playground-bom: v1.8.0.29+a1957b0

> Task :printSemver
semver for openjdk-playground: v1.8.0.29+a1957b0

> Task :playground-catalog:printSemver
semver for playground-catalog: v1.8.0.29+a1957b0

> Task :playground-k8s:printSemver
semver for playground-k8s: v1.8.0.29+a1957b0
JavierSegoviaCordoba commented 1 year ago

I did a few more changes, indeed the plugin id is now com.javiersc.semver for both, project and settings plugins.

I will release 0.5.0-alpha.1 in a few minutes/hours and probably I will promote it next week to 0.5.0 as it is already compatible with Gradle 8.0 + configuration cache + project isolation.

sureshg commented 1 year ago

Hi, is 0.5.0-alpha.1 already released? Sorry i couldn't find it in the Gradle plugin repo under the new id.

JavierSegoviaCordoba commented 1 year ago

It has been published right now.

JavierSegoviaCordoba commented 1 year ago

If you have any additional problem related to precompiled plugin failures, feels free to reopen this issue.

sureshg commented 1 year ago

Hi, how does this plugin work for a composite build (includedBuild) which also has this plugin applied in the same project hierarchy ?

JavierSegoviaCordoba commented 1 year ago

I haven't tried it a lot with composite builds but maybe you should set the git dir by yourself as the git directory will be on the parent directory (or in another directory), and the plugin is looking for the rootDir.

semver {
    gitDir.set(file(...))
}

If you find any issue, report it and I will fix it asap :)