Stacks

Refactor a Legacy Fortran Module with Atlas in 2026

Updated 7 min read

Atlas helps Fortran developers in 2026 refactor legacy modules by mapping public surfaces with the `lsp` tool, pinning behavior with `fpm test (test-drive)`, applying structural changes with `apply_patch`, and ensuring code quality with `fprettify` before final approval.

How to Map Fortran Module Interfaces and Callers with Atlas

Atlas maps Fortran module interfaces and enumerates all callers using its `lsp` tool, ensuring a complete understanding of the module's public surface before any changes are made. This process, vital for any refactor in 2026, leverages AST declarations from tree-sitter for precise symbol identification.

Before restructuring any Fortran module, understanding its public surface and every callsite is paramount to prevent silent breakage. Atlas achieves this by integrating with the Language Server Protocol (LSP) through its `lsp` tool. It performs the `documentSymbol` operation to identify all exported symbols within your Fortran module, including subroutines, functions, and variables. Following this, Atlas executes `findReferences` on each identified symbol. This comprehensive enumeration of callsites ensures that every location interacting with the legacy module is known. Atlas indexes Fortran code by AST declarations using tree-sitter, not blind line windows, allowing it to accurately read your modules, explicit interfaces, and intent declarations on every dummy argument, providing a robust foundation for a safe refactor.

Pinning Fortran Module Behavior with `fpm test (test-drive)`

Pinning the existing behavior of a Fortran module is the critical first step in any refactoring effort in 2026. Atlas facilitates this by running your `fpm test (test-drive)` suite to establish a green baseline, ensuring that subsequent changes do not introduce regressions.

The risk of silent breakage during a refactor is high, especially in legacy Fortran codebases. Atlas mitigates this by making behavior pinning a non-negotiable step. It uses the `bash` tool to execute your project's `fpm test (test-drive)` command. This runs all existing unit tests located under the `test/` directory, recording their output to establish a 'green baseline.' This baseline serves as the definitive proof that the module's current behavior is understood and correct. Atlas can then iterate on `gfortran` diagnostics, providing feedback on compilation issues. By running these tests before and after every significant change, Atlas ensures that the refactoring process maintains the module's original functionality without introducing new bugs.

Applying Structural Changes to Fortran Code with `apply_patch`

Atlas applies structural changes to Fortran source files using its `apply_patch` tool, which anchors on context lines and refuses to apply against a drifted file. This precision is crucial for safely moving a `COMMON` block into a module or restructuring code in 2026, preventing unintended modifications.

Restructuring Fortran code, such as moving a `COMMON` block into a module or reorganizing subroutines, requires precise modifications. Atlas employs the `apply_patch` tool for this purpose. Unlike simple find-and-replace, `apply_patch` seeks each hunk's context and old_lines. If the target file has drifted from the expected state, it fails with 'Failed to find context,' preventing erroneous changes. This ensures that structural modifications are applied exactly as intended, even in a dynamic codebase. Atlas drafts a plan in a read-only plan agent and asks for approval before switching to a build agent to execute these changes. After each hunk lands, Atlas prompts you to re-run `fpm test` to immediately verify that the module's behavior remains unchanged, catching regressions early rather than at the end of the refactor.

Ensuring Fortran Code Quality with `fprettify` and Diff Review

Maintaining consistent Fortran code style is crucial, and Atlas integrates `fprettify` to automatically format changed sources before you approve the diff. Every Atlas tool call is permission-gated, and every file edit generates a unified diff for approval, ensuring full control over your 2026 Fortran codebase.

Beyond functional correctness, code quality and readability are vital for maintainability. Atlas ensures this by integrating with `fprettify`, the standard Fortran formatter. Before presenting a unified diff for your approval, Atlas can run `fprettify` over the changed Fortran sources. This guarantees that all refactored code adheres to your project's formatting standards. Every Atlas tool call is permission-gated against allow, ask, and deny rules, giving you explicit control. Atlas computes a unified diff for every file edit and surfaces it for approval, allowing you to review every line change. Furthermore, Atlas reads git branches, status, and diffs, and can stage and create commits on your behalf, or snapshot file changes as git patches so edits can be diffed and rolled back, providing robust version control integration.

Tracking Fortran Refactoring Progress with `todowrite`

For complex Fortran module refactors, tracking remaining tasks is essential to avoid incomplete migrations. Atlas's `todowrite` tool helps manage the list of remaining callsites or migration steps, ensuring no part of the refactor is overlooked in 2026.

Refactoring a large Fortran module can involve numerous changes across multiple files and callsites. It's easy for a partially migrated module to be mistaken for a finished one, leading to subtle bugs. Atlas addresses this challenge with its `todowrite` tool. After identifying all callsites with `lsp findReferences`, Atlas can use `todowrite` to create and manage a list of remaining tasks. This list might include specific callsite updates, interface adjustments, or further structural changes. By actively tracking these items, `todowrite` ensures that every aspect of the refactor is addressed, providing a clear path to completion and preventing any callsite from being missed or left in an inconsistent state.

Step by step

  1. 01Run Atlas in your Fortran project, ensuring an `fpm.toml` file is present for package management.
  2. 02Use the `lsp` tool's `documentSymbol` operation to map the public surface of the Fortran module, then `findReferences` on each symbol to enumerate all callers.
  3. 03Pin the module's current behavior by running `bash fpm test (test-drive)` and recording the green baseline of your Fortran unit tests.
  4. 04Draft a refactoring plan with Atlas's read-only plan agent, outlining structural changes like moving a `COMMON` block into a Fortran module.
  5. 05Execute structural changes to Fortran source files using the `apply_patch` tool, which anchors on context lines and ensures precise modifications.
  6. 06After each `apply_patch` hunk lands, re-run `bash fpm test` to immediately verify that the Fortran module's behavior remains unchanged.
  7. 07Track remaining callsites or migration tasks using the `todowrite` tool, ensuring a partially migrated Fortran module is not mistaken for a finished one.
  8. 08Before approving changes, have Atlas run `bash fprettify` over the modified Fortran sources to ensure consistent code formatting.
  9. 09Review the unified diff presented by Atlas, approve the changes, and allow Atlas to stage and create a `git` commit on your behalf.

Frequently asked questions

How does Atlas ensure my Fortran refactor doesn't break existing code?
Atlas ensures Fortran refactors don't break existing code by first mapping all callsites with `lsp findReferences`. It then pins the module's behavior by running `fpm test (test-drive)` to establish a green baseline. Structural changes are applied with `apply_patch`, which verifies context, and `fpm test` is re-run after each change to catch regressions immediately.
Can Atlas refactor Fortran `COMMON` blocks into modules?
Yes, Atlas can refactor Fortran `COMMON` blocks into modules. It uses its `apply_patch` tool to precisely move the `COMMON` block declarations and associated variables into a new or existing Fortran module. This process is guided by a plan drafted by the agent and verified by `fpm test` after each modification.
What Fortran testing tools does Atlas integrate with?
Atlas integrates directly with the Fortran Package Manager's testing framework, `fpm test (test-drive)`. It uses the `bash` tool to execute these tests, record baselines, and iterate on `gfortran` diagnostics, ensuring that your Fortran unit tests are central to the refactoring verification process.
How does Atlas handle Fortran code formatting during refactoring?
Atlas handles Fortran code formatting by integrating with `fprettify`. Before presenting a unified diff for approval, Atlas can run `fprettify` over the changed Fortran sources. This ensures that all refactored code adheres to consistent style guidelines, maintaining code quality and readability.
Is Atlas compatible with my existing Fortran `fpm` project setup?
Yes, Atlas is designed to be compatible with existing Fortran projects managed by `fpm`. You simply run Atlas in a project that contains an `fpm.toml` file. Atlas leverages `fpm` for running tests (`fpm test`) and understanding your project structure, making it a natural fit for modern Fortran development.
How does Atlas help track progress on a large Fortran module refactor?
For large Fortran module refactors, Atlas helps track progress using its `todowrite` tool. After identifying all callsites with `lsp findReferences`, `todowrite` can manage a list of remaining tasks, ensuring that every callsite is updated and no part of the partially migrated Fortran module is overlooked.
Can Atlas work with older Fortran code, like FIXED-form numerics?
Yes, Atlas is capable of working with modern Fortran, from legacy FIXED-form numerics to modules, interfaces, and coarrays. Its code indexing uses AST declarations via tree-sitter, allowing it to understand and process a wide range of Fortran syntax, including older idioms, for refactoring tasks.

Try Atlas in your terminal

The terminal-native AI coding agent. Free core, single binary.

Install Atlas

Related guides

Refactor a Legacy Module with Atlas in 2026

How to refactor a legacy module with Atlas in 2026: findReferences maps every callsite, apply_patch refuses to apply against a drifted file, and bash proves behavior.

Atlas for Fortran: fpm.toml, Explicit Interfaces, and fprettify in 2026

Atlas is a terminal-native AI coding agent for Fortran in 2026. It reads modules, explicit interfaces, and intent declarations, runs fpm test behind a prompt, and runs fprettify.

Automate GitHub Issue and Pull Request Triage in Fortran with Atlas in 2026

Automate GitHub issue and pull request triage for Fortran projects using Atlas. Safely respond to PRs and issues, run `fpm test`, and format with `fprettify` for trusted users in 2026, ensuring code quality and security.

Run the Fortran Test Suite and Triage Failures with Atlas in 2026

In 2026, Fortran developers use Atlas to run fpm test suites, triage failures into distinct root causes, and track fixes. Streamline your Fortran debugging workflow.

Audit a Fortran Repo with Parallel Subagents in 2026

Sweep your Fortran codebase for problems without context window limits. Atlas uses parallel subagents to audit fpm projects, leveraging fprettify and fpm test for comprehensive review in 2026.

Review a Fortran Pull Request with Atlas in 2026

Streamline Fortran pull request reviews in 2026 with Atlas. Leverage `fpm`, `fprettify`, and `gfortran` diagnostics to catch subtle bugs and ensure robust code quality.

Migrate a deprecated API across every callsite in Fortran with Atlas in 2026

Streamline Fortran API migrations in 2026 with Atlas. Enumerate all callers, apply context-anchored patches, and validate changes with fpm test and fprettify for a complete, verified transition.

Upgrade a Fortran Dependency and Fix Breakage with Atlas in 2026

Fortran developers in 2026 can use Atlas to upgrade dependencies, resolve compile errors, and fix test failures. Learn how Atlas integrates with fpm, gfortran, and fprettify for efficient migrations.

Browse this resource hub