Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package management #1903

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"vector",
"lsp/nls",
"lsp/lsp-harness",
"package",
"utils",
"wasm-repl",
"pyckel",
Expand All @@ -25,6 +26,7 @@ readme = "README.md"
[workspace.dependencies]
nickel-lang-core = { version = "0.10.0", path = "./core", default-features = false }
nickel-lang-git = { version = "0.1.0", path = "./git" }
nickel-lang-package = { version = "0.1.0", path = "./package" }
nickel-lang-vector = { version = "0.1.0", path = "./vector" }
nickel-lang-utils = { version = "0.1.0", path = "./utils" }
lsp-harness = { version = "0.1.0", path = "./lsp/lsp-harness" }
Expand Down Expand Up @@ -58,6 +60,7 @@ directories = "4.0.1"
env_logger = "0.10"
git-version = "0.3.5"
gix = "0.67.0"
gix-hash = "0.15.0"
indexmap = "1.9.3"
indoc = "2"
insta = "1.29.0"
Expand Down
5 changes: 5 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ metrics = ["dep:metrics", "dep:metrics-util", "nickel-lang-core/metrics"]

[dependencies]
nickel-lang-core = { workspace = true, features = [ "markdown", "clap" ], default-features = false }
# TODO: make optional
nickel-lang-package.workspace = true
gix = { workspace = true, features = ["blocking-http-transport-reqwest-rust-tls"]}
# TODO: use the version parsing in nickel-lang-package instead
semver = { version = "1.0.23", features = ["serde"] }

clap = { workspace = true, features = ["derive", "string"] }
serde = { workspace = true, features = ["derive"] }
Expand Down
5 changes: 4 additions & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use git_version::git_version;

use crate::{
completions::GenCompletionsCommand, eval::EvalCommand, export::ExportCommand,
pprint_ast::PprintAstCommand, query::QueryCommand, typecheck::TypecheckCommand,
package::PackageCommand, pprint_ast::PprintAstCommand, query::QueryCommand,
typecheck::TypecheckCommand,
};

use nickel_lang_core::error::report::ErrorFormat;
Expand Down Expand Up @@ -72,6 +73,8 @@ pub enum Command {
Export(ExportCommand),
/// Prints the metadata attached to an attribute, given as a path
Query(QueryCommand),
/// Performs packaging and dependency-resolution operations
Package(PackageCommand),
/// Typechecks the program but does not run it
Typecheck(TypecheckCommand),
/// Starts a REPL session
Expand Down
39 changes: 39 additions & 0 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Error handling for the CLI.

use std::path::PathBuf;

use nickel_lang_core::{
error::{
report::{report, ColorOpt, ErrorFormat},
Expand Down Expand Up @@ -53,6 +55,14 @@ pub enum Error {
files: Files,
error: CliUsageError,
},
NoManifest,
/// Provided a path without a parent directory.
PathWithoutParent {
path: PathBuf,
},
Package {
error: nickel_lang_package::error::Error,
},
FailedTests,
}

Expand Down Expand Up @@ -240,6 +250,12 @@ impl From<nickel_lang_core::repl::InitError> for Error {
}
}

impl From<nickel_lang_package::error::Error> for Error {
fn from(error: nickel_lang_package::error::Error) -> Self {
Error::Package { error }
}
}

// Report a standalone error which doesn't actually refer to any source code.
//
// Wrapping all errors in a diagnostic makes sure all errors are rendered using
Expand Down Expand Up @@ -280,6 +296,29 @@ impl Error {
Error::Format { error } => report_with_msg("format error", error.to_string()),
Error::CliUsage { error, mut files } => core_report(&mut files, error, format, color),
Error::FailedTests => report_str("tests failed"),
Error::NoManifest => report_str("failed to find a manifest file"),
Error::Package { error } => {
if let nickel_lang_package::error::Error::ManifestEval {
package,
mut files,
error,
} = error
{
let msg = if let Some(package) = package {
format!("failed to evaluate manifest file for package {package}")
} else {
"failed to evaluate package manifest".to_owned()
};
report_str(&msg);
core_report(&mut files, error, format, color);
} else {
report_with_msg("failed to read manifest file", error.to_string())
}
}
Error::PathWithoutParent { path } => report_str(&format!(
"path {} doesn't have a parent directory",
path.display()
)),
}
}
}
30 changes: 29 additions & 1 deletion cli/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::path::PathBuf;

use nickel_lang_core::{eval::cache::lazy::CBNCache, program::Program};
use nickel_lang_package::{config::Config as PackageConfig, lock::LockFile};

use crate::{customize::Customize, global::GlobalContext};
use crate::{customize::Customize, error::Error, global::GlobalContext};

#[derive(clap::Parser, Debug)]
pub struct InputOptions<Customize: clap::Args> {
Expand All @@ -26,6 +27,19 @@ pub struct InputOptions<Customize: clap::Args> {

#[command(flatten)]
pub customize_mode: Customize,

/// Path to a package lock file.
///
/// This is required for package management features to work. (Future
/// versions may auto-detect a lock file.)
#[arg(long, global = true)]
pub lock_file: Option<PathBuf>,

#[arg(long, global = true)]
/// Filesystem location for caching fetched packages.
///
/// Defaults to an appropriate platform-dependent value.
pub package_cache_dir: Option<PathBuf>,
}

pub enum PrepareError {
Expand Down Expand Up @@ -66,6 +80,20 @@ impl<C: clap::Args + Customize> Prepare for InputOptions<C> {
program.add_import_paths(nickel_path.split(':'));
}

if let Some(lock_file_path) = self.lock_file.as_ref() {
let lock_file = LockFile::from_path(lock_file_path);
let lock_dir = lock_file_path
.parent()
.ok_or_else(|| Error::PathWithoutParent {
path: lock_file_path.clone(),
})?;
let mut config = PackageConfig::default();
if let Some(cache_dir) = self.package_cache_dir.as_ref() {
config = config.with_cache_dir(cache_dir.to_owned());
};
program.set_package_map(lock_file.package_map(lock_dir, &config)?);
}

#[cfg(debug_assertions)]
if self.nostdlib {
program.set_skip_stdlib();
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod eval;
mod export;
mod global;
mod input;
mod package;
mod pprint_ast;
mod query;
mod typecheck;
Expand Down Expand Up @@ -48,6 +49,7 @@ fn main() -> ExitCode {
Command::Export(export) => export.run(&mut ctxt),
Command::Query(query) => query.run(&mut ctxt),
Command::Typecheck(typecheck) => typecheck.run(&mut ctxt),
Command::Package(package) => package.run(&mut ctxt),
Command::GenCompletions(completions) => completions.run(&mut ctxt),

#[cfg(feature = "repl")]
Expand Down
Loading
Loading