Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

syntax error for generated code from protobuf; blocker for using protobuf in MOJO development #76

Closed
junzebao opened this issue Feb 2, 2021 · 5 comments

Comments

@junzebao
Copy link

junzebao commented Feb 2, 2021

Protobuf generated code contains comment like // @@protoc_insertion_point(interface_extends:Model). Obviously the parser can't handle the @@ in the comment. Since this library is used in maven-plugin-plugin (https://github.com/apache/maven-plugin-tools/blob/master/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java#L440), it means we can't use protobuf in maven plugin development.
Is it possible to update the grammar?

@rfscholte
Copy link
Collaborator

Can you provide a minimal java source file to reproduce this issue? QDox should always ignore everything after // until the EOL.

@junzebao
Copy link
Author

junzebao commented Feb 3, 2021

Hi @rfscholte, I'm attaching the Java file, Pom and the proto file below. The versions in the dependencies matter. I got NPE with older version of the maven-plugin-plugin, but with this version I got syntax error.
Reproduce Steps:
Try to set up a project with the following example and "mvn package". The protobuf will be used to generate some Java files, which are included in the source. When maven-plugin-plugin kicks in during packaging phase, it tries to discover MOJOs in source files. It'll complain about the syntax error in the generated Java file under target/generated-sources/protobuf.

java

package org.example;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

@Mojo(name="mymojo", defaultPhase = LifecyclePhase.COMPILE)
public class MyMojo extends AbstractMojo {
    public void execute() throws MojoExecutionException {
    }
}

proto

syntax = "proto3";

option java_package = "org.example";
option java_outer_classname = "AddressBookProtos";
option java_multiple_files = true;
option optimize_for = CODE_SIZE;

message Model {
  string name = 1;
}

pom

  <properties>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.source>11</maven.compiler.source>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>3.6.3</version>
    </dependency>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.14.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.6.0</version>
    </dependency>
  </dependencies>

  <build>
    <extensions>
      <extension>
        <groupId>kr.motd.maven</groupId>
        <artifactId>os-maven-plugin</artifactId>
        <version>1.6.0</version>
      </extension>
    </extensions>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.6.0</version>
      </plugin>
      <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.6.1</version>
        <executions>
          <execution>
            <id>generate-code</id>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.6.1</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <protocArtifact>com.google.protobuf:protoc:3.4.0:exe:${os.detected.classifier}</protocArtifact>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/main/proto</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

@rfscholte
Copy link
Collaborator

I'm not interested in these files, but only in the one under target/generated-sources/protobuf. It must be possible to generate a test like https://github.com/paul-hammant/qdox/blob/master/src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java#L1662

@junzebao
Copy link
Author

junzebao commented Feb 4, 2021

There you go. I was trying to use it in the test myself but I'm struggling to run the tests locally. It doesn't seem to build on Big Sur. (error=86, Bad CPU type in executable)

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: model.proto

public interface ModelOrBuilder {
        // @@protoc_insertion_point(interface_extends:Model)

    /**
     * <code>string name = 1;</code>
     */
    java.lang.String getName();
    /**
     * <code>string name = 1;</code>
     */
    String getNameBytes();
}

rfscholte added a commit that referenced this issue Feb 5, 2021
@rfscholte
Copy link
Collaborator

I wrote a test based on your input and I can't reproduce your issue, the test succeeds.
Either upgrade the plugin when possible, or add the latest QDox as dependency to that plugin.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants