Skip to content

Commit

Permalink
better debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed Aug 19, 2024
1 parent 6294b33 commit 151978e
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 57 deletions.
19 changes: 9 additions & 10 deletions packages/mach/src/cmd/build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Serialize;
use super::super::MachOptions;
use crate::core::bundling::bundle;
use crate::core::emit::emit;
use crate::core::emit::emit_file;
use crate::core::packaging::package;
use crate::core::plugins::load_plugins;
use crate::core::resolve_and_transform::resolve_and_transform;
Expand Down Expand Up @@ -40,7 +41,7 @@ pub fn build(
mach_options: MachOptions,
_build_options: BuildOptions,
) -> anyhow::Result<BuildResult> {
let mut compilation = Compilation {
let mut c = Compilation {
machrc: mach_options.config,
rpc_hosts: mach_options.rpc_hosts,
config: MachConfig {
Expand All @@ -57,23 +58,21 @@ pub fn build(
};

// This will read the Machrc and initialize the referenced plugins
load_plugins(&mut compilation)?;
load_plugins(&mut c)?;

// This will resolve imports, transform files and build the AssetGraph.
resolve_and_transform(&mut compilation)?;

compilation.asset_graph.debug_render();
resolve_and_transform(&mut c)?;
emit_file(&c, "asset_graph.dot", c.asset_graph.debug_dot())?;

// This will read the asset graph and organize related assets into groupings (a.k.a bundles)
bundle(&mut compilation)?;

compilation.bundle_graph.debug_render_graph();
bundle(&mut c)?;
emit_file(&c, "bundle_graph.dot", c.bundle_graph.debug_dot())?;

// This will apply the runtime to and optimize the bundles
package(&mut compilation)?;
package(&mut c)?;

// This will write the contents of the packaged bundles to disk
emit(&mut compilation)?;
emit(&mut c)?;

Ok(BuildResult::default())
}
2 changes: 1 addition & 1 deletion packages/mach/src/core/config/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub static ROOT_ASSET: Lazy<Asset> = Lazy::new(|| Asset {
id: AssetId::new(),
name: "ROOT".to_string(),
file_path_absolute: ROOT_NODE.clone(),
file_path_relative: ROOT_NODE.clone(),
file_path: ROOT_NODE.clone(),
kind: Default::default(),
content: Default::default(),
bundle_behavior: Default::default(),
Expand Down
7 changes: 7 additions & 0 deletions packages/mach/src/core/emit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{fs, path::Path};

use crate::types::Compilation;

pub fn emit(_c: &mut Compilation) -> anyhow::Result<()> {
Expand All @@ -18,3 +20,8 @@ pub fn emit(_c: &mut Compilation) -> anyhow::Result<()> {

Ok(())
}

pub fn emit_file<K: AsRef<Path>, D: AsRef<[u8]>>(c: &Compilation, name: K, data: D) -> anyhow::Result<()> {
fs::write(c.config.out_folder.join(name), data)?;
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn resolve_and_transform(c: &mut Compilation) -> anyhow::Result<()> {
let mut new_asset = Asset {
id: new_asset_id.clone(),
file_path_absolute: resolve_result.file_path.clone(),
file_path_relative: resolve_result.file_path_relative.clone(),
file_path: resolve_result.file_path_relative.clone(),
bundle_behavior: dependency.bundle_behavior.clone(),
name: Default::default(),
kind: Default::default(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn _generate_project_snapshot(project_root: &Path) {
snapshot_imports.insert(
dependency.specifier.clone(),
DependencySnapshot {
resolves_to: dest_asset.file_path_relative.clone(),
resolves_to: dest_asset.file_path.clone(),
specifier: dependency.specifier.clone(),
specifier_type: dependency.specifier_type.clone(),
priority: dependency.priority.clone(),
Expand All @@ -110,14 +110,14 @@ pub fn _generate_project_snapshot(project_root: &Path) {
}

let snapshot_entry = GraphSnapshotAsset {
file_path: source_asset.file_path_relative.clone(),
file_path: source_asset.file_path.clone(),
linking_symbols: source_asset.linking_symbols.clone(),
kind: source_asset.kind.clone(),
bundle_behavior: source_asset.bundle_behavior.clone(),
imports: snapshot_imports,
};

snapshot.insert(source_asset.file_path_relative.clone(), snapshot_entry);
snapshot.insert(source_asset.file_path.clone(), snapshot_entry);
}

GraphSnapshot::_save(&snapshot_path, snapshot);
Expand Down
28 changes: 14 additions & 14 deletions packages/mach/src/core/resolve_and_transform/testing/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ pub fn test_project_snapshot(
let mut edges = c.asset_graph.get_dependencies(&node_index);
let source_asset = c.asset_graph.get_asset(node_index).unwrap();

let Some(snap_asset) = snapshot.config.get(&source_asset.file_path_relative) else {
let Some(snap_asset) = snapshot.config.get(&source_asset.file_path) else {
panic!(
"Error: {}\n\tMissing Asset\n\tExpected Asset: {:?}\nGot: null",
project_name, source_asset.file_path_relative
project_name, source_asset.file_path
)
};

Expand All @@ -88,25 +88,25 @@ pub fn test_project_snapshot(
let Some(snap_dependency) = snap_asset.imports.get(&dependency.specifier) else {
panic!(
"Error: {}\n\tMissing Specifier\n\tExpected Asset: {:?}\n\tWith Specifier {}\n\tGot: null",
project_name, source_asset.file_path_relative, dependency.specifier
project_name, source_asset.file_path, dependency.specifier
)
};

assert!(
snap_dependency.resolves_to == dest_asset.file_path_relative,
snap_dependency.resolves_to == dest_asset.file_path,
"Error: {}\n\tIncorrect Dependency Resolved Path\n\tImport: {:?}\n\tSpecifier: {}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
dependency.specifier,
snap_dependency.resolves_to,
dest_asset.file_path_relative
dest_asset.file_path
);

assert!(
snap_dependency.specifier == dependency.specifier,
"Error: {}\n\tIncorrect Dependency Specifier\n\tImport: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
snap_dependency.specifier,
dependency.specifier,
);
Expand All @@ -115,7 +115,7 @@ pub fn test_project_snapshot(
snap_dependency.specifier_type == dependency.specifier_type,
"Error: {}\n\tIncorrect Specifier Type\n\tImport: {:?}\n\tSpecifier: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
dependency.specifier,
snap_dependency.specifier_type,
dependency.specifier_type,
Expand All @@ -125,7 +125,7 @@ pub fn test_project_snapshot(
snap_dependency.priority == dependency.priority,
"Error: {}\n\tIncorrect Priority\n\tImport: {:?}\n\tSpecifier: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
dependency.specifier,
snap_dependency.priority,
dependency.priority,
Expand Down Expand Up @@ -157,7 +157,7 @@ pub fn test_project_snapshot(
snap_dependency.bundle_behavior == dependency.bundle_behavior,
"Error: {}\n\tIncorrect Bundle Behavior\n\tImport: {:?}\n\tSpecifier: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
dependency.specifier,
snap_dependency.bundle_behavior,
dependency.bundle_behavior,
Expand All @@ -168,7 +168,7 @@ pub fn test_project_snapshot(
snap_asset.kind == source_asset.kind,
"Error: {}\n\tIncorrect Asset Kind\n\tAsset: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
snap_asset.kind,
source_asset.kind,
);
Expand All @@ -177,7 +177,7 @@ pub fn test_project_snapshot(
snap_asset.bundle_behavior == source_asset.bundle_behavior,
"Error: {}\n\tIncorrect Asset Bundle Behavior\n\tAsset: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
snap_asset.bundle_behavior,
source_asset.bundle_behavior,
);
Expand All @@ -186,7 +186,7 @@ pub fn test_project_snapshot(
snap_asset.linking_symbols.len() == source_asset.linking_symbols.len(),
"Error: {}\n\tIncorrect Exported Symbols Length\n\tAsset: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
snap_asset.linking_symbols.len(),
source_asset.linking_symbols.len(),
);
Expand All @@ -196,7 +196,7 @@ pub fn test_project_snapshot(
snap_asset.linking_symbols.contains(linking_symbols),
"Error: {}\n\tMissing Linking Symbol\n\tAsset: {:?}\n\tExpected: {:?}\n\tGot: {:?}",
project_name,
source_asset.file_path_relative,
source_asset.file_path,
snap_asset.linking_symbols,
source_asset.linking_symbols,
)
Expand Down
4 changes: 2 additions & 2 deletions packages/mach/src/types/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Asset {
/// Absolute filepath to the asset
pub file_path_absolute: PathBuf,
/// Relative filepath to the asset
pub file_path_relative: PathBuf,
pub file_path: PathBuf,
/// Describes the type of the Asset. Starts as the file extension
pub kind: String,
/// The body of the Asset in bytes
Expand All @@ -41,7 +41,7 @@ impl Debug for Asset {
f.debug_struct("Asset")
.field("id", &self.id.0)
.field("file_path", &self.file_path_absolute)
.field("file_path_rel", &self.file_path_relative)
.field("file_path_rel", &self.file_path)
.field("bundle_behavior", &self.bundle_behavior)
.field("kind", &self.kind)
.finish()
Expand Down
29 changes: 11 additions & 18 deletions packages/mach/src/types/asset_graph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::process::Stdio;

use petgraph::dot::Config;
Expand All @@ -17,7 +16,6 @@ use super::Asset;
use super::AssetId;
use super::Dependency;
use super::DependencyId;
use super::MachConfig;
use crate::core::config::ROOT_ASSET;
use crate::types::DependencyPriority;

Expand Down Expand Up @@ -107,17 +105,18 @@ impl AssetGraph {
impl AssetGraph {
pub fn debug_dot(
&self,
config: &MachConfig,
) -> String {
let get_node_attribute = |_: &StableDiGraph<Asset, Dependency>,
(_, asset): (NodeIndex, &Asset)| {
let mut label = String::from("ROOT");

if asset.id == ROOT_ASSET.id {
return format!("path = \"{}\" ", label);
return format!("shape=box label=\"ROOT\" ");
}
label = asset.file_path_relative.to_str().unwrap().to_string();
format!("path = \"{}\" ", label)

let mut label = String::new();
label += &format!("[{}] ", asset.id.0);
label += &asset.file_path.to_str().unwrap().to_string();

format!("shape=box label=\"{}\" ", label)
};

let get_edge_attribute =
Expand All @@ -127,16 +126,10 @@ impl AssetGraph {

let mut specifier = dependency.specifier.clone();
if dependency.specifier.starts_with("/") || dependency.specifier.starts_with("\\") {
specifier = format!(
"./{}",
pathdiff::diff_paths(&PathBuf::from(&dependency.specifier), &config.project_root)
.unwrap()
.to_str()
.unwrap()
);
specifier = format!("");
}

label += &format!("specifier = \"{}\" ", specifier);
label += &format!("label=\" {}\" ", specifier);

if let DependencyPriority::Lazy = dependency.priority {
label += &format!("; style = \"dashed\" ")
Expand All @@ -161,7 +154,7 @@ impl AssetGraph {
let mut edges = self.get_dependencies(&node_index);
let source_asset = self.get_asset(node_index).unwrap();
let mut src_path = source_asset
.file_path_relative
.file_path
.to_str()
.unwrap()
.to_string();
Expand All @@ -174,7 +167,7 @@ impl AssetGraph {

while let Some(edge) = edges.next() {
let dest_asset = self.get_asset(edge.target().id()).unwrap();
let dest_path = dest_asset.file_path_relative.to_str().unwrap();
let dest_path = dest_asset.file_path.to_str().unwrap();
let dest_path = format!("[{}] {}", dest_asset.id.0, dest_path);

output.push_str(&format!("{} -> {}\n", src_path, dest_path));
Expand Down
2 changes: 1 addition & 1 deletion packages/mach/src/types/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Bundle {
) -> Option<AssetId> {
self
.assets
.insert(asset.file_path_relative.clone(), asset.id.clone())
.insert(asset.file_path.clone(), asset.id.clone())
}
}

Expand Down
48 changes: 48 additions & 0 deletions packages/mach/src/types/bundle_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::collections::HashMap;
use std::io::Write;
use std::process::Stdio;

use petgraph::dot::Config;
use petgraph::dot::Dot;
use petgraph::stable_graph::EdgeIndex;
use petgraph::stable_graph::EdgeReference;
use petgraph::stable_graph::NodeIndex;
use petgraph::stable_graph::StableDiGraph;
use petgraph::visit::EdgeRef;
Expand Down Expand Up @@ -87,6 +90,51 @@ impl BundleGraph {
// Debugging
//
impl BundleGraph {
pub fn debug_dot(&self) -> String {
let get_node_attribute = |_: &StableDiGraph<Bundle, ()>,
(nx, bundle): (NodeIndex, &Bundle)| {

if nx == self.root_node() {
return format!("shape=box label=\"ROOT\"");
}

let mut label = String::new();

label += &format!("kind: {}\\l", bundle.kind);

if let Some(entry) = &bundle.entry_asset {
label += &format!("entry: {}\\l", entry.0);
} else {
label += &format!("None\\l");
};

label += &format!("assets: ");

label += &bundle.assets
.iter()
.map(|id| id.1.0.to_string())
.collect::<Vec<String>>()
.join(", ");

label += &format!("\\l");

return format!("shape=box label=\"{}\"", label);
};

let get_edge_attribute =
|_: &StableDiGraph<Bundle, ()>, _edge_ref: EdgeReference<()>| -> String {
"".to_string()
};

let dot = Dot::with_attr_getters(
&self.graph,
&[Config::EdgeNoLabel, Config::NodeNoLabel],
&get_edge_attribute,
&get_node_attribute,
);
format!("{:?}", dot)
}

pub fn debug_render_graph(&self) {
let mut output = String::new();

Expand Down
Loading

0 comments on commit 151978e

Please sign in to comment.