Skip to content

Add support for resource files #5305

@Pzixel

Description

@Pzixel

This is similar to #2729, however, it's a bit different.

The main problem I currently have is that I cannot place configs near my executable. It's common practice in many languages and IDE's that you have some file that gets copied to output directory. For example, let's look at csproj configuration for C# project:

  <ItemGroup>
    <None Update="appsettings.json">
      <Generator>SettingsSingleFileGenerator</Generator>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Production.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

It literaly says "copy this file to output directory (wherever it is) if file in project is newer". It's common, it's convinient.

Currently in Rust i have following build.rs script:

use std::path::Path;
use std::{env, fs};


const SETTINGS_FILE: &str = "Settings.toml";
const LOG4RS_FILE: &str = "log4rs.toml";
fn main() {
    let target_dir_path = env::var("OUT_DIR").unwrap();
    copy(&target_dir_path, LOG4RS_FILE);
    copy(&target_dir_path, SETTINGS_FILE);
}

fn copy<S: AsRef<std::ffi::OsStr> + ?Sized, P: Copy + AsRef<Path>>(target_dir_path: &S, file_name: P) {
    fs::copy(file_name, Path::new(&target_dir_path).join("../../..").join(file_name)).unwrap();
}

It actually does work, but it's not really convinient and we'd like ot have more declarative description.

We'd probably like to specify in cargo.toml as:

[resources]
preserve_newest = ["log4rs.toml", "Settings.toml"]

or even

[resources]
preserve_newest = {file = "log4rs.toml", target_path = "configs/log4rs/log4rs.toml"}

And be sure that when cargo build an application, resource files gets its place near executable (or at some relative path).

This feature should be limited to executables, so it doesn't work for libraries so we don't care about requiring copying these files transitively.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-executionArea: anything dealing with executing the compilerC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-needs-team-inputStatus: Needs input from team on whether/how to proceed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions