How reference a my app's config directory when running from release

Observation

When I run the app using cargo run --release from the project root, the app finds the config directory ./config. However, when I run the app calling the ./target/release/<app name>, the reference is lost/broken.

The source ./src/config.rs references the config directory as following:

const CONFIG_FILE_PATH: &str = "config/default.toml";

When in a Docker context

In general, what is a good way to copy and point to the config files for a dockerized app?

This should not happen in any normal Unix shell. How are you opening the file?

Docker or no docker, config files are best placed at fixed, pre-determined locations. If you don't want to do that, you can simply pass the path as an arg.

That makes sense.

Perfect. Based on that, the culprit is how I'm using the config.

use config::{Config as RConfig, Environment, File};
...
const CONFIG_FILE_PATH: &str = "config/default.toml";
const CONFIG_FILE_ROOT: &str = "config";

impl Config {
    pub fn new() -> Result<Self> {
        let mut s = RConfig::default();

        // default
        s.merge(File::with_name(CONFIG_FILE_PATH))
            .context("Unable to load the default config")?;

        // use env Development, Production or Testing to set override
        let env = env::var("RUST_ENV").unwrap_or_else(|_| "Development".into());

        s.set("env", env.clone())?;
        s.merge(File::with_name(&format!("{}/{}", CONFIG_FILE_ROOT, env)).required(false))
            .context(format!("Unable to load {}/{}.toml", CONFIG_FILE_ROOT, env))?;

        s.merge(Environment::new().separator("_"))?;

        s.try_into().context("Unable to instantiate Config struct")
    }
}

lazy_static! {
    pub static ref CONFIG: Config = Config::new().unwrap();
}

My guess would be File::with_name does something with the path - it doesn't directly open it. Otherwise it should open.

In the event I copy the app into /usr/local/bin that is included in my PATH, given my current setup, would that mean I need to include the config/ in the /usr/local/bin?

I found the following post that goes into Different output of env::current_dir() - #7 by kornel

The key is to launch the app from a directory where the ./config exits. The working directory is that from where I launch the app... 101 stuff.

@RedDocMD thank you for your help.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.