git.delta.rocks / jrsonnet / refs/commits / bf006f520f66

difftreelog

feat(evaluator) custom source paths

Yaroslav Bolyukin2022-08-27parent: #a7e60a9.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -137,15 +137,21 @@
 	StandaloneSuper,
 
 	#[error("can't resolve {1} from {0}")]
-	ImportFileNotFound(PathBuf, String),
+	ImportFileNotFound(SourcePath, String),
+	#[error("can't resolve absolute {0}")]
+	AbsoluteImportFileNotFound(PathBuf),
 	#[error("resolved file not found: {:?}", .0)]
 	ResolvedFileNotFound(SourcePath),
+	#[error("can't import {0}: is a directory")]
+	ImportIsADirectory(SourcePath),
 	#[error("imported file is not valid utf-8: {0:?}")]
 	ImportBadFileUtf8(SourcePath),
 	#[error("import io error: {0}")]
 	ImportIo(String),
-	#[error("tried to import {1} from {0}, but imports is not supported")]
-	ImportNotSupported(PathBuf, PathBuf),
+	#[error("tried to import {1} from {0}, but imports are not supported")]
+	ImportNotSupported(SourcePath, String),
+	#[error("tried to import {0}, but absolute imports are not supported")]
+	AbsoluteImportNotSupported(PathBuf),
 	#[error("can't import from virtual file")]
 	CantImportFromVirtualFile,
 	#[error(
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use crate::{12	destructure::evaluate_dest,13	error::Error::*,14	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},15	function::{CallLocation, FuncDesc, FuncVal},16	tb, throw,17	typed::Typed,18	val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},19	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,20	Unbound, Val,21};22pub mod destructure;23pub mod operator;2425pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {26	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {27		name,28		ctx,29		params,30		body,31	})))32}3334pub fn evaluate_field_name(s: State, ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {35	Ok(match field_name {36		FieldName::Fixed(n) => Some(n.clone()),37		FieldName::Dyn(expr) => s.push(38			CallLocation::new(&expr.1),39			|| "evaluating field name".to_string(),40			|| {41				let value = evaluate(s.clone(), ctx, expr)?;42				if matches!(value, Val::Null) {43					Ok(None)44				} else {45					Ok(Some(IStr::from_untyped(value, s.clone())?))46				}47			},48		)?,49	})50}5152pub fn evaluate_comp(53	s: State,54	ctx: Context,55	specs: &[CompSpec],56	callback: &mut impl FnMut(Context) -> Result<()>,57) -> Result<()> {58	match specs.get(0) {59		None => callback(ctx)?,60		Some(CompSpec::IfSpec(IfSpecData(cond))) => {61			if bool::from_untyped(evaluate(s.clone(), ctx.clone(), cond)?, s.clone())? {62				evaluate_comp(s, ctx, &specs[1..], callback)?;63			}64		}65		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {66			match evaluate(s.clone(), ctx.clone(), expr)? {67				Val::Arr(list) => {68					for item in list.iter(s.clone()) {69						evaluate_comp(70							s.clone(),71							ctx.clone().with_var(var.clone(), item?.clone()),72							&specs[1..],73							callback,74						)?;75					}76				}77				_ => throw!(InComprehensionCanOnlyIterateOverArray),78			}79		}80	}81	Ok(())82}8384trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}8586fn evaluate_object_locals(87	fctx: Pending<Context>,88	locals: Rc<Vec<BindSpec>>,89) -> impl CloneableUnbound<Context> {90	#[derive(Trace, Clone)]91	struct UnboundLocals {92		fctx: Pending<Context>,93		locals: Rc<Vec<BindSpec>>,94	}95	impl CloneableUnbound<Context> for UnboundLocals {}96	impl Unbound for UnboundLocals {97		type Bound = Context;9899		fn bind(100			&self,101			_s: State,102			sup: Option<ObjValue>,103			this: Option<ObjValue>,104		) -> Result<Context> {105			let fctx = Context::new_future();106			let mut new_bindings = GcHashMap::new();107			for b in self.locals.iter() {108				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;109			}110111			let ctx = self.fctx.unwrap();112			let new_dollar = ctx.dollar().clone().or_else(|| this.clone());113114			let ctx = ctx115				.extend(new_bindings, new_dollar, sup, this)116				.into_future(fctx);117118			Ok(ctx)119		}120	}121122	UnboundLocals { fctx, locals }123}124125#[allow(clippy::too_many_lines)]126pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {127	let mut builder = ObjValueBuilder::new();128	let locals = Rc::new(129		members130			.iter()131			.filter_map(|m| match m {132				Member::BindStmt(bind) => Some(bind.clone()),133				_ => None,134			})135			.collect::<Vec<_>>(),136	);137138	let fctx = Context::new_future();139140	// We have single context for all fields, so we can cache binds141	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));142143	for member in members.iter() {144		match member {145			Member::Field(FieldMember {146				name,147				plus,148				params: None,149				visibility,150				value,151			}) => {152				#[derive(Trace)]153				struct UnboundValue<B: Trace> {154					uctx: B,155					value: LocExpr,156					name: IStr,157				}158				impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {159					type Bound = Thunk<Val>;160					fn bind(161						&self,162						s: State,163						sup: Option<ObjValue>,164						this: Option<ObjValue>,165					) -> Result<Thunk<Val>> {166						Ok(Thunk::evaluated(evaluate_named(167							s.clone(),168							self.uctx.bind(s, sup, this)?,169							&self.value,170							self.name.clone(),171						)?))172					}173				}174175				let name = evaluate_field_name(s.clone(), ctx.clone(), name)?;176				let name = if let Some(name) = name {177					name178				} else {179					continue;180				};181182				builder183					.member(name.clone())184					.with_add(*plus)185					.with_visibility(*visibility)186					.with_location(value.1.clone())187					.bindable(188						s.clone(),189						tb!(UnboundValue {190							uctx: uctx.clone(),191							value: value.clone(),192							name: name.clone()193						}),194					)?;195			}196			Member::Field(FieldMember {197				name,198				params: Some(params),199				value,200				..201			}) => {202				#[derive(Trace)]203				struct UnboundMethod<B: Trace> {204					uctx: B,205					value: LocExpr,206					params: ParamsDesc,207					name: IStr,208				}209				impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {210					type Bound = Thunk<Val>;211					fn bind(212						&self,213						s: State,214						sup: Option<ObjValue>,215						this: Option<ObjValue>,216					) -> Result<Thunk<Val>> {217						Ok(Thunk::evaluated(evaluate_method(218							self.uctx.bind(s, sup, this)?,219							self.name.clone(),220							self.params.clone(),221							self.value.clone(),222						)))223					}224				}225226				let name = if let Some(name) = evaluate_field_name(s.clone(), ctx.clone(), name)? {227					name228				} else {229					continue;230				};231232				builder233					.member(name.clone())234					.hide()235					.with_location(value.1.clone())236					.bindable(237						s.clone(),238						tb!(UnboundMethod {239							uctx: uctx.clone(),240							value: value.clone(),241							params: params.clone(),242							name: name.clone()243						}),244					)?;245			}246			Member::BindStmt(_) => {}247			Member::AssertStmt(stmt) => {248				#[derive(Trace)]249				struct ObjectAssert<B: Trace> {250					uctx: B,251					assert: AssertStmt,252				}253				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {254					fn run(255						&self,256						s: State,257						sup: Option<ObjValue>,258						this: Option<ObjValue>,259					) -> Result<()> {260						let ctx = self.uctx.bind(s.clone(), sup, this)?;261						evaluate_assert(s, ctx, &self.assert)262					}263				}264				builder.assert(tb!(ObjectAssert {265					uctx: uctx.clone(),266					assert: stmt.clone(),267				}));268			}269		}270	}271	let this = builder.build();272	let _ctx = ctx273		.extend(GcHashMap::new(), None, None, Some(this.clone()))274		.into_future(fctx);275	Ok(this)276}277278pub fn evaluate_object(s: State, ctx: Context, object: &ObjBody) -> Result<ObjValue> {279	Ok(match object {280		ObjBody::MemberList(members) => evaluate_member_list_object(s, ctx, members)?,281		ObjBody::ObjComp(obj) => {282			let mut builder = ObjValueBuilder::new();283			let locals = Rc::new(284				obj.pre_locals285					.iter()286					.chain(obj.post_locals.iter())287					.cloned()288					.collect::<Vec<_>>(),289			);290			let mut ctxs = vec![];291			evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {292				let key = evaluate(s.clone(), ctx.clone(), &obj.key)?;293				let fctx = Context::new_future();294				ctxs.push((ctx, fctx.clone()));295				let uctx = evaluate_object_locals(fctx, locals.clone());296297				match key {298					Val::Null => {}299					Val::Str(n) => {300						#[derive(Trace)]301						struct UnboundValue<B: Trace> {302							uctx: B,303							value: LocExpr,304						}305						impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {306							type Bound = Thunk<Val>;307							fn bind(308								&self,309								s: State,310								sup: Option<ObjValue>,311								this: Option<ObjValue>,312							) -> Result<Thunk<Val>> {313								Ok(Thunk::evaluated(evaluate(314									s.clone(),315									self.uctx.bind(s, sup, this.clone())?.extend(316										GcHashMap::new(),317										None,318										None,319										this,320									),321									&self.value,322								)?))323							}324						}325						builder326							.member(n)327							.with_location(obj.value.1.clone())328							.with_add(obj.plus)329							.bindable(330								s.clone(),331								tb!(UnboundValue {332									uctx,333									value: obj.value.clone(),334								}),335							)?;336					}337					v => throw!(FieldMustBeStringGot(v.value_type())),338				}339340				Ok(())341			})?;342343			let this = builder.build();344			for (ctx, fctx) in ctxs {345				let _ctx = ctx346					.extend(GcHashMap::new(), None, None, Some(this.clone()))347					.into_future(fctx);348			}349			this350		}351	})352}353354pub fn evaluate_apply(355	s: State,356	ctx: Context,357	value: &LocExpr,358	args: &ArgsDesc,359	loc: CallLocation,360	tailstrict: bool,361) -> Result<Val> {362	let value = evaluate(s.clone(), ctx.clone(), value)?;363	Ok(match value {364		Val::Func(f) => {365			let body = || f.evaluate(s.clone(), ctx, loc, args, tailstrict);366			if tailstrict {367				body()?368			} else {369				s.push(loc, || format!("function <{}> call", f.name()), body)?370			}371		}372		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),373	})374}375376pub fn evaluate_assert(s: State, ctx: Context, assertion: &AssertStmt) -> Result<()> {377	let value = &assertion.0;378	let msg = &assertion.1;379	let assertion_result = s.push(380		CallLocation::new(&value.1),381		|| "assertion condition".to_owned(),382		|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),383	)?;384	if !assertion_result {385		s.push(386			CallLocation::new(&value.1),387			|| "assertion failure".to_owned(),388			|| {389				if let Some(msg) = msg {390					throw!(AssertionFailed(391						evaluate(s.clone(), ctx, msg)?.to_string(s.clone())?392					));393				}394				throw!(AssertionFailed(Val::Null.to_string(s.clone())?));395			},396		)?;397	}398	Ok(())399}400401pub fn evaluate_named(s: State, ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {402	use Expr::*;403	let LocExpr(raw_expr, _loc) = expr;404	Ok(match &**raw_expr {405		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),406		_ => evaluate(s, ctx, expr)?,407	})408}409410#[allow(clippy::too_many_lines)]411pub fn evaluate(s: State, ctx: Context, expr: &LocExpr) -> Result<Val> {412	use Expr::*;413	let LocExpr(expr, loc) = expr;414	// let bp = with_state(|s| s.0.stop_at.borrow().clone());415	Ok(match &**expr {416		Literal(LiteralType::This) => {417			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)418		}419		Literal(LiteralType::Super) => Val::Obj(420			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(421				ctx.this()422					.clone()423					.expect("if super exists - then this should to"),424			),425		),426		Literal(LiteralType::Dollar) => {427			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)428		}429		Literal(LiteralType::True) => Val::Bool(true),430		Literal(LiteralType::False) => Val::Bool(false),431		Literal(LiteralType::Null) => Val::Null,432		Parened(e) => evaluate(s, ctx, e)?,433		Str(v) => Val::Str(v.clone()),434		Num(v) => Val::new_checked_num(*v)?,435		BinaryOp(v1, o, v2) => evaluate_binary_op_special(s, ctx, v1, *o, v2)?,436		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?,437		Var(name) => s.push(438			CallLocation::new(loc),439			|| format!("variable <{}> access", name),440			|| ctx.binding(name.clone())?.evaluate(s.clone()),441		)?,442		Index(value, index) => {443			match (444				evaluate(s.clone(), ctx.clone(), value)?,445				evaluate(s.clone(), ctx, index)?,446			) {447				(Val::Obj(v), Val::Str(key)) => s.push(448					CallLocation::new(loc),449					|| format!("field <{}> access", key),450					|| match v.get(s.clone(), key.clone()) {451						Ok(Some(v)) => Ok(v),452						#[cfg(not(feature = "friendly-errors"))]453						Ok(None) => throw!(NoSuchField(key.clone(), vec![])),454						#[cfg(feature = "friendly-errors")]455						Ok(None) => {456							let mut heap = Vec::new();457							for field in v.fields_ex(458								true,459								#[cfg(feature = "exp-preserve-order")]460								false,461							) {462								let conf = strsim::jaro_winkler(&field as &str, &key as &str);463								if conf < 0.8 {464									continue;465								}466								heap.push((conf, field));467							}468							heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));469470							throw!(NoSuchField(471								key.clone(),472								heap.into_iter().map(|(_, v)| v).collect()473							))474						}475						Err(e) => Err(e),476					},477				)?,478				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(479					ValType::Obj,480					ValType::Str,481					n.value_type(),482				)),483484				(Val::Arr(v), Val::Num(n)) => {485					if n.fract() > f64::EPSILON {486						throw!(FractionalIndex)487					}488					v.get(s, n as usize)?489						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?490				}491				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),492				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(493					ValType::Arr,494					ValType::Num,495					n.value_type(),496				)),497498				(Val::Str(s), Val::Num(n)) => Val::Str({499					let v: IStr = s500						.chars()501						.skip(n as usize)502						.take(1)503						.collect::<String>()504						.into();505					if v.is_empty() {506						let size = s.chars().count();507						throw!(StringBoundsError(n as usize, size))508					}509					v510				}),511				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(512					ValType::Str,513					ValType::Num,514					n.value_type(),515				)),516517				(v, _) => throw!(CantIndexInto(v.value_type())),518			}519		}520		LocalExpr(bindings, returned) => {521			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =522				GcHashMap::with_capacity(bindings.len());523			let fctx = Context::new_future();524			for b in bindings {525				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;526			}527			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);528			evaluate(s, ctx, &returned.clone())?529		}530		Arr(items) => {531			let mut out = Vec::with_capacity(items.len());532			for item in items {533				// TODO: Implement ArrValue::Lazy with same context for every element?534				#[derive(Trace)]535				struct ArrayElement {536					ctx: Context,537					item: LocExpr,538				}539				impl ThunkValue for ArrayElement {540					type Output = Val;541					fn get(self: Box<Self>, s: State) -> Result<Val> {542						evaluate(s, self.ctx, &self.item)543					}544				}545				out.push(Thunk::new(tb!(ArrayElement {546					ctx: ctx.clone(),547					item: item.clone(),548				})));549			}550			Val::Arr(out.into())551		}552		ArrComp(expr, comp_specs) => {553			let mut out = Vec::new();554			evaluate_comp(s.clone(), ctx, comp_specs, &mut |ctx| {555				out.push(evaluate(s.clone(), ctx, expr)?);556				Ok(())557			})?;558			Val::Arr(ArrValue::Eager(Cc::new(out)))559		}560		Obj(body) => Val::Obj(evaluate_object(s, ctx, body)?),561		ObjExtend(a, b) => evaluate_add_op(562			s.clone(),563			&evaluate(s.clone(), ctx.clone(), a)?,564			&Val::Obj(evaluate_object(s, ctx, b)?),565		)?,566		Apply(value, args, tailstrict) => {567			evaluate_apply(s, ctx, value, args, CallLocation::new(loc), *tailstrict)?568		}569		Function(params, body) => {570			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())571		}572		AssertExpr(assert, returned) => {573			evaluate_assert(s.clone(), ctx.clone(), assert)?;574			evaluate(s, ctx, returned)?575		}576		ErrorStmt(e) => s.push(577			CallLocation::new(loc),578			|| "error statement".to_owned(),579			|| {580				throw!(RuntimeError(581					evaluate(s.clone(), ctx, e)?.to_string(s.clone())?,582				))583			},584		)?,585		IfElse {586			cond,587			cond_then,588			cond_else,589		} => {590			if s.push(591				CallLocation::new(loc),592				|| "if condition".to_owned(),593				|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), &cond.0)?, s.clone()),594			)? {595				evaluate(s, ctx, cond_then)?596			} else {597				match cond_else {598					Some(v) => evaluate(s, ctx, v)?,599					None => Val::Null,600				}601			}602		}603		Slice(value, desc) => {604			fn parse_idx<T: Typed>(605				loc: CallLocation,606				s: State,607				ctx: &Context,608				expr: &Option<LocExpr>,609				desc: &'static str,610			) -> Result<Option<T>> {611				if let Some(value) = expr {612					Ok(Some(s.push(613						loc,614						|| format!("slice {}", desc),615						|| T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),616					)?))617				} else {618					Ok(None)619				}620			}621622			let indexable = evaluate(s.clone(), ctx.clone(), value)?;623			let loc = CallLocation::new(loc);624625			let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;626			let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;627			let step = parse_idx(loc, s.clone(), &ctx, &desc.step, "step")?;628629			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?, s)?630		}631		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {632			let tmp = loc.clone().0;633			let import_location = tmp634				.path()635				.map(|p| {636					let mut p = p.to_owned();637					p.pop();638					p639				})640				.unwrap_or_default();641			let resolved_path = s.resolve_file(&import_location, path as &str)?;642			match i {643				Import(_) => s.push(644					CallLocation::new(loc),645					|| format!("import {:?}", path.clone()),646					|| s.import_resolved(resolved_path),647				)?,648				ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),649				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),650				_ => unreachable!(),651			}652		}653	})654}
after · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use crate::{12	destructure::evaluate_dest,13	error::Error::*,14	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},15	function::{CallLocation, FuncDesc, FuncVal},16	tb, throw,17	typed::Typed,18	val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},19	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,20	Unbound, Val,21};22pub mod destructure;23pub mod operator;2425pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {26	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {27		name,28		ctx,29		params,30		body,31	})))32}3334pub fn evaluate_field_name(s: State, ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {35	Ok(match field_name {36		FieldName::Fixed(n) => Some(n.clone()),37		FieldName::Dyn(expr) => s.push(38			CallLocation::new(&expr.1),39			|| "evaluating field name".to_string(),40			|| {41				let value = evaluate(s.clone(), ctx, expr)?;42				if matches!(value, Val::Null) {43					Ok(None)44				} else {45					Ok(Some(IStr::from_untyped(value, s.clone())?))46				}47			},48		)?,49	})50}5152pub fn evaluate_comp(53	s: State,54	ctx: Context,55	specs: &[CompSpec],56	callback: &mut impl FnMut(Context) -> Result<()>,57) -> Result<()> {58	match specs.get(0) {59		None => callback(ctx)?,60		Some(CompSpec::IfSpec(IfSpecData(cond))) => {61			if bool::from_untyped(evaluate(s.clone(), ctx.clone(), cond)?, s.clone())? {62				evaluate_comp(s, ctx, &specs[1..], callback)?;63			}64		}65		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {66			match evaluate(s.clone(), ctx.clone(), expr)? {67				Val::Arr(list) => {68					for item in list.iter(s.clone()) {69						evaluate_comp(70							s.clone(),71							ctx.clone().with_var(var.clone(), item?.clone()),72							&specs[1..],73							callback,74						)?;75					}76				}77				_ => throw!(InComprehensionCanOnlyIterateOverArray),78			}79		}80	}81	Ok(())82}8384trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}8586fn evaluate_object_locals(87	fctx: Pending<Context>,88	locals: Rc<Vec<BindSpec>>,89) -> impl CloneableUnbound<Context> {90	#[derive(Trace, Clone)]91	struct UnboundLocals {92		fctx: Pending<Context>,93		locals: Rc<Vec<BindSpec>>,94	}95	impl CloneableUnbound<Context> for UnboundLocals {}96	impl Unbound for UnboundLocals {97		type Bound = Context;9899		fn bind(100			&self,101			_s: State,102			sup: Option<ObjValue>,103			this: Option<ObjValue>,104		) -> Result<Context> {105			let fctx = Context::new_future();106			let mut new_bindings = GcHashMap::new();107			for b in self.locals.iter() {108				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;109			}110111			let ctx = self.fctx.unwrap();112			let new_dollar = ctx.dollar().clone().or_else(|| this.clone());113114			let ctx = ctx115				.extend(new_bindings, new_dollar, sup, this)116				.into_future(fctx);117118			Ok(ctx)119		}120	}121122	UnboundLocals { fctx, locals }123}124125#[allow(clippy::too_many_lines)]126pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {127	let mut builder = ObjValueBuilder::new();128	let locals = Rc::new(129		members130			.iter()131			.filter_map(|m| match m {132				Member::BindStmt(bind) => Some(bind.clone()),133				_ => None,134			})135			.collect::<Vec<_>>(),136	);137138	let fctx = Context::new_future();139140	// We have single context for all fields, so we can cache binds141	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));142143	for member in members.iter() {144		match member {145			Member::Field(FieldMember {146				name,147				plus,148				params: None,149				visibility,150				value,151			}) => {152				#[derive(Trace)]153				struct UnboundValue<B: Trace> {154					uctx: B,155					value: LocExpr,156					name: IStr,157				}158				impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {159					type Bound = Thunk<Val>;160					fn bind(161						&self,162						s: State,163						sup: Option<ObjValue>,164						this: Option<ObjValue>,165					) -> Result<Thunk<Val>> {166						Ok(Thunk::evaluated(evaluate_named(167							s.clone(),168							self.uctx.bind(s, sup, this)?,169							&self.value,170							self.name.clone(),171						)?))172					}173				}174175				let name = evaluate_field_name(s.clone(), ctx.clone(), name)?;176				let name = if let Some(name) = name {177					name178				} else {179					continue;180				};181182				builder183					.member(name.clone())184					.with_add(*plus)185					.with_visibility(*visibility)186					.with_location(value.1.clone())187					.bindable(188						s.clone(),189						tb!(UnboundValue {190							uctx: uctx.clone(),191							value: value.clone(),192							name: name.clone()193						}),194					)?;195			}196			Member::Field(FieldMember {197				name,198				params: Some(params),199				value,200				..201			}) => {202				#[derive(Trace)]203				struct UnboundMethod<B: Trace> {204					uctx: B,205					value: LocExpr,206					params: ParamsDesc,207					name: IStr,208				}209				impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {210					type Bound = Thunk<Val>;211					fn bind(212						&self,213						s: State,214						sup: Option<ObjValue>,215						this: Option<ObjValue>,216					) -> Result<Thunk<Val>> {217						Ok(Thunk::evaluated(evaluate_method(218							self.uctx.bind(s, sup, this)?,219							self.name.clone(),220							self.params.clone(),221							self.value.clone(),222						)))223					}224				}225226				let name = if let Some(name) = evaluate_field_name(s.clone(), ctx.clone(), name)? {227					name228				} else {229					continue;230				};231232				builder233					.member(name.clone())234					.hide()235					.with_location(value.1.clone())236					.bindable(237						s.clone(),238						tb!(UnboundMethod {239							uctx: uctx.clone(),240							value: value.clone(),241							params: params.clone(),242							name: name.clone()243						}),244					)?;245			}246			Member::BindStmt(_) => {}247			Member::AssertStmt(stmt) => {248				#[derive(Trace)]249				struct ObjectAssert<B: Trace> {250					uctx: B,251					assert: AssertStmt,252				}253				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {254					fn run(255						&self,256						s: State,257						sup: Option<ObjValue>,258						this: Option<ObjValue>,259					) -> Result<()> {260						let ctx = self.uctx.bind(s.clone(), sup, this)?;261						evaluate_assert(s, ctx, &self.assert)262					}263				}264				builder.assert(tb!(ObjectAssert {265					uctx: uctx.clone(),266					assert: stmt.clone(),267				}));268			}269		}270	}271	let this = builder.build();272	let _ctx = ctx273		.extend(GcHashMap::new(), None, None, Some(this.clone()))274		.into_future(fctx);275	Ok(this)276}277278pub fn evaluate_object(s: State, ctx: Context, object: &ObjBody) -> Result<ObjValue> {279	Ok(match object {280		ObjBody::MemberList(members) => evaluate_member_list_object(s, ctx, members)?,281		ObjBody::ObjComp(obj) => {282			let mut builder = ObjValueBuilder::new();283			let locals = Rc::new(284				obj.pre_locals285					.iter()286					.chain(obj.post_locals.iter())287					.cloned()288					.collect::<Vec<_>>(),289			);290			let mut ctxs = vec![];291			evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {292				let key = evaluate(s.clone(), ctx.clone(), &obj.key)?;293				let fctx = Context::new_future();294				ctxs.push((ctx, fctx.clone()));295				let uctx = evaluate_object_locals(fctx, locals.clone());296297				match key {298					Val::Null => {}299					Val::Str(n) => {300						#[derive(Trace)]301						struct UnboundValue<B: Trace> {302							uctx: B,303							value: LocExpr,304						}305						impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {306							type Bound = Thunk<Val>;307							fn bind(308								&self,309								s: State,310								sup: Option<ObjValue>,311								this: Option<ObjValue>,312							) -> Result<Thunk<Val>> {313								Ok(Thunk::evaluated(evaluate(314									s.clone(),315									self.uctx.bind(s, sup, this.clone())?.extend(316										GcHashMap::new(),317										None,318										None,319										this,320									),321									&self.value,322								)?))323							}324						}325						builder326							.member(n)327							.with_location(obj.value.1.clone())328							.with_add(obj.plus)329							.bindable(330								s.clone(),331								tb!(UnboundValue {332									uctx,333									value: obj.value.clone(),334								}),335							)?;336					}337					v => throw!(FieldMustBeStringGot(v.value_type())),338				}339340				Ok(())341			})?;342343			let this = builder.build();344			for (ctx, fctx) in ctxs {345				let _ctx = ctx346					.extend(GcHashMap::new(), None, None, Some(this.clone()))347					.into_future(fctx);348			}349			this350		}351	})352}353354pub fn evaluate_apply(355	s: State,356	ctx: Context,357	value: &LocExpr,358	args: &ArgsDesc,359	loc: CallLocation,360	tailstrict: bool,361) -> Result<Val> {362	let value = evaluate(s.clone(), ctx.clone(), value)?;363	Ok(match value {364		Val::Func(f) => {365			let body = || f.evaluate(s.clone(), ctx, loc, args, tailstrict);366			if tailstrict {367				body()?368			} else {369				s.push(loc, || format!("function <{}> call", f.name()), body)?370			}371		}372		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),373	})374}375376pub fn evaluate_assert(s: State, ctx: Context, assertion: &AssertStmt) -> Result<()> {377	let value = &assertion.0;378	let msg = &assertion.1;379	let assertion_result = s.push(380		CallLocation::new(&value.1),381		|| "assertion condition".to_owned(),382		|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),383	)?;384	if !assertion_result {385		s.push(386			CallLocation::new(&value.1),387			|| "assertion failure".to_owned(),388			|| {389				if let Some(msg) = msg {390					throw!(AssertionFailed(391						evaluate(s.clone(), ctx, msg)?.to_string(s.clone())?392					));393				}394				throw!(AssertionFailed(Val::Null.to_string(s.clone())?));395			},396		)?;397	}398	Ok(())399}400401pub fn evaluate_named(s: State, ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {402	use Expr::*;403	let LocExpr(raw_expr, _loc) = expr;404	Ok(match &**raw_expr {405		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),406		_ => evaluate(s, ctx, expr)?,407	})408}409410#[allow(clippy::too_many_lines)]411pub fn evaluate(s: State, ctx: Context, expr: &LocExpr) -> Result<Val> {412	use Expr::*;413	let LocExpr(expr, loc) = expr;414	// let bp = with_state(|s| s.0.stop_at.borrow().clone());415	Ok(match &**expr {416		Literal(LiteralType::This) => {417			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)418		}419		Literal(LiteralType::Super) => Val::Obj(420			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(421				ctx.this()422					.clone()423					.expect("if super exists - then this should to"),424			),425		),426		Literal(LiteralType::Dollar) => {427			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)428		}429		Literal(LiteralType::True) => Val::Bool(true),430		Literal(LiteralType::False) => Val::Bool(false),431		Literal(LiteralType::Null) => Val::Null,432		Parened(e) => evaluate(s, ctx, e)?,433		Str(v) => Val::Str(v.clone()),434		Num(v) => Val::new_checked_num(*v)?,435		BinaryOp(v1, o, v2) => evaluate_binary_op_special(s, ctx, v1, *o, v2)?,436		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(s, ctx, v)?)?,437		Var(name) => s.push(438			CallLocation::new(loc),439			|| format!("variable <{}> access", name),440			|| ctx.binding(name.clone())?.evaluate(s.clone()),441		)?,442		Index(value, index) => {443			match (444				evaluate(s.clone(), ctx.clone(), value)?,445				evaluate(s.clone(), ctx, index)?,446			) {447				(Val::Obj(v), Val::Str(key)) => s.push(448					CallLocation::new(loc),449					|| format!("field <{}> access", key),450					|| match v.get(s.clone(), key.clone()) {451						Ok(Some(v)) => Ok(v),452						#[cfg(not(feature = "friendly-errors"))]453						Ok(None) => throw!(NoSuchField(key.clone(), vec![])),454						#[cfg(feature = "friendly-errors")]455						Ok(None) => {456							let mut heap = Vec::new();457							for field in v.fields_ex(458								true,459								#[cfg(feature = "exp-preserve-order")]460								false,461							) {462								let conf = strsim::jaro_winkler(&field as &str, &key as &str);463								if conf < 0.8 {464									continue;465								}466								heap.push((conf, field));467							}468							heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));469470							throw!(NoSuchField(471								key.clone(),472								heap.into_iter().map(|(_, v)| v).collect()473							))474						}475						Err(e) => Err(e),476					},477				)?,478				(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(479					ValType::Obj,480					ValType::Str,481					n.value_type(),482				)),483484				(Val::Arr(v), Val::Num(n)) => {485					if n.fract() > f64::EPSILON {486						throw!(FractionalIndex)487					}488					v.get(s, n as usize)?489						.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?490				}491				(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),492				(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(493					ValType::Arr,494					ValType::Num,495					n.value_type(),496				)),497498				(Val::Str(s), Val::Num(n)) => Val::Str({499					let v: IStr = s500						.chars()501						.skip(n as usize)502						.take(1)503						.collect::<String>()504						.into();505					if v.is_empty() {506						let size = s.chars().count();507						throw!(StringBoundsError(n as usize, size))508					}509					v510				}),511				(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(512					ValType::Str,513					ValType::Num,514					n.value_type(),515				)),516517				(v, _) => throw!(CantIndexInto(v.value_type())),518			}519		}520		LocalExpr(bindings, returned) => {521			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =522				GcHashMap::with_capacity(bindings.len());523			let fctx = Context::new_future();524			for b in bindings {525				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;526			}527			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);528			evaluate(s, ctx, &returned.clone())?529		}530		Arr(items) => {531			let mut out = Vec::with_capacity(items.len());532			for item in items {533				// TODO: Implement ArrValue::Lazy with same context for every element?534				#[derive(Trace)]535				struct ArrayElement {536					ctx: Context,537					item: LocExpr,538				}539				impl ThunkValue for ArrayElement {540					type Output = Val;541					fn get(self: Box<Self>, s: State) -> Result<Val> {542						evaluate(s, self.ctx, &self.item)543					}544				}545				out.push(Thunk::new(tb!(ArrayElement {546					ctx: ctx.clone(),547					item: item.clone(),548				})));549			}550			Val::Arr(out.into())551		}552		ArrComp(expr, comp_specs) => {553			let mut out = Vec::new();554			evaluate_comp(s.clone(), ctx, comp_specs, &mut |ctx| {555				out.push(evaluate(s.clone(), ctx, expr)?);556				Ok(())557			})?;558			Val::Arr(ArrValue::Eager(Cc::new(out)))559		}560		Obj(body) => Val::Obj(evaluate_object(s, ctx, body)?),561		ObjExtend(a, b) => evaluate_add_op(562			s.clone(),563			&evaluate(s.clone(), ctx.clone(), a)?,564			&Val::Obj(evaluate_object(s, ctx, b)?),565		)?,566		Apply(value, args, tailstrict) => {567			evaluate_apply(s, ctx, value, args, CallLocation::new(loc), *tailstrict)?568		}569		Function(params, body) => {570			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())571		}572		AssertExpr(assert, returned) => {573			evaluate_assert(s.clone(), ctx.clone(), assert)?;574			evaluate(s, ctx, returned)?575		}576		ErrorStmt(e) => s.push(577			CallLocation::new(loc),578			|| "error statement".to_owned(),579			|| {580				throw!(RuntimeError(581					evaluate(s.clone(), ctx, e)?.to_string(s.clone())?,582				))583			},584		)?,585		IfElse {586			cond,587			cond_then,588			cond_else,589		} => {590			if s.push(591				CallLocation::new(loc),592				|| "if condition".to_owned(),593				|| bool::from_untyped(evaluate(s.clone(), ctx.clone(), &cond.0)?, s.clone()),594			)? {595				evaluate(s, ctx, cond_then)?596			} else {597				match cond_else {598					Some(v) => evaluate(s, ctx, v)?,599					None => Val::Null,600				}601			}602		}603		Slice(value, desc) => {604			fn parse_idx<T: Typed>(605				loc: CallLocation,606				s: State,607				ctx: &Context,608				expr: &Option<LocExpr>,609				desc: &'static str,610			) -> Result<Option<T>> {611				if let Some(value) = expr {612					Ok(Some(s.push(613						loc,614						|| format!("slice {}", desc),615						|| T::from_untyped(evaluate(s.clone(), ctx.clone(), value)?, s.clone()),616					)?))617				} else {618					Ok(None)619				}620			}621622			let indexable = evaluate(s.clone(), ctx.clone(), value)?;623			let loc = CallLocation::new(loc);624625			let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;626			let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;627			let step = parse_idx(loc, s.clone(), &ctx, &desc.step, "step")?;628629			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?, s)?630		}631		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {632			let tmp = loc.clone().0;633			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;634			match i {635				Import(_) => s.push(636					CallLocation::new(loc),637					|| format!("import {:?}", path.clone()),638					|| s.import_resolved(resolved_path),639				)?,640				ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),641				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),642				_ => unreachable!(),643			}644		}645	})646}
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,51 +1,60 @@
 use std::{
 	any::Any,
+	cell::RefCell,
+	env::current_dir,
 	fs,
-	io::Read,
+	io::{ErrorKind, Read},
 	path::{Path, PathBuf},
 };
 
 use fs::File;
-use jrsonnet_parser::SourcePath;
+use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
 
 use crate::{
-	error::{Error::*, Result},
+	error::{
+		Error::{self, *},
+		Result,
+	},
 	throw,
 };
 
 /// Implements file resolution logic for `import` and `importStr`
 pub trait ImportResolver {
-	/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
+	/// Resolves file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
 	/// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
 	/// where `${vendor}` is a library path.
-	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath>;
+	///
+	/// `from` should only be returned from [`ImportResolver::resolve`], or from other defined file, any other value
+	/// may result in panic
+	fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+		throw!(ImportNotSupported(from.clone(), path.into()))
+	}
+	fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+		self.resolve_from(&SourcePath::default(), path)
+	}
+	/// Resolves absolute path, doesn't supports jpath and other fancy things
+	fn resolve(&self, path: &Path) -> Result<SourcePath> {
+		throw!(AbsoluteImportNotSupported(path.to_owned()))
+	}
 
 	/// Load resolved file
