Repository: tokio-rs/prost (language: rust)

Issue:
Bug:  include_file renders an invalid include path when using `out_dir()`
I believe [this block](https://github.com/tokio-rs/prost/blob/d7324c4bd43b491b728545ed90a325675a3966e4/prost-build/src/lib.rs#L931) has some faulty logic.

```rust
if let Some(buf) = basepath {
    self.write_line(
        outfile,
        depth + 1,
        &format!("include!(\"{}/{}.rs\");", buf.display(), modname),
    )?;
} else {
    self.write_line(
        outfile,
        depth + 1,
        &format!("include!(concat!(env!(\"OUT_DIR\"), \"/{}.rs\"));", modname),
    )?;
}
```

Specifically, the `basepath` should not be prepended to the `modname` when formating the `include!` directive.  When it is, the formatted output includes the path set with `out_dir()`, but the file being rendered is already at that path, and so trying to build with this single file output panics with "invalid path". 

For example, if my config has this:

```rust
.out_dir("src/foo")
.include_file("mod.rs")
```

Then the generated `include!` directive in `mod.rs`, will include that path like this:

```rust
include!("src/foo/someproto.rs");
```

But the generated `mod.rs` file is already in `src/foo` and so fails to compile.

My proposed solution, which works for my use case, is to change the `basepath` handling block to the following:

```rust
if let Some(_) = basepath {
    self.write_line(
        outfile,
        depth + 1,
        &format!("include!(\"{}.rs\");", modname),
    )?;
} else {
```

If there are no objections, I'll submit a PR once I figure out how to correctly add tests.


Discussion / hints:

Fix patch:
diff --git a/prost-build/src/lib.rs b/prost-build/src/lib.rs
index 60888d06d..073e6e0e1 100644
--- a/prost-build/src/lib.rs
+++ b/prost-build/src/lib.rs
@@ -924,11 +924,11 @@ impl Config {
             written += subwritten;
             if subwritten != matching.len() {
                 let modname = matching[0][..=depth].join(".");
-                if let Some(buf) = basepath {
+                if let Some(_) = basepath {
                     self.write_line(
                         outfile,
                         depth + 1,
-                        &format!("include!(\"{}/{}.rs\");", buf.display(), modname),
+                        &format!("include!(\"{}.rs\");", modname),
                     )?;
                 } else {
                     self.write_line(


Test patch:
diff --git a/tests/single-include/build.rs b/tests/single-include/build.rs
index 5ab6a9316..5432d8b5c 100644
--- a/tests/single-include/build.rs
+++ b/tests/single-include/build.rs
@@ -5,4 +5,10 @@ fn main() {
         .include_file("lib.rs")
         .compile_protos(&["protos/search.proto"], &["protos"])
         .unwrap();
+
+    Config::new()
+        .out_dir("src/outdir")
+        .include_file("mod.rs")
+        .compile_protos(&["protos/outdir.proto"], &["protos"])
+        .unwrap();
 }
diff --git a/tests/single-include/protos/outdir.proto b/tests/single-include/protos/outdir.proto
new file mode 100644
index 000000000..e6da6690b
--- /dev/null
+++ b/tests/single-include/protos/outdir.proto
@@ -0,0 +1,9 @@
+syntax = "proto3";
+
+package outdir;
+
+message OutdirRequest {
+  string query = 1;
+  int32 page_number = 2;
+  int32 result_per_page = 3;
+}
diff --git a/tests/single-include/src/lib.rs b/tests/single-include/src/lib.rs
index f7072f67a..faa33e104 100644
--- a/tests/single-include/src/lib.rs
+++ b/tests/single-include/src/lib.rs
@@ -1,1 +1,2 @@
 include!(concat!(env!("OUT_DIR"), "/lib.rs"));
+include!("./outdir/mod.rs");
diff --git a/tests/single-include/src/main.rs b/tests/single-include/src/main.rs
index c36342382..1753ab8a0 100644
--- a/tests/single-include/src/main.rs
+++ b/tests/single-include/src/main.rs
@@ -1,3 +1,5 @@
 fn main() {
     let _search_req = single_include::search::SearchRequest::default();
+
+    let _out_dir_test = single_include::outdir::OutdirRequest::default();
 }
diff --git a/tests/single-include/src/outdir/mod.rs b/tests/single-include/src/outdir/mod.rs
new file mode 100644
index 000000000..8cf70ccb6
--- /dev/null
+++ b/tests/single-include/src/outdir/mod.rs
@@ -0,0 +1,3 @@
+pub mod outdir {
+    include!("outdir.rs");
+}
diff --git a/tests/single-include/src/outdir/outdir.rs b/tests/single-include/src/outdir/outdir.rs
new file mode 100644
index 000000000..4cdb150a8
--- /dev/null
+++ b/tests/single-include/src/outdir/outdir.rs
@@ -0,0 +1,9 @@
+#[derive(Clone, PartialEq, ::prost::Message)]
+pub struct OutdirRequest {
+    #[prost(string, tag="1")]
+    pub query: ::prost::alloc::string::String,
+    #[prost(int32, tag="2")]
+    pub page_number: i32,
+    #[prost(int32, tag="3")]
+    pub result_per_page: i32,
+}