Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only create layer env directories if env vars are being written #385

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libcnb/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Allow compilation of libcnb.rs buildpacks on Windows. Please note that this does not imply Windows container support, it's meant to allow running unit tests without cross-compiling. ([#368](https://github.com/Malax/libcnb.rs/pull/368))
- Expose `runtime::libcnb_runtime_detect`, `runtime::libcnb_runtime_build` and their related types for advanced use-cases. Buildpack authors should not use these. ([#375](https://github.com/Malax/libcnb.rs/pull/375)).
- Only create layer `env`, `env.build` and `env.launch` directories when environment variables are being set within them ([#385](https://github.com/Malax/libcnb.rs/pull/385)).

## [0.6.0] 2022-02-28

Expand Down
41 changes: 22 additions & 19 deletions libcnb/src/layer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,30 +545,33 @@ impl LayerEnvDelta {
fs::remove_dir_all(path.as_ref())?;
}

fs::create_dir_all(path.as_ref())?;
// Avoid creating empty env directories.
if !self.entries.is_empty() {
fs::create_dir_all(path.as_ref())?;

for ((modification_behavior, name), value) in &self.entries {
let file_extension = match modification_behavior {
ModificationBehavior::Append => ".append",
ModificationBehavior::Default => ".default",
ModificationBehavior::Delimiter => ".delim",
ModificationBehavior::Override => ".override",
ModificationBehavior::Prepend => ".prepend",
};

for ((modification_behavior, name), value) in &self.entries {
let file_extension = match modification_behavior {
ModificationBehavior::Append => ".append",
ModificationBehavior::Default => ".default",
ModificationBehavior::Delimiter => ".delim",
ModificationBehavior::Override => ".override",
ModificationBehavior::Prepend => ".prepend",
};
let mut file_name = name.clone();
file_name.push(file_extension);

let mut file_name = name.clone();
file_name.push(file_extension);
let file_path = path.as_ref().join(file_name);

let file_path = path.as_ref().join(file_name);
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
fs::write(file_path, &value.as_bytes())?;
}

#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
fs::write(file_path, &value.as_bytes())?;
#[cfg(not(target_family = "unix"))]
fs::write(file_path, &value.to_string_lossy().as_bytes())?;
}

#[cfg(not(target_family = "unix"))]
fs::write(file_path, &value.to_string_lossy().as_bytes())?;
}

Ok(())
Expand Down