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

# Upgrade from Codemod 1.x to 2.0

> What changes when you move from the 1.x CLI to 2.0: commands and flags, running 1.x packages, exit codes, resuming runs, and the changes for codemod authors.

Codemod 2.0 runs workflows written in TypeScript (`workflow.ts`). Packages written for 1.x, with a `workflow.yaml`, keep running: you don't have to change them. This page lists what changes for the people who **run** codemods and for the people who **write** them.

> [!NOTE]
>
> Codemod 2.0 is published on npm under the `next` tag, and `latest` is still 1.x. Run 2.0 with `npx codemod@next`, or pin a version such as `npx codemod@2.0.0`.

## If you run codemods

### Running 1.x packages

Run a 1.x registry or local package with `workflow.yaml` without converting it:

```bash theme={null}
npx codemod@next @org/my-1x-package --target .
npx codemod@next workflow run -w ./my-1x-package --target .
```

The CLI translates the workflow in memory and runs it with 1.x compatibility for transform metrics, workflow state and target-local `fs`: reads, writes (a write replaces a file that is there, as on 1.x) and `unlinkSync`, all part of the step's diff. It does not rewrite the package. Use `workflow upgrade` only when you want to convert the source to `workflow.ts`.

A few 1.x features run differently. The full list is in [What behaves differently](https://docs.codemod.com/community/workflows/migrate.md#what-behaves-differently). The ones you are most likely to notice:

* **Steps with `runtime: docker` or `runtime: podman` are refused.** The CLI refuses the workflow before running any steps and names the node or template. Use [`codemod run --v1 <package>`](https://docs.codemod.com/community/workflows/migrate#running-a-package-with-codemod-1x) if the workflow needs the 1.x runner.
* **Semantic analysis is on by default** in `js-ast-grep` steps, so `definition()` and `references()` now return results in workflows.
* **`$STATE_OUTPUTS` in shell steps needs a POSIX shell.** On Windows, a workflow that uses it fails before any step runs.
* **Registry packages need `--approve` to run shell steps unattended.** In CI or with `--no-interactive`, a registry package's `run:` step (and its agent, HTTP and pull request steps) stops the run until you pass `--approve` or the kind it needs, such as `--approve=shell`. The message names it. Local packages are trusted as before.
* **Shell steps run in the target directory**, not the directory you ran the CLI from.
* **`definition()` follows re-exports** to the file that declares a symbol, so a transform that rewrites imports from it can write different imports than on 1.x.

### Commands and flags that changed

| 1.x | 2.0 |
| - | - |
| `codemod workflow resume -w <workflow> -i <id>` | `codemod workflow resume --id <id>`. The run knows its workflow and target. |
| `workflow resume --trigger-all` | `workflow resume --id <id> --approve`. `--trigger-all` still works as an alias. |
| `workflow resume -t <task-uuid>` / `--tasks_ids` | `workflow resume --id <id> --approve <question-id>`, for example `--approve approval:1`. `workflow status --id <id>` shows the question id. |
| `workflow resume --dry-run`, `workflow resume --target` | Removed. A dry run is never saved, so there is nothing to resume. |
| `codemod workflow tui` | Removed. Use `workflow list`, `workflow status --id <id>` and `workflow attach --id <id>`. |
| `codemod init --with-skill` | Removed. Use `codemod init --skill`. |
| `codemod jssg run --verbose` | Accepted, but has no effect. |
| `--no-color` | Now turns off color in all output, not only in dry-run diffs. |
| `workflow resume --allow-dirty` | Only applies to a run that stopped before it wrote anything. |
| `--allow-fetch` | Deprecated. Callbacks can't make network requests in 2.0; use an `http()` step. The flag is accepted and does nothing. |
| Running a package with the 1.x binary | Use `codemod run --v1 <package>` or `CODEMOD_V1=on` to opt in to the deprecated fallback. Default package runs use the 2.0 engine. |

New in 2.0: `workflow status --format json`, `workflow list --format json`, `workflow inspect`, `workflow attach`, `workflow send`, `workflow upgrade`, `--approve` on `workflow run`, `--control stdio`, and `--managed-git` for git steps. See the [CLI reference](https://docs.codemod.com/community/cli.md#codemod-workflow).

### Exit codes

2.0 tells you how a run ended with its exit code. A script that only checked for `0` keeps working, but it can now tell a run that is waiting for someone apart from one that failed.

| Code | Meaning |
| - | - |
| `0` | The run finished. |
| `1` | The run failed, or was paused, canceled or rejected. |
| `2` | The run finished but some files failed, or the workflow called `fail()`. |
| `3` | The run stopped at an approval and is waiting (`--no-interactive`). |

See [Exit codes](https://docs.codemod.com/community/workflows/exit-codes.md) for the details.

### Resuming runs

* Resume a run by its id: `codemod workflow resume --id <id>`. Find the id in the run's output or with `codemod workflow list`.
* Runs started by the 1.x CLI can't be resumed by 2.0. Finish them with 1.x, or start them again with 2.0.
* Each run records the CLI version that started it. Resume with the same version when you can: if finished steps no longer match, `workflow resume` refuses unless you pass `--force` to run those steps again.
* Only one run at a time can work on a repository. Starting a second run on a target while another run is working on it fails with an error that names the other run.
* Runs are saved under your data directory (`~/Library/Application Support/codemod/runs` on macOS). Set `CODEMOD_RUN_STORE_DIR` to keep them somewhere else.

### Telemetry

The CLI sends anonymous usage data, as 1.x did. Turn it off with `--disable-analytics`, `DISABLE_ANALYTICS=1`, or `DO_NOT_TRACK=1`. See [Telemetry](https://docs.codemod.com/community/cli.md#telemetry).

## If you write codemods

Your `workflow.yaml` package keeps working and you can keep publishing it. When you want TypeScript workflows, tests that run the whole workflow, and step results you can pass around, convert it:

```bash theme={null}
npx codemod@next workflow upgrade --dry-run
npx codemod@next workflow upgrade
```

`--dry-run` shows the changes as a diff and writes nothing. The upgrade checks every package before it writes anything: when one can't be upgraded, it names every problem, writes nothing and exits with code `2`. It refuses workflow files that are symbolic links, two workflows whose new files would differ only in letter case, and a package whose transforms would not load in 2.0, naming the transform and the import that stopped it.

Upgraded transforms keep 1.x `fs` inside the target (reads, writes that replace a file, and `unlinkSync`), so helpers that read or update `package.json` keep working. That holds while the line `upgrade` writes, ``// Generated from workflow.yaml by `codemod workflow upgrade`.``, is among the comments `workflow.ts` starts with; hand-written workflows use `codemod/files` instead. A transform that imports `child_process` is not upgraded: move its commands to a `shell` step first.

If the upgrade itself won't run, `workflow upgrade --legacy` uses the earlier translator without the workflow engine: it writes the files directly, and with `--dry-run` it prints the generated TypeScript instead of a diff. It doesn't take `--format`.

[Upgrade a 1.x package](https://docs.codemod.com/community/workflows/tutorials/upgrade-a-1x-package.md) walks through it, and [Migrate from workflow.yaml](https://docs.codemod.com/community/workflows/migrate.md) shows how each YAML feature maps to TypeScript.

### Metrics are now return values

1.x YAML runs still support `codemod:metrics` and `useMetricAtom`. In a TypeScript workflow, return what you counted from the callback, then add it up with a reducer:

```ts theme={null}
// 1.x transform
import { useMetricAtom } from "codemod:metrics";
const calls = useMetricAtom("console-calls");
calls.increment();

// 2.0 workflow
const { reduced } = await step("Count console calls", forEachFile(
  "src/**/*.ts",
  (file) => file.text.split("console.log(").length - 1,
  (total, count) => total + count,
  0,
));
```

What the workflow returns is the run's output: `Output: …` in the text output and `output` in `workflow status --format json`. See [Build a report that fails CI](https://docs.codemod.com/community/workflows/tutorials/reports.md) and [Metrics](https://docs.codemod.com/community/jssg/metrics.md).

### Fail the run with `fail()`

To fail the whole run and keep a report, call `fail()` from the workflow body. The run ends as `failed`, keeps its output, and the CLI exits `2`:

```ts theme={null}
import { fail } from "codemod/workflow";

if (problems.length) fail(`${problems.length} files need a manual fix`, { output: { problems } });
```

`failStep()` from `codemod/runtime` still exists: it fails the step it is called in, and the error ends the run unless the workflow catches it. Use `fail()` when you want CI to see a report.

### Callbacks don't reach the outside world

`forEachFile` callbacks, function steps and reducers only compute: they can read files inside the target but cannot write through `fs`. They return values or use the file API to edit the current file. They can't call `fetch`, start processes with `child_process`, or set environment variables. Do that with steps the run can show, ask about and replay:

| You did this in a transform or script | Use this step |
| - | - |
| Ran a command (`child_process`, `execSync`) | `shell` |
| Called an API (`fetch`) | `http` |
| Asked an AI model to edit code | `agent` |

Workflow runs still accept `--allow-fetch`, `--allow-fs` and `--allow-child-process`, but warn and ignore them. Capability declarations in `codemod.yaml` do not change what callbacks can do. Standalone `codemod jssg run` and `codemod jssg test` still use capability flags.

### `runtime: docker`

2.0 does not automatically run steps in containers. A `workflow.yaml` with `runtime: docker` or `runtime: podman` is refused before any step runs, and `workflow upgrade` reports it. Keep running the original package with 1.x, or explicitly invoke the container from a `shell` step when porting the workflow.

### Other changes

* Import modules as `codemod/workflow`, `codemod/ast-grep` and so on. The `codemod:` spelling still works.
* In TypeScript workflows, `getState`, `setState` and `$STATE_OUTPUTS` are replaced by passing step results to later steps. See [Values between steps](https://docs.codemod.com/community/workflows/reference.md#values-between-steps).
* A parameter you declare as `string` arrives as a string, even when it looks like a number (`--param version=19` gives `"19"`). 1.x parsed it as JSON.
* Add `codemod` as a dev dependency (`npm install --save-dev codemod@next`) so your editor knows the workflow types.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.