-	/// This should only be called with value returned from `resolve_file`, this cannot be resolved using associated type,
-	/// as evaluator uses object instead of generic for [`ImportResolver`]
+	/// This should only be called with value returned from [`ImportResolver::resolve_file`]/[`ImportResolver::resolve`],
+	/// this cannot be resolved using associated type, as evaluator uses object instead of generic for [`ImportResolver`]
 	fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
 
-	/// # Safety
-	///
-	/// For use only in bindings, should not be used elsewhere.
-	/// Implementations which are not intended to be used in bindings
-	/// should panic on call to this method.
-	unsafe fn as_any(&self) -> &dyn Any;
+	/// For downcasts
+	fn as_any(&self) -> &dyn Any;
 }
 
 /// Dummy resolver, can't resolve/load any file
 pub struct DummyImportResolver;
 impl ImportResolver for DummyImportResolver {
-	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
-		throw!(ImportNotSupported(from.into(), path.into()))
-	}
-
 	fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {
 		panic!("dummy resolver can't load any file")
 	}
 
-	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("`as_any(&self)` is not supported by dummy resolver")
+	fn as_any(&self) -> &dyn Any {
+		self
 	}
 }
 #[allow(clippy::use_self)]
@@ -60,36 +69,82 @@
 pub struct FileImportResolver {
 	/// Library directories to search for file.
 	/// Referred to as `jpath` in original jsonnet implementation.
-	pub library_paths: Vec<PathBuf>,
+	library_paths: RefCell<Vec<PathBuf>>,
 }
