git.delta.rocks / jrsonnet / refs/commits / 4ad9956e5f9b

difftreelog

refactor keep source code alongside source path

Yaroslav Bolyukin2022-08-05parent: #68c8ac0.patch.diff
in: master

18 files changed

modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -16,6 +16,7 @@
 	error::{Error::*, Result},
 	throw, ImportResolver, State,
 };
+use jrsonnet_parser::SourcePath;
 
 pub type JsonnetImportCallback = unsafe extern "C" fn(
 	ctx: *mut c_void,
@@ -29,10 +30,10 @@
 pub struct CallbackImportResolver {
 	cb: JsonnetImportCallback,
 	ctx: *mut c_void,
-	out: RefCell<HashMap<PathBuf, Vec<u8>>>,
+	out: RefCell<HashMap<SourcePath, Vec<u8>>>,
 }
 impl ImportResolver for CallbackImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
 		let rel = CString::new(path).unwrap().into_raw();
 		let found_here: *mut c_char = null_mut();
@@ -61,7 +62,7 @@
 		}
 
 		let found_here_raw = unsafe { CStr::from_ptr(found_here) };
-		let found_here_buf = PathBuf::from(found_here_raw.to_str().unwrap());
+		let found_here_buf = SourcePath::Path(PathBuf::from(found_here_raw.to_str().unwrap()));
 		unsafe {
 			let _ = CString::from_raw(found_here);
 		}
@@ -74,7 +75,7 @@
 
 		Ok(found_here_buf)
 	}
-	fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
+	fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>> {
 		Ok(self.out.borrow().get(resolved).unwrap().clone())
 	}
 
@@ -108,24 +109,28 @@
 	}
 }
 impl ImportResolver for NativeImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		let mut new_path = from.to_owned();
 		new_path.push(path);
 		if new_path.exists() {
-			Ok(new_path)
+			Ok(SourcePath::Path(new_path))
 		} else {
 			for library_path in self.library_paths.borrow().iter() {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(cloned);
+					return Ok(SourcePath::Path(cloned));
 				}
 			}
 			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
-	fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
-		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
+	fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
+		let path = match id {
+			SourcePath::Path(path) => path,
+			_ => unreachable!("NativeImportResolver::resolve_file may only return plain paths"),
+		};
+		let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
 		let mut out = Vec::new();
 		file.read_to_end(&mut out)
 			.map_err(|e| ImportIo(e.to_string()))?;
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -10,9 +10,9 @@
 
 use std::{
 	alloc::Layout,
+	env,
 	ffi::{CStr, CString},
 	os::raw::{c_char, c_double, c_int, c_uint},
-	path::PathBuf,
 };
 
 use import::NativeImportResolver;
@@ -112,7 +112,10 @@
 ) -> *const c_char {
 	let filename = CStr::from_ptr(filename);
 	match vm
-		.import(PathBuf::from(filename.to_str().unwrap()))
+		.import(
+			&env::current_dir().expect("cwd"),
+			filename.to_str().unwrap(),
+		)
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest(v))
 	{
@@ -141,10 +144,7 @@
 	let filename = CStr::from_ptr(filename);
 	let snippet = CStr::from_ptr(snippet);
 	match vm
-		.evaluate_snippet(
-			filename.to_str().unwrap().into(),
-			snippet.to_str().unwrap().into(),
-		)
+		.evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest(v))
 	{
@@ -186,7 +186,10 @@
 ) -> *const c_char {
 	let filename = CStr::from_ptr(filename);
 	match vm
-		.import(PathBuf::from(filename.to_str().unwrap()))
+		.import(
+			&env::current_dir().expect("cwd"),
+			filename.to_str().unwrap(),
+		)
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_multi(v))
 	{
@@ -213,10 +216,7 @@
 	let filename = CStr::from_ptr(filename);
 	let snippet = CStr::from_ptr(snippet);
 	match vm
-		.evaluate_snippet(
-			filename.to_str().unwrap().into(),
-			snippet.to_str().unwrap().into(),
-		)
+		.evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_multi(v))
 	{
@@ -256,7 +256,10 @@
 ) -> *const c_char {
 	let filename = CStr::from_ptr(filename);
 	match vm
-		.import(PathBuf::from(filename.to_str().unwrap()))
+		.import(
+			&env::current_dir().expect("cwd"),
+			filename.to_str().unwrap(),
+		)
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_stream(v))
 	{
@@ -283,10 +286,7 @@
 	let filename = CStr::from_ptr(filename);
 	let snippet = CStr::from_ptr(snippet);
 	match vm
-		.evaluate_snippet(
-			filename.to_str().unwrap().into(),
-			snippet.to_str().unwrap().into(),
-		)
+		.evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
 		.and_then(|v| vm.with_tla(v))
 		.and_then(|v| vm.manifest_stream(v))
 	{
modifiedbindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -32,7 +32,7 @@
 		.as_any()
 		.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
 		.expect("only stdlib context initializer supported")
-		.add_ext_code(name.to_str().unwrap(), value.to_str().unwrap().into())
+		.add_ext_code(name.to_str().unwrap(), value.to_str().unwrap())
 		.unwrap()
 }
 /// # Safety
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -133,14 +133,14 @@
 
 	let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
 	let val = if opts.input.exec {
-		s.evaluate_snippet("<cmdline>".to_owned(), (&input as &str).into())?
+		s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?
 	} else if input == "-" {
 		let mut input = Vec::new();
 		std::io::stdin().read_to_end(&mut input)?;
-		let input_str = std::str::from_utf8(&input)?.into();
+		let input_str = std::str::from_utf8(&input)?;
 		s.evaluate_snippet("<stdin>".to_owned(), input_str)?
 	} else {
-		s.import(s.resolve_file(&current_dir().expect("cwd"), &input)?)?
+		s.import(&current_dir().expect("cwd"), &input)?
 	};
 
 	let val = s.with_tla(val)?;
modifiedcrates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -118,10 +118,10 @@
 			ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
 		}
 		for ext in self.ext_code.iter() {
-			ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+			ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
 		}
 		for ext in self.ext_code_file.iter() {
-			ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+			ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
 		}
 		s.settings_mut().context_initializer = Box::new(ctx);
 		Ok(())
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,7 @@
 
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};
 use jrsonnet_types::ValType;
 use thiserror::Error;
 
@@ -138,10 +138,10 @@
 
 	#[error("can't resolve {1} from {0}")]
 	ImportFileNotFound(PathBuf, String),
-	#[error("resolved file not found: {0}")]
-	ResolvedFileNotFound(PathBuf),
+	#[error("resolved file not found: {:?}", .0)]
+	ResolvedFileNotFound(SourcePath),
 	#[error("imported file is not valid utf-8: {0:?}")]
-	ImportBadFileUtf8(PathBuf),
+	ImportBadFileUtf8(SourcePath),
 	#[error("import io error: {0}")]
 	ImportIo(String),
 	#[error("tried to import {1} from {0}, but imports is not supported")]
@@ -151,12 +151,11 @@
 	#[error(
 		"syntax error: expected {}, got {:?}",
 		.error.expected,
