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 AddDependency when dependent on other recipes #3942

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

knutwannheden
Copy link
Contributor

@knutwannheden knutwannheden commented Jan 22, 2024

The AddDependency#onlyIfUsing option currently only works if the source files already in the initial state (before any other recipes were applied) already contained references to what is specified in the onlyIfUsing option. Thus, if another recipe were to modify the code such that it adds a reference to code matching onlyIfUsing, this edit on its own would not lead to the dependency being added.

The problem is that AddDependency only runs in one cycle and thus uses the scanning phase to decide if a dependency needs to be added. To properly describe the use case described above, it should however request a second cycle in some scenarios.

As for the implementation the idea would be for the recipe to in the edit phase continue to check for uses of the onlyIfUsing regardless of whether it already found anything or not as soon as the build script for the respective project / source set has been processed. If a match is found it would indicate this to the recipe scheduler via a new API and could then also immediately stop scanning for more uses.

The `AddDependency#onlyIfUsing` option currently only works if the source files already in the initial state (before any other recipes were applied) already contained references to what is specified in the `onlyIfUsing` option. Thus, if another recipe were to modify the code such that it adds a reference to code matching `onlyIfUsing`, this edit on its own would not lead to the dependency being added.

The problem is that `AddDependency` only runs in one cycle and thus uses the scanning phase to decide if a dependency needs to be added. To properly describe the use case described above, it should however request a second cycle in some scenarios.
@@ -116,6 +116,8 @@ public class AddDependency extends ScanningRecipe<AddDependency.Scanned> {
@Nullable
Boolean acceptTransitive;

transient boolean requiresAnotherCycle = true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In a first iteration I was planning on having this transient field (initially false) which the recipe in the edit phase sets to true if it detects any source files which match onlyIfUsing so that a second cycle gets run.

A more proper fix would be a dedicated API. One idea would be to add an ExecutionContext parameter to the existing Recipe#causesAnotherCycle() method, which would allow the recipe to store something in the ExecutionContext. Another idea would be to add a new API to RecipeRunCycle (can be obtained via ExecutionContext#getCycleDetails()) or directory on ExecutionContext. I am thinking something like a requestAnotherCycle(). But the existing Recipe#causesAnotherCycle() would likely have to remain for declarative recipes.

Depending on the approach, some additional changes may be required to the recipe scheduler, as it currently expects the recipe requesting another cycle to also have performed changes in the current cycle. This may not always be the case.

@timtebeek timtebeek added the bug Something isn't working label Jan 22, 2024
@shanman190
Copy link
Contributor

In the past the detail on onlyIfUsing was that the type was already being used on the before side, rather than the after side.

@knutwannheden
Copy link
Contributor Author

@shanman190 I would have to look at the history of the recipe, but assuming it was a "scanning recipe" before 8.0, it would still have picked up on changes performed by earlier recipes, as the scanning recipes worked entirely different back then. But it would be worth it to verify this.

@shanman190
Copy link
Contributor

Ahh, right right. Change type would put the type in use, then the onlyIfUsing would have tripped automatically. Now I see the bug you're addressing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

None yet

3 participants