lcm-proj / lcm

Lightweight Communications and Marshalling
GNU Lesser General Public License v2.1
981 stars 388 forks source link

Don't import the same package multiple times in generated Go code. #246

Closed ethoress closed 6 years ago

ethoress commented 6 years ago

For this definition:

package GoGen;

struct Bug
{
  PkgA.A  a;
  PkgA.B  b;
}

The Go code generator generates two import statements for PkgA:

// THIS IS AN AUTOMATICALLY GENERATED FILE.  DO NOT MODIFY
// BY HAND!!
//
// Generated by lcm-gen

package GoGen

import (
    "encoding/binary"
    "fmt"
    "math"
    "math/bits"
    "PkgA"
    "PkgA"
)
...

This results in Go compile error:

src/GoGen/Bug.go:14:2: PkgA redeclared as imported package name
    previous declaration at src/GoGen/Bug.go:13:2

This Dockerfile can be used to reproduce the issue:

FROM golang:1.10

RUN apt-get update && apt-get install -y \
    build-essential \
    libglib2.0-dev \
    cmake \
    python-dev \
    python-epydoc \
    openjdk-8-jdk \
    lua5.2-dev \
    doxygen

RUN git clone --branch v1.4.0 --depth 1 https://github.com/lcm-proj/lcm.git && \
    mkdir lcm/build && \
    cd lcm/build && \
    cmake .. && \
    make all && \
    make install && \
    cd ../.. && \
    rm -r ./lcm

RUN echo "package GoGen; struct Bug { PkgA.A  a; PkgA.B  b; }" > /Bug.lcm && \
    echo "package PkgA; struct A { double number_a; } struct B { double number_b; }" > /PkgA.lcm && \
    lcm-gen --go --go-path /go/src/ /Bug.lcm && lcm-gen --go --go-path /go/src/ /PkgA.lcm

RUN go build GoGen
gustafj commented 6 years ago

LGTM thanks