-		.source_code.chars().nth(error.location.offset)
+		.path.code().chars().nth(error.location.offset)
 		.map_or_else(|| "EOF".into(), |c| c.to_string())
 	)]
 	ImportSyntaxError {
 		path: Source,
-		source_code: IStr,
 		#[trace(skip)]
 		error: Box<jrsonnet_parser::ParseError>,
 	},
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -416,13 +416,13 @@
 		Literal(LiteralType::This) => {
 			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
 		}
-		Literal(LiteralType::Super) => {
-			Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
+		Literal(LiteralType::Super) => Val::Obj(
+			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
 				ctx.this()
 					.clone()
 					.expect("if super exists - then this should to"),
-			))
-		}
+			),
+		),
 		Literal(LiteralType::Dollar) => {
 			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
 		}
@@ -643,10 +643,10 @@
 				Import(_) => s.push(
 					CallLocation::new(loc),
 					|| format!("import {:?}", path.clone()),
-					|| s.import(resolved_path.clone()),
+					|| s.import_resolved(resolved_path),
 				)?,
-				ImportStr(_) => Val::Str(s.import_str(resolved_path)?),
-				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_bin(resolved_path)?)),
+				ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
+				ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),
 				_ => unreachable!(),
 			}
 		}
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -6,6 +6,7 @@
 };
 
 use fs::File;
+use jrsonnet_parser::SourcePath;
 
 use crate::{
 	error::{Error::*, Result},
@@ -17,9 +18,12 @@
 	/// Resolves real 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(&self, from: &Path, path: &str) -> Result<PathBuf>;
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath>;
 
-	fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>>;
+	/// 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`]
+	fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
 
 	/// # Safety
 	///
@@ -32,11 +36,11 @@
 /// Dummy resolver, can't resolve/load any file
 pub struct DummyImportResolver;
 impl ImportResolver for DummyImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		throw!(ImportNotSupported(from.into(), path.into()))
 	}
 
-	fn load_file_contents(&self, _resolved: &Path) -> Result<Vec<u8>> {
+	fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {
 		panic!("dummy resolver can't load any file")
 	}
 
@@ -59,25 +63,35 @@
 	pub library_paths: Vec<PathBuf>,
 }
 impl ImportResolver for FileImportResolver {
-	fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		let mut direct = from.to_path_buf();
 		direct.push(path);
 		if direct.exists() {
-			Ok(direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?)
+			Ok(SourcePath::Path(
+				direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+			))
 		} else {
 			for library_path in &self.library_paths {
 				let mut cloned = library_path.clone();
 				cloned.push(path);
 				if cloned.exists() {
-					return Ok(cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?);
+					return Ok(SourcePath::Path(
+						cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
+					));
 				}
 			}
 			throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
 		}
 	}
 
-	fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
-		let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
+	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 mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
 		let mut out = Vec::new();
 		file.read_to_end(&mut out)
 			.map_err(|e| ImportIo(e.to_string()))?;
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -48,7 +48,7 @@
 	cell::{Ref, RefCell, RefMut},
 	collections::HashMap,
 	fmt::{self, Debug},
-	path::{Path, PathBuf},
+	path::Path,
 	rc::Rc,
 };
 
@@ -65,7 +65,7 @@
 pub use jrsonnet_parser as parser;
 use jrsonnet_parser::*;
 pub use obj::*;
-use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
+use trace::{CompactFormat, TraceFormat};
 pub use val::{ManifestFormat, Thunk, Val};
 
 pub trait Unbound: Trace {
@@ -170,10 +170,7 @@
 	breakpoints: Breakpoints,
 
 	/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
-	files: GcHashMap<PathBuf, FileData>,
-	/// Contains tla arguments and others, which aren't needed to be obtained by name, however may be used for receiving source
-	/// TODO: look into nix approach, storing source code in `Source` object
-	volatile_files: GcHashMap<String, String>,
+	files: GcHashMap<SourcePath, FileData>,
 }
 struct FileData {
 	string: Option<IStr>,
@@ -249,7 +246,8 @@
 pub struct State(Rc<EvaluationStateInternals>);
 
 impl State {
-	pub fn import_str(&self, path: PathBuf) -> Result<IStr> {
+	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+	pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {
 		let mut data = self.data_mut();
 		let mut file = data.files.raw_entry_mut().from_key(&path);
 
@@ -283,7 +281,8 @@
 		}
 		Ok(file.string.as_ref().expect("just set").clone())
 	}
-	pub fn import_bin(&self, path: PathBuf) -> Result<IBytes> {
+	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+	pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {
 		let mut data = self.data_mut();
 		let mut file = data.files.raw_entry_mut().from_key(&path);
 
@@ -309,7 +308,8 @@
 		}
 		Ok(file.bytes.as_ref().expect("just set").clone())
 	}
-	pub fn import(&self, path: PathBuf) -> Result<Val> {
+	/// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+	pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {
 		let mut data = self.data_mut();
 		let mut file = data.files.raw_entry_mut().from_key(&path);
 
@@ -343,7 +343,8 @@
 			);
 		}
 		let code = file.string.as_ref().expect("just set");
-		let file_name = Source::new(path.clone()).expect("resolver should return correct name");
+		let file_name =
+			Source::new(path.clone(), code.clone()).expect("resolver should return correct name");
 		if file.parsed.is_none() {
 			file.parsed = Some(
 				jrsonnet_parser::parse(
@@ -354,7 +355,6 @@
 				)
 				.map_err(|e| ImportSyntaxError {
 					path: file_name.clone(),
-					source_code: code.clone(),
 					error: Box::new(e),
 				})?,
 			);
@@ -386,34 +386,11 @@
 				Ok(v)
 			}
 			Err(e) => Err(e),
-		}
-	}
-
-	pub fn get_source(&self, name: Source) -> Option<String> {
-		let data = self.data();
-		match name.repr() {
-			Ok(real) => data
-				.files
-				.get(real)
-				.and_then(|f| f.string.as_ref())
-				.map(ToString::to_string),
-			Err(e) => data.volatile_files.get(e).map(ToOwned::to_owned),
 		}
-	}
-	pub fn map_source_locations(&self, file: Source, locs: &[u32]) -> Vec<CodeLocation> {
-		offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)
 	}
