Skip to content

Commit

Permalink
Fixing dependency leaks finally
Browse files Browse the repository at this point in the history
  • Loading branch information
carter committed May 21, 2024
1 parent 8a614ee commit 1b74623
Show file tree
Hide file tree
Showing 8 changed files with 1,426 additions and 1,086 deletions.
17 changes: 6 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 3 additions & 10 deletions example_package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@ version = "0.1.0"
edition = "2021"

[dependencies]
# This is what we need for using this package in our mono-repo
roslibrust = { path = "../roslibrust" }
# Normally you would have: roslibrust = "0.7"
# TODO in the current state of this example, we don't actually need roslibrust
# These dependencies are needed by the code that roslibrust_codegen autogenerates
# They are silently "leaked dependencies of the crate"
# See https://github.com/Carter12s/roslibrust/issues/72 for resolution path
serde = "1.0"
smart-default = "0.7"
# The code generated by roslibrust_codegen has dependendencies
# We need to depend on the crate at build time so that the generate code has access to these dependencies
roslibrust_codegen = { path = "../roslibrust_codegen" }

[build-dependencies]
# We depend on codegen as a build dependency as we (should) only need it to generate our types
# We depend on codegen as a build dependency as we invoke it in build.rs
roslibrust_codegen = { path = "../roslibrust_codegen" }
# This crate is very helpful for build.rs files but not required
cargo-emit = "0.2"
5 changes: 5 additions & 0 deletions roslibrust_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ syn = "1.0"
tokio = { version = "1.0", features = ["time", "signal"], optional = true}
walkdir = "2.3"
xml-rs = "0.8"
# Not a direct dependencies of the crate, but something generated code uses
# We include them as dependencies here and expose them with a `pub use`
# So that the generate code can find them, and users don't have to added dependencies themselves
smart-default = "0.7"
serde-big-array = "0.5"

[dev-dependencies]
env_logger = "0.10"
Expand Down
13 changes: 5 additions & 8 deletions roslibrust_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ use crate::{bail, Error};
use crate::{ConstantInfo, FieldInfo, MessageFile, RosLiteral, ServiceFile};

fn derive_attrs() -> Vec<syn::Attribute> {
// TODO we should look into using $crate here...
// The way we're currently doing it leaks a dependency on these crates to users...
// However using $crate breaks the generated code in non-macro usage
// Pass a flag in "if_macro"?
vec![
parse_quote! { #[derive(::serde::Deserialize)] },
parse_quote! { #[derive(::serde::Serialize)] },
parse_quote! { #[derive(::smart_default::SmartDefault)] },
parse_quote! { #[derive(::roslibrust_codegen::Deserialize)] },
parse_quote! { #[derive(::roslibrust_codegen::Serialize)] },
parse_quote! { #[derive(::roslibrust_codegen::SmartDefault)] },
parse_quote! { #[derive(Debug)] },
parse_quote! { #[derive(Clone)] },
parse_quote! { #[derive(PartialEq)] },
parse_quote! { #[serde(crate = "::roslibrust_codegen::serde")] },
]
}

Expand Down Expand Up @@ -179,7 +176,7 @@ fn generate_field_definition(
const MAX_FIXED_ARRAY_LEN: usize = 32;
let serde_line = match field.field_type.array_info {
Some(Some(fixed_array_len)) if fixed_array_len > MAX_FIXED_ARRAY_LEN => {
quote! { #[serde(with = "::serde_big_array::BigArray")] }
quote! { #[serde(with = "::roslibrust_codegen::BigArray")] }
}
_ => quote! {},
};
Expand Down
11 changes: 9 additions & 2 deletions roslibrust_codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use log::*;
use proc_macro2::TokenStream;
use quote::quote;
use serde::de::DeserializeOwned;
use serde::Serialize;
use simple_error::{bail, SimpleError as Error};
use std::collections::{BTreeMap, VecDeque};
use std::fmt::{Debug, Display};
Expand All @@ -19,6 +17,15 @@ use utils::RosVersion;
pub mod integral_types;
pub use integral_types::*;

// These pub use statements are here to be able to export the dependencies of the generated code
// so that crates using this crate don't need to add these dependencies themselves.
// Our generated code should find these exports.
// Modeled from: https://users.rust-lang.org/t/proc-macros-using-third-party-crate/42465/4
pub use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub use ::serde;
pub use smart_default::SmartDefault;
pub use serde_big_array::BigArray;

/// Fundamental traits for message types this crate works with
/// This trait will be satisfied for any types generated with this crate's message_gen functionality
pub trait RosMessageType:
Expand Down
5 changes: 0 additions & 5 deletions roslibrust_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ env_logger = "0.10"
roslibrust = { path = "../roslibrust" }
roslibrust_codegen = { path = "../roslibrust_codegen" }
lazy_static = "1.4"
# None of these three dependencies should have to be in here, they are leaking from generated code...
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
smart-default = "0.6"
serde-big-array = "0.5"

[dev-dependencies]
diffy = "0.3.0"
Loading

0 comments on commit 1b74623

Please sign in to comment.