+impl FileImportResolver {
+	pub fn new(jpath: Vec<PathBuf>) -> Self {
+		Self {
+			library_paths: RefCell::new(jpath),
+		}
+	}
+	/// Dynamically add new jpath, used by bindings
+	pub fn add_jpath(&self, path: PathBuf) {
+		self.library_paths.borrow_mut().push(path);
+	}
+}
 impl ImportResolver for FileImportResolver {
-	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
-		let mut direct = from.to_path_buf();
+	fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+		let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
+			let mut o = f.path().to_owned();
+			o.pop();
+			o
+		} else if let Some(d) = from.downcast_ref::<SourceDirectory>() {
+			d.path().to_owned()
+		} else if from.is_default() {
+			current_dir().map_err(|e| Error::ImportIo(e.to_string()))?
+		} else {
+			unreachable!("resolver can't return this path")
+		};
 		direct.push(path);
-		if direct.exists() {
-			Ok(SourcePath::Path(
+		if direct.is_file() {
+			Ok(SourcePath::new(SourceFile::new(
 				direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-			))
+			)))
 		} else {
-			for library_path in &self.library_paths {
+			for library_path in self.library_paths.borrow().iter() {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(SourcePath::Path(
+					return Ok(SourcePath::new(SourceFile::new(
 						cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
-					));
+					)));
 				}
 			}
-			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
+			throw!(ImportFileNotFound(from.clone(), path.to_owned()))
+		}
+	}
+	fn resolve(&self, path: &Path) -> Result<SourcePath> {
+		let meta = match fs::metadata(path) {
+			Ok(v) => v,
+			Err(e) if e.kind() == ErrorKind::NotFound => {
+				throw!(AbsoluteImportFileNotFound(path.to_owned()))
+			}
+			Err(e) => throw!(Error::ImportIo(e.to_string())),
+		};
+		if meta.is_file() {
+			Ok(SourcePath::new(SourceFile::new(
+				path.canonicalize()
+					.map_err(|e| ImportIo(e.to_string()))?
+					.to_owned(),
+			)))
+		} else if meta.is_dir() {
+			Ok(SourcePath::new(SourceDirectory::new(
+				path.canonicalize()
+					.map_err(|e| ImportIo(e.to_string()))?
+					.to_owned(),
+			)))
+		} else {
+			unreachable!("this can't be a symlink")
 		}
 	}
 
 	fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
-		let path = match id {
-			SourcePath::Path(path) => path,
-			_ => {
-				panic!("this resolver can only resolve to path")
-			}
+		let path = if let Some(f) = id.downcast_ref::<SourceFile>() {
+			f.path()
+		} else if id.downcast_ref::<SourceDirectory>().is_some() || id.is_default() {
+			throw!(Error::ImportIsADirectory(id.clone()))
+		} else {
+			unreachable!("other types are not supported in resolve");
 		};
 		let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
 		let mut out = Vec::new();
@@ -97,7 +152,12 @@
 			.map_err(|e| ImportIo(e.to_string()))?;
 		Ok(out)
 	}
-	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("this resolver can't be used as any")
+
+	fn as_any(&self) -> &dyn Any {
+		self
+	}
+
+	fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+		self.resolve_from(&SourcePath::default(), path)
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -44,7 +44,6 @@
 
 use std::{
 	any::Any,
-	borrow::Cow,
 	cell::{Ref, RefCell, RefMut},
 	collections::HashMap,
 	fmt::{self, Debug},
@@ -103,12 +102,7 @@
 pub trait ContextInitializer {
 	fn initialize(&self, state: State, for_file: Source) -> Context;
 
-	/// # Safety
-	///
-	/// For use only in bindings, should not be used elsewhere.
-	/// Implementations which are not intended to be used in bindings
-	/// should panic on call to this method.
-	unsafe fn as_any(&self) -> &dyn Any;
+	fn as_any(&self) -> &dyn Any;
 }
 
 /// Context initializer, which adds noth
@@ -117,8 +111,8 @@
 	fn initialize(&self, _state: State, _for_file: Source) -> Context {
 		Context::default()
 	}
-	unsafe fn as_any(&self) -> &dyn Any {
-		panic!("`as_any(&self)` is not supported by dummy initializer")
+	fn as_any(&self) -> &dyn Any {
+		self
 	}
 }
 
@@ -343,8 +337,7 @@
 			);
 		}
 		let code = file.string.as_ref().expect("just set");
