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

Generated source folder should be registered on the SourceSet #474

Closed
oehme opened this issue Feb 25, 2021 · 6 comments · Fixed by #477 or #506
Closed

Generated source folder should be registered on the SourceSet #474

oehme opened this issue Feb 25, 2021 · 6 comments · Fixed by #477 or #506

Comments

@oehme
Copy link
Contributor

oehme commented Feb 25, 2021

The plugin currently registers its generated sources directly on the JavaCompile task. This is too low level and means that other plugins/tasks will not pick up on those generated sources. For example, they will be missing from the source jar.

On top of that, it should integrate with the IDEA plugin and register that folder as a "generated" folder, so that it is correctly marked as generated in IDEA. That way, users are warned if they try to edit those files.

@voidzcy
Copy link
Collaborator

voidzcy commented Feb 26, 2021

This is too low level and means that other plugins/tasks will not pick up on those generated sources. For example, they will be missing from the source jar.

This is kind of working as intended. Protobuf projects work in the way that APIs are exposed through protobuf definitions, so that consuming projects can import protos from dependencies to build their own proto APIs. Generated code is not the real source, but protos are. On the other hand, adding generated code into source sets automatically includes them Javadoc as generated classes are always public. This is generally not wanted (e.g., for gRPC-Java) given that proto sources are exposed as APIs.

On top of that, it should integrate with the IDEA plugin and register that folder as a "generated" folder, so that it is correctly marked as generated in IDEA. That way, users are warned if they try to edit those files.

Right, that's the better approach. There is a TODO item left in the code for migrating to model.module.generatedSourceDirs. Are you interested in contributing a PR?

@oehme
Copy link
Contributor Author

oehme commented Feb 26, 2021

Sure, I can do that.

oehme added a commit to oehme/protobuf-gradle-plugin that referenced this issue Feb 28, 2021
@pan3793
Copy link

pan3793 commented May 5, 2021

@oehme Sorry for reply on a closed issue, but I still encounter this issue with the patched version 0.8.16, see #493.

Further, I'm also using antlr in my project, which has a build-in gradle plugin, and I found some difference between antlr and protobuf plugins.

-

For anltr, with the default configuration, such as

apply plugin: 'antlr'
dependencies {
    antlr "org.antlr:antlr4:$antlr_version"
}

put g4 files into src/main/antlr and the generated code will at build/generated-src/antlr/main, which will auto-recognized by IDEA as sources.

-

For protobuf, with the default configuration(I saw different demos in README, documents, example projects, etc. just pickup one of them), such as

apply plugin: "com.google.protobuf"
dependencies {
    implementation "io.grpc:grpc-netty-shaded:$grpc_version"
    // will import the protobuf dependencies transitively
    implementation "io.grpc:grpc-protobuf:$grpc_version"
    implementation "io.grpc:grpc-stub:$grpc_version"
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:$protoc_version" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:$grpc_version" }
    }
    generateProtoTasks {
        ofSourceSet('main')*.plugins { grpc {} }
    }
}

put proto files into src/main/proto and the generated code will at build/generated/source/proto/main/grpc&java, which will NOT recognized by IDEA as sources.

image

Thus, I found a workaround but only works on gradle 6.8, will failed on gradle 7.0 (see #493, mentioned at the begin)

sourceSets.main.scala {
    srcDirs += file("${project.buildDir}/generated/source/proto/main/grpc")
    srcDirs += file("${project.buildDir}/generated/source/proto/main/java")
}

-

I know little on gradle internal, but from an user perspective, as that antlr is an build-in gradle plugin which demonstrated how should a code-gen plugin works and which folder the generated code should use in default, and it's works smoothly with IDEA. I really want a consistent experiences in gradle code-gen plugins.

@oehme
Copy link
Contributor Author

oehme commented Jun 17, 2021

Sorry, I can't reproduce that. I've created a minimal protobuf project and the source directories are set up automatically on IDEA import. No additional configuration required.

Please attach a reproducible example.

@pan3793
Copy link

pan3793 commented Jun 17, 2021

@oehme thanks for the reply, it has been solved by #493 (comment)

@oehme
Copy link
Contributor Author

oehme commented Jun 17, 2021

Yes and I just realized that the problem is reproducible when I remove the idea plugin. That plugin should not be necessary, because IntelliJ already applies it upon import.

The problem is that the protobuf plugin eagerly checks for the idea plugin instead of using a callback.

oehme added a commit to oehme/protobuf-gradle-plugin that referenced this issue Jun 17, 2021
Instead of requiring users to apply the `idea` plugin, which is normally
not necessary when using IDEA's importer.

Fixes google#474
ejona86 pushed a commit that referenced this issue Jun 17, 2021
Instead of requiring users to apply the `idea` plugin, which is normally
not necessary when using IDEA's importer.

Fixes #474
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants