Skip to content

Commit

Permalink
Wire it up to eval and export
Browse files Browse the repository at this point in the history
  • Loading branch information
jneem committed May 10, 2024
1 parent 8d65ef9 commit 77eedfd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
11 changes: 11 additions & 0 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ pub enum Error {
error: CliUsageError,
},
NoManifest,
/// The provided manifest path doesn't have a parent directory.
NoPackageRoot {
manifest_path: std::path::PathBuf,
},
Package {
error: nickel_lang_package::Error,
},
Expand Down Expand Up @@ -290,6 +294,13 @@ impl Error {
report_standalone("failed to read manifest file", Some(error.to_string()))
}
}
Error::NoPackageRoot { manifest_path } => report_standalone(
&format!(
"invalid manifest path `{}` has no parent",
manifest_path.display()
),
None,
),
}
}
}
16 changes: 16 additions & 0 deletions cli/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use nickel_lang_core::{eval::cache::lazy::CBNCache, program::Program};
use nickel_lang_package::ManifestFile;

use crate::{cli::GlobalOptions, customize::Customize, error::CliResult};

Expand All @@ -23,6 +24,9 @@ pub struct InputOptions<Customize: clap::Args> {
#[arg(long, short = 'I', global = true)]
pub import_path: Vec<PathBuf>,

#[arg(long, global = true)]
pub manifest_path: Option<PathBuf>,

#[command(flatten)]
pub customize_mode: Customize,
}
Expand All @@ -47,6 +51,18 @@ impl<C: clap::Args + Customize> Prepare for InputOptions<C> {
program.add_import_paths(nickel_path.split(':'));
}

if let Some(manifest_path) = self.manifest_path.as_ref() {
let root_path =
manifest_path
.parent()
.ok_or_else(|| crate::error::Error::NoPackageRoot {
manifest_path: manifest_path.clone(),
})?;
let lock_file = ManifestFile::from_path(manifest_path)?.lock()?;
let package_map = lock_file.resolve_package_map(root_path.to_owned())?;
program.set_package_map(package_map);
}

#[cfg(debug_assertions)]
if self.nostdlib {
program.set_skip_stdlib();
Expand Down
11 changes: 10 additions & 1 deletion core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,16 @@ impl IntoDiagnostics<FileId> for ImportError {

vec![Diagnostic::error().with_message(msg).with_labels(labels)]
}
ImportError::NoPackageMap { pos } => todo!(),
ImportError::NoPackageMap { pos } => {
let labels = pos
.as_opt_ref()
.map(|span| vec![primary(span).with_message("imported here")])
.unwrap_or_default();
vec![Diagnostic::error()
.with_message("tried to import from a package, but no package manifest found")
.with_labels(labels)
.with_notes(vec!["did you forget a --manifest-path argument?".to_owned()])]
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions package/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub use manifest::ManifestFile;

// TODO: enrich all these errors with the location of the upstream manifest file
pub enum Error {
Io {
error: std::io::Error,
},
ManifestEval {
package: Option<Name>,
program: Program<CacheImpl>,
Expand All @@ -32,6 +35,9 @@ pub enum Error {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Io { error } => {
write!(f, "I/O error: {error}")
}
Error::ManifestEval { .. } => todo!(),
Error::InvalidPathImport { spec } => {
write!(f, "invalid import path: {spec:?}")
Expand All @@ -44,6 +50,12 @@ impl std::fmt::Display for Error {
}
}

impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::Io { error }
}
}

/// A source includes the place to fetch a package from (e.g. git or a registry),
/// along with possibly some narrowing-down of the allowed versions (e.g. a range
/// of versions, or a git commit id).
Expand Down
4 changes: 3 additions & 1 deletion package/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use directories::ProjectDirs;
use nickel_lang_core::{
cache::normalize_abs_path,
cache::{normalize_abs_path, normalize_path},
package::{Name, ObjectId, PackageMap},
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -149,6 +149,8 @@ impl LockFile {
// been re-written to git dependencies; and there are no path dependencies of path dependencies
// because those haven't been expanded yet.

let root_path = normalize_path(root_path)?;

let mut ret = PackageMap {
// Make all path dependencies of the root absolute.
top_level: self
Expand Down

0 comments on commit 77eedfd

Please sign in to comment.