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

Cannot include generated code #117

Closed
xitep opened this issue Jun 15, 2015 · 14 comments
Closed

Cannot include generated code #117

xitep opened this issue Jun 15, 2015 · 14 comments

Comments

@xitep
Copy link
Contributor

xitep commented Jun 15, 2015

Hi,

following cargo's documentation I've tried to generate my protobufs at build time (through a build.rs script) and include them in my main.rs using:

...
mod model {
    include!(concat!(env!("OUT_DIR"), "/model.rs"));
}
...

However, the (rustc) compilation then fails with:

.../target/debug/build/pubmonitor-42b07ea6e237e2f6/out/model.rs:3:3: 3:4 error: an inner attribute is not permitted in this context
.../target/debug/build/pubmonitor-42b07ea6e237e2f6/out/model.rs:3 #![allow(dead_code)]
                                                                    ^
.../target/debug/build/pubmonitor-42b07ea6e237e2f6/out/model.rs:3:3: 3:4 help: place inner attribute at the top of the module or block
...

It seems that module level attributes (e.g. #![allow(dead_code)] here) are not allowed in included content. Would there be a way to avoid these in the generated code by turning them into item-level attributes where needed?

Many thanks,
P.

@stepancheg
Copy link
Owner

Why can't you just copy model.rs into source directory, and then include generated file as mod model; as regular source file?

@xitep
Copy link
Contributor Author

xitep commented Jun 16, 2015

Well, that's what I'm doing right now. But it feels rather like a workaround to me.

@WillDignazio
Copy link

I've gotten around this by using a build.rs script that generates the source before the build process, then cargo will pick it up if you include it in a mod.rs. It's a little clunky, but not far off from what other large projects do.

@stepancheg
Copy link
Owner

I think rust-protobuf does it properly. But if you need to include generated code in another file, I might suggest stripping module attributes with sed scripts.

@xitep
Copy link
Contributor Author

xitep commented Dec 21, 2015

ok with me, i think that's feasible.

@mpartel
Copy link

mpartel commented Apr 17, 2016

FWIW another dirty workaround is to modify the generated file in your build.rs to add a mod foo { ... } around it. Then it can be included from a file under src and doesn't need to be copied there. I think this is a bit better than removing the attributes, which results in build warnings.

@SirVer
Copy link

SirVer commented Jan 15, 2018

Here is an example of @mpartel idea to wrap the generated proto in mod proto { ... } inside the build.rs script and how it is then included in lib.rs. We do all of this to avoid checking in the proto.rs file into source control, instead regenerating it whenever it changes.

@stepancheg It would be really nice if this could be supported more natively. Prost does this really well with prost-build.

@aep
Copy link

aep commented Apr 22, 2018

@SirVer Prost looks unmaintained. Should we somehow standardize that workaround in a crate?

@stepancheg
Copy link
Owner

@SirVer @aep I think this thing can be implemented as part of rust-protobuf codegen.

If you have specific proposal, e. g.

  • option (wrap_in_module)
  • option to push macros to declarations from the top level

please share.

@aep
Copy link

aep commented Apr 22, 2018

i think it should just be a simple option to protoc_rust::run. And some docs on that would be helpful, since the default is rather surprising.

@stepancheg
Copy link
Owner

@aep option to do what exactly?

Not sure why the default is surprising. Could you explain please?

@aep
Copy link

aep commented Apr 22, 2018

the wrap_in_module option sounds good to me :)

well it's quite standard to put generated files into OUT_DIR, and the solution for those errors is non-obvious.
so it might help to include that in README.md

@SirVer
Copy link

SirVer commented Apr 23, 2018

@aep Rust Tower seems to be betting on Prost, so I guess development will pick up eventually again. https://medium.com/@carllerche/announcing-tower-a-library-for-writing-robust-network-services-with-rust-67273f052c40

@clintfred
Copy link

I spent a good chunk of the day figuring this out since docs.rs no longer allows files to be written outside of $OUT_DIR. I eventually went with @SirVer's workaround. https://github.com/googlecartographer/point_cloud_viewer/blob/440d875f12e32dff6107233f24b5a02cf28776dc/point_viewer_proto_rust/build.rs#L33

@stepancheg I would ask that you reconsider you stance of writing files to the src/ directory as the official solution as writing to src/ will block any crates using rust-protobuf from having documentation on docs.rs.
Or maybe there is a more official solution now and I failed to find it? If that's the case, please point me in the right direction.

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

No branches or pull requests

7 participants