feat(tvix/glue): context-aware toFile

This removes the reference tracking and uses instead the context for
references and returns some.

Change-Id: Ic359ca6b903b63f1a9c679c566004c617b792442
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10435
Autosubmit: raitobezarius <tvl@lahfa.xyz>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
This commit is contained in:
Ryan Lahfa 2023-12-26 01:30:05 +01:00 committed by clbot
parent cc098b9aaa
commit 20c894e232
2 changed files with 23 additions and 21 deletions

View file

@ -202,6 +202,9 @@ pub enum ErrorKind {
context: String, context: String,
underlying: Box<ErrorKind>, underlying: Box<ErrorKind>,
}, },
/// Unexpected context string
UnexpectedContext,
} }
impl error::Error for Error { impl error::Error for Error {
@ -484,6 +487,10 @@ to a missing value in the attribute set(s) included via `with`."#,
ErrorKind::WithContext { .. } => { ErrorKind::WithContext { .. } => {
panic!("internal ErrorKind::WithContext variant leaked") panic!("internal ErrorKind::WithContext variant leaked")
} }
ErrorKind::UnexpectedContext => {
write!(f, "unexpected context string")
}
} }
} }
} }
@ -736,6 +743,7 @@ impl Error {
ErrorKind::InvalidAttributeName(_) => "in this attribute set", ErrorKind::InvalidAttributeName(_) => "in this attribute set",
ErrorKind::RelativePathResolution(_) => "in this path literal", ErrorKind::RelativePathResolution(_) => "in this path literal",
ErrorKind::UnexpectedArgument { .. } => "in this function call", ErrorKind::UnexpectedArgument { .. } => "in this function call",
ErrorKind::UnexpectedContext => "in this string",
// The spans for some errors don't have any more descriptive stuff // The spans for some errors don't have any more descriptive stuff
// in them, or we don't utilise it yet. // in them, or we don't utilise it yet.
@ -810,6 +818,7 @@ impl Error {
ErrorKind::Xml(_) => "E034", ErrorKind::Xml(_) => "E034",
ErrorKind::FromTomlError(_) => "E035", ErrorKind::FromTomlError(_) => "E035",
ErrorKind::NotSerialisableToJson(_) => "E036", ErrorKind::NotSerialisableToJson(_) => "E036",
ErrorKind::UnexpectedContext => "E037",
// Special error code for errors from other Tvix // Special error code for errors from other Tvix
// components. We may want to introduce a code namespacing // components. We may want to introduce a code namespacing

View file

@ -464,30 +464,23 @@ pub(crate) mod derivation_builtins {
.to_str() .to_str()
.context("evaluating the `name` parameter of builtins.toFile")?; .context("evaluating the `name` parameter of builtins.toFile")?;
let content = content let content = content
.to_str() .to_contextful_str()
.context("evaluating the `content` parameter of builtins.toFile")?; .context("evaluating the `content` parameter of builtins.toFile")?;
let mut refscan = state.borrow().reference_scanner(); if content.iter_derivation().count() > 0 || content.iter_single_outputs().count() > 0 {
refscan.scan(content.as_str()); return Err(ErrorKind::UnexpectedContext);
let refs = { }
let paths = state.borrow();
refscan
.finalise()
.into_iter()
.map(|path| paths[&path].path.to_string())
.collect::<Vec<_>>()
};
// TODO: fail on derivation references (only "plain" is allowed here) let path = nix_compat::store_path::build_text_path(
name.as_str(),
let path = nix_compat::store_path::build_text_path(name.as_str(), content.as_str(), refs) content.as_str(),
.map_err(|_e| { content.iter_plain(),
nix_compat::derivation::DerivationError::InvalidOutputName( )
name.as_str().to_string(), .map_err(|_e| {
) nix_compat::derivation::DerivationError::InvalidOutputName(name.as_str().to_string())
}) })
.map_err(DerivationError::InvalidDerivation)? .map_err(DerivationError::InvalidDerivation)?
.to_absolute_path(); .to_absolute_path();
state.borrow_mut().plain(&path); state.borrow_mut().plain(&path);