difftreelog
fix ignore jpath when resolving filename passed to jrsonnet
in: master
4 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -11,6 +11,7 @@
error::{Error as JrError, ErrorKind},
ResultExt, State, Val,
};
+use jrsonnet_parser::{SourceDefaultIgnoreJpath, SourcePath};
#[cfg(feature = "mimalloc")]
#[global_allocator]
@@ -182,7 +183,7 @@
let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(input.as_str())?
+ s.import_from(&SourcePath::new(SourceDefaultIgnoreJpath), input.as_str())?
};
let tla = opts.tla.tla_opts()?;
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -10,7 +10,9 @@
use fs::File;
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::IBytes;
-use jrsonnet_parser::{IStr, SourceDirectory, SourceFifo, SourceFile, SourcePath};
+use jrsonnet_parser::{
+ IStr, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
+};
use crate::{
bail,
@@ -183,6 +185,13 @@
o
} else if let Some(d) = from.downcast_ref::<SourceDirectory>() {
d.path().to_owned()
+ } else if from.downcast_ref::<SourceDefaultIgnoreJpath>().is_some() {
+ let mut direct = current_dir().map_err(|e| ImportIo(e.to_string()))?;
+ direct.push(path);
+ if let Some(direct) = check_path(&direct)? {
+ return Ok(direct);
+ }
+ bail!(ImportFileNotFound(from.clone(), path.to_owned()))
} else if from.is_default() {
current_dir().map_err(|e| ImportIo(e.to_string()))?
} else {
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -12,7 +12,8 @@
mod unescape;
pub use location::CodeLocation;
pub use source::{
- Source, SourceDirectory, SourceFifo, SourceFile, SourcePath, SourcePathT, SourceVirtual,
+ Source, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
+ SourcePathT, SourceVirtual,
};
pub struct ParserSettings {
crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth1use std::{2 any::Any,3 fmt::{self, Debug, Display},4 hash::{Hash, Hasher},5 path::{Path, PathBuf},6 rc::Rc,7};89use jrsonnet_gcmodule::Acyclic;10use jrsonnet_interner::{IBytes, IStr};1112use crate::location::{location_to_offset, offset_to_location, CodeLocation};1314macro_rules! any_ext_methods {15 ($T:ident) => {16 fn as_any(&self) -> &dyn Any;17 fn dyn_hash(&self, hasher: &mut dyn Hasher);18 fn dyn_eq(&self, other: &dyn $T) -> bool;19 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;20 };21}22macro_rules! any_ext_impl {23 ($T:ident) => {24 fn as_any(&self) -> &dyn Any {25 self26 }27 fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {28 self.hash(&mut hasher)29 }30 fn dyn_eq(&self, other: &dyn $T) -> bool {31 let Some(other) = other.as_any().downcast_ref::<Self>() else {32 return false;33 };34 let this = <Self as $T>::as_any(self)35 .downcast_ref::<Self>()36 .expect("restricted by impl");37 this == other38 }39 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40 <Self as std::fmt::Debug>::fmt(self, fmt)41 }42 };43}44macro_rules! any_ext {45 ($T:ident) => {46 impl Hash for dyn $T {47 fn hash<H: Hasher>(&self, state: &mut H) {48 self.dyn_hash(state)49 }50 }51 impl PartialEq for dyn $T {52 fn eq(&self, other: &Self) -> bool {53 self.dyn_eq(other)54 }55 }56 impl Eq for dyn $T {}57 };58}59pub trait SourcePathT: Acyclic + Debug + Display {60 /// This method should be checked by resolver before panicking with bad SourcePath input61 /// if `true` - then resolver may threat this path as default, and default is usally a CWD62 fn is_default(&self) -> bool;63 fn path(&self) -> Option<&Path>;64 any_ext_methods!(SourcePathT);65}66any_ext!(SourcePathT);6768/// Represents location of a file69///70/// Standard CLI only operates using71/// - [`SourceFile`] - for any file72/// - [`SourceDirectory`] - for resolution from CWD73/// - [`SourceVirtual`] - for stdlib/ext-str74/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)75///76/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained77/// from assigned `ImportResolver`78/// However, you should always check `is_default` method return, as it will return true for any paths, where default79/// search location is applicable80///81/// Resolver may also return custom implementations of this trait, for example it may return http url in case of remotely loaded files82#[derive(Eq, Debug, Clone, Acyclic)]83pub struct SourcePath(Rc<dyn SourcePathT>);84impl SourcePath {85 pub fn new(inner: impl SourcePathT) -> Self {86 Self(Rc::new(inner))87 }88 pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {89 self.0.as_any().downcast_ref()90 }91 pub fn is_default(&self) -> bool {92 self.0.is_default()93 }94 pub fn path(&self) -> Option<&Path> {95 self.0.path()96 }97}98impl Hash for SourcePath {99 fn hash<H: Hasher>(&self, state: &mut H) {100 self.0.hash(state);101 }102}103impl PartialEq for SourcePath {104 #[allow(clippy::op_ref)]105 fn eq(&self, other: &Self) -> bool {106 &*self.0 == &*other.0107 }108}109impl Display for SourcePath {110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {111 write!(f, "{}", self.0)112 }113}114impl Default for SourcePath {115 fn default() -> Self {116 Self(Rc::new(SourceDefault))117 }118}119120#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]121struct SourceDefault;122impl Display for SourceDefault {123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {124 write!(f, "<default>")125 }126}127impl SourcePathT for SourceDefault {128 fn is_default(&self) -> bool {129 true130 }131 fn path(&self) -> Option<&Path> {132 None133 }134 any_ext_impl!(SourcePathT);135}136137/// Represents path to the file on the disk138/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:139///140/// When `file` is being resolved from `SourceFile(a/b/c)`, it should be resolved to `SourceFile(a/b/file)`,141/// however if it is being resolved from `SourceDirectory(a/b/c)`, then it should be resolved to `SourceDirectory(a/b/c/file)`142#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]143pub struct SourceFile(PathBuf);144impl SourceFile {145 pub fn new(path: PathBuf) -> Self {146 Self(path)147 }148 pub fn path(&self) -> &Path {149 &self.0150 }151}152impl Display for SourceFile {153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {154 write!(f, "{}", self.0.display())155 }156}157impl SourcePathT for SourceFile {158 fn is_default(&self) -> bool {159 false160 }161 fn path(&self) -> Option<&Path> {162 Some(&self.0)163 }164 any_ext_impl!(SourcePathT);165}166167/// Represents path to the directory on the disk168///169/// See also [`SourceFile`]170#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]171pub struct SourceDirectory(PathBuf);172impl SourceDirectory {173 pub fn new(path: PathBuf) -> Self {174 Self(path)175 }176 pub fn path(&self) -> &Path {177 &self.0178 }179}180impl Display for SourceDirectory {181 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {182 write!(f, "{}", self.0.display())183 }184}185impl SourcePathT for SourceDirectory {186 fn is_default(&self) -> bool {187 false188 }189 fn path(&self) -> Option<&Path> {190 Some(&self.0)191 }192 any_ext_impl!(SourcePathT);193}194195/// Represents virtual file, whose are located in memory, and shouldn't be cached196///197/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,198/// and user can construct arbitrary values by hand, without asking import resolver199#[derive(Acyclic, Hash, PartialEq, Eq, Debug, Clone)]200pub struct SourceVirtual(pub IStr);201impl Display for SourceVirtual {202 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {203 write!(f, "{}", self.0)204 }205}206impl SourcePathT for SourceVirtual {207 fn is_default(&self) -> bool {208 true209 }210 fn path(&self) -> Option<&Path> {211 None212 }213 any_ext_impl!(SourcePathT);214}215216/// Represents resolved FIFO file, those files may only be read once, and this type is only used for217/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`218/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`219/// for better cross-platform support.220// PartialEq is limited to ptr equality221#[allow(clippy::derived_hash_with_manual_eq)]222#[derive(Acyclic, Debug, Hash)]223pub struct SourceFifo(pub String, pub IBytes);224impl PartialEq for SourceFifo {225 fn eq(&self, other: &Self) -> bool {226 std::ptr::eq(self, other)227 }228}229impl fmt::Display for SourceFifo {230 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {231 write!(f, "fifo({:?})", self.0)232 }233}234impl SourcePathT for SourceFifo {235 fn is_default(&self) -> bool {236 // In case of FD input, user won't expect relative paths to be resolved from /dev/fd/237 true238 }239240 fn path(&self) -> Option<&Path> {241 None242 }243244 any_ext_impl!(SourcePathT);245}246247/// Either real file, or virtual248/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut249#[derive(Clone, PartialEq, Eq, Debug, Acyclic)]250pub struct Source(pub Rc<(SourcePath, IStr)>);251252impl Source {253 pub fn new(path: SourcePath, code: IStr) -> Self {254 Self(Rc::new((path, code)))255 }256257 pub fn new_virtual(name: IStr, code: IStr) -> Self {258 Self::new(SourcePath::new(SourceVirtual(name)), code)259 }260261 pub fn code(&self) -> &str {262 &self.0 .1263 }264265 pub fn source_path(&self) -> &SourcePath {266 &self.0 .0267 }268269 pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {270 offset_to_location(&self.0 .1, locs)271 }272 pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {273 location_to_offset(&self.0 .1, line, column)274 }275}1use std::{2 any::Any,3 fmt::{self, Debug, Display},4 hash::{Hash, Hasher},5 path::{Path, PathBuf},6 rc::Rc,7};89use jrsonnet_gcmodule::Acyclic;10use jrsonnet_interner::{IBytes, IStr};1112use crate::location::{location_to_offset, offset_to_location, CodeLocation};1314macro_rules! any_ext_methods {15 ($T:ident) => {16 fn as_any(&self) -> &dyn Any;17 fn dyn_hash(&self, hasher: &mut dyn Hasher);18 fn dyn_eq(&self, other: &dyn $T) -> bool;19 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result;20 };21}22macro_rules! any_ext_impl {23 ($T:ident) => {24 fn as_any(&self) -> &dyn Any {25 self26 }27 fn dyn_hash(&self, mut hasher: &mut dyn Hasher) {28 self.hash(&mut hasher)29 }30 fn dyn_eq(&self, other: &dyn $T) -> bool {31 let Some(other) = other.as_any().downcast_ref::<Self>() else {32 return false;33 };34 let this = <Self as $T>::as_any(self)35 .downcast_ref::<Self>()36 .expect("restricted by impl");37 this == other38 }39 fn dyn_debug(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40 <Self as std::fmt::Debug>::fmt(self, fmt)41 }42 };43}44macro_rules! any_ext {45 ($T:ident) => {46 impl Hash for dyn $T {47 fn hash<H: Hasher>(&self, state: &mut H) {48 self.dyn_hash(state)49 }50 }51 impl PartialEq for dyn $T {52 fn eq(&self, other: &Self) -> bool {53 self.dyn_eq(other)54 }55 }56 impl Eq for dyn $T {}57 };58}59pub trait SourcePathT: Acyclic + Debug + Display {60 /// This method should be checked by resolver before panicking with bad SourcePath input61 /// if `true` - then resolver may threat this path as default, and default is usally a CWD62 fn is_default(&self) -> bool;63 fn path(&self) -> Option<&Path>;64 any_ext_methods!(SourcePathT);65}66any_ext!(SourcePathT);6768/// Represents location of a file69///70/// Standard CLI only operates using71/// - [`SourceFile`] - for any file72/// - [`SourceDirectory`] - for resolution from CWD73/// - [`SourceVirtual`] - for stdlib/ext-str74/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)75///76/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained77/// from assigned `ImportResolver`78/// However, you should always check `is_default` method return, as it will return true for any paths, where default79/// search location is applicable80///81/// Resolver may also return custom implementations of this trait, for example it may return http url in case of remotely loaded files82#[derive(Eq, Debug, Clone, Acyclic)]83pub struct SourcePath(Rc<dyn SourcePathT>);84impl SourcePath {85 pub fn new(inner: impl SourcePathT) -> Self {86 Self(Rc::new(inner))87 }88 pub fn downcast_ref<T: SourcePathT>(&self) -> Option<&T> {89 self.0.as_any().downcast_ref()90 }91 pub fn is_default(&self) -> bool {92 self.0.is_default()93 }94 pub fn path(&self) -> Option<&Path> {95 self.0.path()96 }97}98impl Hash for SourcePath {99 fn hash<H: Hasher>(&self, state: &mut H) {100 self.0.hash(state);101 }102}103impl PartialEq for SourcePath {104 #[allow(clippy::op_ref)]105 fn eq(&self, other: &Self) -> bool {106 &*self.0 == &*other.0107 }108}109impl Display for SourcePath {110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {111 write!(f, "{}", self.0)112 }113}114impl Default for SourcePath {115 fn default() -> Self {116 Self(Rc::new(SourceDefault))117 }118}119120#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]121struct SourceDefault;122impl Display for SourceDefault {123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {124 write!(f, "<default>")125 }126}127impl SourcePathT for SourceDefault {128 fn is_default(&self) -> bool {129 true130 }131 fn path(&self) -> Option<&Path> {132 None133 }134 any_ext_impl!(SourcePathT);135}136137#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]138pub struct SourceDefaultIgnoreJpath;139impl Display for SourceDefaultIgnoreJpath {140 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {141 write!(f, "<default (ignoring jpath)>")142 }143}144impl SourcePathT for SourceDefaultIgnoreJpath {145 fn is_default(&self) -> bool {146 true147 }148 fn path(&self) -> Option<&Path> {149 None150 }151 any_ext_impl!(SourcePathT);152}153154/// Represents path to the file on the disk155/// Directories shouldn't be put here, as resolution for files differs from resolution for directories:156///157/// When `file` is being resolved from `SourceFile(a/b/c)`, it should be resolved to `SourceFile(a/b/file)`,158/// however if it is being resolved from `SourceDirectory(a/b/c)`, then it should be resolved to `SourceDirectory(a/b/c/file)`159#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]160pub struct SourceFile(PathBuf);161impl SourceFile {162 pub fn new(path: PathBuf) -> Self {163 Self(path)164 }165 pub fn path(&self) -> &Path {166 &self.0167 }168}169impl Display for SourceFile {170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {171 write!(f, "{}", self.0.display())172 }173}174impl SourcePathT for SourceFile {175 fn is_default(&self) -> bool {176 false177 }178 fn path(&self) -> Option<&Path> {179 Some(&self.0)180 }181 any_ext_impl!(SourcePathT);182}183184/// Represents path to the directory on the disk185///186/// See also [`SourceFile`]187#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]188pub struct SourceDirectory(PathBuf);189impl SourceDirectory {190 pub fn new(path: PathBuf) -> Self {191 Self(path)192 }193 pub fn path(&self) -> &Path {194 &self.0195 }196}197impl Display for SourceDirectory {198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {199 write!(f, "{}", self.0.display())200 }201}202impl SourcePathT for SourceDirectory {203 fn is_default(&self) -> bool {204 false205 }206 fn path(&self) -> Option<&Path> {207 Some(&self.0)208 }209 any_ext_impl!(SourcePathT);210}211212/// Represents virtual file, whose are located in memory, and shouldn't be cached213///214/// It is used for --ext-code=.../--tla-code=.../standard library source code by default,215/// and user can construct arbitrary values by hand, without asking import resolver216#[derive(Acyclic, Hash, PartialEq, Eq, Debug, Clone)]217pub struct SourceVirtual(pub IStr);218impl Display for SourceVirtual {219 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {220 write!(f, "{}", self.0)221 }222}223impl SourcePathT for SourceVirtual {224 fn is_default(&self) -> bool {225 true226 }227 fn path(&self) -> Option<&Path> {228 None229 }230 any_ext_impl!(SourcePathT);231}232233/// Represents resolved FIFO file, those files may only be read once, and this type is only used for234/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`235/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`236/// for better cross-platform support.237// PartialEq is limited to ptr equality238#[allow(clippy::derived_hash_with_manual_eq)]239#[derive(Acyclic, Debug, Hash)]240pub struct SourceFifo(pub String, pub IBytes);241impl PartialEq for SourceFifo {242 fn eq(&self, other: &Self) -> bool {243 std::ptr::eq(self, other)244 }245}246impl fmt::Display for SourceFifo {247 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {248 write!(f, "fifo({:?})", self.0)249 }250}251impl SourcePathT for SourceFifo {252 fn is_default(&self) -> bool {253 // In case of FD input, user won't expect relative paths to be resolved from /dev/fd/254 true255 }256257 fn path(&self) -> Option<&Path> {258 None259 }260261 any_ext_impl!(SourcePathT);262}263264/// Either real file, or virtual265/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut266#[derive(Clone, PartialEq, Eq, Debug, Acyclic)]267pub struct Source(pub Rc<(SourcePath, IStr)>);268269impl Source {270 pub fn new(path: SourcePath, code: IStr) -> Self {271 Self(Rc::new((path, code)))272 }273274 pub fn new_virtual(name: IStr, code: IStr) -> Self {275 Self::new(SourcePath::new(SourceVirtual(name)), code)276 }277278 pub fn code(&self) -> &str {279 &self.0 .1280 }281282 pub fn source_path(&self) -> &SourcePath {283 &self.0 .0284 }285286 pub fn map_source_locations<const S: usize>(&self, locs: &[u32; S]) -> [CodeLocation; S] {287 offset_to_location(&self.0 .1, locs)288 }289 pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {290 location_to_offset(&self.0 .1, line, column)291 }292}