Skip to content

feat: add env_logger #32

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
152 changes: 114 additions & 38 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ which = "4.4.0"
[dependencies]
blake3 = "1.3.3"
bytelines = "2.4.0"
env_logger = { version = "0.10.1", default-features = false }
itertools = "0.10.5"
log = "0.4.20"
nix = "0.26.2"
once_cell = "1.17.1"
serde_json = "1.0.96"
Expand Down
7 changes: 4 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::os::unix::ffi::OsStrExt;
use std::os::unix::process::CommandExt;
use std::process::{exit, Command};
use ufcs::Pipe;
use log::info;

pub enum RunMode {
/// no arg
Expand Down Expand Up @@ -196,17 +197,17 @@ fn is_alpha(b: u8) -> bool {
}

fn exit_version() {
println!(
info!(
"cached-nix-shell v{}{}",
env!("CARGO_PKG_VERSION"),
option_env!("CNS_GIT_COMMIT")
.map(|x| format!("-{x}"))
.unwrap_or("".into())
);
if env!("CNS_NIX").is_empty() {
println!("Using nix-shell from $PATH");
info!("Using nix-shell from $PATH");
} else {
println!("Using {}nix-shell", env!("CNS_NIX"));
info!("Using {}nix-shell", env!("CNS_NIX"));
Comment on lines +200 to +210
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cached-nix-shell --version should not hide its version by default as it would confuse the user.
Also, since the original nix-shell --version prints its version to stdout (not stderr), CNS should too.

Copy link
Author

@olebedev olebedev Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I think hiding/swallowing something, version for example or any other useful information is not great, 100%.

However, the intention of the PR is to hide everything that confuse tooling/scripting around automatic nix/nix-shell invocations, not humans. So I would like to tap on this one a bit more if you don't mind :)

In particular, printing anything extra into stdout (not stderr) can have consequences like some tooling can fail due to an unexpected output, like three lines instead of one, like in this example. So the output is unrecognizable by tools if we replace vanilla nix-shell with the cached one.

Another thing that worries me is that the output for nix-store --version and nix-shell --version should be different in case if we decide to print it, right? Because nix-store has nothing to do with cached-nix-shell. So we kind of introduce a divergence that we would rather avoid.

cached-nix-shell --version should not hide its version by default as it would confuse the user.

What do you mean, a user will be confused with which tool nix-shell or cached-nix-shell is actually in use or the versions combination is important?

I think we can reach consensus here by set CNS_LOG_LEVEL=trace by default via Env::filter_or so the output is actually what it was before, but with slightly different formatting, due to the env_logger library use. This would allow to switch all the logs related to cached nix shell by setting CNS_LOG_LEVEL= which it a bit unfortunate for me but I am fine with that option if this sounds to you like something more appropriate.

Let me know what do you think about this.

In the meantime, I redone that so it prints to stdout (in case of --version argument only) and all the logging is still in place by default but can be turned off with CNS_LOG_LEVEL= or downgrade the logging level like CNS_LOG_LEVEL=info, please have a look.

Kind regards,
Oleg

}
std::io::stdout().flush().unwrap();
Command::new(concat!(env!("CNS_NIX"), "nix-shell"))
Expand Down
Loading