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

Fix some IP problems in the build #28929

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build-logic-commons/basics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ java {
}

dependencies {
api("gradlebuild:environment")
api(platform(project(":build-platform")))

implementation("com.google.guava:guava") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ import org.gradle.internal.os.OperatingSystem
import org.gradle.kotlin.dsl.*


abstract class BuildEnvironmentExtension {
abstract val gitCommitId: Property<String>
abstract val gitBranch: Property<String>
abstract val repoRoot: DirectoryProperty
}


// `generatePrecompiledScriptPluginAccessors` task invokes this method without `gradle.build-environment` applied
fun Project.getBuildEnvironmentExtension(): BuildEnvironmentExtension? = rootProject.extensions.findByType(BuildEnvironmentExtension::class.java)
fun Project.getBuildEnvironmentExtension(): BuildEnvironmentExtension? = extensions.findByType(BuildEnvironmentExtension::class.java)


fun Project.repoRoot(): Directory = getBuildEnvironmentExtension()?.repoRoot?.get() ?: layout.projectDirectory.parentOrRoot()
Expand Down Expand Up @@ -65,24 +58,6 @@ fun Project.currentGitBranchViaFileSystemQuery(): Provider<String> = getBuildEnv
fun Project.currentGitCommitViaFileSystemQuery(): Provider<String> = getBuildEnvironmentExtension()?.gitCommitId ?: objects.property(String::class.java)


@Suppress("UnstableApiUsage")
fun Project.git(vararg args: String): Provider<String> {
val projectDir = layout.projectDirectory.asFile
val execOutput = providers.exec {
workingDir = projectDir
isIgnoreExitValue = true
commandLine = listOf("git", *args)
if (OperatingSystem.current().isWindows) {
commandLine = listOf("cmd", "/c") + commandLine
}
}
return execOutput.result.zip(execOutput.standardOutput.asText) { result, outputText ->
if (result.exitValue == 0) outputText.trim()
else "<unknown>" // It's a source distribution, we don't know.
}
}


// pre-test/master/queue/alice/feature -> master
// pre-test/release/current/bob/bugfix -> release
// gh-readonly-queue/master/pr-1234-5678abcdef -> master
Expand Down
2 changes: 2 additions & 0 deletions build-logic-commons/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
}

includeBuild("../build-logic-settings")

// Shared basics for all
include("basics")

Expand Down
17 changes: 17 additions & 0 deletions build-logic-settings/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

description = "Provides settings plugins for configuring global build configuration"
30 changes: 30 additions & 0 deletions build-logic-settings/environment/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
`kotlin-dsl`
}

description = "Provides plugins for configuring global settings"

group = "gradlebuild"

java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
vendor = JvmVendorSpec.ADOPTIUM
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import gradlebuild.basics.BuildEnvironmentExtension
import gradlebuild.basics.git
import gradlebuild.basics.parentOrRoot

/*
* Copyright 2022 the original author or authors.
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,8 +14,13 @@ import gradlebuild.basics.parentOrRoot
* limitations under the License.
*/

import gradlebuild.basics.BuildEnvironmentExtension
import gradlebuild.basics.git

