| 1 |
<!-- spell-checker:ignore reimplementing toybox RUNTEST CARGOFLAGS nextest embeddable Rustonomicon rustdoc's --> |
| 2 |
|
| 3 |
# Contributing to coreutils |
| 4 |
|
| 5 |
Hi! Welcome to uutils/coreutils! |
| 6 |
|
| 7 |
Thanks for wanting to contribute to this project! This document explains |
| 8 |
everything you need to know to contribute. Before you start make sure to also |
| 9 |
check out these documents: |
| 10 |
|
| 11 |
- Our community's [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md). |
| 12 |
- [DEVELOPMENT.md](./DEVELOPMENT.md) for setting up your development |
| 13 |
environment. |
| 14 |
- Our [Review Guidelines](https://uutils.org/reviews/) for what we expect |
| 15 |
from a pull request and how reviews are done. |
| 16 |
|
| 17 |
Now follows a very important warning: |
| 18 |
|
| 19 |
> [!WARNING] |
| 20 |
> uutils is original code and cannot contain any code from GNU or |
| 21 |
> other implementations. This means that **we cannot accept any changes based on |
| 22 |
> the GNU source code**. To make sure that cannot happen, **you cannot link to |
| 23 |
> the GNU source code** either. It is however possible to look at other implementations |
| 24 |
> under a BSD or MIT license like [Apple's implementation](https://github.com/apple-oss-distributions/file_cmds/) |
| 25 |
> or [OpenBSD](https://github.com/openbsd/src/tree/master/bin). |
| 26 |
|
| 27 |
Finally, feel free to join our [Discord](https://discord.gg/wQVJbvJ)! |
| 28 |
|
| 29 |
## Getting Oriented |
| 30 |
|
| 31 |
uutils is a big project consisting of many parts. Here are the most important |
| 32 |
parts for getting started: |
| 33 |
|
| 34 |
- [`src/uu`](https://github.com/uutils/coreutils/tree/main/src/uu/): The code for all utilities |
| 35 |
- [`src/uucore`](https://github.com/uutils/coreutils/tree/main/src/uucore/): Crate containing all the shared code between |
| 36 |
the utilities. |
| 37 |
- [`tests/by-util`](https://github.com/uutils/coreutils/tree/main/tests/by-util/): The tests for all utilities. |
| 38 |
- [`src/bin/coreutils.rs`](https://github.com/uutils/coreutils/tree/main/src/bin/coreutils.rs): Code for the multicall |
| 39 |
binary. |
| 40 |
- [`docs`](https://github.com/uutils/coreutils/tree/main/docs/src): the documentation for the website |
| 41 |
- [`tests/uutests/`](https://github.com/uutils/coreutils/tree/main/tests/uutests/): |
| 42 |
Crate implementing the various functions to test uutils commands. |
| 43 |
- [`fuzz/`](https://github.com/uutils/coreutils/tree/main/fuzz/): The differential fuzzers. |
| 44 |
|
| 45 |
Each utility is defined as a separate crate. The structure of each of these |
| 46 |
crates is as follows: |
| 47 |
|
| 48 |
- `Cargo.toml` |
| 49 |
- `src/main.rs`: contains only a single macro call |
| 50 |
- `src/<util name>.rs`: the actual code for the utility |
| 51 |
- `locales/en-US.ftl`: the util's strings |
| 52 |
- `locales/fr-FR.ftl`: French translation of the util's strings |
| 53 |
|
| 54 |
We have separated repositories for crates that we maintain but also publish for |
| 55 |
use by others: |
| 56 |
|
| 57 |
- [coreutils-i10n](https://github.com/uutils/coreutils-l10n) |
| 58 |
- [num-prime](https://github.com/uutils/num-prime) |
| 59 |
- [uutils-term-grid](https://github.com/uutils/uutils-term-grid) |
| 60 |
- [parse_datetime](https://github.com/uutils/parse_datetime) |
| 61 |
|
| 62 |
## Design Goals |
| 63 |
|
| 64 |
We have the following goals with our development: |
| 65 |
|
| 66 |
- **Compatible**: The utilities should be a drop-in replacement for the GNU |
| 67 |
coreutils. |
| 68 |
- **Cross-platform**: All utilities should run on as many of the supported |
| 69 |
platforms as possible. |
| 70 |
- **Reliable**: The utilities should never unexpectedly fail. |
| 71 |
- **Performant**: Our utilities should be written in fast idiomatic Rust. We aim |
| 72 |
to match or exceed the performance of the GNU utilities. |
| 73 |
- **Well-tested**: We should have a lot of tests to be able to guarantee |
| 74 |
reliability and compatibility. Code coverage should not regress; new |
| 75 |
behavior should come with tests that cover it. |
| 76 |
|
| 77 |
## How to Help |
| 78 |
|
| 79 |
There are several ways to help and writing code is just one of them. Reporting |
| 80 |
issues and writing documentation are just as important as writing code. |
| 81 |
|
| 82 |
### Reference version of GNU coreutils |
| 83 |
|
| 84 |
We try to stay compatible with the latest stable release of GNU coreutils, |
| 85 |
and sometimes backport changes from GNU's master branch as well. |
| 86 |
|
| 87 |
Since many Linux distributions ship an older GNU coreutils, Canonical |
| 88 |
provides a [more recent build](https://launchpad.net/~bamf0/+archive/ubuntu/coreutils-reference) |
| 89 |
you can use to compare our utilities against upstream before filing a bug |
| 90 |
report. A few caveats: |
| 91 |
|
| 92 |
- The package is PATCHED, so it may differ from upstream. |
| 93 |
- NEVER install this package on your system — it isn't meant for daily use. |
| 94 |
Instead, manually download and extract the [tarball](https://launchpad.net/~bamf0/+archive/ubuntu/coreutils-reference/+files/gnu-coreutils_9.12-0ubuntu1~ppa1_amd64.deb) |
| 95 |
and run it from there. |
| 96 |
- Bug reports and fixes for this package itself are not accepted here. |
| 97 |
|
| 98 |
### Reporting Issues |
| 99 |
|
| 100 |
We can't fix bugs we don't know about, so good issues are super helpful! Here |
| 101 |
are some tips for writing good issues: |
| 102 |
|
| 103 |
- Confirm the bug is in coreutils; some tools (e.g., `find`, `sed`) are maintained in separate repositories under the uutils project. |
| 104 |
- If you find a bug, make sure it's still a problem on the [`main` branch](https://github.com/uutils/coreutils/releases/tag/latest-commit). |
| 105 |
- Search through the existing issues to see whether it has already been |
| 106 |
reported. |
| 107 |
- Make sure to include all relevant information, such as: |
| 108 |
- Which version or commit hash of uutils did you check? |
| 109 |
- Which version of GNU coreutils are you comparing with? |
| 110 |
- Did you test against the latest version of GNU coreutils? |
| 111 |
- What platform are you on? |
| 112 |
- Provide a way to reliably reproduce the issue. |
| 113 |
- Be as specific as possible! |
| 114 |
- Please provide the output with LANG=C, except for locale-related bugs. |
| 115 |
|
| 116 |
### Writing Documentation |
| 117 |
|
| 118 |
There's never enough documentation. If you come across any documentation that |
| 119 |
could be improved, feel free to submit a PR for it! |
| 120 |
|
| 121 |
### Writing Code |
| 122 |
|
| 123 |
If you want to submit a PR, make sure that you've discussed the solution with |
| 124 |
the maintainers beforehand. We want to avoid situations where you put a lot of |
| 125 |
work into a fix that we can't merge! If there's no issue for what you're trying |
| 126 |
to fix yet, make one _before_ you start working on the PR. |
| 127 |
|
| 128 |
Generally, we try to follow what GNU is doing in terms of options and behavior. |
| 129 |
It is recommended to look at the GNU coreutils manual |
| 130 |
([on the web](https://www.gnu.org/software/coreutils/manual/html_node/index.html), |
| 131 |
or locally using `info <utility>`). It is more in depth than the man pages and |
| 132 |
provides a good description of available features and their implementation |
| 133 |
details. But remember, you cannot look at the GNU source code! |
| 134 |
|
| 135 |
Also remember that we can only merge PRs which pass our test suite, follow |
| 136 |
rustfmt, and do not have any warnings from clippy. See |
| 137 |
[DEVELOPMENT.md](./DEVELOPMENT.md) for more information. Be sure to also read |
| 138 |
about our [Rust style](#our-rust-style). |
| 139 |
|
| 140 |
## Our Rust Style |
| 141 |
|
| 142 |
We want uutils to be written in idiomatic Rust, so here are some guidelines to |
| 143 |
follow. Some of these are aspirational, meaning that we don't do them correctly |
| 144 |
everywhere in the code. If you find violations of the advice below, feel free to |
| 145 |
submit a patch! |
| 146 |
|
| 147 |
### Don't `panic!` |
| 148 |
|
| 149 |
The coreutils should be very reliable. This means that we should never `panic!`. |
| 150 |
Therefore, you should avoid using `println!`, `.unwrap()` and `panic!`. Sometimes the use of |
| 151 |
`unreachable!` can be justified with a comment explaining why that code is |
| 152 |
unreachable. |
| 153 |
|
| 154 |
### Don't `exit` |
| 155 |
|
| 156 |
We want uutils to be embeddable in other programs. This means that no function |
| 157 |
in uutils should exit the program. Doing so would also lead to code with more |
| 158 |
confusing control flow. Avoid therefore `std::process::exit` and similar |
| 159 |
functions which exit the program early. |
| 160 |
|
| 161 |
### `unsafe` |
| 162 |
|
| 163 |
uutils cannot be entirely safe, because we have to call out to `libc` and do |
| 164 |
syscalls. However, we still want to limit our use of `unsafe`. We generally only |
| 165 |
accept `unsafe` for FFI, with very few exceptions. Note that performance is very |
| 166 |
rarely a valid argument for using `unsafe`. |
| 167 |
|
| 168 |
If you still need to write code with `unsafe`, make sure to read the |
| 169 |
[Rustonomicon](https://doc.rust-lang.org/nomicon/intro.html) and annotate the |
| 170 |
calls with `// SAFETY:` comments explaining why the use of `unsafe` is sound. |
| 171 |
|
| 172 |
### Macros |
| 173 |
|
| 174 |
Macros can be a great tool, but they are also usually hard to understand. They |
| 175 |
should be used sparingly. Make sure to explore simpler options before you reach |
| 176 |
for a solution involving macros. |
| 177 |
|
| 178 |
### `str`, `OsStr` & `Path` |
| 179 |
|
| 180 |
Rust has many string-like types, and sometimes it's hard to choose the right |
| 181 |
one. It's tempting to use `str` (and `String`) for everything, but that is not |
| 182 |
always the right choice for uutils, because we need to support invalid UTF-8, |
| 183 |
just like the GNU coreutils. For example, paths on Linux might not be valid |
| 184 |
UTF-8! Whenever we are dealing with paths, we should therefore stick with |
| 185 |
`OsStr` and `Path`. Make sure that you only convert to `str`/`String` if you |
| 186 |
know that something is always valid UTF-8. If you need more operations on |
| 187 |
`OsStr`, you can use the [`bstr`](https://docs.rs/bstr/latest/bstr/) crate. |
| 188 |
|
| 189 |
### Doc-comments |
| 190 |
|
| 191 |
We use rustdoc for our documentation, so it's best to follow |
| 192 |
[rustdoc's guidelines](https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html#documenting-components). |
| 193 |
Make sure that your documentation is not just repeating the name of the |
| 194 |
function, but actually giving more useful information. Rustdoc recommends the |
| 195 |
following structure: |
| 196 |
|
| 197 |
``` |
| 198 |
[short sentence explaining what it is] |
| 199 |
|
| 200 |
[more detailed explanation] |
| 201 |
|
| 202 |
[at least one code example that users can copy/paste to try it] |
| 203 |
|
| 204 |
[even more advanced explanations if necessary] |
| 205 |
``` |
| 206 |
|
| 207 |
### Other comments |
| 208 |
|
| 209 |
Comments should be written to _explain_ the code, not to _describe_ the code. |
| 210 |
Try to focus on explaining _why_ the code is the way it is. If you feel like you |
| 211 |
have to describe the code, that's usually a sign that you could improve the |
| 212 |
naming of variables and functions. |
| 213 |
|
| 214 |
If you edit a piece of code, make sure to update any comments that need to |
| 215 |
change as a result. The only thing worse than having no comments is having |
| 216 |
outdated comments! |
| 217 |
|
| 218 |
## AI policy |
| 219 |
|
| 220 |
AI-assisted contributions are allowed, but the same standards apply as for |
| 221 |
any other patch. If you use an AI tool to help write a change, you are still |
| 222 |
responsible for it: you should understand every line you submit and be able |
| 223 |
to explain and justify the change in review. Be especially careful that the |
| 224 |
output is not derived from GNU coreutils or other GPL code - AI assistants |
| 225 |
are trained on GPL sources and can reproduce them verbatim, which this |
| 226 |
project cannot accept. Keep patches small and focused |
| 227 |
so they are easy to review, and self-review the diff carefully before opening |
| 228 |
a pull request - reviewers are there to double-check a human's work, not an |
| 229 |
LLM's output. |
| 230 |
|
| 231 |
## Git Etiquette |
| 232 |
|
| 233 |
To ensure easy collaboration, we have guidelines for using Git and GitHub. |
| 234 |
|
| 235 |
### Commits |
| 236 |
|
| 237 |
- Make small and atomic commits. |
| 238 |
- Keep a clean history of commits. |
| 239 |
- Write informative commit messages. |
| 240 |
- Annotate your commit message with the component you're editing. For example: |
| 241 |
`cp: do not overwrite on with -i` or `uucore: add support for FreeBSD`. |
| 242 |
- Do not unnecessarily move items around in the code. This makes the changes |
| 243 |
much harder to review. If you do need to move things around, do that in a |
| 244 |
separate commit. |
| 245 |
|
| 246 |
### Commit messages |
| 247 |
|
| 248 |
You can read [this section in the Git book](https://git-scm.com/book/ms/v2/Distributed-Git-Contributing-to-a-Project) to learn how to write good commit |
| 249 |
messages. |
| 250 |
|
| 251 |
In addition, here are a few examples for a summary line when committing to |
| 252 |
uutils: |
| 253 |
|
| 254 |
- commit for a single utility |
| 255 |
|
| 256 |
``` |
| 257 |
nohup: cleanup and refactor |
| 258 |
``` |
| 259 |
|
| 260 |
- commit for a utility's tests |
| 261 |
|
| 262 |
``` |
| 263 |
tests/rm: test new feature |
| 264 |
``` |
| 265 |
|
| 266 |
Beyond changes to an individual utility or its tests, other summary lines for |
| 267 |
non-utility modules include: |
| 268 |
|
| 269 |
``` |
| 270 |
README: add help |
| 271 |
uucore: add new modules |
| 272 |
uutils: add new utility |
| 273 |
gitignore: add temporary files |
| 274 |
``` |
| 275 |
|
| 276 |
### PRs |
| 277 |
|
| 278 |
- Make the titles of PRs descriptive. |
| 279 |
- This means describing the problem you solve. For example, do not write |
| 280 |
`Fix #1234`, but `ls: fix version sort order`. |
| 281 |
- You can prefix the title with the utility the PR concerns. |
| 282 |
- Keep PRs small and self-contained. A set of small PRs is much more likely to |
| 283 |
get merged quickly than one large PR. If you have a large change to submit, |
| 284 |
consider using [`gh stack`](https://github.com/github/gh-stack) to split it |
| 285 |
into a series of stacked PRs that are easier to review. |
| 286 |
- Make sure the CI passes (up to intermittently failing tests). |
| 287 |
- You know your code best, that's why it's best if you can solve merge conflicts |
| 288 |
on your branch yourself. |
| 289 |
- It's up to you whether you want to use `git merge main` or |
| 290 |
`git rebase main`. |
| 291 |
- Feel free to ask for help with merge conflicts. |
| 292 |
- You do not need to ping maintainers to request a review immediately after submission. If you do not get a response to your patch within a few days, it is fine to request a review. |
| 293 |
- If after a week your patch has still not been reviewed, we recommend that you ping the maintainers on our Discord channel in `#coreutils-chat`. |
| 294 |
|
| 295 |
## Platforms |
| 296 |
|
| 297 |
We take pride in supporting many operating systems and architectures. Any code |
| 298 |
you contribute must at least compile without warnings for all platforms in the |
| 299 |
CI. However, you can use `#[cfg(...)]` attributes to create platform dependent |
| 300 |
features. |
| 301 |
|
| 302 |
## Improving the GNU compatibility |
| 303 |
|
| 304 |
Please make sure you have installed |
| 305 |
[GNU utils and prerequisites](DEVELOPMENT.md#gnu-utils-and-prerequisites) and |
| 306 |
can execute commands described in |
| 307 |
[Comparing with GNU](DEVELOPMENT.md#comparing-with-gnu) section of |
| 308 |
[DEVELOPMENT.md](DEVELOPMENT.md) |
| 309 |
|
| 310 |
The Python script `./util/remaining-gnu-error.py` shows the list of failing |
| 311 |
tests in the CI. |
| 312 |
|
| 313 |
To improve the GNU compatibility, the following process is recommended: |
| 314 |
|
| 315 |
1. Identify a test (the smaller, the better) on a program that you understand or |
| 316 |
is easy to understand. You can use the `./util/remaining-gnu-error.py` script |
| 317 |
to help with this decision. |
| 318 |
1. Build both the GNU and Rust coreutils using: `bash util/build-gnu.sh` |
| 319 |
1. Run the test with `bash util/run-gnu-test.sh <your test>` |
| 320 |
1. Start to modify `<your test>` to understand what is wrong. Examples: |
| 321 |
1. Add `set -v` to have the bash verbose mode |
| 322 |
1. Add `echo $?` where needed |
| 323 |
1. When the variable `fail` is used in the test, `echo $fail` to see when the |
| 324 |
test started to fail |
| 325 |
1. Bump the content of the output (ex: `cat err`) |
| 326 |
1. ... |
| 327 |
1. Or, if the test is simple, extract the relevant information to create a new |
| 328 |
test case running both GNU & Rust implementation |
| 329 |
1. Start to modify the Rust implementation to match the expected behavior |
| 330 |
1. Add a test to make sure that we don't regress (our test suite is super quick) |
| 331 |
|
| 332 |
## Code coverage |
| 333 |
|
| 334 |
To generate code coverage report locally please follow |
| 335 |
[Code coverage report](DEVELOPMENT.md#code-coverage-report) section of |
| 336 |
[DEVELOPMENT.md](DEVELOPMENT.md) |
| 337 |
|
| 338 |
## Other implementations |
| 339 |
|
| 340 |
The Coreutils have different implementations, with different levels of |
| 341 |
completions: |
| 342 |
|
| 343 |
- [GNU's](https://git.savannah.gnu.org/gitweb/?p=coreutils.git) |
| 344 |
- [OpenBSD](https://github.com/openbsd/src/tree/master/bin) |
| 345 |
- [Busybox](https://github.com/mirror/busybox/tree/master/coreutils) |
| 346 |
- [Toybox (Android)](https://github.com/landley/toybox/tree/master/toys/posix) |
| 347 |
- [Mac OS](https://github.com/apple-oss-distributions/file_cmds/) |
| 348 |
- [V lang](https://github.com/vlang/coreutils) |
| 349 |
- [SerenityOS](https://github.com/SerenityOS/serenity/tree/master/Userland/Utilities) |
| 350 |
- [Initial Unix](https://github.com/dspinellis/unix-history-repo) |
| 351 |
- [Perl Power Tools](https://metacpan.org/pod/PerlPowerTools) |
| 352 |
|
| 353 |
However, when reimplementing the tools/options in Rust, don't read their source |
| 354 |
codes when they are using reciprocal licenses (ex: GNU GPL, GNU LGPL, etc). |
| 355 |
|
| 356 |
## Licensing |
| 357 |
|
| 358 |
uutils is distributed under the terms of the MIT License; see the `LICENSE` file |
| 359 |
for details. This is a permissive license, which allows the software to be used |
| 360 |
with few restrictions. |
| 361 |
|
| 362 |
Copyrights in the uutils project are retained by their contributors, and no |
| 363 |
copyright assignment is required to contribute. |
| 364 |
|
| 365 |
If you wish to add or change dependencies as part of a contribution to the |
| 366 |
project, a tool like `cargo-license` can be used to show their license details. |
| 367 |
The following types of license are acceptable: |
| 368 |
|
| 369 |
- MIT License |
| 370 |
- Dual- or tri-license with an MIT License option ("Apache-2.0 or MIT" is a |
| 371 |
popular combination) |
| 372 |
- "MIT equivalent" license (2-clause BSD, 3-clause BSD, ISC) |
| 373 |
- License less restrictive than the MIT License (CC0 1.0 Universal) |
| 374 |
- Apache License version 2.0 |
| 375 |
|
| 376 |
Licenses we will not use: |
| 377 |
|
| 378 |
- An ambiguous license, or no license |
| 379 |
- Strongly reciprocal licenses (GNU GPL, GNU LGPL) |
| 380 |
|
| 381 |
If you wish to add a reference but it doesn't meet these requirements, please |
| 382 |
raise an issue to describe the dependency. |