Skip to content

Commit

Permalink
fix(hydro_lang): use correct __staged path when rewriting crate::
Browse files Browse the repository at this point in the history
… imports

Previously, a rewrite would first turn `crate` into `crate::__staged`, and another would rewrite `crate::__staged` into `hydro_test::__staged`. The latter global rewrite is unnecessary because the stageleft logic already will use the full crate name when handling public types, so we drop it.
  • Loading branch information
shadaj committed Jan 16, 2025
1 parent 965e38c commit 6b1d56e
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

18 changes: 12 additions & 6 deletions hydro_deploy/core/src/hydroflow_crate/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::error::Error;
use std::fmt::Display;
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::process::{Command, ExitStatus, Stdio};
use std::sync::OnceLock;

use cargo_metadata::diagnostic::Diagnostic;
Expand Down Expand Up @@ -179,14 +179,20 @@ pub async fn build_crate_memoized(params: BuildParams) -> Result<&'static BuildO
ProgressTracker::println(msg.message.rendered.as_deref().unwrap());
diagnostics.push(msg.message);
}
cargo_metadata::Message::TextLine(line) => {
ProgressTracker::println(&line);
}
cargo_metadata::Message::BuildFinished(_) => {}
cargo_metadata::Message::BuildScriptExecuted(_) => {}
_ => {}
}
}

if spawned.wait().unwrap().success() {
let exit_code = spawned.wait().unwrap();
if exit_code.success() {
Err(BuildError::NoBinaryEmitted)
} else {
Err(BuildError::FailedToBuildCrate(diagnostics))
Err(BuildError::FailedToBuildCrate(exit_code, diagnostics))
}
})
.await
Expand All @@ -198,16 +204,16 @@ pub async fn build_crate_memoized(params: BuildParams) -> Result<&'static BuildO

#[derive(Clone, Debug)]
pub enum BuildError {
FailedToBuildCrate(Vec<Diagnostic>),
FailedToBuildCrate(ExitStatus, Vec<Diagnostic>),
TokioJoinError,
NoBinaryEmitted,
}

