summaryrefslogtreecommitdiff
path: root/devdocs
diff options
context:
space:
mode:
authorStefan Holderbach <sholderbach@users.noreply.github.com>2023-11-21 18:12:00 +0100
committerGitHub <noreply@github.com>2023-11-21 18:12:00 +0100
commitffb80b8873584a78a65312fcb9f9ec3bed31df3f (patch)
tree74545df12c212e94331bda6698bb89781d1ac3ed /devdocs
parent177e800a078f4b797f10788d9cc8226be1b5af92 (diff)
Curate developer documentation in tree (#11052)
This is still work in progress. But let's start somewhere. Goal for this being in the repo is to make sure we update it more frequently than the https://github.com/nushell/nushell.github.io/tree/main/contributor-book - Add folder for in-repo developer documentation - Move PLATFORM_SUPPORT into devdocs - Move our rust style to devdocs - Use nushell code formatting in CONTRIBUTING - Add FAQ file for developers with example questions - Describe error handling best practices
Diffstat (limited to 'devdocs')
-rw-r--r--devdocs/FAQ.md40
-rw-r--r--devdocs/PLATFORM_SUPPORT.md70
-rw-r--r--devdocs/README.md11
-rw-r--r--devdocs/rust_style.md51
4 files changed, 172 insertions, 0 deletions
diff --git a/devdocs/FAQ.md b/devdocs/FAQ.md
new file mode 100644
index 000000000..1527e1642
--- /dev/null
+++ b/devdocs/FAQ.md
@@ -0,0 +1,40 @@
+# Frequently asked question for developers
+
+Let's collect some questions a lot of Nushell contributors have.
+- How do I do....?
+- Why do I need to do certain things a certain way?
+
+Let's keep the answers concise and up to date (or general enough) to remain relevant
+
+## How do I properly test my feature or bugfix?
+TODO (Probably fork out into its own file)
+
+## I want to report an error to the user
+
+Approximate flow:
+
+1. Are you reporting the error in the parser/static checking phase?
+ - Use `nu_protocol::ParseError` variants
+ - Follow the logic used in the context as we need to collect multiple errors for a good IDE experience
+2. Pick the right `nu_protocol::ShellError` variant
+ - Does a matching existing variant fit your need? (go to references of the `ShellError` variant for inspiration)
+ - Check what context the [`miette`](https://docs.rs/miette) macros add during formatting! (go to definition of `ShellError`)
+ - If it is a one-of specific error, consider using a generic variant
+ - Else add a new class of errors
+ - add the necessary `Span` information
+ - general shared error text, to inform and point to a resolution
+ - dynamic information gathered from the error site
+ - Don't use a tuple enum variant, named structs going forward only!
+3. Are you in a command?
+ - `return Err(ShellError::...)` and you're done in a `Command::run`
+4. Do you want to report a warning but not stop execution?
+ - **NEVER** `println!`, we can write to stderr if necessary but...
+ - good practice: `nu_protocol::cli_error::report_error` or `report_error_new`
+ - depending on whether you have access to a `StateWorkingSet`
+ - if only relevant to in the field debugging: `log`-crate macros.
+
+## How do I check an environment variable?
+TODO
+
+## WTF is `PipelineMetadata`?
+TODO
diff --git a/devdocs/PLATFORM_SUPPORT.md b/devdocs/PLATFORM_SUPPORT.md
new file mode 100644
index 000000000..4f40c073b
--- /dev/null
+++ b/devdocs/PLATFORM_SUPPORT.md
@@ -0,0 +1,70 @@
+# Nushell platform support policy
+
+Nushell envisions to be a cross-platform shell, despite taking some strong design inspiration from UNIX and POSIX command names and style conventions we explicitly support Windows.
+
+## cross-platform design
+This commitment to a cross-platform Nushell forces us to make provisions so users on Windows can have the generally same pleasant experience: e.g. supporting paths with backslash as the directory separator, forces us to support string literals that accept those.
+
+In general our design strives to have a consistent behavior across all platforms if defining the semantics is possible for Nushell.
+In some cases where the platform requirements dominate we may choose to follow the platform specific defaults. (some nuances around the file system)
+Only rarely do we want to accept commands/language features that only support a single platform, only to access common system behavior of this particular platform (e.g. `registry query` command for the windows registry, `exec` for Linux and MacOS)
+
+## cross-platform builds and testing
+
+The Nushell team runs **testing of Nushell for the following platforms** through our CI:
+
+- macOS (latest version available through GitHub CI)
+- Windows (10 and 11)
+- Linux (our test runners use `ubuntu-20.04` to represent distributions with not the latest glibc versions.)
+
+All PR level tests are performed on x86/AMD64 (at least at the time of writing the default macOS runner was not yet using arm64).
+
+As an additional layer of validation we perform [nightly builds](https://github.com/nushell/nightly/releases).
+
+Those target **additional build targets**:
+- **aarch64 for all platforms**
+- musl as an alternative to Glibc on linux
+- riscv only for linux
+- armv7 only for linux
+
+We will try to provide builds for all of them but a standard configuration for x86-64 or aarch64 will take priority for us should we face technical challenges in a release cycle.
+
+### Supported feature flags
+
+We have features of Nushell behind flags that can be passed at compilation time.
+
+The design focus of Nushell is primarily expressed by everything accessible without passing additional feature flag. This provides a standard command set and receives the most attention.
+Two other feature flags are actively tested but are not guaranteed to express the stable design direction of Nushell:
+- `extra`
+ - This includes commands where we are not convinced that they are ready to be stabilized for 1.0 or popular enough
+- `dataframe`
+ - This includes dataframe support via `polars` and `arrow2`. Introduces a significant additional compilation and binary size.
+ - Due to the use of SIMD extensions may not be compatible with every minimal architecture.
+
+## Passively supported platforms
+
+These platforms are not actively managed through our CI so may encounter unintended regressions.
+Furthermore certain features may not yet be available, even though we are willing to accept PRs trying to close that gap.
+
+
+- OpenBSD
+ - e.g. missing the `ps` command
+- FreeBSD
+ - e.g. missing the `ps` command
+- Android via Termux
+
+Help from the community to make sure they get tested and improved so they can become first class targets would be greatly appreciated!
+
+
+## Providing builds and packaging
+
+The Nushell team only provides a select few distribution sources and so far encourages community members to maintain the individual packages for particular package managers:
+
+We provide:
+- source code distribution via `crates.io` -> `cargo install nu`
+- GitHub builds with each release: (following the build matrix of the nightly builds)
+- the setup for `winget` packaging
+
+### For package maintainers:
+
+We aim to support the rust version that is two releases behind the most recent version of stable Rust so the build infrastructure of your packaging environment can already be proven out.
diff --git a/devdocs/README.md b/devdocs/README.md
new file mode 100644
index 000000000..a404ee254
--- /dev/null
+++ b/devdocs/README.md
@@ -0,0 +1,11 @@
+# Basic developer documentation
+
+This is intended to give some immediate guidance to how to navigate the codebase and lives from everyone updating it if you make a design change.
+
+A complementary (currently stale) resource has been the [Nushell contributor book](https://www.nushell.sh/contributor-book/), which until further notice contains broader design guidance.
+
+## Contents
+
+- [Developer FAQ](FAQ.md)
+- [Platform support policy](PLATFORM_SUPPORT.md)
+- [Our Rust style](devdocs/rust_style.md)
diff --git a/devdocs/rust_style.md b/devdocs/rust_style.md
new file mode 100644
index 000000000..e40e09531
--- /dev/null
+++ b/devdocs/rust_style.md
@@ -0,0 +1,51 @@
+# Our Rust style
+
+## Table of contents
+- [Introduction](#introduction)
+- [Generally discouraged](#generally-discouraged)
+- [Things we want to get better at](#things-we-want-to-get-better-at)
+
+## Introduction
+To make the collaboration on a project the scale of Nushell easy, we want to work towards a style of Rust code that can easily be understood by all of our contributors. We conservatively rely on most of [`clippy`s suggestions](https://github.com/rust-lang/rust-clippy) to get to the holy grail of "idiomatic" code. Good code in our eyes is not the most clever use of all available language features or with the most unique personal touch but readable and strikes a balance between being concise, and also unsurprising and explicit in the places where it matters.
+One example of this philosophy is that we generally avoid to fight the borrow-checker in our data model but rather try to get to a correct and simple solution first and then figure out where we should reuse data to achieve the necessary performance. As we are still pre-1.0 this served us well to be able to quickly refactor or change larger parts of the code base.
+
+## Generally discouraged
+### `+nightly` language features or things only available in the most recent `+stable`
+To make life for the people easier that maintain the Nushell packages in various distributions with their own release cycle of `rustc` we typically rely on slightly older Rust versions. We do not make explicit guarantees how far back in the past we live but you can find out in our [`rust-toolchain.toml`](https://github.com/nushell/nushell/blob/main/rust-toolchain.toml)
+(As a rule of thumb this has been typically been approximately 2 releases behind the newest stable compiler.)
+The use of nightly features is prohibited.
+
+### Panicking
+As Nushell aims to provide a reliable foundational way for folks to interact with their computer, we cannot carelessly crash the execution of their work by panicking Nushell.
+Thus panicking is not an allowed error handling strategy for anything that could be triggered by user input OR behavior of the outside system. If Nushell panics this is a bug or we are against all odds already in an unrecoverable state (The system stopped cooperating, we went out of memory). The use of `.unwrap()` is thus outright banned and any uses of `.expect()` or related panicking macros like `unreachable!` should include a helpful description which assumptions have been violated.
+
+### `unsafe` code
+For any use of `unsafe` code we need to require even higher standards and additional review. If you add or alter `unsafe` blocks you have to be familiar with the promises you need to uphold as found in the [Rustonomicon](https://doc.rust-lang.org/nomicon/intro.html). All `unsafe` uses should include `// SAFETY:` comments explaining how the invariants are upheld and thus alerting you what to watch out for when making a change.
+#### FFI with system calls and the outside world
+As a shell Nushell needs to interact with system APIs in several places, for which FFI code with unsafe blocks may be necessary. In some cases this can be handled by safe API wrapper crates but in some cases we may choose to directly do those calls.
+If you do so you need to document the system behavior on top of the Rust memory model guarantees that you uphold. This means documenting whether using a particular system call is safe to use in a particular context and all failure cases are properly recovered.
+#### Implementing self-contained data structures
+Another motivation for reaching to `unsafe` code might be to try to implement a particular data structure that is not expressible on safe `std` library APIs. Doing so in the Nushell code base would have to clear a high bar for need based on profiling results. Also you should first do a survey of the [crate ecosystem](https://crates.io) that there doesn't exist a usable well vetted crate that already provides safe APIs to the desired datastructure.
+#### Make things go faster by removing checks
+This is probably a bad idea if you feel tempted to do so. Don't
+### Macros
+Another advanced feature people feel tempted to use to work around perceived limitations of Rusts syntax and we are not particularly fans of are custom macros.
+They have clear downsides not only in terms of readability if they locally introduce a different syntax. Most tooling apart from the compiler will struggle more with them. This limits for example consistent automatic formatting or automated refactors with `rust-analyzer`.
+That you can fluently read `macro_rules!` is less likely than regular code. This can lead people to introduce funky behavior when using a macro. Be it because a macro is not following proper hygiene rules or because it introduces excessive work at compile time.
+
+So we generally discourage the addition of macros. In a lot of cases your macro may start do something that can be expressed with functions or generics in a much more reusable fashion.
+The only exceptions we may allow need to demonstrate that the macro can fix something that is otherwise extremely unreadable, error-prone, or consistently worse at compile time.
+## Things we want to get better at
+These are things we did pretty liberally to get Nushell off the ground, that make things harder for a high quality stable product. You may run across them but shouldn't take them as an endorsed example.
+### Liberal use of third-party dependencies
+The amazing variety of crates on [crates.io](https://crates.io) allowed us to quickly get Nushell into a feature rich state but it left us with a bunch of baggage to clean up.
+Each dependency introduces a compile time cost and duplicated code can add to the overall binary size. Also vetting more for correct and secure implementations takes unreasonably more time as this is also a continuous process of reacting to updates or potential vulnerabilities.
+
+Thus we only want to accept dependencies that are essential and well tested implementations of a particular requirement of Nushells codebase.
+Also as a project for the move to 1.0 we will try to unify among a set of dependencies if they possibly implement similar things in an area. We don't need three different crates with potentially perfect fit for three problems but rather one reliable crate with a maximized overlap between what it provides and what we need.
+We will favor crates that are well tested and used and promise to be more stable and still frequently maintained.
+### Deeply nested code
+As Nushell uses a lot of enums in its internal data representation there are a lot of `match` expressions. Combined with the need to handle a lot of edge cases and be defensive about any errors this has led to some absolutely hard to read deeply nested code (e.g. in the parser but also in the implementation of several commands).
+This can be observed both as a "rightward drift" where the main part of the code is found after many levels of indentations or by long function bodies with several layers of branching with seemingly repeated branching inside the higher branch level.
+This can also be exacerbated by "quick" bugfixes/enhancements that may just try to add a special case to catch a previously unexpected condition. The likelihood of introducing a bug in a sea of code duplication is high.
+To combat this, consider using the early-`return` pattern to reject invalid data early in one place instead of building a tree through Rust's expression constructs with a lot of duplicated paths. Unpacking data into a type that expresses that the necessary things already have been checked and using functions to properly deal with separate and common behavior can also help.