Coder Social home page Coder Social logo

Comments (14)

rlabrecque avatar rlabrecque commented on September 23, 2024 2

Hey @kevingzhang, rustfmt can be disabled with a feature flag on tonic-build. You can turn it off like so:

[build-dependencies]
tonic-build = { git = "https://github.com/hyperium/tonic", default-features = false, features = ["transport"] }

Replae git = "", with version = "" if you're using a release from crates.io!

from tonic.

kevingzhang avatar kevingzhang commented on September 23, 2024

BTW, Tower-gRPC compile and works fine. since it doesn't require rustfmt.
so the worst case I can use Tower-gRPC instead going all the way back to Hyper. :)

from tonic.

kevingzhang avatar kevingzhang commented on September 23, 2024

thanks, it works !!

from tonic.

LucioFranco avatar LucioFranco commented on September 23, 2024

Yeah, the one issue with not rustfmting the code is that you get super super super nasty errors since it puts all the code into one line and the compiler cant figure out how to show it.

from tonic.

xmclark avatar xmclark commented on September 23, 2024

Alternatively, can tonic import rustfmt as a library and do the text processing at string-build-time instead of calling the external cli tool? Maybe that has tradeoffs, but at least then consumers of this library who do not want or cannot install rustfmt can get nice error messages.

from tonic.

LucioFranco avatar LucioFranco commented on September 23, 2024

@xmclark last I checked that was not possible, or I couldn't figure it out. If someone could figure that out that would be amazing!

from tonic.

davidgraeff avatar davidgraeff commented on September 23, 2024

I have done that in another crate of mine.

https://github.com/davidgraeff/braintree-payment-graphql-rs/blob/master/braintree-queries-generator/src/generate.rs#L10

#[cfg(feature = "rustfmt")]
fn format(input_rust_code: String) -> Option<String> {
    use rustfmt_nightly::{Config, Input, Session};

    let mut config = Config::default();

    config.set().emit_mode(rustfmt_nightly::EmitMode::Stdout);
    config.set().verbose(rustfmt_nightly::Verbosity::Quiet);

    let mut out = Vec::with_capacity(input_rust_code.len() * 2);

    let r = Session::new(config, Some(&mut out)).format(Input::Text(input_rust_code));
    match r {
        Err(e) => {
            format_err!("rustfmt error: {:?}", e);
            None
        }
        _ => match out.len() {
            0 => None,
            // Unwrap should be safe here, rustfmt generates utf8 only
            _ => Some(String::from_utf8(out).unwrap()),
        },
    }
}

from tonic.

LucioFranco avatar LucioFranco commented on September 23, 2024

@davidgraeff can this code run on stable?

from tonic.

davidgraeff avatar davidgraeff commented on September 23, 2024

To be honest, I don't know. Because of the current async situation in the rust ecosystem I'm on rust nightly. I haven't found a toolchain file in their repo, so I assume it works on stable, but also haven't checked their dependencies.

from tonic.

LucioFranco avatar LucioFranco commented on September 23, 2024

Yeah, unfortunately, this depends on unstable nightly features :(

error[E0554]: `#![feature]` may not be used on the stable release channel
   --> /Users/lucio/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-ap-graphviz-606.0.0/lib.rs:277:1
    |
277 | #![feature(rustc_private, nll)]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the stable release channel
  --> /Users/lucio/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.10/lib.rs:33:32
   |
33 | #![cfg_attr(feature = "union", feature(untagged_unions))]
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the stable release channel
  --> /Users/lucio/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.10/lib.rs:35:37
   |
35 | #![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
   |                                     ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

from tonic.

sprhawk avatar sprhawk commented on September 23, 2024

I encounter a same situation.
I'm using yocto with meta-rust-bin to build rust application. a rustfmt doesn't exist in meta-rust-bin.
Although I can disable rustfmt feature for yocto build, but it will also disabled in normal development environment.

I think there is no way to set to use "rustfmt" in development environment, and no "rustfmt" when build for yocto ( build script is built for HOST same like the development environment )

from tonic.

LucioFranco avatar LucioFranco commented on September 23, 2024

I am not familiar with yocto but is it possible to pass a feature flag to the create that contains tonic build? that way you can do something like:

[features]
fmt = ["tonic-build/rustfmt"]

then you can build it with

cargo build --features fmt

from tonic.

sprhawk avatar sprhawk commented on September 23, 2024

I am not familiar with yocto but is it possible to pass a feature flag to the create that contains tonic build? that way you can do something like:

[features]
fmt = ["tonic-build/rustfmt"]

then you can build it with

cargo build --features fmt

I mean meta-rust-bin doesn't include a rustfmt, so I had to disable fmt feature, not enable it.

from tonic.

hechaoli avatar hechaoli commented on September 23, 2024

I am not familiar with yocto but is it possible to pass a feature flag to the create that contains tonic build? that way you can do something like:

[features]
fmt = ["tonic-build/rustfmt"]

then you can build it with

cargo build --features fmt

I mean meta-rust-bin doesn't include a rustfmt, so I had to disable fmt feature, not enable it.

I encountered the same issue with yocto. However, I'm using meta-rust which should have rustfmt. Yet somehow it complains about rustfmt missing. I couldn't figure out a way to configure yocto to work. Eventually, I found that it works if you just disable format using configure. That is, change build.rs to the following instead of using tonic_build::compile_protos("proto/helloworld.proto")?; like in the example.

fn main() -> Result<(), Box<dyn std::error::Error>> {
   tonic_build::configure()
        .format(false)
        .compile(
            &["proto/helloworld.proto"],
            &["proto/"],
        )?;
   Ok(())
}

The side-effect is of course all generated code in one line.

from tonic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.