-		let file_name =
-			Source::new(path.clone(), code.clone()).expect("resolver should return correct name");
+		let file_name = Source::new(path.clone(), code.clone());
 		if file.parsed.is_none() {
 			file.parsed = Some(
 				jrsonnet_parser::parse(
@@ -388,8 +381,14 @@
 			Err(e) => Err(e),
 		}
 	}
-	pub fn import(&self, from: &Path, path: &str) -> Result<Val> {
-		let resolved = self.resolve_file(from, path)?;
+
+	/// Has same semantics as `import 'path'` called from `from` file
+	pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {
+		let resolved = self.resolve_from(from, path)?;
+		self.import_resolved(resolved)
+	}
+	pub fn import(&self, path: &impl AsRef<Path>) -> Result<Val> {
+		let resolved = self.resolve(path)?;
 		self.import_resolved(resolved)
 	}
 
@@ -532,7 +531,7 @@
 					func.evaluate(
 						self.clone(),
 						self.create_default_context(Source::new_virtual(
-							Cow::Borrowed("<tla>"),
+							"<tla>".into(),
 							IStr::empty(),
 						)),
 						CallLocation::native(),
@@ -565,9 +564,9 @@
 /// Raw methods evaluate passed values but don't perform TLA execution
 impl State {
 	/// Parses and evaluates the given snippet
-	pub fn evaluate_snippet(&self, name: String, code: impl Into<IStr>) -> Result<Val> {
+	pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {
 		let code = code.into();
-		let source = Source::new_virtual(Cow::Owned(name), code.clone());
+		let source = Source::new_virtual(name.into(), code.clone());
 		let parsed = jrsonnet_parser::parse(
 			&code,
 			&ParserSettings {
@@ -596,7 +595,7 @@
 	}
 	pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {
 		let source_name = format!("<top-level-arg:{}>", name);
-		let source = Source::new_virtual(Cow::Owned(source_name), code.into());
+		let source = Source::new_virtual(source_name.into(), code.into());
 		let parsed = jrsonnet_parser::parse(
 			code,
 			&ParserSettings {
@@ -613,12 +612,17 @@
 		Ok(())
 	}
 
-	pub fn resolve_file(&self, from: &Path, path: &str) -> Result<SourcePath> {
-		self.settings()
-			.import_resolver
-			.resolve_file_relative(from, path.as_ref())
+	// Only panics in case of [`ImportResolver`] contract violation
+	#[allow(clippy::missing_panics_doc)]
+	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+		self.import_resolver().resolve_from(from, path.as_ref())
 	}
 
+	// Only panics in case of [`ImportResolver`] contract violation
+	#[allow(clippy::missing_panics_doc)]
+	pub fn resolve(&self, path: &impl AsRef<Path>) -> Result<SourcePath> {
+		self.import_resolver().resolve(path.as_ref())
+	}
 	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
 		Ref::map(self.settings(), |s| &*s.import_resolver)
 	}
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -5,6 +5,7 @@
 use crate::{error::Error, LocError, State};
 
 /// The way paths should be displayed
+#[derive(Clone)]
 pub enum PathResolver {
 	/// Only filename
 	FileName,
@@ -15,6 +16,13 @@
 }
 
 impl PathResolver {
+	/// Will return Self::Relative(cwd), or Self::Absolute on cwd failure
+	pub fn new_cwd_fallback() -> Self {
+		match std::env::current_dir() {
+			Ok(v) => Self::Relative(v),
+			Err(_) => Self::Absolute,
+		}
+	}
 	pub fn resolve(&self, from: &Path) -> String {
 		match self {
 			Self::FileName => from
@@ -89,9 +97,9 @@
 			use std::fmt::Write;
 
 			writeln!(out)?;
-			let mut n = match path.path() {
+			let mut n = match path.source_path().path() {
 				Some(r) => self.resolver.resolve(r),
-				None => path.short_display().to_string(),
+				None => path.source_path().to_string(),
 			};
 			let mut offset = error.location.offset;
 			let is_eof = if offset >= path.code().len() {
@@ -122,9 +130,9 @@
 				use std::fmt::Write;
 				#[allow(clippy::option_if_let_else)]
 				if let Some(location) = location {
-					let mut resolved_path = match location.0.path() {
+					let mut resolved_path = match location.0.source_path().path() {
 						Some(r) => self.resolver.resolve(r),
-						None => location.0.short_display().to_string(),
+						None => location.0.source_path().to_string(),
 					};
 					// TODO: Process all trace elements first
 					let location = location.0.map_source_locations(&[location.1, location.2]);
@@ -177,9 +185,9 @@
 			let desc = &item.desc;
 			if let Some(source) = &item.location {
 				let start_end = source.0.map_source_locations(&[source.1, source.2]);
-				let resolved_path = match source.0.path() {
+				let resolved_path = match source.0.source_path().path() {
 					Some(r) => r.display().to_string(),
-					None => source.0.short_display().to_string(),
+					None => source.0.source_path().to_string(),
 				};
 
 				write!(
@@ -272,9 +280,9 @@
 			.take(end.line_end_offset - end.line_start_offset)
 			.collect();
 
-		let origin = match origin.path() {
+		let origin = match origin.source_path().path() {
 			Some(r) => self.resolver.resolve(r),
-			None => origin.short_display().to_string(),
+			None => origin.source_path().to_string(),
 		};
 		let snippet = Snippet {
 			opt: FormatOptions {