Skip to content

Commit

Permalink
Merge pull request #159 from tyoeer/main
Browse files Browse the repository at this point in the history
Respect Cargo target directory configuration
  • Loading branch information
gbj authored Oct 3, 2023
2 parents 010b65f + ce827f1 commit 15535b2
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 40 deletions.
4 changes: 4 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 @@ -65,6 +65,7 @@ tar = "0.4"
dunce = "1.0"
bytes = "1.4"
leptos_hot_reload = { git = "https://github.com/leptos-rs/leptos", version = "0.4.8" }
pathdiff = { version = "0.2.1", features = ["camino"] }
semver = "1.0.18"
async-trait = "0.1.72"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ output-name = "myproj"
# NOTE: It is relative to the workspace root when running in a workspace.
# WARNING: all content of this folder will be erased on a rebuild.
#
# Optional, defaults to "target/site". Env: LEPTOS_SITE_ROOT.
# Optional, defaults to "/site" in the Cargo target directory. Env: LEPTOS_SITE_ROOT.
site-root = "target/site"

# The site-root relative folder where all compiled output (JS, WASM and CSS) is written.
Expand Down
1 change: 0 additions & 1 deletion src/compile/front.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pub fn build_cargo_front_cmd(
cmd.to_string(),
format!("--package={}", proj.lib.name.as_str()),
"--lib".to_string(),
"--target-dir=target/front".to_string(),
];
if wasm {
args.push("--target=wasm32-unknown-unknown".to_string());
Expand Down
7 changes: 3 additions & 4 deletions src/compile/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ pub fn build_cargo_server_cmd(
if cmd != "test" {
args.push(format!("--bin={}", proj.bin.target))
}
args.push(format!(
"--target-dir={}",
proj.bin.target_dir.as_deref().unwrap_or("target/server")
));
if let Some(target_dir) = &proj.bin.target_dir {
args.push(format!("--target-dir={target_dir}"));
}
if let Some(triple) = &proj.bin.target_triple {
args.push(format!("--target={triple}"));
}
Expand Down
16 changes: 8 additions & 8 deletions src/compile/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ fn test_project_dev() {
LEPTOS_WATCH=ON";
assert_eq!(ENV_REF, envs);

assert_display_snapshot!(cargo, @"cargo build --package=example --bin=example --target-dir=target/server --no-default-features --features=ssr");
assert_display_snapshot!(cargo, @"cargo build --package=example --bin=example --no-default-features --features=ssr");

let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_front_cmd("build", true, &conf.projects[0], &mut command);

assert_display_snapshot!(cargo, @"cargo build --package=example --lib --target-dir=target/front --target=wasm32-unknown-unknown --no-default-features --features=hydrate");
assert_display_snapshot!(cargo, @"cargo build --package=example --lib --target=wasm32-unknown-unknown --no-default-features --features=hydrate");
}

#[test]
Expand All @@ -65,12 +65,12 @@ fn test_project_release() {
let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_server_cmd("build", &conf.projects[0], &mut command);

assert_display_snapshot!(cargo, @"cargo build --package=example --bin=example --target-dir=target/server --no-default-features --features=ssr --release");
assert_display_snapshot!(cargo, @"cargo build --package=example --bin=example --no-default-features --features=ssr --release");

let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_front_cmd("build", true, &conf.projects[0], &mut command);

assert_display_snapshot!(cargo, @"cargo build --package=example --lib --target-dir=target/front --target=wasm32-unknown-unknown --no-default-features --features=hydrate --release");
assert_display_snapshot!(cargo, @"cargo build --package=example --lib --target=wasm32-unknown-unknown --no-default-features --features=hydrate --release");
}

#[test]
Expand Down Expand Up @@ -105,14 +105,14 @@ fn test_workspace_project1() {

assert_eq!(ENV_REF, envs);

assert_display_snapshot!(cargo, @"cargo build --package=server-package --bin=server-package --target-dir=target/server --no-default-features");
assert_display_snapshot!(cargo, @"cargo build --package=server-package --bin=server-package --no-default-features");

let mut command = Command::new("cargo");
let (envs, cargo) = build_cargo_front_cmd("build", true, &conf.projects[0], &mut command);

assert_eq!(ENV_REF, envs);

assert_display_snapshot!(cargo, @"cargo build --package=front-package --lib --target-dir=target/front --target=wasm32-unknown-unknown --no-default-features");
assert_display_snapshot!(cargo, @"cargo build --package=front-package --lib --target=wasm32-unknown-unknown --no-default-features");
}

#[test]
Expand All @@ -123,10 +123,10 @@ fn test_workspace_project2() {
let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_server_cmd("build", &conf.projects[1], &mut command);

assert_display_snapshot!(cargo, @"cargo build --package=project2 --bin=project2 --target-dir=target/server --no-default-features --features=ssr");
assert_display_snapshot!(cargo, @"cargo build --package=project2 --bin=project2 --no-default-features --features=ssr");

let mut command = Command::new("cargo");
let (_, cargo) = build_cargo_front_cmd("build", true, &conf.projects[1], &mut command);

assert_display_snapshot!(cargo, @"cargo build --package=project2 --lib --target-dir=target/front --target=wasm32-unknown-unknown --no-default-features --features=hydrate");
assert_display_snapshot!(cargo, @"cargo build --package=project2 --lib --target=wasm32-unknown-unknown --no-default-features --features=hydrate");
}
3 changes: 2 additions & 1 deletion src/config/bin_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ impl BinPackage {
};
let mut file = config.bin_target_dir.as_ref()
.map(|dir| dir.into())
.unwrap_or_else(|| metadata.rel_target_dir().join("server"));
// Can't use absolute path because the path gets stored in snapshot testing, and it differs between developers
.unwrap_or_else(|| metadata.rel_target_dir());
if let Some(triple) = &config.bin_target_triple {
file = file.join(triple)
};
Expand Down
2 changes: 1 addition & 1 deletion src/config/lib_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ impl LibPackage {

let wasm_file = {
let source = metadata
// Can't use absolute path because the path gets stored in snapshot testing, and it differs between developers
.rel_target_dir()
.join("front")
.join("wasm32-unknown-unknown")
.join(profile.to_string())
.join(name.replace('-', "_"))
Expand Down
45 changes: 37 additions & 8 deletions src/config/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ use super::{
style::StyleConfig,
};

/// If the site root path starts with this marker, the marker should be replaced with the Cargo target directory
const CARGO_TARGET_DIR_MARKER: &str = "CARGO_TARGET_DIR";
/// If the site root path starts with this marker, the marker should be replaced with the Cargo target directory
const CARGO_BUILD_TARGET_DIR_MARKER: &str = "CARGO_BUILD_TARGET_DIR";

pub struct Project {
/// absolute path to the working dir
pub working_dir: Utf8PathBuf,
Expand Down Expand Up @@ -180,17 +185,39 @@ pub struct ProjectConfig {
}

impl ProjectConfig {
fn parse(dir: &Utf8Path, metadata: &serde_json::Value) -> Result<Self> {
fn parse(dir: &Utf8Path, metadata: &serde_json::Value, cargo_metadata: &Metadata) -> Result<Self> {
let mut conf: ProjectConfig = serde_json::from_value(metadata.clone())?;
conf.config_dir = dir.to_path_buf();
let dotenvs = load_dotenvs(dir)?;
overlay_env(&mut conf, dotenvs)?;
if conf.site_root == "/" || conf.site_root == "." {
if conf.site_root == "/"
|| conf.site_root == "."
|| conf.site_root == CARGO_TARGET_DIR_MARKER
|| conf.site_root == CARGO_BUILD_TARGET_DIR_MARKER
{
bail!(
"site-root cannot be '{}'. All the content is erased when building the site.",
conf.site_root
);
}
if conf.site_root.starts_with(CARGO_TARGET_DIR_MARKER) {
conf.site_root = {
let mut path = cargo_metadata.target_directory.clone();
// unwrap() should be safe because we just checked
let sub = conf.site_root.unbase(CARGO_TARGET_DIR_MARKER.into()).unwrap();
path.push(sub);
path
};
}
if conf.site_root.starts_with(CARGO_BUILD_TARGET_DIR_MARKER) {
conf.site_root = {
let mut path = cargo_metadata.target_directory.clone();
// unwrap() should be safe because we just checked
let sub = conf.site_root.unbase(CARGO_BUILD_TARGET_DIR_MARKER.into()).unwrap();
path.push(sub);
path
};
}
if conf.site_addr.port() == conf.reload_port {
bail!(
"The site-addr port and reload-port cannot be the same: {}",
Expand All @@ -212,11 +239,12 @@ impl ProjectDefinition {
fn from_workspace(
metadata: &serde_json::Value,
dir: &Utf8Path,
cargo_metadata: &Metadata,
) -> Result<Vec<(Self, ProjectConfig)>> {
let mut found = Vec::new();
if let Some(arr) = metadata.as_array() {
for section in arr {
let conf = ProjectConfig::parse(dir, section)?;
let conf = ProjectConfig::parse(dir, section, cargo_metadata)?;
let def: Self = serde_json::from_value(section.clone())?;
found.push((def, conf))
}
Expand All @@ -228,8 +256,9 @@ impl ProjectDefinition {
package: &Package,
metadata: &serde_json::Value,
dir: &Utf8Path,
cargo_metadata: &Metadata,
) -> Result<(Self, ProjectConfig)> {
let conf = ProjectConfig::parse(dir, metadata)?;
let conf = ProjectConfig::parse(dir, metadata, cargo_metadata)?;

ensure!(
package.cdylib_target().is_some(),
Expand All @@ -256,16 +285,16 @@ impl ProjectDefinition {
let workspace_dir = &metadata.workspace_root;
let mut found: Vec<(Self, ProjectConfig)> =
if let Some(md) = leptos_metadata(&metadata.workspace_metadata) {
Self::from_workspace(md, &Utf8PathBuf::default())?
Self::from_workspace(md, &Utf8PathBuf::default(), metadata)?
} else {
Default::default()
};

for package in metadata.workspace_packages() {
let dir = package.manifest_path.unbase(workspace_dir)?.without_last();

if let Some(metadata) = leptos_metadata(&package.metadata) {
found.push(Self::from_project(package, metadata, &dir)?);
if let Some(leptos_metadata) = leptos_metadata(&package.metadata) {
found.push(Self::from_project(package, leptos_metadata, &dir, metadata)?);
}
}
Ok(found)
Expand All @@ -285,7 +314,7 @@ fn default_pkg_dir() -> Utf8PathBuf {
}

fn default_site_root() -> Utf8PathBuf {
Utf8PathBuf::from("target").join("site")
Utf8PathBuf::from(CARGO_TARGET_DIR_MARKER).join("site")
}

fn default_reload_port() -> u16 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config {
name: "example",
rel_dir: ".",
wasm_file: SourcedSiteFile {
source: "target/front/wasm32-unknown-unknown/debug/example.wasm",
source: "target/wasm32-unknown-unknown/debug/example.wasm",
dest: "target/site/pkg/example.wasm",
site: "pkg/example.wasm",
},
Expand All @@ -30,7 +30,7 @@ Config {
bin: BinPackage {
name: "example",
rel_dir: ".",
exe_file: "target/server/debug/example",
exe_file: "target/debug/example",
target: "example",
features: [
"ssr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config {
name: "front-package",
rel_dir: "project1/front",
wasm_file: SourcedSiteFile {
source: "target/front/wasm32-unknown-unknown/debug/front_package.wasm",
source: "target/wasm32-unknown-unknown/debug/front_package.wasm",
dest: "target/site/project1/pkg/project1.wasm",
site: "pkg/project1.wasm",
},
Expand All @@ -28,7 +28,7 @@ Config {
bin: BinPackage {
name: "server-package",
rel_dir: "project1/server",
exe_file: "target/server/debug/server-package",
exe_file: "target/debug/server-package",
target: "server-package",
features: [],
default_features: false,
Expand Down Expand Up @@ -76,7 +76,7 @@ Config {
name: "project2",
rel_dir: "project2",
wasm_file: SourcedSiteFile {
source: "target/front/wasm32-unknown-unknown/debug/project2.wasm",
source: "target/wasm32-unknown-unknown/debug/project2.wasm",
dest: "target/site/project2/pkg/project2.wasm",
site: "pkg/project2.wasm",
},
Expand All @@ -96,7 +96,7 @@ Config {
bin: BinPackage {
name: "project2",
rel_dir: "project2",
exe_file: "target/server/debug/project2",
exe_file: "target/debug/project2",
target: "project2",
features: [
"ssr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config {
name: "project2",
rel_dir: "project2",
wasm_file: SourcedSiteFile {
source: "target/front/wasm32-unknown-unknown/debug/project2.wasm",
source: "target/wasm32-unknown-unknown/debug/project2.wasm",
dest: "target/site/project2/pkg/project2.wasm",
site: "pkg/project2.wasm",
},
Expand All @@ -30,7 +30,7 @@ Config {
bin: BinPackage {
name: "project2",
rel_dir: "project2",
exe_file: "target/server/debug/project2",
exe_file: "target/debug/project2",
target: "project2",
features: [
"ssr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config {
name: "front-package",
rel_dir: "project1/front",
wasm_file: SourcedSiteFile {
source: "target/front/wasm32-unknown-unknown/debug/front_package.wasm",
source: "target/wasm32-unknown-unknown/debug/front_package.wasm",
dest: "target/site/project1/pkg/project1.wasm",
site: "pkg/project1.wasm",
},
Expand All @@ -28,7 +28,7 @@ Config {
bin: BinPackage {
name: "server-package",
rel_dir: "project1/server",
exe_file: "target/server/debug/server-package",
exe_file: "target/debug/server-package",
target: "server-package",
features: [],
default_features: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Config {
name: "project2",
rel_dir: "project2",
wasm_file: SourcedSiteFile {
source: "target/front/wasm32-unknown-unknown/debug/project2.wasm",
source: "target/wasm32-unknown-unknown/debug/project2.wasm",
dest: "target/site/project2/pkg/project2.wasm",
site: "pkg/project2.wasm",
},
Expand All @@ -30,7 +30,7 @@ Config {
bin: BinPackage {
name: "project2",
rel_dir: "project2",
exe_file: "target/server/debug/project2",
exe_file: "target/debug/project2",
target: "project2",
features: [
"ssr",
Expand Down
5 changes: 1 addition & 4 deletions src/ext/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ impl MetadataExt for Metadata {
}

fn rel_target_dir(&self) -> Utf8PathBuf {
self.target_directory
.clone()
.unbase(&self.workspace_root)
.unwrap()
pathdiff::diff_utf8_paths(&self.target_directory, &self.workspace_root).unwrap()
}

fn package_for(&self, id: &PackageId) -> Option<&Package> {
Expand Down

0 comments on commit 15535b2

Please sign in to comment.