Skip to content

Commit b6ded63

Browse files
authored
Gate SQLite history behind existing sqlite feature (nushell#16682)
This PR gates the reedline SQLite history behind the top-level crate's `sqlite` feature. Reedline already feature gates the SQLite history, but currently Nushell always pulls it in anyways. Gating the reedline `sqlite` feature behind Nushell's `sqlite` feature makes it possible to build Nushell without pulling in SQLite at all, which can shave off a couple seconds during compilation, especially for the test and release profiles. Here's the commands I used for testing: ``` cargo clean --release cargo build --release --no-default-features --features plugin,rustls-tls,system-clipboard,trash-support ``` Here's the time the `cargo build` command took on my system (using the default `.cargo/config.toml`) * `main`: 2m 34s * `sqlite-history-gate`: 2m 24s Some of the `#[cfg(feature = "sqlite")]`s are pretty haphazard, @cptpiepmatz you might have some better ideas on how to make it a bit nicer and this may also conflict with your SQLite changes ## Release notes summary - What our users need to know Nushell can now be built without pulling in SQLite as a dependency.
1 parent 9ff90b6 commit b6ded63

File tree

12 files changed

+67
-23
lines changed

12 files changed

+67
-23
lines changed

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ nu-protocol = { path = "./crates/nu-protocol", version = "0.107.1" }
228228
nu-std = { path = "./crates/nu-std", version = "0.107.1" }
229229
nu-system = { path = "./crates/nu-system", version = "0.107.1" }
230230
nu-utils = { path = "./crates/nu-utils", version = "0.107.1" }
231-
reedline = { workspace = true, features = ["bashisms", "sqlite"] }
231+
reedline = { workspace = true, features = ["bashisms"] }
232232

233233
crossterm = { workspace = true }
234234
ctrlc = { workspace = true }
@@ -314,7 +314,13 @@ system-clipboard = [
314314
trash-support = ["nu-command/trash-support"]
315315

316316
# SQLite commands for nushell
317-
sqlite = ["nu-command/sqlite", "nu-std/sqlite"]
317+
sqlite = [
318+
"nu-cli/sqlite",
319+
"nu-command/sqlite",
320+
"nu-std/sqlite",
321+
"nu-protocol/sqlite",
322+
"reedline/sqlite"
323+
]
318324

319325
[profile.release]
320326
opt-level = "s" # Optimize for size

crates/nu-cli/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ nu-utils = { path = "../nu-utils", version = "0.107.1" }
3030
nu-color-config = { path = "../nu-color-config", version = "0.107.1" }
3131
nu-experimental = { path = "../nu-experimental", version = "0.107.1" }
3232
nu-ansi-term = { workspace = true }
33-
reedline = { workspace = true, features = ["bashisms", "sqlite"] }
33+
reedline = { workspace = true, features = ["bashisms"] }
3434

3535
chrono = { default-features = false, features = ["std"], workspace = true }
3636
crossterm = { workspace = true }
@@ -50,6 +50,7 @@ which = { workspace = true }
5050
[features]
5151
plugin = ["nu-plugin-engine"]
5252
system-clipboard = ["reedline/system_clipboard"]
53+
sqlite = ["reedline/sqlite"]
5354

5455
[lints]
5556
workspace = true

crates/nu-cli/src/commands/default_context.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ pub fn add_cli_context(mut engine_state: EngineState) -> EngineState {
1717
CommandlineGetCursor,
1818
CommandlineSetCursor,
1919
History,
20-
HistoryImport,
21-
HistorySession,
2220
Keybindings,
2321
KeybindingsDefault,
2422
KeybindingsList,
2523
KeybindingsListen,
2624
};
2725

26+
#[cfg(feature = "sqlite")]
27+
bind_command! {
28+
HistoryImport,
29+
HistorySession
30+
};
31+
2832
working_set.render()
2933
};
3034

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
// Each const is named after a HistoryItem field, and the value is the field name to be displayed to
22
// the user (or accept during import).
33
pub const COMMAND_LINE: &str = "command";
4-
pub const START_TIMESTAMP: &str = "start_timestamp";
5-
pub const HOSTNAME: &str = "hostname";
6-
pub const CWD: &str = "cwd";
7-
pub const EXIT_STATUS: &str = "exit_status";
8-
pub const DURATION: &str = "duration";
9-
pub const SESSION_ID: &str = "session_id";
4+
5+
#[cfg_attr(not(feature = "sqlite"), allow(dead_code))]
6+
mod sqlite_only_fields {
7+
pub const START_TIMESTAMP: &str = "start_timestamp";
8+
pub const HOSTNAME: &str = "hostname";
9+
pub const CWD: &str = "cwd";
10+
pub const EXIT_STATUS: &str = "exit_status";
11+
pub const DURATION: &str = "duration";
12+
pub const SESSION_ID: &str = "session_id";
13+
}
14+
15+
pub use sqlite_only_fields::*;

crates/nu-cli/src/commands/history/history_.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use nu_protocol::{
33
HistoryFileFormat,
44
shell_error::{self, io::IoError},
55
};
6-
use reedline::{
7-
FileBackedHistory, History as ReedlineHistory, HistoryItem, SearchDirection, SearchQuery,
8-
SqliteBackedHistory,
9-
};
6+
use reedline::{FileBackedHistory, History as ReedlineHistory, SearchDirection, SearchQuery};
7+
#[cfg(feature = "sqlite")]
8+
use reedline::{HistoryItem, SqliteBackedHistory};
109

1110
use super::fields;
1211

@@ -58,9 +57,12 @@ impl Command for History {
5857
return Ok(PipelineData::empty());
5958
}
6059

60+
#[cfg_attr(not(feature = "sqlite"), allow(unused_variables))]
6161
let long = call.has_flag(engine_state, stack, "long")?;
62+
6263
let signals = engine_state.signals().clone();
6364
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
65+
#[cfg(feature = "sqlite")]
6466
HistoryFileFormat::Sqlite => {
6567
SqliteBackedHistory::with_file(history_path.clone(), None, None)
6668
.map(|inner| {
@@ -102,6 +104,7 @@ impl Command for History {
102104
history_path,
103105
))?
104106
.into_pipeline_data(head, signals)),
107+
#[cfg(feature = "sqlite")]
105108
HistoryFileFormat::Sqlite => Ok(history_reader
106109
.and_then(|h| {
107110
h.search(SearchQuery::everything(SearchDirection::Forward, None))
@@ -142,6 +145,7 @@ impl Command for History {
142145
}
143146
}
144147

148+
#[cfg(feature = "sqlite")]
145149
fn create_sqlite_history_record(idx: usize, entry: HistoryItem, long: bool, head: Span) -> Value {
146150
//1. Format all the values
147151
//2. Create a record of either short or long columns and values
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
mod fields;
22
mod history_;
3+
4+
pub use history_::History;
5+
6+
// if more history formats are added, will need to reconsider this
7+
#[cfg(feature = "sqlite")]
38
mod history_import;
9+
#[cfg(feature = "sqlite")]
410
mod history_session;
511

6-
pub use history_::History;
12+
#[cfg(feature = "sqlite")]
713
pub use history_import::HistoryImport;
14+
#[cfg(feature = "sqlite")]
815
pub use history_session::HistorySession;

crates/nu-cli/src/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod keybindings_list;
77
mod keybindings_listen;
88

99
pub use commandline::{Commandline, CommandlineEdit, CommandlineGetCursor, CommandlineSetCursor};
10-
pub use history::{History, HistoryImport, HistorySession};
10+
pub use history::*;
1111
pub use keybindings::Keybindings;
1212
pub use keybindings_default::KeybindingsDefault;
1313
pub use keybindings_list::KeybindingsList;

crates/nu-cli/src/repl.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ use nu_utils::{
3434
filesystem::{PermissionResult, have_permission},
3535
perf,
3636
};
37+
#[cfg(feature = "sqlite")]
38+
use reedline::SqliteBackedHistory;
3739
use reedline::{
3840
CursorConfig, CwdAwareHinter, DefaultCompleter, EditCommand, Emacs, FileBackedHistory,
39-
HistorySessionId, Reedline, SqliteBackedHistory, Vi,
41+
HistorySessionId, Reedline, Vi,
4042
};
4143
use std::sync::atomic::Ordering;
4244
use std::{
@@ -512,10 +514,11 @@ fn loop_iteration(ctx: LoopContext) -> (bool, Stack, Reedline) {
512514
let line_editor_input_time = std::time::Instant::now();
513515
match input {
514516
Ok(Signal::Success(repl_cmd_line_text)) => {
515-
let history_supports_meta = matches!(
516-
engine_state.history_config().map(|h| h.file_format),
517-
Some(HistoryFileFormat::Sqlite)
518-
);
517+
let history_supports_meta = match engine_state.history_config().map(|h| h.file_format) {
518+
#[cfg(feature = "sqlite")]
519+
Some(HistoryFileFormat::Sqlite) => true,
520+
_ => false,
521+
};
519522

520523
if history_supports_meta {
521524
prepare_history_metadata(
@@ -1232,6 +1235,7 @@ fn update_line_editor_history(
12321235
FileBackedHistory::with_file(history.max_size as usize, history_path)
12331236
.into_diagnostic()?,
12341237
),
1238+
#[cfg(feature = "sqlite")]
12351239
HistoryFileFormat::Sqlite => Box::new(
12361240
SqliteBackedHistory::with_file(
12371241
history_path.to_path_buf(),
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
mod history_import;
21
mod keybindings_list;
32
mod nu_highlight;
3+
4+
#[cfg(feature = "sqlite")]
5+
mod history_import;

crates/nu-protocol/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ plugin = [
6666
"os",
6767
"rmp-serde",
6868
]
69+
# enables SQLite history
70+
sqlite = []
6971

7072
[dev-dependencies]
7173
serde_json = { workspace = true }

0 commit comments

Comments
 (0)