File not found for out-of-line `mod` via `include!`

My file structure looks like this (yes this is for advent of code):

days.rs
days/
  day01.rs
  day02.rs
  ...

I generate a days.in file in build.rs which looks something like this:

pub mod day01;
pub mod day02;
...

I then include! this file in days.rs:

include!(concat!(env!("OUT_DIR"), "/days.in"));

When I then cargo run or cargo build, I get this error repeated for each day:

error[E0583]: file not found for module `day01`                                                                                       
 --> D:Projectsadvent-of-code-2024targetdebugbuildadvent-of-code-2024-59c0dcca155b0f0eoutdays.in:1:1
  |
1 | pub mod day01;
  | ^^^^^^^^^^^^^^
  |
  = help: to create the module `day01`, create file "D:Projectsadvent-of-code-2024targetdebugbuildadvent-of-code-2024-59c0dcca155b0f0eoutday01.rs" or "D:Projectsadvent-of-code-2024targetdebugbuildadvent-of-code-2024-59c0dcca155b0f0eoutday01mod.rs"      
  = note: if there is a `mod day01` elsewhere in the crate already, import it with `use crate::...` instead

I notice that the error is located in days.in, while I would expect it to be in days.rs.

I suspect this is where I’m perhaps misunderstanding something. Does include! not just directly “copy-and-paste” the contents of the target file in place of the macro?


When I manually include the file, by just copying the contents of days.in and replacing the include! with it, it works perfectly fine!

1

Your assumption simply is wrong, include! does not “copy-and-paste” anything it:

Parses a file as an expression or an item according to the context.

Parsing a file involves declaring modules so they still remain relative to the included file, not the including one.

1

For future viewers with a similar question, this is what my build.rs ended up looking like. The important part is to add #[path = "<absolute path to the mod file>"] for each mod. (But really I’ll probably just manually attach the files, since that seems to give better IDE support.)

use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
use std::path::Path;
use std::{env, fs};

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=src/days");

    // Collect days `(mod name, mod path)`
    let mut days = vec![];
    for entry in fs::read_dir("src/days").unwrap() {
        let entry = entry.unwrap();
        let day_name = entry
            .path()
            .file_stem()
            .unwrap()
            .to_str()
            .unwrap()
            .to_string();
        let day_path = entry
            .path()
            .to_str()
            .unwrap()
            .to_string()
            .replace('\', "\\");

        // Skip day template
        if day_name == "dayN" {
            continue;
        }

        days.push((day_name, day_path));
    }

    let out_dir = env::var_os("OUT_DIR").unwrap();
    let out_days_path = Path::new(&out_dir).join("days.in");
    let out_days_file = File::create(out_days_path).unwrap();
    let mut out_days_writer = BufWriter::new(out_days_file);

    // Attach mods, with absolute path specified to the actual mod file
    for (day_name, day_path) in &days {
        writeln!(out_days_writer, "#[path = "{}"]", day_path).unwrap();
        writeln!(out_days_writer, "pub mod {};", day_name).unwrap();
    }

    // Implement wrapper functions
    writeln!(out_days_writer, "impl_days! {{").unwrap();
    for (day_name, _) in &days {
        writeln!(out_days_writer, "    {},", day_name).unwrap();
    }
    writeln!(out_days_writer, "}}").unwrap();

    out_days_writer.flush().unwrap();
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

File not found for out-of-line `mod` via `include!`

My file structure looks like this (yes this is for advent of code):

days.rs
days/
  day01.rs
  day02.rs
  ...

I generate a days.in file in build.rs which looks something like this:

pub mod day01;
pub mod day02;
...

I then include! this file in days.rs:

include!(concat!(env!("OUT_DIR"), "/days.in"));

When I then cargo run or cargo build, I get this error repeated for each day:

error[E0583]: file not found for module `day01`                                                                                       
 --> D:Projectsadvent-of-code-2024targetdebugbuildadvent-of-code-2024-59c0dcca155b0f0eoutdays.in:1:1
  |
1 | pub mod day01;
  | ^^^^^^^^^^^^^^
  |
  = help: to create the module `day01`, create file "D:Projectsadvent-of-code-2024targetdebugbuildadvent-of-code-2024-59c0dcca155b0f0eoutday01.rs" or "D:Projectsadvent-of-code-2024targetdebugbuildadvent-of-code-2024-59c0dcca155b0f0eoutday01mod.rs"      
  = note: if there is a `mod day01` elsewhere in the crate already, import it with `use crate::...` instead

I notice that the error is located in days.in, while I would expect it to be in days.rs.

I suspect this is where I’m perhaps misunderstanding something. Does include! not just directly “copy-and-paste” the contents of the target file in place of the macro?


When I manually include the file, by just copying the contents of days.in and replacing the include! with it, it works perfectly fine!

1

Your assumption simply is wrong, include! does not “copy-and-paste” anything it:

Parses a file as an expression or an item according to the context.

Parsing a file involves declaring modules so they still remain relative to the included file, not the including one.

1

For future viewers with a similar question, this is what my build.rs ended up looking like. The important part is to add #[path = "<absolute path to the mod file>"] for each mod. (But really I’ll probably just manually attach the files, since that seems to give better IDE support.)

use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
use std::path::Path;
use std::{env, fs};

fn main() {
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=src/days");

    // Collect days `(mod name, mod path)`
    let mut days = vec![];
    for entry in fs::read_dir("src/days").unwrap() {
        let entry = entry.unwrap();
        let day_name = entry
            .path()
            .file_stem()
            .unwrap()
            .to_str()
            .unwrap()
            .to_string();
        let day_path = entry
            .path()
            .to_str()
            .unwrap()
            .to_string()
            .replace('\', "\\");

        // Skip day template
        if day_name == "dayN" {
            continue;
        }

        days.push((day_name, day_path));
    }

    let out_dir = env::var_os("OUT_DIR").unwrap();
    let out_days_path = Path::new(&out_dir).join("days.in");
    let out_days_file = File::create(out_days_path).unwrap();
    let mut out_days_writer = BufWriter::new(out_days_file);

    // Attach mods, with absolute path specified to the actual mod file
    for (day_name, day_path) in &days {
        writeln!(out_days_writer, "#[path = "{}"]", day_path).unwrap();
        writeln!(out_days_writer, "pub mod {};", day_name).unwrap();
    }

    // Implement wrapper functions
    writeln!(out_days_writer, "impl_days! {{").unwrap();
    for (day_name, _) in &days {
        writeln!(out_days_writer, "    {},", day_name).unwrap();
    }
    writeln!(out_days_writer, "}}").unwrap();

    out_days_writer.flush().unwrap();
}

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật