---
title: "Lesson 5: Search"
description: "Search the code on main by text and by file, from any workspace, and see what search can and cannot show you."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.forge.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Lesson 5: Search

**Time:** 15 minutes. **Track:** [developer](https://docs.forge.workreef.dev/learn/#developer). **Before this:** [lesson 1](https://docs.forge.workreef.dev/learn/developer/first-landing/).

By the end of this lesson you will be able to:

- search main's code with a regular expression, and narrow it by file;
- find files by name;
- say what search sees: main's landed files you may read, a moment after they land, and not work in progress.

## Code to search

`af search` asks the forge, which keeps an index of main's files, as Zoekt and Google's code search do. The server builds it in the background as code lands. `af search` does not read your workspace, so it finds code you have never checked out. Start a forge and land a little code:

```console server
$ af-server --dev
...
```

```console check
$ source ~/af-dev/dev/env
$ af workspace add ~/search
Workspace at /home/you/search
Paths: **
main has no landing yet
$ cd ~/search
$ af new -m "Add the service"
Working on change rmmskmtwrkpmlzumroxoqqzstutxsnmr at commit 98b6f14d027730a5b2a4a8797283ce81a02f0a754994e3aa66c7b018ef180d65
$ mkdir -p src docs
$ printf 'fn connect() {\n    // TODO: retry on failure\n}\n\nfn serve() {\n    connect();\n}\n' > src/server.rs
$ printf 'fn parse() {}\n' > src/config.rs
$ printf '# Operations\n\nTODO: document the retry limit\n' > docs/operations.md
$ af submit
Submitted commit 317b8220d84e69a0c0cce89e530089db832397ea8de32f152a33c2134f692579, 3 edits in its last snapshot
```

## Search by text

Once the landing is in the index, each matching line comes back as `path:line:text`:

```console eventually
$ af search TODO
docs/operations.md:3:TODO: document the retry limit
src/server.rs:2:    // TODO: retry on failure
```

The pattern is a regular expression, in Rust's syntax. `(?i)` makes it ignore case, and `\b` marks a word's edge:

```console check
$ af search '(?i)\bretry\b'
docs/operations.md:3:TODO: document the retry limit
src/server.rs:2:    // TODO: retry on failure
```

## Narrow it by file

`-f` keeps files whose path matches a second expression:

```console check
$ af search TODO -f '\.rs$'
src/server.rs:2:    // TODO: retry on failure
```

With `-f` alone, it lists the paths that match, a quick way to find files:

```console check
$ af search -f '^src/'
src/config.rs
src/server.rs
```

## What search sees

Search reads main as it has landed. Work in progress is not there until it lands, and a landing is searchable a moment after it lands: the index takes main in at most once a second. Start a change with a new word in it:

```console check
$ af new -m "Add a health check"
Working on change syyorwlrrsuummnqvpzqxyxyllzmkllr at commit 285c268b12bef259c3d3278e5fe089e93706d6164d615638e10c327db40c9d80
$ printf 'fn health() {\n    // FIXME: report the database\n}\n' > src/health.rs
$ af search FIXME
```

Nothing: the change is open, not on main. Land it, and search finds it:

```console check
$ af submit
Submitted commit b9ae7b063ff30c246beb1b64b367e8e8c6668e36bc727621394893151684d10d, 1 edit in its last snapshot
```

```console eventually
$ af search FIXME
src/health.rs:2:    // FIXME: report the database
```

The search above is tried again until the line shows, because the index takes a landing in a moment after it lands.

Search also leaves out files in folders you may not read. The forge searches for you as you, so what it returns is only what you could check out.

And it leaves out files that are not text. A file is in the index only if it is valid UTF-8, has no line longer than 2,000 bytes and is at most a mebibyte; [Search, caches and releases](https://docs.forge.workreef.dev/concepts/search-caches-releases/#code-search) has the whole rule. A file that is not text is not in the index at all, so `-f` does not list it either.

## Exercises

1. Find every function definition in `src`.

   ```console check
   $ af search '^fn \w+' -f '^src/'
   src/config.rs:1:fn parse() {}
   src/health.rs:1:fn health() {
   src/server.rs:1:fn connect() {
   src/server.rs:5:fn serve() {
   ```

2. Find the line that calls `connect`, but not the line that defines it.

   ```console check
   $ af search '^\s+connect\('
   src/server.rs:6:    connect();
   ```

## Check your understanding

<details>

<summary>

You add a TODO in an open change, and `af search TODO` does not show it. Why?

</summary>

Search reads main's landed files, from the forge's index of them. An open change is not on main; once it lands, and the index has taken the landing in, its lines are found.

</details>

<details>

<summary>

Does `af search` need a workspace that holds the files?

</summary>

No. The forge answers from its own index of main, so what a search finds does not depend on what your workspace holds: it finds files your workspace has never held.

</details>

<details>
<summary>You land a change and search for a word in it straight away, and nothing shows. Is something wrong?</summary>

Not necessarily. The index takes main in at most once a second while landings keep coming, so a search can be a moment behind a landing. Search again.

</details>

## Next

[Lesson 6: Approvals](https://docs.forge.workreef.dev/learn/developer/approvals/) asks a person to approve a change before it lands.

Source: https://docs.forge.workreef.dev/learn/developer/search/index.mdx