val buildEnvironmentExtension = extensions.create("buildEnvironment", BuildEnvironmentExtension::class)
buildEnvironmentExtension.gitCommitId = git("rev-parse", "HEAD")
buildEnvironmentExtension.gitBranch = git("rev-parse", "--abbrev-ref", "HEAD")
buildEnvironmentExtension.repoRoot = layout.projectDirectory.parentOrRoot()
val buildLayout = layout
gradle.lifecycle.beforeProject {
val buildEnvironmentExtension = extensions.create("buildEnvironment", BuildEnvironmentExtension::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Before git would be executed only twice per build, right? And now it's once per project?

Copy link
Member Author

@asodja asodja Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly it seems it's called 3-times in both cases during sync, at least when trying to do some println in the zip part I get for both:

> Configure project :
Executed 'git rev-parse --abbrev-ref HEAD' for ':'
Executed 'git rev-parse --abbrev-ref HEAD' for ':'
Executed 'git rev-parse --abbrev-ref HEAD' for ':'

So it looks like provider is evaulated anytime git() is called anyway, or that should not be the case?

If you have any ideas how to improve that, let me know.
We can also just merge improvement for gradle/shared-with-buildSrc/mirrors.settings.gradle.kts that is trivial and we leave this one for some other time when we'll have more tools.

Copy link
Member Author

@asodja asodja Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts if that solution could be a problem or not @gradle/bt-developer-productivity?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like provider is evaulated anytime git() is called anyway, or that should not be the case?

This seems to be expected behavior. BiProvider's implementation did run the combiner function every time:

R combinedUnpackedValue = combiner.apply(leftValue.getWithoutSideEffect(), rightValue.getWithoutSideEffect());

That being said, it's still a bit inefficient to have the provider instance in every Project, even though it may not be a problem right now. Suppose somebody start using gitCommitId in every Project in the future, that would be executed hundreds of times.

Right now I don't have a very good idea for this, is it possible to store the information in the similar way as DevelocityConfiguration? I think there's only one instance stored in the build right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be possible to always execute git once in the settings plugin and then simply propagate the data to every project as a Kotlin data class or similar construct (until we have a proper solution via the Shared Data API):

captureBuildEnvironment().let { buildEnvironment ->
  gradle.lifecycle.beforeProject {
     extensions.add("buildEnvironment", buildEnvironment)
  }
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will that affect configuration cache? E.g. will that git read be an input to configuration cache?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my example, yes. Is the git result currently directly wired as a task input or is it ever used at configuration time?

Copy link
Member Author

@asodja asodja May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in some cases it's still used at a configuration time, but there was a PR that improved that #27954.

I was thinking that maybe a solution with a Build service could be ok in this case. So in root we would register a build service that would then cache the result of a git command, so it could be reused in other projects.

Copy link
Member Author

@asodja asodja May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think solution with BuildService works. I tested and cc is reused if commit id changes, at least for integration tests.

And if

* The external process is executed only once and only when the value is requested for the first
* time.
is true, then git process should be executed just once

buildEnvironmentExtension.gitCommitId = git("rev-parse", "HEAD")
buildEnvironmentExtension.gitBranch = git("rev-parse", "--abbrev-ref", "HEAD")
buildEnvironmentExtension.repoRoot = buildLayout.rootDirectory
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gradlebuild.basics

import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.internal.os.OperatingSystem

abstract class BuildEnvironmentExtension {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💅 Why not an interface?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess because I just copy/pasted the class. :)
Changed it to interface.

abstract val gitCommitId: Property<String>
abstract val gitBranch: Property<String>
abstract val repoRoot: DirectoryProperty
}

@Suppress("UnstableApiUsage")
fun Project.git(vararg args: String): Provider<String> {
val projectDir = layout.projectDirectory.asFile
val execOutput = providers.exec {
workingDir = projectDir
isIgnoreExitValue = true
commandLine = listOf("git", *args)
if (OperatingSystem.current().isWindows) {
commandLine = listOf("cmd", "/c") + commandLine
}
}
return execOutput.result.zip(execOutput.standardOutput.asText) { result, outputText ->
if (result.exitValue == 0) outputText.trim()
else "<unknown>" // It's a source distribution, we don't know.
}
}
29 changes: 29 additions & 0 deletions build-logic-settings/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

dependencyResolutionManagement {
repositories {
gradlePluginPortal()
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.8.0")
}

include("environment")

rootProject.name = "build-logic-settings"
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
id("gradlebuild.build-environment")
id("gradlebuild.root-build")

id("gradlebuild.teamcity-import-test-data") // CI: Import Test tasks' JUnit XML if they're UP-TO-DATE or FROM-CACHE
Expand Down
2 changes: 1 addition & 1 deletion gradle/shared-with-buildSrc/mirrors.settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fun normalizeUrl(url: String): String {
return if (result.endsWith("/")) result else "$result/"
}

gradle.allprojects {
gradle.lifecycle.beforeProject {
buildscript.configurations["classpath"].incoming.beforeResolve {
withMirrors(buildscript.repositories)
}
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ pluginManagement {
}
gradlePluginPortal()
}
includeBuild("build-logic-settings")
}

plugins {
id("gradlebuild.build-environment")
id("com.gradle.develocity").version("3.17.1") // Sync with `build-logic-commons/build-platform/build.gradle.kts`
id("io.github.gradle.gradle-enterprise-conventions-plugin").version("0.9.1")
id("org.gradle.toolchains.foojay-resolver-convention") version ("0.8.0")
Expand Down