This message was deleted.
# community-support
s
This message was deleted.
c
chicken and egg. The configuration phase (task graph/dependencies) has already happened by the time you execute a task. Perhaps modelling this as a build service and doing all this at configuration time would work.
😢 1
m
Yeah this is what I was afraid of. I'd rather not take that configuration time hit at the moment. I've been pouring over this taskDependencies doc but can't find a way that's both: 1. Configuration-cache-safe 2. Not run at configuration time
c
another option, perhaps viable (depends on the specifics / complexity of your soln) is to always add the task dependencies, and have an
onlyIf
to conditionally execute them (which would look at something for that logic)
m
So in an example build graph: app -> library -> core I could define a
Task
that always adds
app:test
library:test
and
core:test
as task dependencies, but use
onlyIf
to only run
app:test
based on some condition that I can run during the execution phase?
c
correct
m
Interesting, I'll give this a try. Is that the only possible option, though?
One Issue I want to solve is we have two builds:
shared
and
platform
. I want to only run tests on relevant code, and there doesn't seem to be a way to register a task in
platform
that has knowledge of the substituted projects from
shared
c
you’re looking for something beyond running
test
in the root project, which would run all
test
tasks in sub-projects (with their up-to-date checks, etc)?
m
Indeed! It would need to run tests for every buildable artifact in a given graph, not just the
subProjects
of a given
RootProject
c
is each subproject not a buildable artifact? If there are variants within a project, could their test tasks be added as dependenceis on the lifecycle
test
task?
m
From what I've seen
subprojects
doesn't include any
Project
that is not a direct descendent of the root, though I could be wrong in that. e.g. if you have two builds,
platform
and
shared
,
platform:subprojects
won't have the projects it uses from
shared
c
wasn’t suggesting
subprojects
, that’s legacy and broken. When you run
test
on the root project Gradle finds all
test
tasks in any child project (of any level) and runs those. This accounts for any hierarchy, project substitutions, etc.
if those are separate builds you could create a wrapper build that includes them as composite builds (or include one in the other, if there is a logical parent).
m
We do have a composite parent build, yeah, that includes both
platform
and
shared
. What's strange is if I run
test
from the root (parent) directory like
./gradlew platform:test
, I get a task not found exception But when I cd into
platform
and run
test
, that works as expected (As you described)
c
check what you see in
./gradlew tasks
Of note, the current project in front of me has an included plugin build, those tasks don’t show up (as they shouldn’t - it’s a different lifecycle being a plugin). Or
./gradlew tasks --all
👍 1
m
./gradlew tasks --all
doesn't return anything from the included builds - is that to be expected?
c
ah yea. yes, composite builds are separately executed, not merged into overall build. They are intended to substitute dependencies. If that is not what your composite builds are doing perhaps they are better reflected as an included build (settings.gradle.kts -> include)
m
Btw if
subprojects
is legacy, what should we use instead?
c
convention plugins are the correct way to configure multi-module projects, avoiding coupling via
subprojects
(or
allprojects
) that breaks project isolation, etc.
m
So we have convention plugins for each of our different project "types", but we use
subprojects
to do things like:
Copy code
project.subprojects {
            val testTask: String? = when {
                project.isAndroidApp() || project.isAndroidLib() -> "$path:testDebugUnitTest"
                project.isJavaProject() -> "$path:test"
                else -> null
            }
            if (testTask != null) {
                this@TestAll.dependsOn(testTask)
            }
        }
I've been meaning to get rid of this logic anyway - what we should convert this logic to? This
TestAll
task was added so we could only run certain variants, as
Android
projects have both a
debug
and
release
test that runs
c
not an Android developer so may miss some specifics, but would suggest modelling this as: • each convention plugin exposes ‘testAndroid’, ’testAndroidLib` as appropriate; • In your root project create the same task names, which implicitly will run the corresponding task in all subprojects. so
./gradlew testAndroid
or
./gradlew testAndroidLib
As desired add these as dependencies of
test
or have a
testAll
as you’ve noted.
🙌 1
m
This is perfect, thanks so much!
👍 1
So I have what I think is a very ugly initial implementation of this working:
Apologies for the text blob, thought it'd be formatted a bit better
My only question left: if I have those two builds
platform
and
shared
, and I want to run tests for
shared
, I can just run
test
on
platform
right?
c
depends: if its a composite build it’s a separate execution and won’t propagate tasks; if its an included build, it will propagate tasks.
🙌 1
m
That goes back to my initial question, then. I'm designing something that'll only run tests on changed projects - if I run
test
, that'll run
tests
on all dependencies even if we don't need to re-validate them. How can I run
test
for just the paths in the graph that we know we need to validate? For context, we do have Gradle Enterprise, some
build/
folder caching, etc setup on our CI server.
c
generally all that is handled by task up-to-date checks - only projects that have changes will need to re-run tests.
m
Gotcha, lemme do some testing validation then get back to you
👍 1
Circling back to this discussion: 1. The "automatic task finding" feature (i.e. I run
X
in my root project, I expect
X
to run for all subprojects) works as expected but not for our case because we have included builds 2. I was able to dynamically generate a list of tasks to run based on files changed (on top of our Gradle Enterprise/task output caching), but I'm struggling how to model this logic. You mentioned previously that I should model this all as a build service. I currently have three tasks: 1. Find list of changed files using git 2. Find affected projects by traversing
ResolvedComponentResult
3. Use result of 2) to run stuff, but running into a chicken-and-egg problem as you mentioned How would I migrate this logic to the configuration phase and not the execution phase?
UPDATE: I understand how to do this in the configuration phase, but now how do I ensure this is properly tested? I had a bunch of TestKit tests that ran a given task and asserted on its output - can the same be done for things done not in the execution phase?