-	pub fn map_from_source_location(
-		&self,
-		file: Source,
-		line: usize,
-		column: usize,
-	) -> Option<usize> {
-		location_to_offset(
-			&self.get_source(file).expect("file not found"),
-			line,
-			column,
-		)
+	pub fn import(&self, from: &Path, path: &str) -> Result<Val> {
+		let resolved = self.resolve_file(from, path)?;
+		self.import_resolved(resolved)
 	}
 
 	/// Creates context with all passed global variables
@@ -554,7 +531,10 @@
 				|| {
 					func.evaluate(
 						self.clone(),
-						self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),
+						self.create_default_context(Source::new_virtual(
+							Cow::Borrowed("<tla>"),
+							IStr::empty(),
+						)),
 						CallLocation::native(),
 						&self.settings().tla_vars,
 						true,
@@ -568,9 +548,9 @@
 
 /// Internals
 impl State {
-	fn data(&self) -> Ref<EvaluationData> {
-		self.0.data.borrow()
-	}
+	// fn data(&self) -> Ref<EvaluationData> {
+	// 	self.0.data.borrow()
+	// }
 	fn data_mut(&self) -> RefMut<EvaluationData> {
 		self.0.data.borrow_mut()
 	}
@@ -585,8 +565,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: String) -> Result<Val> {
-		let source = Source::new_virtual(Cow::Owned(name.clone()));
+	pub fn evaluate_snippet(&self, name: String, code: impl Into<IStr>) -> Result<Val> {
+		let code = code.into();
+		let source = Source::new_virtual(Cow::Owned(name), code.clone());
 		let parsed = jrsonnet_parser::parse(
 			&code,
 			&ParserSettings {
@@ -595,10 +576,8 @@
 		)
 		.map_err(|e| ImportSyntaxError {
 			path: source.clone(),
-			source_code: code.clone().into(),
 			error: Box::new(e),
 		})?;
-		self.data_mut().volatile_files.insert(name, code);
 		evaluate(self.clone(), self.create_default_context(source), &parsed)
 	}
 }
@@ -617,7 +596,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.clone()));
+		let source = Source::new_virtual(Cow::Owned(source_name.clone()), code.into());
 		let parsed = jrsonnet_parser::parse(
 			code,
 			&ParserSettings {
@@ -626,22 +605,18 @@
 		)
 		.map_err(|e| ImportSyntaxError {
 			path: source,
-			source_code: code.into(),
 			error: Box::new(e),
 		})?;
-		self.data_mut()
-			.volatile_files
-			.insert(source_name, code.to_owned());
 		self.settings_mut()
 			.tla_vars
 			.insert(name, TlaArg::Code(parsed));
 		Ok(())
 	}
 
-	pub fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+	pub fn resolve_file(&self, from: &Path, path: &str) -> Result<SourcePath> {
 		self.settings()
 			.import_resolver
-			.resolve_file(from, path.as_ref())
+			.resolve_file_relative(from, path.as_ref())
 	}
 
 	pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
deletedcrates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ /dev/null
@@ -1,124 +0,0 @@
-#[allow(clippy::module_name_repetitions)]
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub struct CodeLocation {
-	pub offset: usize,
-
-	pub line: usize,
-	pub column: usize,
-
-	pub line_start_offset: usize,
-	pub line_end_offset: usize,
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
-	let mut offset = 0;
-	while line > 1 {
-		let pos = file.find('\n')?;
-		offset += pos + 1;
-		file = &file[pos + 1..];
-		line -= 1;
-	}
-	offset += column - 1;
-	Some(offset)
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
-	if offsets.is_empty() {
-		return vec![];
-	}
-	let mut line = 1;
-	let mut column = 1;
-	let max_offset = *offsets.iter().max().expect("offsets is not empty");
-
-	let mut offset_map = offsets
-		.iter()
-		.enumerate()
-		.map(|(pos, offset)| (*offset, pos))
-		.collect::<Vec<_>>();
-	offset_map.sort_by_key(|v| v.0);
-	offset_map.reverse();
-
-	let mut out = vec![
-		CodeLocation {
-			offset: 0,
-			column: 0,
-			line: 0,
-			line_start_offset: 0,
-			line_end_offset: 0
-		};
-		offsets.len()
-	];
-	let mut with_no_known_line_ending = vec![];
-	let mut this_line_offset = 0;
-	for (pos, ch) in file
-		.chars()
-		.enumerate()
-		.chain(std::iter::once((file.len(), ' ')))
-	{
-		column += 1;
-		match offset_map.last() {
-			Some(x) if x.0 == pos as u32 => {
-				let out_idx = x.1;
-				with_no_known_line_ending.push(out_idx);
-				out[out_idx].offset = pos;
-				out[out_idx].line = line;
-				out[out_idx].column = column;
-				out[out_idx].line_start_offset = this_line_offset;
-				offset_map.pop();
-			}
-			_ => {}
-		}
-		if ch == '\n' {
-			line += 1;
-			column = 1;
-
-			for idx in with_no_known_line_ending.drain(..) {
-				out[idx].line_end_offset = pos;
-			}
-			this_line_offset = pos + 1;
-
-			if pos == max_offset as usize + 1 {
-				break;
-			}
-		}
-	}
-	let file_end = file.chars().count();
-	for idx in with_no_known_line_ending {
-		out[idx].line_end_offset = file_end;
-	}
-
-	out
-}
-
-#[cfg(test)]
-pub mod tests {
-	use super::{offset_to_location, CodeLocation};
-
-	#[test]
-	fn test() {
-		assert_eq!(
-			offset_to_location(
-				"hello world\n_______________________________________________________",
-				&[0, 14]
-			),
-			vec![
-				CodeLocation {
-					offset: 0,
-					line: 1,
-					column: 2,
-					line_start_offset: 0,
-					line_end_offset: 11,
-				},
-				CodeLocation {
-					offset: 14,
-					line: 2,
-					column: 4,
-					line_start_offset: 12,
-					line_end_offset: 67
-				}
-			]
-		)
-	}
-}
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,9 +1,6 @@
-mod location;
-
 use std::path::{Path, PathBuf};
 
-use jrsonnet_parser::Source;
-pub use location::*;
+use jrsonnet_parser::{CodeLocation, Source};
 
 use crate::{error::Error, LocError, State};
 
@@ -84,31 +81,27 @@
 	fn write_trace(
 		&self,
 		out: &mut dyn std::fmt::Write,
-		s: &State,
+		_s: &State,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
 		write!(out, "{}", error.error())?;
-		if let Error::ImportSyntaxError {
-			path,
-			source_code,
-			error,
-		} = error.error()
-		{
+		if let Error::ImportSyntaxError { path, error } = error.error() {
 			use std::fmt::Write;
 
 			writeln!(out)?;
-			let mut n = match path.repr() {
-				Ok(r) => self.resolver.resolve(r),
-				Err(v) => v.to_string(),
+			let mut n = match path.path() {
+				Some(r) => self.resolver.resolve(r),
+				None => path.short_display().to_string(),
 			};
 			let mut offset = error.location.offset;
-			let is_eof = if offset >= source_code.len() {
-				offset = source_code.len().saturating_sub(1);
+			let is_eof = if offset >= path.code().len() {
+				offset = path.code().len().saturating_sub(1);
 				true
 			} else {
 				false
 			};
-			let mut location = offset_to_location(source_code, &[offset as u32])
+			let mut location = path
+				.map_source_locations(&[offset as u32])
 				.into_iter()
 				.next()
 				.unwrap();
@@ -129,13 +122,12 @@
 				use std::fmt::Write;
 				#[allow(clippy::option_if_let_else)]
 				if let Some(location) = location {
-					let mut resolved_path = match location.0.repr() {
-						Ok(r) => self.resolver.resolve(r),
-						Err(v) => v.to_string(),
+					let mut resolved_path = match location.0.path() {
+						Some(r) => self.resolver.resolve(r),
+						None => location.0.short_display().to_string(),
 					};
 					// TODO: Process all trace elements first
-					let location =
-						s.map_source_locations(location.0.clone(), &[location.1, location.2]);
+					let location = location.0.map_source_locations(&[location.1, location.2]);
 					write!(resolved_path, ":").unwrap();
 					print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();
 					write!(resolved_path, ":").unwrap();
@@ -176,7 +168,7 @@
 	fn write_trace(
 		&self,
 		out: &mut dyn std::fmt::Write,
-		s: &State,
+		_s: &State,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
 		write!(out, "{}", error.error())?;
@@ -184,10 +176,10 @@
 			writeln!(out)?;
 			let desc = &item.desc;
 			if let Some(source) = &item.location {
-				let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
-				let resolved_path = match source.0.repr() {
-					Ok(r) => r.display().to_string(),
-					Err(v) => v.to_string(),
+				let start_end = source.0.map_source_locations(&[source.1, source.2]);
+				let resolved_path = match source.0.path() {
+					Some(r) => r.display().to_string(),
+					None => source.0.short_display().to_string(),
 				};
 
 				write!(
@@ -213,19 +205,15 @@
 	fn write_trace(
 		&self,
 		out: &mut dyn std::fmt::Write,
-		s: &State,
+		_s: &State,
 		error: &LocError,
 	) -> Result<(), std::fmt::Error> {
 		write!(out, "{}", error.error())?;
-		if let Error::ImportSyntaxError {
-			path,
-			source_code,
-			error,
-		} = error.error()
-		{
+		if let Error::ImportSyntaxError { path, error } = error.error() {
 			writeln!(out)?;
 			let offset = error.location.offset;
-			let location = offset_to_location(source_code, &[offset as u32])
+			let location = path
+				.map_source_locations(&[offset as u32])
 				.into_iter()
 				.next()
 				.unwrap();
@@ -234,7 +222,7 @@
 
 			self.print_snippet(
 				out,
-				source_code,
+				path.code(),
 				path,
 				&location,
 				&end_location,
@@ -246,10 +234,10 @@
 			writeln!(out)?;
 			let desc = &item.desc;
 			if let Some(source) = &item.location {
-				let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
+				let start_end = source.0.map_source_locations(&[source.1, source.2]);
 				self.print_snippet(
 					out,
-					&s.get_source(source.0.clone()).unwrap(),
+					&source.0.code(),
 					&source.0,
 					&start_end[0],
 					&start_end[1],
@@ -284,9 +272,9 @@
 			.take(end.line_end_offset - end.line_start_offset)
 			.collect();
 
-		let origin = match origin.repr() {
-			Ok(r) => self.resolver.resolve(r),
-			Err(v) => v.to_string(),
+		let origin = match origin.path() {
+			Some(r) => self.resolver.resolve(r),
+			None => origin.short_display().to_string(),
 		};
 		let snippet = Snippet {
 			opt: FormatOptions {
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -33,6 +33,10 @@
 
 impl IStr {
 	#[must_use]
+	pub fn empty() -> Self {
+		"".into()
+	}
+	#[must_use]
 	pub fn as_str(&self) -> &str {
 		self as &str
 	}
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]23use std::rc::Rc;45use peg::parser;6mod expr;7pub use expr::*;8pub use jrsonnet_interner::IStr;9pub use peg;10mod source;11mod unescape;12pub use source::Source;1314pub struct ParserSettings {15	pub file_name: Source,16}1718macro_rules! expr_bin {19	($a:ident $op:ident $b:ident) => {20		Expr::BinaryOp($a, $op, $b)21	};22}23macro_rules! expr_un {24	($op:ident $a:ident) => {25		Expr::UnaryOp($op, $a)26	};27}2829parser! {30	grammar jsonnet_parser() for str {31		use peg::ParseLiteral;3233		rule eof() = quiet!{![_]} / expected!("<eof>")34		rule eol() = "\n" / eof()3536		/// Standard C-like comments37		rule comment()38			= "//" (!eol()[_])* eol()39			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"40			/ "#" (!eol()[_])* eol()4142		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")43		rule _() = quiet!{([' ' | '\r' | '\n' | '\t']+) / comment()}* / expected!("<whitespace>")4445		/// For comma-delimited elements46		rule comma() = quiet!{_ "," _} / expected!("<comma>")47		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}48		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}49		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']50		/// Sequence of digits51		rule uint_str() -> &'input str = a:$(digit()+) { a }52		/// Number in scientific notation format53		rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")5455		/// Reserved word followed by any non-alphanumberic56		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()57		rule id() -> IStr = v:$(quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")) { v.into() }5859		rule keyword(id: &'static str) -> ()60			= ##parse_string_literal(id) end_of_ident()6162		pub rule param(s: &ParserSettings) -> expr::Param = name:destruct(s) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name, expr) }63		pub rule params(s: &ParserSettings) -> expr::ParamsDesc64			= params:param(s) ** comma() comma()? { expr::ParamsDesc(Rc::new(params)) }65			/ { expr::ParamsDesc(Rc::new(Vec::new())) }6667		pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)68			= quiet! { name:(s:id() _ "=" !['='] _ {s})? expr:expr(s) {(name, expr)} }69			/ expected!("<argument>")7071		pub rule args(s: &ParserSettings) -> expr::ArgsDesc72			= args:arg(s)**comma() comma()? {?73				let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();74				let mut unnamed = Vec::with_capacity(unnamed_count);75				let mut named = Vec::with_capacity(args.len() - unnamed_count);76				let mut named_started = false;77				for (name, value) in args {78					if let Some(name) = name {79						named_started = true;80						named.push((name, value));81					} else {82						if named_started {83							return Err("<named argument>")84						}85						unnamed.push(value);86					}87				}88				Ok(expr::ArgsDesc::new(unnamed, named))89			}9091		pub rule destruct_rest() -> expr::DestructRest92			= "..." into:(_ into:id() {into})? {if let Some(into) = into {93				expr::DestructRest::Keep(into)94			} else {expr::DestructRest::Drop}}95		pub rule destruct_array(s: &ParserSettings) -> expr::Destruct96			= "[" _ start:destruct(s)**comma() rest:(97				comma() _ rest:destruct_rest()? end:(98					comma() end:destruct(s)**comma() (_ comma())? {end}99					/ comma()? {Vec::new()}100				) {(rest, end)}101				/ comma()? {(None, Vec::new())}102			) _ "]" {?103				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Array {104					start,105					rest: rest.0,106					end: rest.1,107				});108				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")109			}110		pub rule destruct_object(s: &ParserSettings) -> expr::Destruct111			= "{" _112				fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()113				rest:(114					comma() rest:destruct_rest()? {rest}115					/ comma()? {None}116				)117			_ "}" {?118				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Object {119					fields,120					rest,121				});122				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")123			}124		pub rule destruct(s: &ParserSettings) -> expr::Destruct125			= v:id() {expr::Destruct::Full(v)}126			/ "?" {?127				#[cfg(feature = "exp-destruct")] return Ok(expr::Destruct::Skip);128				#[cfg(not(feature = "exp-destruct"))] Err("experimental destructuring was not enabled")129			}130			/ arr:destruct_array(s) {arr}131			/ obj:destruct_object(s) {obj}132133		pub rule bind(s: &ParserSettings) -> expr::BindSpec134			= into:destruct(s) _ "=" _ expr:expr(s) {expr::BindSpec::Field{into, value: expr}}135			/ name:id() _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec::Function{name, params, value: expr}}136137		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt138			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }139140		pub rule whole_line() -> &'input str141			= str:$((!['\n'][_])* "\n") {str}142		pub rule string_block() -> String143			= "|||" (!['\n']single_whitespace())* "\n"144			  empty_lines:$(['\n']*)145			  prefix:[' ' | '\t']+ first_line:whole_line()146			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*147			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"148			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}149150		rule hex_char()151			= quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("<hex char>")152153		rule string_char(c: rule<()>)154			= (!['\\']!c()[_])+155			/ "\\\\"156			/ "\\u" hex_char() hex_char() hex_char() hex_char()157			/ "\\x" hex_char() hex_char()158			/ ['\\'] (quiet! { ['b' | 'f' | 'n' | 'r' | 't' | '"' | '\''] } / expected!("<escape character>"))159		pub rule string() -> String160			= ['"'] str:$(string_char(<"\"">)*) ['"'] {? unescape::unescape(str).ok_or("<escaped string>")}161			/ ['\''] str:$(string_char(<"\'">)*) ['\''] {? unescape::unescape(str).ok_or("<escaped string>")}162			/ quiet!{ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}163			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}164			/ string_block() } / expected!("<string>")165166		pub rule field_name(s: &ParserSettings) -> expr::FieldName167			= name:id() {expr::FieldName::Fixed(name)}168			/ name:string() {expr::FieldName::Fixed(name.into())}169			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}170		pub rule visibility() -> expr::Visibility171			= ":::" {expr::Visibility::Unhide}172			/ "::" {expr::Visibility::Hidden}173			/ ":" {expr::Visibility::Normal}174		pub rule field(s: &ParserSettings) -> expr::FieldMember175			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{176				name,177				plus: plus.is_some(),178				params: None,179				visibility,180				value,181			}}182			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{183				name,184				plus: false,185				params: Some(params),186				visibility,187				value,188			}}189		pub rule obj_local(s: &ParserSettings) -> BindSpec190			= keyword("local") _ bind:bind(s) {bind}191		pub rule member(s: &ParserSettings) -> expr::Member192			= bind:obj_local(s) {expr::Member::BindStmt(bind)}193			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}194			/ field:field(s) {expr::Member::Field(field)}195		pub rule objinside(s: &ParserSettings) -> expr::ObjBody196			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {197				let mut compspecs = vec![CompSpec::ForSpec(forspec)];198				compspecs.extend(others.unwrap_or_default());199				expr::ObjBody::ObjComp(expr::ObjComp{200					pre_locals,201					key,202					plus: plus.is_some(),203					value,204					post_locals,205					compspecs,206				})207			}208			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}209		pub rule ifspec(s: &ParserSettings) -> IfSpecData210			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}211		pub rule forspec(s: &ParserSettings) -> ForSpecData212			= keyword("for") _ id:id() _ keyword("in") _ cond:expr(s) {ForSpecData(id, cond)}213		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>214			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}215		pub rule local_expr(s: &ParserSettings) -> Expr216			= keyword("local") _ binds:bind(s) ** comma() (_ ",")? _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }217		pub rule string_expr(s: &ParserSettings) -> Expr218			= s:string() {Expr::Str(s.into())}219		pub rule obj_expr(s: &ParserSettings) -> Expr220			= "{" _ body:objinside(s) _ "}" {Expr::Obj(body)}221		pub rule array_expr(s: &ParserSettings) -> Expr222			= "[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}223		pub rule array_comp_expr(s: &ParserSettings) -> Expr224			= "[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {225				let mut specs = vec![CompSpec::ForSpec(forspec)];226				specs.extend(others.unwrap_or_default());227				Expr::ArrComp(expr, specs)228			}229		pub rule number_expr(s: &ParserSettings) -> Expr230			= n:number() { expr::Expr::Num(n) }231		pub rule var_expr(s: &ParserSettings) -> Expr232			= n:id() { expr::Expr::Var(n) }233		pub rule id_loc(s: &ParserSettings) -> LocExpr234			= a:position!() n:id() b:position!() { LocExpr(Rc::new(expr::Expr::Str(n)), ExprLocation(s.file_name.clone(), a as u32,b as u32)) }235		pub rule if_then_else_expr(s: &ParserSettings) -> Expr236			= cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{237				cond,238				cond_then,239				cond_else,240			}}241242		pub rule literal(s: &ParserSettings) -> Expr243			= v:(244				keyword("null") {LiteralType::Null}245				/ keyword("true") {LiteralType::True}246				/ keyword("false") {LiteralType::False}247				/ keyword("self") {LiteralType::This}248				/ keyword("$") {LiteralType::Dollar}249				/ keyword("super") {LiteralType::Super}250			) {Expr::Literal(v)}251252		pub rule expr_basic(s: &ParserSettings) -> Expr253			= literal(s)254255			/ string_expr(s) / number_expr(s)256			/ array_expr(s)257			/ obj_expr(s)258			/ array_expr(s)259			/ array_comp_expr(s)260261			/ keyword("importstr") _ path:string() {Expr::ImportStr(path.into())}262			/ keyword("importbin") _ path:string() {Expr::ImportBin(path.into())}263			/ keyword("import") _ path:string() {Expr::Import(path.into())}264265			/ var_expr(s)266			/ local_expr(s)267			/ if_then_else_expr(s)268269			/ keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}270			/ assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }271272			/ keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }273274		rule slice_part(s: &ParserSettings) -> Option<LocExpr>275			= _ e:(e:expr(s) _{e})? {e}276		pub rule slice_desc(s: &ParserSettings) -> SliceDesc277			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {278				let (end, step) = if let Some((end, step)) = pair {279					(end, step)280				}else{281					(None, None)282				};283284				SliceDesc { start, end, step }285			}286287		rule binop(x: rule<()>) -> ()288			= quiet!{ x() } / expected!("<binary op>")289		rule unaryop(x: rule<()>) -> ()290			= quiet!{ x() } / expected!("<unary op>")291292293		use BinaryOpType::*;294		use UnaryOpType::*;295		rule expr(s: &ParserSettings) -> LocExpr296			= precedence! {297				start:position!() v:@ end:position!() { LocExpr(Rc::new(v), ExprLocation(s.file_name.clone(), start as u32, end as u32)) }298				--299				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}300				--301				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}302				--303				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}304				--305				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}306				--307				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}308				--309				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}310				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}311				--312				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}313				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}314				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}315				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}316				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}317				--318				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}319				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}320				--321				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}322				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}323				--324				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}325				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}326				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}327				--328						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}329						unaryop(<"!">) _ b:@ {expr_un!(Not b)}330						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}331				--332				a:(@) _ "[" _ e:slice_desc(s) _ "]" {Expr::Slice(a, e)}333				a:(@) _ "." _ a:position!() e:id_loc(s) b:position!() {Expr::Index(a, e)}334				a:(@) _ "[" _ e:expr(s) _ "]" {Expr::Index(a, e)}335				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {Expr::Apply(a, args, ts.is_some())}336				a:(@) _ "{" _ body:objinside(s) _ "}" {Expr::ObjExtend(a, body)}337				--338				e:expr_basic(s) {e}339				"(" _ e:expr(s) _ ")" {Expr::Parened(e)}340			}341342		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}343	}344}345346pub type ParseError = peg::error::ParseError<peg::str::LineCol>;347pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {348	jsonnet_parser::jsonnet(str, settings)349}350/// Used for importstr values351pub fn string_to_expr(str: IStr, settings: &ParserSettings) -> LocExpr {352	let len = str.len();353	LocExpr(354		Rc::new(Expr::Str(str)),355		ExprLocation(settings.file_name.clone(), 0, len as u32),356	)357}358359#[cfg(test)]360pub mod tests {361	use std::borrow::Cow;362363	use BinaryOpType::*;364365	use super::{expr::*, parse};366	use crate::{source::Source, ParserSettings};367368	macro_rules! parse {369		($s:expr) => {370			parse(371				$s,372				&ParserSettings {373					file_name: Source::new_virtual(Cow::Borrowed("<test>")),374				},375			)376			.unwrap()377		};378	}379380	macro_rules! el {381		($expr:expr, $from:expr, $to:expr$(,)?) => {382			LocExpr(383				std::rc::Rc::new($expr),384				ExprLocation(Source::new_virtual(Cow::Borrowed("<test>")), $from, $to),385			)386		};387	}388389	#[test]390	fn multiline_string() {391		assert_eq!(392			parse!("|||\n    Hello world!\n     a\n|||"),393			el!(Expr::Str("Hello world!\n a\n".into()), 0, 31),394		);395		assert_eq!(396			parse!("|||\n  Hello world!\n   a\n|||"),397			el!(Expr::Str("Hello world!\n a\n".into()), 0, 27),398		);399		assert_eq!(400			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),401			el!(Expr::Str("Hello world!\n\ta\n".into()), 0, 27),402		);403		assert_eq!(404			parse!("|||\n   Hello world!\n    a\n |||"),405			el!(Expr::Str("Hello world!\n a\n".into()), 0, 30),406		);407	}408409	#[test]410	fn slice() {411		parse!("a[1:]");412		parse!("a[1::]");413		parse!("a[:1:]");414		parse!("a[::1]");415		parse!("str[:len - 1]");416	}417418	#[test]419	fn string_escaping() {420		assert_eq!(421			parse!(r#""Hello, \"world\"!""#),422			el!(Expr::Str(r#"Hello, "world"!"#.into()), 0, 19),423		);424		assert_eq!(425			parse!(r#"'Hello \'world\'!'"#),426			el!(Expr::Str("Hello 'world'!".into()), 0, 18),427		);428		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into()), 0, 6));429	}430431	#[test]432	fn string_unescaping() {433		assert_eq!(434			parse!(r#""Hello\nWorld""#),435			el!(Expr::Str("Hello\nWorld".into()), 0, 14),436		);437	}438439	#[test]440	fn string_verbantim() {441		assert_eq!(442			parse!(r#"@"Hello\n""World""""#),443			el!(Expr::Str("Hello\\n\"World\"".into()), 0, 19),444		);445	}446447	#[test]448	fn imports() {449		assert_eq!(450			parse!("import \"hello\""),451			el!(Expr::Import("hello".into()), 0, 14),452		);453		assert_eq!(454			parse!("importstr \"garnish.txt\""),455			el!(Expr::ImportStr("garnish.txt".into()), 0, 23)456		);457		assert_eq!(458			parse!("importbin \"garnish.bin\""),459			el!(Expr::ImportBin("garnish.bin".into()), 0, 23)460		);461	}462463	#[test]464	fn empty_object() {465		assert_eq!(466			parse!("{}"),467			el!(Expr::Obj(ObjBody::MemberList(vec![])), 0, 2)468		);469	}470471	#[test]472	fn basic_math() {473		assert_eq!(474			parse!("2+2*2"),475			el!(476				Expr::BinaryOp(477					el!(Expr::Num(2.0), 0, 1),478					Add,479					el!(480						Expr::BinaryOp(el!(Expr::Num(2.0), 2, 3), Mul, el!(Expr::Num(2.0), 4, 5)),481						2,482						5483					)484				),485				0,486				5487			)488		);489	}490491	#[test]492	fn basic_math_with_indents() {493		assert_eq!(494			parse!("2	+ 	  2	  *	2   	"),495			el!(496				Expr::BinaryOp(497					el!(Expr::Num(2.0), 0, 1),498					Add,499					el!(500						Expr::BinaryOp(el!(Expr::Num(2.0), 7, 8), Mul, el!(Expr::Num(2.0), 13, 14),),501						7,502						14503					),504				),505				0,506				14507			)508		);509	}510511	#[test]512	fn basic_math_parened() {513		assert_eq!(514			parse!("2+(2+2*2)"),515			el!(516				Expr::BinaryOp(517					el!(Expr::Num(2.0), 0, 1),518					Add,519					el!(520						Expr::Parened(el!(521							Expr::BinaryOp(522								el!(Expr::Num(2.0), 3, 4),523								Add,524								el!(525									Expr::BinaryOp(526										el!(Expr::Num(2.0), 5, 6),527										Mul,528										el!(Expr::Num(2.0), 7, 8),529									),530									5,531									8532								),533							),534							3,535							8536						)),537						2,538						9539					),540				),541				0,542				9543			)544		);545	}546547	/// Comments should not affect parsing548	#[test]549	fn comments() {550		assert_eq!(551			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),552			el!(553				Expr::BinaryOp(554					el!(Expr::Num(2.0), 0, 1),555					Add,556					el!(557						Expr::BinaryOp(558							el!(Expr::Num(3.0), 22, 23),559							Mul,560							el!(Expr::Num(4.0), 40, 41)561						),562						22,563						41564					)565				),566				0,567				41568			)569		);570	}571572	/// Comments should be able to be escaped573	#[test]574	fn comment_escaping() {575		assert_eq!(576			parse!("2/*\\*/+*/ - 22"),577			el!(578				Expr::BinaryOp(el!(Expr::Num(2.0), 0, 1), Sub, el!(Expr::Num(22.0), 12, 14)),579				0,580				14581			)582		);583	}584585	#[test]586	fn suffix() {587		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));588		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));589		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));590		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))591	}592593	#[test]594	fn array_comp() {595		use Expr::*;596		/*597		`ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Var("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`,598		`ArrComp(Apply(Index(Var("std") from "test.jsonnet":1-4, Str("deepJoin") from "test.jsonnet":5-13) from "test.jsonnet":1-13, ArgsDesc { unnamed: [Var("x") from "test.jsonnet":14-15], named: [] }, false) from "test.jsonnet":1-16, [ForSpec(ForSpecData("x", Var("arr") from "test.jsonnet":26-29))]) from "test.jsonnet":0-30`599				*/600		assert_eq!(601			parse!("[std.deepJoin(x) for x in arr]"),602			el!(603				ArrComp(604					el!(605						Apply(606							el!(607								Index(608									el!(Var("std".into()), 1, 4),609									el!(Str("deepJoin".into()), 5, 13)610								),611								1,612								13613							),614							ArgsDesc::new(vec![el!(Var("x".into()), 14, 15)], vec![]),615							false,616						),617						1,618						16619					),620					vec![CompSpec::ForSpec(ForSpecData(621						"x".into(),622						el!(Var("arr".into()), 26, 29)623					))]624				),625				0,626				30627			),628		)629	}630631	#[test]632	fn reserved() {633		use Expr::*;634		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null), 0, 4));635		assert_eq!(parse!("nulla"), el!(Var("nulla".into()), 0, 5));636	}637638	#[test]639	fn multiple_args_buf() {640		parse!("a(b, null_fields)");641	}642643	#[test]644	fn infix_precedence() {645		use Expr::*;646		assert_eq!(647			parse!("!a && !b"),648			el!(649				BinaryOp(650					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),651					And,652					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 7, 8)), 6, 8)653				),654				0,655				8656			)657		);658	}659660	#[test]661	fn infix_precedence_division() {662		use Expr::*;663		assert_eq!(664			parse!("!a / !b"),665			el!(666				BinaryOp(667					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 1, 2)), 0, 2),668					Div,669					el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()), 6, 7)), 5, 7)670				),671				0,672				7673			)674		);675	}676677	#[test]678	fn double_negation() {679		use Expr::*;680		assert_eq!(681			parse!("!!a"),682			el!(683				UnaryOp(684					UnaryOpType::Not,685					el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()), 2, 3)), 1, 3)686				),687				0,688				3689			)690		)691	}692693	#[test]694	fn array_test_error() {695		parse!("[a for a in b if c for e in f]");696		//                    ^^^^ failed code697	}698699	#[test]700	fn missing_newline_between_comment_and_eof() {701		parse!(702			"{a:1}703704			//+213"705		);706	}707708	#[test]709	fn default_param_before_nondefault() {710		parse!("local x(foo = 'foo', bar) = null; null");711	}712713	#[test]714	fn can_parse_stdlib() {715		parse!(jrsonnet_stdlib::STDLIB_STR);716	}717718	#[test]719	fn add_location_info_to_all_sub_expressions() {720		use Expr::*;721722		let file_name = Source::new_virtual(Cow::Borrowed("<test>"));723		let expr = parse(724			"{} { local x = 1, x: x } + {}",725			&ParserSettings { file_name },726		)727		.unwrap();728		assert_eq!(729			expr,730			el!(731				BinaryOp(732					el!(733						ObjExtend(734							el!(Obj(ObjBody::MemberList(vec![])), 0, 2),735							ObjBody::MemberList(vec![736								Member::BindStmt(BindSpec::Field {737									into: Destruct::Full("x".into()),738									value: el!(Num(1.0), 15, 16)739								}),740								Member::Field(FieldMember {741									name: FieldName::Fixed("x".into()),742									plus: false,743									params: None,744									visibility: Visibility::Normal,745									value: el!(Var("x".into()), 21, 22),746								})747							])748						),749						0,750						24751					),752					BinaryOpType::Add,753					el!(Obj(ObjBody::MemberList(vec![])), 27, 29),754				),755				0,756				29757			),758		);759	}760}
addedcrates/jrsonnet-parser/src/location.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-parser/src/location.rs
@@ -0,0 +1,124 @@
+#[allow(clippy::module_name_repetitions)]
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct CodeLocation {
+	pub offset: usize,
+
+	pub line: usize,
+	pub column: usize,
+
+	pub line_start_offset: usize,
+	pub line_end_offset: usize,
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
+	let mut offset = 0;
+	while line > 1 {
+		let pos = file.find('\n')?;
+		offset += pos + 1;
+		file = &file[pos + 1..];
+		line -= 1;
+	}
+	offset += column - 1;
+	Some(offset)
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
+	if offsets.is_empty() {
+		return vec![];
+	}
+	let mut line = 1;
+	let mut column = 1;
+	let max_offset = *offsets.iter().max().expect("offsets is not empty");
+
+	let mut offset_map = offsets
+		.iter()
+		.enumerate()
+		.map(|(pos, offset)| (*offset, pos))
+		.collect::<Vec<_>>();
+	offset_map.sort_by_key(|v| v.0);
+	offset_map.reverse();
+
+	let mut out = vec![
+		CodeLocation {
+			offset: 0,
+			column: 0,
+			line: 0,
+			line_start_offset: 0,
+			line_end_offset: 0
+		};
+		offsets.len()
+	];
+	let mut with_no_known_line_ending = vec![];
+	let mut this_line_offset = 0;
+	for (pos, ch) in file
+		.chars()
+		.enumerate()
+		.chain(std::iter::once((file.len(), ' ')))
+	{
+		column += 1;
+		match offset_map.last() {
+			Some(x) if x.0 == pos as u32 => {
+				let out_idx = x.1;
+				with_no_known_line_ending.push(out_idx);
+				out[out_idx].offset = pos;
+				out[out_idx].line = line;
+				out[out_idx].column = column;
+				out[out_idx].line_start_offset = this_line_offset;
+				offset_map.pop();
+			}
+			_ => {}
+		}
+		if ch == '\n' {
+			line += 1;
+			column = 1;
+
+			for idx in with_no_known_line_ending.drain(..) {
+				out[idx].line_end_offset = pos;
+			}
+			this_line_offset = pos + 1;
+
+			if pos == max_offset as usize + 1 {
+				break;
+			}
+		}
+	}
+	let file_end = file.chars().count();
+	for idx in with_no_known_line_ending {
+		out[idx].line_end_offset = file_end;
+	}
+
+	out
+}
+
+#[cfg(test)]
+pub mod tests {
+	use super::{offset_to_location, CodeLocation};
+
+	#[test]
+	fn test() {
+		assert_eq!(
+			offset_to_location(
+				"hello world\n_______________________________________________________",
+				&[0, 14]
+			),
+			vec![
+				CodeLocation {
+					offset: 0,
+					line: 1,
+					column: 2,
+					line_start_offset: 0,
+					line_end_offset: 11,
+				},
+				CodeLocation {
+					offset: 14,
+					line: 2,
+					column: 4,
+					line_start_offset: 12,
+					line_end_offset: 67
+				}
+			]
+		)
+	}
+}
modifiedcrates/jrsonnet-parser/src/source.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -6,21 +6,42 @@
 };
 
 use jrsonnet_gcmodule::{Trace, Tracer};
+use jrsonnet_interner::IStr;
 #[cfg(feature = "serde")]
 use serde::{Deserialize, Serialize};
 
+use crate::location::{location_to_offset, offset_to_location, CodeLocation};
+
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(PartialEq, Eq, Debug, Hash)]
-enum Inner {
-	Real(PathBuf),
+#[derive(PartialEq, Eq, Debug, Hash, Clone)]
+pub enum SourcePath {
+	/// This file is located on disk
+	Path(PathBuf),
+	/// This file is located somewhere else (I.e http), but it can refer to relative paths, and is egilible for caching
+	Custom(String),
+	/// This file is only located in memory, and can't be cached
 	Virtual(Cow<'static, str>),
 }
+impl Trace for SourcePath {
+	fn trace(&self, _tracer: &mut Tracer) {}
 
+	fn is_type_tracked() -> bool {
+		false
+	}
+}
+
+impl SourcePath {
+	/// Should import resolver be able to read file by this path?
+	pub fn can_load(&self) -> bool {
+		matches!(self, Self::Path(_) | Self::Custom(_))
+	}
+}
+
 /// Either real file, or virtual
 /// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 #[derive(Clone, PartialEq, Eq, Debug)]
-pub struct Source(Rc<Inner>);
+pub struct Source(Rc<(SourcePath, IStr)>);
 static_assertions::assert_eq_size!(Source, *const ());
 
 impl Trace for Source {
@@ -33,55 +54,63 @@
 
 impl Source {
 	/// Fails when path contains inner /../ or /./ references, or not absolute
-	pub fn new(path: PathBuf) -> Option<Self> {
-		if !path.is_absolute()
-			|| path
-				.components()
-				.any(|c| matches!(c, Component::CurDir | Component::ParentDir))
-		{
-			return None;
+	pub fn new(path: SourcePath, code: IStr) -> Option<Self> {
+		if let SourcePath::Path(path) = &path {
+			if !path.is_absolute()
+				|| path
+					.components()
+					.any(|c| matches!(c, Component::CurDir | Component::ParentDir))
+			{
+				return None;
+			}
 		}
-		Some(Self(Rc::new(Inner::Real(path))))
+		Some(Self(Rc::new((path, code))))
 	}
 
-	pub fn new_virtual(n: Cow<'static, str>) -> Self {
-		Self(Rc::new(Inner::Virtual(n)))
+	pub fn new_virtual(n: Cow<'static, str>, code: IStr) -> Self {
+		Self(Rc::new((SourcePath::Virtual(n), code)))
 	}
 
 	pub fn short_display(&self) -> ShortDisplay {
 		ShortDisplay(self.clone())
 	}
 
-	/// Returns None if file is virtual
+	/// Returns Some if this file is loaded from FS
 	pub fn path(&self) -> Option<&Path> {
-		match self.inner() {
-			Inner::Real(r) => Some(r),
-			Inner::Virtual(_) => None,
+		match self.source_path() {
+			SourcePath::Path(r) => Some(r),
+			SourcePath::Custom(_) => None,
+			SourcePath::Virtual(_) => None,
 		}
 	}
-	pub fn repr(&self) -> Result<&Path, &str> {
-		match self.inner() {
-			Inner::Real(r) => Ok(r),
-			Inner::Virtual(v) => Err(v.as_ref()),
-		}
+	pub fn code(&self) -> &str {
+		&self.0 .1
+	}
+
+	pub fn source_path(&self) -> &SourcePath {
+		&self.0 .0 as &SourcePath
 	}
 
-	fn inner(&self) -> &Inner {
-		&self.0 as &Inner
+	pub fn map_source_locations(&self, locs: &[u32]) -> Vec<CodeLocation> {
+		offset_to_location(&self.0 .1, locs)
+	}
+	pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {
+		location_to_offset(&self.0 .1, line, column)
 	}
 }
 pub struct ShortDisplay(Source);
 impl fmt::Display for ShortDisplay {
 	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-		match &self.0 .0 as &Inner {
-			Inner::Real(r) => {
+		match &self.0 .0 .0 as &SourcePath {
+			SourcePath::Path(r) => {
 				write!(
 					f,
 					"{}",
 					r.file_name().expect("path is valid").to_string_lossy()
 				)
 			}
-			Inner::Virtual(n) => write!(f, "{}", n),
+			SourcePath::Custom(r) => write!(f, "{}", r),
+			SourcePath::Virtual(n) => write!(f, "{}", n),
 		}
 	}
 }
modifiedcrates/jrsonnet-stdlib/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/build.rs
+++ b/crates/jrsonnet-stdlib/build.rs
@@ -7,7 +7,10 @@
 	let parsed = parse(
 		include_str!("./src/std.jsonnet"),
 		&ParserSettings {
-			file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+			file_name: Source::new_virtual(
+				Cow::Borrowed("<std>"),
+				include_str!("./src/std.jsonnet").into(),
+			),
 		},
 	)
 	.expect("parse");
modifiedcrates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/expr.rs
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -15,7 +15,7 @@
 	jrsonnet_parser::parse(
 		STDLIB_STR,
 		&ParserSettings {
-			file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+			file_name: Source::new_virtual(Cow::Borrowed("<std>"), STDLIB_STR.into()),
 		},
 	)
 	.unwrap()
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -183,10 +183,10 @@
 
 pub struct StdTracePrinter;
 impl TracePrinter for StdTracePrinter {
-	fn print_trace(&self, s: State, loc: CallLocation, value: IStr) {
+	fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {
 		eprint!("TRACE:");
 		if let Some(loc) = loc.0 {
-			let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
+			let locs = loc.0.map_source_locations(&[loc.1]);
 			eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
 		}
 		eprintln!(" {}", value);
@@ -212,9 +212,9 @@
 	}
 }
 
-pub fn extvar_source(name: &str) -> Source {
+pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
 	let source_name = format!("<extvar:{}>", name);
-	Source::new_virtual(Cow::Owned(source_name))
+	Source::new_virtual(Cow::Owned(source_name), code.into())
 }
 
 pub struct ContextInitializer {
@@ -260,8 +260,9 @@
 			.ext_vars
 			.insert(name, TlaArg::String(value));
 	}
-	pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {
-		let source = extvar_source(name);
+	pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {
+		let code = code.into();
+		let source = extvar_source(name, code.clone());
 		let parsed = jrsonnet_parser::parse(
 			&code,
 			&jrsonnet_parser::ParserSettings {
@@ -270,7 +271,6 @@
 		)
 		.map_err(|e| ImportSyntaxError {
 			path: source,
-			source_code: code.clone().into(),
 			error: Box::new(e),
 		})?;
 		// self.data_mut().volatile_files.insert(source_name, code);
@@ -297,11 +297,13 @@
 			.hide()
 			.value(
 				s,
-				Val::Str(match source.repr() {
-					Ok(p) => p.display().to_string().into(),
-					// Virtual files end up as empty strings in std.thisFile
-					Err(_e) => "".into(),
-				}),
+				Val::Str(
+					source
+						.path()
+						.map(|p| p.display().to_string())
+						.unwrap_or_else(String::new)
+						.into(),
+				),
 			)
 			.expect("this object builder is empty");
 		let stdlib_with_this_file = builder.build();
@@ -343,7 +345,7 @@
 	settings: Rc<RefCell<Settings>>,
 ))]
 fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {
-	let ctx = s.create_default_context(extvar_source(&x));
+	let ctx = s.create_default_context(extvar_source(&x, ""));
 	Ok(Any(this
 		.settings
 		.borrow()