difftreelog
fix ignore jpath when resolving filename passed to jrsonnet
in: master
4 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth1use std::{2 fs::{create_dir_all, File},3 io::{Read, Write},4};56use clap::{CommandFactory, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{GcOpts, ManifestOpts, MiscOpts, OutputOpts, StdOpts, TlaOpts, TraceOpts};9use jrsonnet_evaluator::{10 apply_tla, bail,11 error::{Error as JrError, ErrorKind},12 ResultExt, State, Val,13};14use jrsonnet_parser::{SourceDefaultIgnoreJpath, SourcePath};1516#[cfg(feature = "mimalloc")]17#[global_allocator]18static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1920#[derive(Parser)]21enum SubOpts {22 /// Generate completions for specified shell23 Generate {24 /// Target shell name25 shell: Shell,26 },27}2829#[derive(Parser)]30#[clap(next_help_heading = "DEBUG")]31struct DebugOpts {32 /// Required OS stack size.33 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.34 #[clap(long, name = "size")]35 pub os_stack: Option<usize>,36}3738#[derive(Parser)]39#[clap(next_help_heading = "INPUT")]40struct InputOpts {41 /// Treat input as code, evaluate it instead of reading file.42 #[clap(long, short = 'e')]43 pub exec: bool,4445 /// Path to the file to be compiled if `--exec` is unset, otherwise code itself.46 pub input: Option<String>,4748 /// After executing input, apply specified code.49 /// Output of the initial input will be accessible using `_`.50 #[cfg(feature = "exp-apply")]51 #[clap(long)]52 pub exp_apply: Vec<String>,53}5455/// Jsonnet commandline interpreter (Rust implementation)56#[derive(Parser)]57#[clap(58 args_conflicts_with_subcommands = true,59 disable_version_flag = true,60 version,61 author62)]63struct Opts {64 #[clap(subcommand)]65 sub: Option<SubOpts>,66 /// Print version67 #[clap(long)]68 version: bool,6970 #[clap(flatten)]71 input: InputOpts,72 #[clap(flatten)]73 misc: MiscOpts,74 #[clap(flatten)]75 tla: TlaOpts,76 #[clap(flatten)]77 std: StdOpts,78 #[clap(flatten)]79 gc: GcOpts,8081 #[clap(flatten)]82 trace: TraceOpts,83 #[clap(flatten)]84 manifest: ManifestOpts,85 #[clap(flatten)]86 output: OutputOpts,87 #[clap(flatten)]88 debug: DebugOpts,89}9091// TODO: Add unix_sigpipe = "sig_dfl"92fn main() {93 let opts: Opts = Opts::parse();9495 if opts.version {96 print!("{}", Opts::command().render_version());97 std::process::exit(0)98 }99100 if let Some(sub) = opts.sub {101 match sub {102 SubOpts::Generate { shell } => {103 use clap_complete::generate;104 let app = &mut Opts::command();105 let buf = &mut std::io::stdout();106 generate(shell, app, "jrsonnet", buf);107 std::process::exit(0)108 }109 }110 }111112 let success = if let Some(size) = opts.debug.os_stack {113 std::thread::Builder::new()114 .stack_size(size * 1024 * 1024)115 .spawn(|| main_catch(opts))116 .expect("new thread spawned")117 .join()118 .expect("thread finished successfully")119 } else {120 main_catch(opts)121 };122 if !success {123 std::process::exit(1);124 }125}126127#[derive(thiserror::Error, Debug)]128enum Error {129 // Handled differently130 #[error("evaluation error")]131 Evaluation(JrError),132 #[error("io error")]133 Io(#[from] std::io::Error),134 #[error("input is not utf8 encoded")]135 Utf8(#[from] std::str::Utf8Error),136 #[error("missing input argument")]137 MissingInputArgument,138}139impl From<JrError> for Error {140 fn from(e: JrError) -> Self {141 Self::Evaluation(e)142 }143}144impl From<ErrorKind> for Error {145 fn from(e: ErrorKind) -> Self {146 Self::from(JrError::from(e))147 }148}149150fn main_catch(opts: Opts) -> bool {151 let trace = opts.trace.trace_format();152 if let Err(e) = main_real(opts) {153 if let Error::Evaluation(e) = e {154 let mut out = String::new();155 trace.write_trace(&mut out, &e).expect("format error");156 eprintln!("{out}");157 } else {158 eprintln!("{e}");159 }160 return false;161 }162 true163}164165fn main_real(opts: Opts) -> Result<(), Error> {166 let _gc_leak_guard = opts.gc.leak_on_exit();167 let _gc_print_stats = opts.gc.stats_printer();168 let _stack_depth_override = opts.misc.stack_size_override();169170 let import_resolver = opts.misc.import_resolver();171 let std = opts.std.context_initializer()?;172173 let mut s = State::builder();174 s.import_resolver(import_resolver).context_initializer(std);175 let s = s.build();176177 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;178 let val = if opts.input.exec {179 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?180 } else if input == "-" {181 let mut input = Vec::new();182 std::io::stdin().read_to_end(&mut input)?;183 let input_str = std::str::from_utf8(&input)?;184 s.evaluate_snippet("<stdin>".to_owned(), input_str)?185 } else {186 s.import_from(&SourcePath::new(SourceDefaultIgnoreJpath), input.as_str())?187 };188189 let tla = opts.tla.tla_opts()?;190 #[allow(191 // It is not redundant/unused in exp-apply192 unused_mut,193 clippy::redundant_clone,194 )]195 let mut val = apply_tla(s.clone(), &tla, val)?;196197 #[cfg(feature = "exp-apply")]198 for apply in opts.input.exp_apply {199 use jrsonnet_evaluator::{InitialUnderscore, Thunk};200 val = s.evaluate_snippet_with(201 "<exp_apply>".to_owned(),202 &apply,203 InitialUnderscore(Thunk::evaluated(val)),204 )?;205 }206207 let manifest_format = opts.manifest.manifest_format();208 if let Some(multi) = opts.output.multi {209 if opts.output.create_output_dirs {210 let mut dir = multi.clone();211 dir.pop();212 create_dir_all(dir)?;213 }214 let Val::Obj(obj) = val else {215 bail!(216 "value should be object for --multi manifest, got {}",217 val.value_type()218 )219 };220 for (field, data) in obj.iter(221 #[cfg(feature = "exp-preserve-order")]222 opts.manifest.preserve_order,223 ) {224 let data = data.with_description(|| format!("getting field {field} for manifest"))?;225226 let mut path = multi.clone();227 path.push(&field as &str);228 if opts.output.create_output_dirs {229 let mut dir = path.clone();230 dir.pop();231 create_dir_all(dir)?;232 }233 println!("{}", path.to_str().expect("path"));234 let mut file = File::create(path)?;235 write!(236 file,237 "{}",238 data.manifest(&manifest_format)239 .with_description(|| format!("manifesting {field}"))?,240 )?;241 if manifest_format.file_trailing_newline() {242 writeln!(file)?;243 }244 file.flush()?;245 }246 } else if let Some(path) = opts.output.output_file {247 if opts.output.create_output_dirs {248 let mut dir = path.clone();249 dir.pop();250 create_dir_all(dir)?;251 }252 let mut file = File::create(path)?;253 writeln!(file, "{}", val.manifest(manifest_format)?)?;254 } else {255 let output = val.manifest(manifest_format)?;256 if !output.is_empty() {257 println!("{output}");258 }259 }260261 Ok(())262}crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -10,7 +10,9 @@
use fs::File;
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::IBytes;
-use jrsonnet_parser::{IStr, SourceDirectory, SourceFifo, SourceFile, SourcePath};
+use jrsonnet_parser::{
+ IStr, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
+};
use crate::{
bail,
@@ -183,6 +185,13 @@
o
} else if let Some(d) = from.downcast_ref::<SourceDirectory>() {
d.path().to_owned()
+ } else if from.downcast_ref::<SourceDefaultIgnoreJpath>().is_some() {
+ let mut direct = current_dir().map_err(|e| ImportIo(e.to_string()))?;
+ direct.push(path);
+ if let Some(direct) = check_path(&direct)? {
+ return Ok(direct);
+ }
+ bail!(ImportFileNotFound(from.clone(), path.to_owned()))
} else if from.is_default() {
current_dir().map_err(|e| ImportIo(e.to_string()))?
} else {
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -12,7 +12,8 @@
mod unescape;
pub use location::CodeLocation;
pub use source::{
- Source, SourceDirectory, SourceFifo, SourceFile, SourcePath, SourcePathT, SourceVirtual,
+ Source, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
+ SourcePathT, SourceVirtual,
};
pub struct ParserSettings {
crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -134,6 +134,23 @@
any_ext_impl!(SourcePathT);
}
+#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]
+pub struct SourceDefaultIgnoreJpath;
+impl Display for SourceDefaultIgnoreJpath {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "<default (ignoring jpath)>")
+ }
+}
+impl SourcePathT for SourceDefaultIgnoreJpath {
+ fn is_default(&self) -> bool {
+ true
+ }
+ fn path(&self) -> Option<&Path> {
+ None
+ }
+ any_ext_impl!(SourcePathT);
+}
+
/// Represents path to the file on the disk
/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:
///