Skip to content

Commit

Permalink
fix path and crlf errors on windows (railwayapp#436)
Browse files Browse the repository at this point in the history
* fix path and crlf errors on windows

improve docker test performance

* fix some pesky CRLFs

* fix clippy lints

* fix some merge-related issues

* remove out of scope changes

* run ci tests on windows too

* windows ci runners cant run linux shell stuff

* turns out ci needs an os to run :/

* fix ci.yml

* matrix != tests

* specify dockerfile syntax

* remove windows ci and fix windows docker (again)

* put the missing test back
  • Loading branch information
nebulatgs authored Aug 25, 2022
1 parent cf3cd99 commit 6a9dff1
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ box_drawing = "0.1.2"
textwrap = { default-features = false, version = "0.15.0" }
cargo_toml = "0.11.5"
rand = "0.8.5"
path-slash = "0.2.1"

[dev-dependencies]
dotenv-parser = "0.1.3"
Expand Down
8 changes: 6 additions & 2 deletions src/nixpacks/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::anyhow;
use path_slash::PathBufExt;
use std::collections::BTreeMap;
use std::path::Path;
use std::{env, fs, path::PathBuf};
Expand Down Expand Up @@ -103,7 +103,11 @@ impl App {
/// # Errors
/// This will error if the path doesn't exist, or if the contents isn't UTF-8
pub fn read_file(&self, name: &str) -> Result<String> {
fs::read_to_string(self.source.join(name)).map_err(|e| anyhow!(e))
let data = fs::read_to_string(PathBuf::from_slash_lossy(
self.source.join(name).as_os_str(),
))?;

Ok(data.replace("\r\n", "\n"))
}

pub fn find_match(&self, re: &Regex, pattern: &str) -> Result<bool> {
Expand Down
16 changes: 10 additions & 6 deletions src/nixpacks/builder/docker/dockerfile_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::nixpacks::{
};
use anyhow::{Context, Ok, Result};
use indoc::formatdoc;
use path_slash::PathBufExt;
use std::{
fs::{self, File},
io::Write,
Expand Down Expand Up @@ -119,11 +120,11 @@ impl DockerfileGenerator for BuildPlan {
let assets_copy_cmd = if static_assets.is_empty() {
"".to_string()
} else {
format!(
"COPY {} {}",
output.get_relative_path("assets").display(),
app::ASSETS_DIR
)
let rel_assets_path = output.get_relative_path("assets");
let rel_assets_slash_path = rel_assets_path
.to_slash()
.context("Failed to convert nix file path to slash path.")?;
format!("COPY {} {}", rel_assets_slash_path, app::ASSETS_DIR)
};

let dockerfile_phases = plan
Expand Down Expand Up @@ -296,7 +297,10 @@ impl DockerfileGenerator for Phase {
// Install nix packages and libraries
let install_nix_pkgs_str = if self.uses_nix() {
let nix_file = output.get_relative_path(format!("{}.nix", phase.name));
let nix_file_path = nix_file.to_str().unwrap();

let nix_file_path = nix_file
.to_slash()
.context("Failed to convert nix file path to slash path.")?;
format!(
"COPY {nix_file_path} {nix_file_path}\nRUN nix-env -if {nix_file_path}",
nix_file_path = nix_file_path
Expand Down
8 changes: 6 additions & 2 deletions src/nixpacks/files.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Ok, Result};
use anyhow::Result;
use std::{fs, io, path::Path};
use walkdir::WalkDir;

Expand All @@ -21,7 +21,11 @@ pub fn recursive_copy_dir<T: AsRef<Path>, Q: AsRef<Path>>(source: T, dest: Q) ->
}
// copy files
else if entry.file_type().is_file() {
fs::copy(from, to)?;
fs::copy(&from, &to)?;
// replace CRLF with LF
if let Ok(data) = fs::read_to_string(from) {
fs::write(&to, data.replace("\r\n", "\n"))?;
}
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/providers/clojure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Provider for ClojureProvider {
let move_file_cmd = "if [ -f /app/target/uberjar/*standalone.jar ]; then mv /app/target/uberjar/*standalone.jar /app/target/*standalone.jar; fi";
let build = Phase::build(Some(format!("{}; {}", build_cmd, move_file_cmd)));

let start = StartPhase::new("java $JAVA_OPTS -jar /app/target/*standalone.jar");
let start = StartPhase::new("bash -c \"java $JAVA_OPTS -jar /app/target/*standalone.jar\"");

let plan = BuildPlan::new(vec![setup, build], Some(start));
Ok(Some(plan))
Expand Down
9 changes: 5 additions & 4 deletions src/providers/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::nixpacks::{
},
};
use anyhow::{Context, Result};
use path_slash::PathBufExt;
use regex::Regex;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -67,8 +68,8 @@ impl DenoProvider {
Ok(Some(format!(
"deno cache {}",
start_file
.to_str()
.context("Failed to convert start_file to string")?
.to_slash()
.context("Failed to convert start_file to slash_path")?
)))
} else {
Ok(None)
Expand All @@ -92,8 +93,8 @@ impl DenoProvider {
Some(start_file) => Ok(Some(format!(
"deno run --allow-all {}",
start_file
.to_str()
.context("Failed to convert start_file to string")?
.to_slash()
.context("Failed to convert start_file to slash_path")?
))),
None => Ok(None),
}
Expand Down
5 changes: 3 additions & 2 deletions src/providers/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
};
use anyhow::bail;
use anyhow::Result;
use path_slash::PathExt;
use regex::Regex;
use serde::{Deserialize, Serialize};
mod nx;
Expand Down Expand Up @@ -364,7 +365,7 @@ impl NodeProvider {
let deps = NodeProvider::get_deps_from_package_json(&json);
if deps.contains("next") {
let relative = app.strip_source_path(file.as_path())?;
cache_dirs.push(relative.parent().unwrap().to_str().unwrap().to_string());
cache_dirs.push(relative.parent().unwrap().to_slash().unwrap().into_owned());
}
}

Expand Down Expand Up @@ -692,7 +693,7 @@ mod test {
}

#[test]
fn test_find_next_pacakges() -> Result<()> {
fn test_find_next_packages() -> Result<()> {
assert_eq!(
NodeProvider::find_next_packages(&App::new("./examples/node-monorepo")?)?,
vec!["packages/client".to_string()]
Expand Down
5 changes: 3 additions & 2 deletions src/providers/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::nixpacks::{
},
};
use anyhow::{bail, Result};
use path_slash::PathExt;

const DEFAULT_SWIFT_VERSION: &str = "5.4.2";

Expand Down Expand Up @@ -130,11 +131,11 @@ impl SwiftProvider {
let raw_paths = app.find_files("Sources/**/main.swift")?;
let paths = raw_paths
.iter()
.filter(|&path| !path.to_string_lossy().contains(".build"))
.filter(|&path| !path.to_slash().unwrap().contains(".build"))
.collect::<Vec<_>>();

let path = match paths.first() {
Some(path) => path.to_string_lossy().to_string(),
Some(path) => path.to_slash().unwrap().to_string(),
None => bail!("Your swift app doesn't have a main.swift file"),
};

Expand Down
Loading

0 comments on commit 6a9dff1

Please sign in to comment.