# ant lock

```sh
ant lock               # the lock of the project
ant lock <file.ant>    # the lock of a script
```

`ant lock` selects the version of every [package](https://ant-lang.org/docs/guide/packages.md) a program uses and writes
the lock: one line for each package, with its version and the hash of its files.

## The lock of a project

With no argument, `ant lock` works on the project file, the `ant.json` in the current directory or
the nearest above, and writes `ant.lock` beside it. [`ant add`](https://ant-lang.org/docs/tools/add.md), [`ant remove`](https://ant-lang.org/docs/tools/remove.md),
and [`ant update`](https://ant-lang.org/docs/tools/update.md) write the lock themselves, so run `ant lock` after you:

- change a path or a version in `ant.json` by hand
- merge two branches that both changed `ant.lock`, keeping the lines of both sides
- delete the lock

After `slug1` in `ant.json` goes from `1.2.0` to `1.4.0` by hand:

```sh
$ ant lock
- example.com/slug 1.2.0
+ example.com/slug 1.4.0
```

Standard output lists what changed in the lock, and nothing when the lock was right already. A
version that the lock names keeps its hash: `ant lock` never replaces the files a project runs with
others under the same version. Deleting the lock drops its hashes: `ant lock` then records the files
the store holds, or those the server serves now.

While the lock is missing or out of date, a program that imports a package does not load. `ant run`,
`ant check`, and `ant test` stop with exit status 2:

```text
ant.lock: the lock is out of date: the requirements changed since it was written; run ant lock, which selects again [missing-module]
```

A project whose `ant.json` has no dependencies needs no lock. `ant lock` writes none there and
removes one that exists.

## The lock of a script

A script with no project file pins its packages in its imports, and runs with no lock.
`ant lock file.ant` writes one for it, `file.ant.lock`, beside the script:

```ant [slugify.ant]
#!/usr/bin/env ant
import "example.com/slug@1.2.0" Slug

for title in Os.args() {
  println(Slug.make(title))
}
```

```text
+ example.com/slug 1.2.0
```

While `slugify.ant.lock` is there, every command that loads the script takes each package under the
hash the lock gives. Change a pin and the lock is out of date, until `ant lock slugify.ant` writes
it again.

- The lock is found beside the script's real path, so a symlink to the script finds it.
- Two pins of one package and [line](https://ant-lang.org/docs/guide/packages.md#versions) have to agree, and a pin cannot
  be below the version a package of the script asks for. `ant lock` stops at either, and writes
  nothing.
- A file with an `ant.json` in its directory or above is part of that project, not a script of its
  own. `ant lock file.ant` is an error there, and its message names `ant lock`.

## Merge conflicts

When both sides of a merge changed the entries of `ant.json`, both changed the second line of the
lock, so git reports a conflict in `ant.lock`. Keep the package lines of both sides, take out git's
conflict markers, and run `ant lock`. It reads the hashes from the lines and writes the lock the
merged `ant.json` asks for.

Two lines that give one version two hashes are an error that names both: the two sides fetched
different files under one tag.

## Exit status

| Exit status | When                                                                                                                                                            |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `0`         | the lock was written, or was right already                                                                                                                      |
| `1`         | a package cannot be fetched or is refused, the lock is malformed, there is no project file, the pins of a script do not agree, or the file belongs to a project |
| `2`         | the usage is wrong: an unknown flag, or more than one script                                                                                                    |

## See also

[Packages](https://ant-lang.org/docs/guide/packages.md), [`ant add`](https://ant-lang.org/docs/tools/add.md), [`ant remove`](https://ant-lang.org/docs/tools/remove.md),
[`ant update`](https://ant-lang.org/docs/tools/update.md)
