---
title: Exec command
description: Run one command inside each selected package.
---
> ## Documentation Index
> Fetch the complete documentation index at: https://melos.invertase.dev/llms.txt
> Use this file to discover all available pages before exploring further.

<Info>Supports all [Melos filtering](/filters) flags.</Info>

Execute an arbitrary command in each package.

```bash
melos exec
# for example: melos exec -- pub global run tuneup check

```

The command is executed in a shell. On Windows the shell is `cmd.exe` and on all
other platforms it is `sh`.

If multiple commands run and you need to stop after a command fails, connect the
commands with `&&`:

```shell
melos exec -- "melos bootstrap && melos run build"
```

Pass multiple commands as a single string to `melos exec`. Otherwise the shell
passes only the first command to `melos exec`.

`dart test` and `flutter test` exit with the exit code `79` when no tests ran,
for example when no tests in a package match the requested `--tags`. Melos
treats this exit code as a success, so that packages without matching tests do
not fail the command or trigger `--fail-fast`:

```shell
melos exec --fail-fast -- "flutter test --tags integration"
```

## concurrency (-c)

Defines how many packages execute the command concurrently. Defaults to the
number of processors of the machine.

```bash
# Set concurrency to 1

melos exec -c 1 -- "dart analyze ."
```

## --fail-fast (-f)

Whether exec fails fast and does not execute the script in further packages if
the script fails in an individual package. Defaults to `false`.

```bash
# Fail fast

melos exec --fail-fast -- "dart analyze ."
```

## --order-dependents (-o)

Whether exec orders the execution of the script in multiple packages based on
the dependency graph of the packages. The script runs in leaf packages first,
then in the packages that depend on them. Ordering helps when a script generates
code in packages that depend on each other. Defaults to `false`.

```bash
melos exec --order-dependents -- "dart run build_runner build"
```

## --group-logs

When a command runs in multiple packages concurrently, Melos streams each
package's output as it is produced. The logs of different packages are then
interleaved and hard to follow.

With `--group-logs`, Melos buffers each package's output instead. After every
package finishes, Melos prints the output grouped per package, in the order it
was produced. Melos prints output from failed packages last, so failures appear at the end of
the log. Defaults to `false`.


The flag only has an effect when the command runs in more than one package with
a concurrency greater than 1. When the command runs in a single package or with
`--concurrency 1`, output cannot interleave. In those cases `--group-logs` is a
no-op and Melos streams the output as usual.

```bash
# Print the output grouped per package, after all packages have finished

melos exec --group-logs -- "dart analyze ."
```

<Info>
  Because Melos prints the output only after all packages have finished, nothing
  is printed during the run.
</Info>

## --sources

Globs, relative to the root of each package, that match the files the result of
the command depends on. Melos stores a checksum of the matching files in every
package in which the command succeeds. On the next run of the same command,
Melos skips the package if none of the files changed. The option can be
specified multiple times.

```bash
# Only generate code in the packages in which the Dart files or the pubspec
# changed after the last successful run

melos exec --order-dependents --sources "lib/**.dart" --sources "pubspec.yaml" -- "dart run build_runner build"
```

A glob that matches a directory, such as `lib`, also matches all of the files
inside of it. A glob can also match files outside of the package. For example,
`../../pubspec.lock` matches the workspace lock file from a package in
`packages/<name>`. Melos prints a warning for a glob that does not match a file
in any package, since that usually is a mistake.

The checksum of a package also covers the matching files of all of its
dependencies in the workspace. The result of a command such as code generation
can depend on those files. A change in a package therefore makes the command run
again in every package that depends on it. Packages that are excluded from the
workspace with [`ignore`](/configuration/overview#ignore) are not taken into
account.

A package in which the command fails is not considered up to date, so the
command runs in it again the next time. The packages that depend on it are not
affected by that failure, as long as its files did not change.

The checksums are stored in the `.dart_tool/melos/fingerprints` directory in each
package, separately for every combination of command and sources.
[`melos clean`](/reference/commands/clean) removes them.

<Info>
  Melos calculates the checksum of the files in the package itself after the
  command has finished. Files the command generates that also match the sources
  do not make the package look changed the next time. Changes made to these
  files during the run are therefore not tracked as they happen.
</Info>

<Warning>
  Combine a command that generates files that match the sources, such as code
  generation, with `--order-dependents`, or
  [`orderDependents`](/configuration/scripts#orderdependents) in a script. Then
  the command finishes in the dependencies of a package before it starts in the
  package. Otherwise a package can be run or skipped based on files that the
  command is still generating in a dependency. Melos detects that race, prints a
  warning, and runs the command in the package the next time.
</Warning>

Commands that do not change the sources, such as running tests or the analyzer,
do not need to be ordered. They keep running in all packages concurrently.

<Warning>
  Only the files that match the sources are taken into account. Melos does not
  account for a changed environment variable, a new version of a tool, or a
  changed file outside of the sources. Use
  [`--run-unchanged`](#--run-unchanged) in these cases.
</Warning>

## --run-unchanged

Runs the command in every package, even when the files matching
[`--sources`](#--sources) did not change. The checksums of the packages in which
the command succeeds are still updated.

```bash
melos exec --run-unchanged --sources "lib/**.dart" -- "dart run build_runner build"
```

## --ignore-sources

Ignores [`--sources`](#--sources), so that the command runs in every package
without calculating or storing checksums. Ignoring sources helps when the
checksums are never reused, for example on a continuous integration server that
starts from a fresh checkout. It avoids reading all of the source files.

```bash
melos exec --ignore-sources --sources "lib/**.dart" -- "dart test"
```

In contrast to [`--run-unchanged`](#--run-unchanged), no checksums are stored.
The stored checksums of the packages that the command runs in are removed. The
command also runs in them the next time that the sources are taken into account.

The sources can be ignored for the whole workspace with
[`command/exec/ignoreSources`](/configuration/overview#ignoresources) or the
[`MELOS_IGNORE_SOURCES`](/environment-variables#melos_ignore_sources)
environment variable. Use `--no-ignore-sources` to take them into account
regardless.
