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

[Extension] setForcedDependencies not present inside ApplicationArchivesBuildItem #42188

Closed
dcdh opened this issue Jul 28, 2024 · 5 comments
Closed
Labels
area/core kind/bug Something isn't working

Comments

@dcdh
Copy link
Contributor

dcdh commented Jul 28, 2024

Describe the bug

I would like to check the presence of a dependency using the ApplicationArchivesBuildItem.

However, the dependency is not found.

Expected behavior

The dependency should be found and validation should fail.

Actual behavior

The test fail in an unexpected manner and I expect it to catch a validation error..

Failing this way

org.opentest4j.AssertionFailedError: The build was expected to fail

How to Reproduce?

The processor definition

class BannedDependenciesProcessor {

    @BuildStep
    void banDependencies(final ApplicationArchivesBuildItem applicationArchivesBuildItem,
                         final BuildProducer<ValidationPhaseBuildItem.ValidationErrorBuildItem> validationErrorBuildItemProducer) {
        applicationArchivesBuildItem.getApplicationArchives().stream()
                .map(ApplicationArchive::getKey)
                .filter(new IsBannedDependency())
                .forEach(bannedDependency ->
                        validationErrorBuildItemProducer.produce(new ValidationPhaseBuildItem.ValidationErrorBuildItem(
                                new IllegalStateException(String.format("Dependency '%s' '%s' is banned and so cannot be used", bannedDependency.getGroupId(), bannedDependency.getArtifactId()))
                        ))
                );
    }

    private record IsBannedDependency() implements Predicate<ArtifactKey> {
        private final static List<ArtifactKey> BANNED_ARTIFACTS = List.of(
                ArtifactKey.ga("io.quarkus", "quarkus-config-yaml"));

        @Override
        public boolean test(final ArtifactKey artifactKey) {
            return BANNED_ARTIFACTS.contains(artifactKey);
        }
    }
}

The test definition

public class ShouldFailWhenABannedDependencyIsUsedTest {

    @RegisterExtension
    static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
            .withEmptyApplication()
            .setForcedDependencies(
                    List.of(
                            Dependency.of("io.quarkus", "quarkus-config-yaml", Version.getVersion())))
            .assertException(throwable -> assertThat(throwable)
                    .hasNoSuppressedExceptions()
                    .rootCause()
                    .hasMessage("TODO")
                    .hasNoSuppressedExceptions());

    @Test
    public void test() {
        Assertions.fail("Startup should have failed");
    }
}

Output of uname -a or ver

Linux 2a02-8428-dff8-c601-234b-8c10-a3c4-2308.rev.sfr.net 6.9.6-200.fc40.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jun 21 15:48:21 UTC 2024 x86_64 GNU/Linux

Output of java -version

openjdk version "21.0.3" 2024-04-16

Quarkus version or git rev

3.12.3

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

Maybe I am using the wrong build item ?

@dcdh dcdh added the kind/bug Something isn't working label Jul 28, 2024
@dcdh
Copy link
Contributor Author

dcdh commented Jul 28, 2024

When I add the dependency inside an application with

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-config-yaml</artifactId>
    </dependency>

the application starts meanwhile I expect it to fail at startup.

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 29, 2024

/cc @Sanne (core), @aloubyansky (core), @gsmet (core), @radcortez (core), @stuartwdouglas (core)

@aloubyansky
Copy link
Member

ArtifactKey.ga("io.quarkus", "quarkus-config-yaml") results in an incomplete key. It's confusing but it's for different purposes. The complete key will be ArtifactKey.of("io.quarkus", "quarkus-config-yaml", null, "jar").

@aloubyansky
Copy link
Member

That's actually the first part of it. The other thing to keep in mind is that ApplicationArchivesBuildItem does not represent all the application dependencies. CurateOutcomeBuildItem.getApplicationModel().getDependencies() or (in case you are interested only in the runtime dependencies) CurateOutcomeBuildItem.getApplicationModel().getRuntimeDependencies() will return all the necessary dependencies.

@dcdh
Copy link
Contributor Author

dcdh commented Jul 29, 2024

Thanks, I was looking for it.

@dcdh dcdh closed this as completed Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants