diff --git a/src/config/env/using_flags.rs b/src/config/env/using_flags.rs index c916585d..6800a664 100644 --- a/src/config/env/using_flags.rs +++ b/src/config/env/using_flags.rs @@ -3,6 +3,7 @@ //! This module groups all `using` system and namespace flags. //! Use this for IDE autocomplete to discover using/namespace flags easily. +use super::env_bool; use super::warn_alias_once; pub fn enable_using() -> bool { @@ -68,3 +69,18 @@ pub fn using_ast_enabled() -> bool { _ => !using_is_prod(), // dev/ci → true, prod → false } } + +// ---- Using/resolve diagnostics ---- +pub fn resolve_trace() -> bool { + env_bool("NYASH_RESOLVE_TRACE") +} + +pub fn resolve_seam_debug() -> bool { + env_bool("NYASH_RESOLVE_SEAM_DEBUG") +} + +pub fn resolve_dump_merged_path() -> Option { + std::env::var("NYASH_RESOLVE_DUMP_MERGED") + .ok() + .filter(|s| !s.is_empty()) +} diff --git a/src/runner/modes/common_util/resolve/prelude_manager.rs b/src/runner/modes/common_util/resolve/prelude_manager.rs index 67ebe147..c436fd85 100644 --- a/src/runner/modes/common_util/resolve/prelude_manager.rs +++ b/src/runner/modes/common_util/resolve/prelude_manager.rs @@ -45,7 +45,7 @@ impl<'a> PreludeManagerBox<'a> { filename: &str, prelude_paths: &[String], ) -> Result { - let trace = std::env::var("NYASH_RESOLVE_TRACE").ok().as_deref() == Some("1"); + let trace = crate::config::env::resolve_trace(); if prelude_paths.is_empty() { return Ok(MergeResult { @@ -83,7 +83,7 @@ impl<'a> PreludeManagerBox<'a> { filename: &str, prelude_paths: &[String], ) -> Result { - let trace = std::env::var("NYASH_RESOLVE_TRACE").ok().as_deref() == Some("1"); + let trace = crate::config::env::resolve_trace(); if prelude_paths.is_empty() { return Ok(MergeResult { @@ -154,7 +154,7 @@ impl<'a> PreludeManagerBox<'a> { } // デバッグモードなら境界マーカーを追加 - if std::env::var("NYASH_RESOLVE_SEAM_DEBUG").ok().as_deref() == Some("1") { + if crate::config::env::resolve_seam_debug() { merged.push_str("\n/* --- using prelude/main boundary --- */\n\n"); // boundary line(s) are attributed to a synthetic "" pseudo-file let boundary_lines = 3usize; @@ -217,8 +217,8 @@ impl<'a> PreludeManagerBox<'a> { // `}` の前の `;` を除去(複数回パス) for _ in 0..2 { - let mut tmp = String::with_capacity(out.len()); let bytes = out.as_bytes(); + let mut tmp: Vec = Vec::with_capacity(bytes.len()); let mut i = 0usize; while i < bytes.len() { @@ -239,10 +239,10 @@ impl<'a> PreludeManagerBox<'a> { continue; } } - tmp.push(bytes[i] as char); + tmp.push(bytes[i]); i += 1; } - out = tmp; + out = String::from_utf8(tmp).expect("normalize_text_for_inline: invalid UTF-8"); } // ファイル末尾に改行を追加 diff --git a/src/runner/modes/common_util/resolve/selfhost_pipeline.rs b/src/runner/modes/common_util/resolve/selfhost_pipeline.rs index 4c2f9b1a..e54f5df8 100644 --- a/src/runner/modes/common_util/resolve/selfhost_pipeline.rs +++ b/src/runner/modes/common_util/resolve/selfhost_pipeline.rs @@ -94,8 +94,8 @@ impl<'a> SelfhostPipelineBox<'a> { PipelineConfig { enable_using: crate::config::env::enable_using(), enable_ast_merge: crate::config::env::using_ast_enabled(), - trace_execution: std::env::var("NYASH_RESOLVE_TRACE").ok().as_deref() == Some("1"), - debug_mode: std::env::var("NYASH_RESOLVE_SEAM_DEBUG").ok().as_deref() == Some("1"), + trace_execution: crate::config::env::resolve_trace(), + debug_mode: crate::config::env::resolve_seam_debug(), } } diff --git a/src/runner/modes/common_util/resolve/strip/merge.rs b/src/runner/modes/common_util/resolve/strip/merge.rs index b5ac35de..6a2098d7 100644 --- a/src/runner/modes/common_util/resolve/strip/merge.rs +++ b/src/runner/modes/common_util/resolve/strip/merge.rs @@ -40,7 +40,7 @@ pub fn merge_prelude_text( source: &str, filename: &str, ) -> Result { - let trace = crate::config::env::env_bool("NYASH_RESOLVE_TRACE"); + let trace = crate::config::env::resolve_trace(); // First pass: collect and resolve prelude paths let (cleaned_main, prelude_paths) = resolve_prelude_paths_profiled(runner, source, filename)?; @@ -122,7 +122,7 @@ pub fn merge_prelude_text( } // Add boundary marker if debug mode - if crate::config::env::env_bool("NYASH_RESOLVE_SEAM_DEBUG") { + if crate::config::env::resolve_seam_debug() { merged.push_str("\n/* --- using prelude/main boundary --- */\n\n"); let boundary_lines = 3usize; spans.push(crate::runner::modes::common_util::resolve::LineSpan { @@ -165,10 +165,8 @@ pub fn merge_prelude_text( } // Optional dump of merged text for diagnostics - if let Ok(dump_path) = std::env::var("NYASH_RESOLVE_DUMP_MERGED") { - if !dump_path.is_empty() { - let _ = std::fs::write(&dump_path, &merged); - } + if let Some(dump_path) = crate::config::env::resolve_dump_merged_path() { + let _ = std::fs::write(&dump_path, &merged); } crate::runner::modes::common_util::resolve::set_last_text_merge_line_spans(spans); diff --git a/src/runner/modes/common_util/resolve/strip/using.rs b/src/runner/modes/common_util/resolve/strip/using.rs index 99c33b09..84fc51e9 100644 --- a/src/runner/modes/common_util/resolve/strip/using.rs +++ b/src/runner/modes/common_util/resolve/strip/using.rs @@ -53,7 +53,7 @@ fn apply_using_strip_plan( out.push('\n'); } // Optional prelude boundary comment (helps manual inspection; parser ignores comments) - if crate::config::env::env_bool("NYASH_RESOLVE_SEAM_DEBUG") { + if crate::config::env::resolve_seam_debug() { let mut with_marker = String::with_capacity(out.len() + 64); with_marker.push_str("\n/* --- using boundary (AST) --- */\n"); with_marker.push_str(&out); @@ -70,8 +70,7 @@ fn plan_using_strip( let using_ctx = runner.init_using_context(); let prod = crate::config::env::using_is_prod(); let strict = crate::config::env::env_bool("NYASH_USING_STRICT"); - let verbose = - crate::config::env::cli_verbose() || crate::config::env::env_bool("NYASH_RESOLVE_TRACE"); + let verbose = crate::config::env::cli_verbose() || crate::config::env::resolve_trace(); let ctx_dir = std::path::Path::new(filename).parent(); let mut kept_lines: Vec = Vec::new(); @@ -154,10 +153,10 @@ fn plan_using_strip( p = cand; } } - // Also try NYASH_ROOT when available (repo-root relative like "apps/...") + // Also try repo root when available (repo-root relative like "apps/...") if p.is_relative() { - if let Ok(root) = std::env::var("NYASH_ROOT") { - let cand = std::path::Path::new(&root).join(&p); + if let Some(root) = crate::runner::modes::common_util::resolve::root::resolve_repo_root(Some(filename)) { + let cand = root.join(&p); if cand.exists() { p = cand; } @@ -459,22 +458,20 @@ fn plan_using_strip( } } if p.is_relative() { - if let Ok(root) = std::env::var("NYASH_ROOT") { - let cand = std::path::Path::new(&root).join(&p); + if let Some(root) = crate::runner::modes::common_util::resolve::root::resolve_repo_root(Some(filename)) { + let cand = root.join(&p); if cand.exists() { p = cand; } - } else { - if let Ok(exe) = std::env::current_exe() { - if let Some(root) = exe - .parent() - .and_then(|p| p.parent()) - .and_then(|p| p.parent()) - { - let cand = root.join(&p); - if cand.exists() { - p = cand; - } + } else if let Ok(exe) = std::env::current_exe() { + if let Some(root) = exe + .parent() + .and_then(|p| p.parent()) + .and_then(|p| p.parent()) + { + let cand = root.join(&p); + if cand.exists() { + p = cand; } } } diff --git a/src/runner/modes/common_util/resolve/using_resolution.rs b/src/runner/modes/common_util/resolve/using_resolution.rs index d552b4a1..1c1f0016 100644 --- a/src/runner/modes/common_util/resolve/using_resolution.rs +++ b/src/runner/modes/common_util/resolve/using_resolution.rs @@ -49,7 +49,7 @@ impl<'a> UsingResolutionBox<'a> { prod: crate::config::env::using_is_prod(), strict: crate::config::env::env_bool("NYASH_USING_STRICT"), verbose: crate::config::env::cli_verbose() - || crate::config::env::env_bool("NYASH_RESOLVE_TRACE"), + || crate::config::env::resolve_trace(), allow_file_using: crate::config::env::allow_using_file(), }; @@ -162,8 +162,8 @@ impl<'a> UsingResolutionBox<'a> { // NYASH_ROOTも試す if p.is_relative() { - if let Ok(root) = std::env::var("NYASH_ROOT") { - let cand = Path::new(&root).join(&p); + if let Some(root) = crate::runner::modes::common_util::resolve::root::resolve_repo_root(None) { + let cand = root.join(&p); if cand.exists() { p = cand; }