impl Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FailedToBuildCrate(diagnostics) => {
writeln!(f, "Failed to build crate:")?;
Self::FailedToBuildCrate(exit_status, diagnostics) => {
writeln!(f, "Failed to build crate (exit status {}):", exit_status)?;
for diagnostic in diagnostics {
write!(f, "{}", diagnostic)?;
}
Expand Down
2 changes: 1 addition & 1 deletion hydro_lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ build = [ "dep:dfir_lang" ]

[dependencies]
bincode = "1.3.1"
ctor = "0.2.8"
hydro_deploy = { path = "../hydro_deploy/core", version = "^0.11.0", optional = true }
dfir_rs = { path = "../dfir_rs", version = "^0.11.0", default-features = false, features = ["deploy_integration"] }
dfir_lang = { path = "../dfir_lang", version = "^0.11.0", optional = true }
Expand All @@ -41,6 +40,7 @@ syn = { version = "2.0.46", features = [ "parsing", "extra-traits", "visit-mut"
tokio = { version = "1.29.0", features = [ "full" ] }
toml = { version = "0.8.0", optional = true }
trybuild-internals-api = { version = "1.0.99", optional = true }
ctor = "0.2"

[build-dependencies]
stageleft_tool = { path = "../stageleft_tool", version = "^0.5.0" }
Expand Down
22 changes: 11 additions & 11 deletions hydro_lang/src/deploy/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use trybuild_internals_api::env::Update;
use trybuild_internals_api::run::{PathDependency, Project};
use trybuild_internals_api::{dependencies, features, path, Runner};

use super::trybuild_rewriters::{ReplaceCrateNameWithStaged, ReplaceCrateWithOrig};
use super::trybuild_rewriters::ReplaceCrateNameWithStaged;

static IS_TEST: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

Expand Down Expand Up @@ -44,19 +44,19 @@ pub fn create_graph_trybuild(

ReplaceCrateNameWithStaged {
crate_name: crate_name.clone(),
is_test,
}
.visit_file_mut(&mut generated_code);

let mut inlined_staged = stageleft_tool::gen_staged_trybuild(
&path!(source_dir / "src" / "lib.rs"),
crate_name.clone(),
is_test,
);

ReplaceCrateWithOrig {
crate_name: crate_name.clone(),
}
.visit_file_mut(&mut inlined_staged);
let inlined_staged = if is_test {
stageleft_tool::gen_staged_trybuild(
&path!(source_dir / "src" / "lib.rs"),
crate_name.clone(),
is_test,
)
} else {
syn::parse_quote!()
};

let source = prettyplease::unparse(&syn::parse_quote! {
#generated_code
Expand Down
26 changes: 11 additions & 15 deletions hydro_lang/src/deploy/trybuild_rewriters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,30 @@ use syn::visit_mut::VisitMut;

pub struct ReplaceCrateNameWithStaged {
pub crate_name: String,
pub is_test: bool,
}

impl VisitMut for ReplaceCrateNameWithStaged {
fn visit_type_path_mut(&mut self, i: &mut syn::TypePath) {
if let Some(first) = i.path.segments.first() {
if first.ident == self.crate_name {
let tail = i.path.segments.iter().skip(1).collect::<Vec<_>>();
*i = syn::parse_quote!(crate::__staged #(::#tail)*);

if self.is_test {
*i = syn::parse_quote!(crate::__staged #(::#tail)*);
} else {
let crate_ident = syn::Ident::new(&self.crate_name, first.ident.span());
*i = syn::parse_quote!(#crate_ident::__staged #(::#tail)*);
}
}
}

syn::visit_mut::visit_type_path_mut(self, i);
}
}

pub struct ReplaceCrateWithOrig {
pub crate_name: String,
}

impl VisitMut for ReplaceCrateWithOrig {
fn visit_item_use_mut(&mut self, i: &mut syn::ItemUse) {
if let syn::UseTree::Path(p) = &mut i.tree {
if p.ident == "crate" {
p.ident = syn::Ident::new(&self.crate_name, p.ident.span());
i.leading_colon = Some(Default::default());
}
fn visit_use_path_mut(&mut self, i: &mut syn::UsePath) {
if i.ident == "crate" && !self.is_test {
i.ident = syn::Ident::new(&self.crate_name, i.ident.span());
}

syn::visit_mut::visit_item_use_mut(self, i);
}
}
2 changes: 1 addition & 1 deletion hydro_lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn add_private_reexports() {

#[stageleft::runtime]
#[cfg(test)]
mod tests {
mod test_init {
#[ctor::ctor]
fn init() {
crate::deploy::init_test();
Expand Down
2 changes: 1 addition & 1 deletion hydro_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ hydro_lang = { path = "../hydro_lang", version = "^0.11.0" }
insta = "1.39"
hydro_deploy = { path = "../hydro_deploy/core", version = "^0.11.0" }
async-ssh2-lite = { version = "0.5.0", features = ["vendored-openssl"] }
ctor = "0.2.8"
ctor = "0.2"
2 changes: 1 addition & 1 deletion hydro_std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod request_response;

#[stageleft::runtime]
#[cfg(test)]
mod tests {
mod test_init {
#[ctor::ctor]
fn init() {
hydro_lang::deploy::init_test();
Expand Down
1 change: 1 addition & 0 deletions hydro_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde = { version = "1.0.197", features = [ "derive" ] }
stageleft_tool = { path = "../stageleft_tool", version = "^0.5.0" }

[dev-dependencies]
ctor = "0.2"
insta = "1.39"
hydro_deploy = { path = "../hydro_deploy/core", version = "^0.11.0" }
hydro_lang = { path = "../hydro_lang", version = "^0.11.0", features = [ "deploy" ] }
Expand Down
9 changes: 9 additions & 0 deletions hydro_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ pub mod distributed;
mod docs {
dfir_macro::doctest_markdown_glob!("docs/docs/hydro/**/*.md*");
}

#[stageleft::runtime]
#[cfg(test)]
mod test_init {
#[ctor::ctor]
fn init() {
hydro_lang::deploy::init_test();
}
}
16 changes: 16 additions & 0 deletions stageleft_tool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ impl VisitMut for GenFinalPubVistor {
syn::visit_mut::visit_use_path_mut(self, i);
}

fn visit_vis_restricted_mut(&mut self, _i: &mut syn::VisRestricted) {
// don't treat the restriction as a path, we don't want to rewrite that to `__staged`
}

fn visit_path_mut(&mut self, i: &mut syn::Path) {
if !i.segments.is_empty() && i.segments[0].ident == "crate" {
i.segments.insert(
1,
syn::PathSegment {
ident: parse_quote!(__staged),
arguments: Default::default(),
},
);
}
}

fn visit_item_mod_mut(&mut self, i: &mut syn::ItemMod) {
let is_runtime_or_test = i.attrs.iter().any(|a| {
a.path().to_token_stream().to_string() == "stageleft :: runtime"
Expand Down
1 change: 1 addition & 0 deletions template/hydro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ stageleft_tool = { git = "{{ hydro_git | default: 'https://github.com/hydro-proj

[dev-dependencies]
async-ssh2-lite = { version = "0.5.0", features = ["vendored-openssl"] }
ctor = "0.2"
hydro_deploy = { git = "{{ hydro_git | default: 'https://github.com/hydro-project/hydro.git' }}", branch = "{{ hydro_branch | default: 'main' }}" }
hydro_lang = { git = "{{ hydro_git | default: 'https://github.com/hydro-project/hydro.git' }}", branch = "{{ hydro_branch | default: 'main' }}", features = [
"deploy",
Expand Down
9 changes: 9 additions & 0 deletions template/hydro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ stageleft::stageleft_no_entry_crate!();
pub mod first_ten;
pub mod first_ten_cluster;
pub mod first_ten_distributed;

#[stageleft::runtime]
#[cfg(test)]
mod test_init {
#[ctor::ctor]
fn init() {
hydro_lang::deploy::init_test();
}
}

0 comments on commit 6b1d56e

Please sign in to comment.