stdlib: add an automatic loading of "prelude" commands#8627
stdlib: add an automatic loading of "prelude" commands#8627kubouch merged 18 commits intonushell:mainfrom
Conversation
0d05fd4 to
72f52fb
Compare
|
thanks to @kubouch, it's almost working
however, i have an error when trying to run a command imported from reproduce the 👍 and the 🐛
cargo run -- --no-config-file
❗ TL;DR: almost there 😌 |
src/run.rs
Outdated
| working_set.add_file(name.clone(), content); | ||
| let end = working_set.next_span_start(); | ||
|
|
||
| let (_, module, _, _) = parse_module_block( |
There was a problem hiding this comment.
Here it would be good to preserve the module comments and pass them to add_module() so you can have a nice help message when calling help std.
There was a problem hiding this comment.
should be good in 61436bc, help std gives the comment now
src/run.rs
Outdated
| .decls | ||
| .get(&search_name.as_bytes().to_vec()) | ||
| .unwrap_or_else(|| { | ||
| panic!("could not load `{}` from `std`.", search_name) |
There was a problem hiding this comment.
We can't panic here, it should probably print an error and continue running. There are report_error() and report_error_new() functions for printing the error.
There was a problem hiding this comment.
it does not appear to solve the issue, but still in d0101dd 👌
src/run.rs
Outdated
| working_set.add_file(name.clone(), content); | ||
| let end = working_set.next_span_start(); | ||
|
|
||
| let (_, module, _, _) = parse_module_block( |
There was a problem hiding this comment.
Oh, and the panic you're seeing is probably caused by missing working_set.add_block(). You'll need to call that and pass the block returned from here to it. You can see an example in use implementation code:
nushell/crates/nu-parser/src/parse_keywords.rs
Line 2269 in a49e5b3
There was a problem hiding this comment.
i've tried something in fccbcb0, see my comment at the top where i explain how it behaves now 👍
|
Looks good, almost there. One thing I would change is to load |
src/run.rs
Outdated
| working_set.add_file(name.clone(), content); | ||
| let end = working_set.next_span_start(); | ||
|
|
||
| let (_, module, _, _) = parse_module_block( |
There was a problem hiding this comment.
Also (sorry for the spam :-D ), the error returned from parsing the module should be printed, if there is any. See the comment below how to report errors.
|
@amtoine Could you paste a backtrace from the panic? It's strange that it needs a mutable block... |
of course, there it is >_ RUST_BACKTRACE=full cargo run -- -c "assert"
thread 'main' panicked at 'Attempt to mutate a block that is in the permanent (immutable) state', crates/nu-protocol/src/engine/engine_state.rs:2055:13
stack backtrace:
0: 0x562dc41ecca0 - std::backtrace_rs::backtrace::libunwind::trace::h1d00f3fcf4cb5ac4
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
1: 0x562dc41ecca0 - std::backtrace_rs::backtrace::trace_unsynchronized::h920a6ff332484ee2
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
2: 0x562dc41ecca0 - std::sys_common::backtrace::_print_fmt::hd7323920c925af6d
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:65:5
3: 0x562dc41ecca0 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h3155a8c966b4beb5
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:44:22
4: 0x562dc4217c1e - core::fmt::write::h062c617411b691df
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/fmt/mod.rs:1209:17
5: 0x562dc41e4c85 - std::io::Write::write_fmt::hb61fdf1275c61e1c
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/io/mod.rs:1682:15
6: 0x562dc41eca65 - std::sys_common::backtrace::_print::hd1b4d9664ab500e0
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:47:5
7: 0x562dc41eca65 - std::sys_common::backtrace::print::hca896ae22beb06cb
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:34:9
8: 0x562dc41ee92f - std::panicking::default_hook::{{closure}}::h0b5eeed5cf36ab5f
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:267:22
9: 0x562dc41ee66a - std::panicking::default_hook::h8932b573145a321b
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:286:9
10: 0x562dc14259c0 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h420bcb63e1514d33
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/alloc/src/boxed.rs:2001:9
11: 0x562dc142d79a - nu::main::{{closure}}::ha942bc60a773a159
at /home/disc/a.stevan/.local/share/gitstore/github.com/nushell/nushell/src/main.rs:39:9
12: 0x562dc41ef049 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hbb63890df2a28e48
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/alloc/src/boxed.rs:2001:9
13: 0x562dc41ef049 - std::panicking::rust_panic_with_hook::h4b1447a24e3e94f8
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:692:13
14: 0x562dc41eed81 - std::panicking::begin_panic_handler::{{closure}}::h8701da9995a3820c
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:577:13
15: 0x562dc41ed14c - std::sys_common::backtrace::__rust_end_short_backtrace::hb696c5ed02a01598
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:137:18
16: 0x562dc41eeae2 - rust_begin_unwind
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:575:5
17: 0x562dc1419b93 - core::panicking::panic_fmt::h8aa706a976963c88
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/panicking.rs:65:14
18: 0x562dc3cf7436 - nu_protocol::engine::engine_state::StateWorkingSet::get_block_mut::h86bdc8ee64c13065
at /home/disc/a.stevan/.local/share/gitstore/github.com/nushell/nushell/crates/nu-protocol/src/engine/engine_state.rs:2055:13
19: 0x562dc2d19423 - nu_parser::parser::parse::h0783cf7d1fc1f8a6
at /home/disc/a.stevan/.local/share/gitstore/github.com/nushell/nushell/crates/nu-parser/src/parser.rs:6639:25
20: 0x562dc1482f05 - nu_cli::commands::evaluate_commands::hb98951cbe939bc98
at /home/disc/a.stevan/.local/share/gitstore/github.com/nushell/nushell/crates/nu-cli/src/commands.rs:37:29
21: 0x562dc142a635 - nu::run::run_commands::h61a6206c70952e3b
at /home/disc/a.stevan/.local/share/gitstore/github.com/nushell/nushell/src/run.rs:158:19
22: 0x562dc1445bb3 - nu::main::h7b8665fb2568403e
at /home/disc/a.stevan/.local/share/gitstore/github.com/nushell/nushell/src/main.rs:231:9
23: 0x562dc141c7cb - core::ops::function::FnOnce::call_once::he6b99eaca0ec581d
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/ops/function.rs:251:5
24: 0x562dc141b0fe - std::sys_common::backtrace::__rust_begin_short_backtrace::h15d55510aa9ec694
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/sys_common/backtrace.rs:121:18
25: 0x562dc141d301 - std::rt::lang_start::{{closure}}::h644ff56691dbb3fe
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/rt.rs:166:18
26: 0x562dc41ddb8b - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h8cbb48ae40ddb046
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/core/src/ops/function.rs:286:13
27: 0x562dc41ddb8b - std::panicking::try::do_call::h92db802eb38b49b7
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:483:40
28: 0x562dc41ddb8b - std::panicking::try::ha8949d2082cf3644
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:447:19
29: 0x562dc41ddb8b - std::panic::catch_unwind::h5e34c1f8a5992ed9
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panic.rs:137:14
30: 0x562dc41ddb8b - std::rt::lang_start_internal::{{closure}}::hea52a0bb3f8ff16a
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/rt.rs:148:48
31: 0x562dc41ddb8b - std::panicking::try::do_call::h5bc358faf3d68a8b
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:483:40
32: 0x562dc41ddb8b - std::panicking::try::h675304212928379d
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panicking.rs:447:19
33: 0x562dc41ddb8b - std::panic::catch_unwind::h7ce3ad349ed5c844
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/panic.rs:137:14
34: 0x562dc41ddb8b - std::rt::lang_start_internal::hcd7e45acd25ab5ab
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/rt.rs:148:20
35: 0x562dc141d2da - std::rt::lang_start::h2d36e0a92261b7a9
at /rustc/90743e7298aca107ddaa0c202a4d3604e29bfeb6/library/std/src/rt.rs:165:17
36: 0x562dc14463fe - main
37: 0x7f7f52829d90 - __libc_start_call_main
at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
38: 0x7f7f52829e40 - __libc_start_main_impl
at ./csu/../csu/libc-start.c:392:3
39: 0x562dc141a035 - _start
40: 0x0 - <unknown>on |
|
@kubouch top comment updated 👌 |
|
following the backtrace, in
...
👀 |
This commit - 👍 does not crash anymore when running a command from `std` - 👎 loads all the commands from `std`
This actually solves the issue of loading everything from `std`!!
This commit is here to ensure the bug i had at the beginning does not happen again: - the prelude is loaded - but running a command from the prelude, or from a `use std ...` crashes the shell This commit runs the `assert` command, which is in the prelude, and needs to pass.
|
standard library, here you come 😤 💪 |
The clip command has been introduced to the standard library in nushell/nushell/pull/8695 and has been available since nushell/nushell/pull/8627.
# Description as we now have a prelude thanks to #8627, i'd like to work on the structure of the library 😋 and i think the first step is to make it a true standalone crate 😏 this PR - moves all the library from `crates/nu-utils/standard_library/` to `crates/nu-std/` - moves the `rust` loading code from `src/run.rs` to `crates/nu-std/src/lib.rs`
Should address some points in #8450.
Related to #8611.
Description
std.numodule and addstdto the main "namespace"help stduse(name, search_name)to be added automaticallynameis the name of the command that the user can use withoutusesearch_nameis the name of the command in thestd.numodulenameandsearch_namewill be the same, but thenamecan be madestd ...to make the difference with built-in commands🧪 tests have been added to test the bad behaviours and bug i have stumbled upon and the expected behaviour.
User-Facing Changes
normal behaviour
the user will have access, for free, to the commands listed in the prelude, e.g. the
helpimplementations innushellfrom #8611.the standard library will be available, for free, as
std.the user can import any command from the standard library with
or the whole library with
from anywhere, without having to have the
std.nufile locally on disk.when the prelude can not be loaded
Tests + Formatting
toolkit fmttoolkit clippytoolkit testAfter Submitting
this will make the annoucement of the standard library possible!
we will be able to land #8406 and #8415 and #8611 when we want 👌