Skip to content
Open
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
17 changes: 12 additions & 5 deletions crates/tower-package/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,15 @@ impl Package {
for (physical_path, logical_path) in file_paths {
// All of the app code goes into the "app" directory.
let logical_path = app_dir.join(logical_path);
// Normalize to forward slashes so archive entry names are POSIX-compatible
// on all platforms (Windows PathBuf uses backslashes).
let archive_name = normalize_path(&logical_path)?;

let hash = compute_sha256_file(&physical_path).await?;
path_hashes.insert(logical_path.clone(), hash);
path_hashes.insert(PathBuf::from(&archive_name), hash);

builder
.append_path_with_name(physical_path, logical_path)
.append_path_with_name(physical_path, &archive_name)
.await?;
}

Expand All @@ -301,7 +304,8 @@ impl Package {
// The file_name should constitute the logical path
let import_path = import_path.file_name().unwrap();
let import_path = module_dir.join(import_path);
let import_path_str = import_path.into_os_string().into_string().unwrap();
// Normalize to forward slashes for the manifest (POSIX, cross-platform).
let import_path_str = normalize_path(&import_path)?;
import_paths.push(import_path_str);

// Now we write all of these paths to the modules directory.
Expand All @@ -310,13 +314,16 @@ impl Package {
Ok(p) => module_dir.join(p),
Err(_) => continue,
};
// Normalize to forward slashes so archive entry names are POSIX-compatible
// on all platforms (Windows PathBuf uses backslashes).
let archive_name = normalize_path(&logical_path)?;

let hash = compute_sha256_file(&physical_path).await?;
path_hashes.insert(logical_path.clone(), hash);
path_hashes.insert(PathBuf::from(&archive_name), hash);

debug!("adding file {}", logical_path.display());
builder
.append_path_with_name(physical_path, logical_path)
.append_path_with_name(physical_path, &archive_name)
.await?;
}
}
Expand Down
30 changes: 8 additions & 22 deletions crates/tower-package/tests/package_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,7 @@ use tokio_tar::Archive;
use tower_package::{Manifest, Package, PackageSpec, Parameter};
use tower_telemetry::debug;

macro_rules! make_path {
($($component:expr),+ $(,)?) => {
{
let mut path = PathBuf::new();
$(
path.push($component);
)+
&path.to_string_lossy().to_string()
}
};
}


#[tokio::test]
async fn it_creates_package() {
Expand Down Expand Up @@ -289,12 +279,9 @@ async fn it_packages_import_paths() {
.await
.expect("Manifest was not valid JSON");

// NOTE: These paths are joined by the OS so we need to be more specific about the expected
// path.
// Archive paths are always normalized to forward slashes regardless of OS.
assert!(
manifest
.import_paths
.contains(make_path!("modules", "shared")),
manifest.import_paths.contains(&"modules/shared".to_string()),
"Import paths {:?} did not contain expected path",
manifest.import_paths
);
Expand Down Expand Up @@ -333,18 +320,19 @@ async fn it_packages_import_paths_nested_within_base_dir() {
let files = read_package_files(package).await;

// Module files should be under modules/shared/..., NOT modules/libs/shared/...
// Archive paths are always normalized to forward slashes regardless of OS.
assert!(
files.contains_key(make_path!("modules", "shared", "__init__.py")),
files.contains_key("modules/shared/__init__.py"),
"files {:?} was missing modules/shared/__init__.py",
files
);
assert!(
files.contains_key(make_path!("modules", "shared", "util.py")),
files.contains_key("modules/shared/util.py"),
"files {:?} was missing modules/shared/util.py",
files
);
assert!(
!files.contains_key(make_path!("modules", "libs", "shared", "__init__.py")),
!files.contains_key("modules/libs/shared/__init__.py"),
"files {:?} should NOT contain modules/libs/shared/__init__.py",
files
);
Expand All @@ -355,9 +343,7 @@ async fn it_packages_import_paths_nested_within_base_dir() {
.expect("Manifest was not valid JSON");

assert!(
manifest
.import_paths
.contains(make_path!("modules", "shared")),
manifest.import_paths.contains(&"modules/shared".to_string()),
"Import paths {:?} did not contain expected path modules/shared",
manifest.import_paths
);
Expand Down