Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Added

- `doctor` names the `.fantomasignore` above the one that governs a file, when a pattern in it would have skipped that file. Fantomas reads the nearest ignore file at or above a file and no other, so a pattern written at the root of a repository has no effect on a folder that has an ignore file of its own beside it. Up to now the report named the ignore file that did decide and said nothing about the one that did not, which answers "which file" and leaves "then why did mine not" for the reader to work out. It quotes the pattern that would have matched, with its line number, the way it already quotes the line that decided. `--json` carries every ignore file above the governing one under `shadowed`, each with `wouldIgnore` and the lines of it that match, whether or not it would have decided anything. [#3423](https://github.com/fsprojects/fantomas/pull/3423)

## [8.0.0-alpha-017] - 2026-08-25

### Added
Expand Down Expand Up @@ -37,7 +43,7 @@
- Breaking: `Fantomas.Core.ParseException` now derives from `FormatException`, as the other exceptions the library raises already did, so `:? FormatException` catches every way formatting can fail. It could not before: it was declared with F#'s `exception` keyword, which cannot name a base class. Its `Message` names the first error by position instead of dumping every diagnostic record through `%A`, and the records are reachable as `.Diagnostics` rather than only by matching the exception pattern. Code matching `| ParseException diagnostics ->` becomes `| :? ParseException as e -> e.Diagnostics`; raising and constructing it are unchanged. [#3415](https://github.com/fsprojects/fantomas/pull/3415)
- A construct Fantomas cannot model is now reported through the same mechanism as a parse failure, instead of as a `%A` dump of a syntax tree node in the middle of the message. The report says in words what could not be modelled, names the union case responsible, positions it in the file being formatted rather than in the `tmp.fsx` the parser was handed, and draws a snippet of the source with a caret under the construct, so the enclosing declaration is visible without having to bisect the file by hand. The syntax tree node is still reported, at `--verbosity d`, where it serves whoever triages the issue rather than whoever files it. This covers every such report, from a type the transformer has no Oak node for to the chain and leading-keyword invariants behind it. `--check` and the daemon position them the same way, so an editor shows its user the snippet rather than a bare line and column. [#3415](https://github.com/fsprojects/fantomas/pull/3415)
- Breaking: everything a format or check run prints was rewritten. Each file gets a sentence of its own opening with a status character, `+ src/A.fs was formatted.`, and the run ends with a line of counts, `2 files formatted, 30 unchanged.` This replaces the bordered table of headings that a folder run printed, which bypassed the logger and so carried no timestamp at `--verbosity d` while everything around it did. A run over a single named file is answered on its own terms and has no summary added to it. Any script reading this output needs updating; `--json` exists for a caller that has to act on the result rather than read it. [#3416](https://github.com/fsprojects/fantomas/pull/3416)
- Breaking: `.fantomasignore` is resolved per file, the nearest one at or above it, which is what the daemon has always done. The command line used to resolve one file for the whole run from the directory it started in, so an ignore file in a subfolder was honoured by an editor and invisible to a pipeline: the same file was skipped in one and formatted in the other. A nested ignore file that used to have no effect on a command line run now has one. [#3416](https://github.com/fsprojects/fantomas/pull/3416)
- Breaking: the `.fantomasignore` that applies to a file is found by walking up from that file, rather than from the directory Fantomas was started in. This is what the daemon has always done, and of everything in this release it is the change most likely to be noticed. Where you run the command from no longer decides which ignore file applies: `cd tools && fantomas ../src` used to match files under `src` against `tools/.fantomasignore`, and could skip every one of them on the strength of an ignore file that had nothing to do with them, while never reading the one sitting above `src`. In the other direction, an ignore file in a subfolder was honoured by an editor and invisible to a pipeline, so the same file was skipped in one and formatted in the other; a nested ignore file that used to have no effect on a command line run now has one. Fantomas still reads one ignore file per file, the nearest at or above it, and does not merge in the ones above that. [#3416](https://github.com/fsprojects/fantomas/pull/3416)
- Breaking: a folder that `.fantomasignore` names is no longer opened. Up to now every file inside it was found and then rejected one at a time; the folder is now passed over, so nothing inside it is read, counted or reported. A folder pattern spelled with a trailing separator, `vendor/`, closes the folder as `vendor` always did. Nothing is formatted that was not formatted before. [#3416](https://github.com/fsprojects/fantomas/pull/3416)
- Breaking: a run that skipped files no longer says how many. The count could not be honest: an ignore pattern naming a file can be counted, and one naming a folder cannot, because the folder is never opened and what is inside it is unknown by design. A number right about the first and blind to the second reads as though it covered both. A file named on the command line still gets a line of its own, since a count is the only other place a path could be accounted for and no count carries this one; a file found by walking a folder, and the folder itself, are named at `--verbosity d`. [#3416](https://github.com/fsprojects/fantomas/pull/3416)
- Breaking: `--json` no longer names a file that `.fantomasignore` matched, and `ignored` is no longer a status a file can carry. Nothing counts them in its place, for the reason above: `files` is what the run looked at. [#3416](https://github.com/fsprojects/fantomas/pull/3416)
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/end-users/IgnoreFiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ If you are not sure which ignore file governs a given source file, or which line
dotnet fantomas doctor src/App.fs
```

It names the ignore file, quotes the line that matched with its line number, and writes nothing. See [Getting Started](./GettingStarted.html) for the rest of what it reports.
It names the ignore file, quotes the line that matched with its line number, and writes nothing. Where a pattern in an ignore file further up would have skipped the file, it names that file too and quotes the pattern, which is the answer when something you wrote at the root of a repository turns out to have had no effect. See [Getting Started](./GettingStarted.html) for the rest of what it reports.

Also note that if you are less familiar with `.gitignore`, `.gitgnore` processes everything using Unix slashes `/`.
Windows slashes ` \ ` will not work correctly. See [official Git documentation](https://git-scm.com/docs/gitignore#_pattern_format) for more info.
Expand Down
26 changes: 20 additions & 6 deletions docs/docs/end-users/UpgradeGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fsharp_experimental_stroustrup_style = true
- `--force` announces invalid output as a warning on standard error rather than on standard out, and says what happened rather than only that it happened.
- Output that Fantomas will not accept from itself is reported as a bug in Fantomas rather than as a problem with your file, and says that nothing was written and where to report it. `Formatting A.fs leads to invalid F# code` became `src/A.fs could not be formatted by Fantomas:` followed by that explanation, what the parser said about the output, and the lines of the output around it with a caret under the failure. Those are lines of the output, which is written nowhere and is not your file, and the report says so; the diagnostics carry no line and column of their own for the same reason. Up to `v7` you were told only that something was invalid and left to run again with `--force` and find it yourself. `--check` reports it the same way. A script matching on the old text needs updating.
- `--version` prints the commit hash trimmed to the short form the `--help` page has always shown, and the whole of it at `--verbosity d`. `Fantomas.Client` is unaffected: it cuts the version at `+`.
- A `.fantomasignore` in a subfolder is now honoured by the command line. See "`.fantomasignore` in a subfolder" below.
- The `.fantomasignore` that applies to a file is found by walking up from that file rather than from the directory Fantomas was started in, so where you run the command from no longer decides which ignore file applies. See "Which `.fantomasignore` applies" below.
- `--json` no longer names a file that `.fantomasignore` matched, and `ignored` is no longer a status a file can carry. See "What a run says about skipped files" below.
- A run writing to `--out` reports what was written rather than what changed, `32 files written to build, 2 reformatted.`, since under `--out` every input produces an output file whether or not its content changed. A single file named on the command line says where it went, `+ src/A.fs was formatted and written to build/A.fs.`

Expand Down Expand Up @@ -155,12 +155,23 @@ decided separately, so piping one of them does not take the colour off the other
If you parse this output, move to `--json`, which puts one document on standard out describing
every file the run looked at.

#### `.fantomasignore` in a subfolder
#### Which `.fantomasignore` applies

Up to now the command line resolved one ignore file for the whole run, the nearest at or above the
directory it started in. The daemon resolved one per file, the nearest at or above that file. So an
ignore file in a subfolder was honoured by an editor and invisible to a pipeline, and the same file
was skipped in one and formatted in the other:
directory it started in. It is now found by walking up from the file being formatted, which is what
the daemon has always done. Of everything in this release this is the change most likely to be
noticed.

The first thing it changes is that the directory you run from no longer decides which ignore file
applies. Up to `v7` this skipped every file under `src`, on the strength of an ignore file that had
nothing to do with them, and never read the one sitting above `src`:

```text
$ cd tools && dotnet fantomas ../src # v7: matched ../src/*.fs against tools/.fantomasignore
```

The second is that an ignore file in a subfolder was honoured by an editor and invisible to a
pipeline, so the same file was skipped in one and formatted in the other:

```text
repo/.fantomasignore # found by both
Expand All @@ -173,7 +184,10 @@ run has one. If a subfolder of your repository carries a `.fantomasignore`, chec
files you have been formatting all along.

Note that this is still the nearest ignore file and not the union of every one above it, which is
where Fantomas differs from `.gitignore`.
where Fantomas differs from `.gitignore`. A pattern you wrote at the root of a repository therefore
has no effect on a folder that carries an ignore file of its own, and `fantomas doctor <file>` will
say so: it names the ignore file that governs the file, and names one further up whose pattern
would have skipped it.

A folder that `.fantomasignore` names is no longer opened at all. Up to now every file inside it
was found and then rejected one at a time. Nothing is formatted that was not formatted before, and
Expand Down
59 changes: 56 additions & 3 deletions src/Fantomas.Tests/DoctorCommandTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ let private diagnosing (content: string) : DoctorReport =
write fs file content
doctor fs None (InputPath.File file) |> completed

/// The doctor over a tree whose ignore files it resolves itself, rather than being handed the one
/// that governs. What is being tested is which ignore file was reached and which were passed over,
/// so handing one over would answer the question the test is asking.
let private doctorResolving (fs: IFileSystem) (inputPath: InputPath) : DoctorCommandResult =
let recorded: RecordedRun = recordingEnvironment fs None

let environment: Fantomas.Cli.CliEnvironment =
{ recorded.Environment with
FindIgnoreFile = IgnoreFile.cachedFinder fs (IgnoreFile.loadIgnoreList fs)
FindIgnoreFilesAbove = IgnoreFile.findAbove fs (IgnoreFile.loadIgnoreList fs)
}

runDoctorCommand environment inputPath

/// The ignore file at the mock root, resolved the way a run resolves it.
let private ignoreFileAt (fs: IFileSystem) (content: string) : IgnoreFile option =
fs.File.WriteAllText(fs.Path.Combine(mockRoot fs, IgnoreFile.IgnoreFileName), content)
Expand Down Expand Up @@ -167,7 +181,7 @@ let ``an ignored file names the line of the ignore file that matched`` () =
let ignoreFile: IgnoreFile option = ignoreFileAt fs "# not these\n*.fsx\nA.fs\n"

match (doctor fs ignoreFile (InputPath.File file) |> completed).Ignore with
| Some(IgnoreStep.Governed(_, true, matches)) ->
| Some(IgnoreStep.Governed(_, true, matches, _)) ->
matches
|> shouldEqual
[
Expand Down Expand Up @@ -206,7 +220,7 @@ let ``a pattern that takes a file back out is reported as the line that decided`
write fs file "let a = 1\n"

match (doctor fs (ignoreFileAt fs "*.fs\n!A.fs\n") (InputPath.File file) |> completed).Ignore with
| Some(IgnoreStep.Governed(_, false, matches)) ->
| Some(IgnoreStep.Governed(_, false, matches, _)) ->
matches
|> List.map (fun m -> m.LineNumber, m.Pattern, m.Negated)
|> shouldEqual [ (1, "*.fs", false); (2, "!A.fs", true) ]
Expand All @@ -219,9 +233,48 @@ let ``a file no pattern matches is governed by the ignore file all the same`` ()
write fs file "let a = 1\n"

match (doctor fs (ignoreFileAt fs "*.fsx\n") (InputPath.File file) |> completed).Ignore with
| Some(IgnoreStep.Governed(_, false, [])) -> ()
| Some(IgnoreStep.Governed(_, false, [], _)) -> ()
| other -> failwith $"Expected the file to be governed and unmatched, got %A{other}"

[<Test>]
let ``an ignore file the nearest one shadows is reported with what it would have decided`` () =
// The whole reason the files above are looked for. Someone wrote `*.g.fs` at the root of their
// repository, a subfolder has an ignore file of its own that says nothing about it, and the
// root file is never opened. Naming the ignore file that did decide answers "which file" and
// leaves "then why did mine not" for this.
let fs: IFileSystem = MockFileSystem()
let root: string = mockRoot fs
let file: string = fs.Path.Combine(root, "src", "Generated.g.fs")
write fs file "let a = 1\n"
fs.File.WriteAllText(fs.Path.Combine(root, IgnoreFile.IgnoreFileName), "*.g.fs\n")
fs.File.WriteAllText(fs.Path.Combine(root, "src", IgnoreFile.IgnoreFileName), "Scratch.fs\n")

match (doctorResolving fs (InputPath.File file) |> completed).Ignore with
| Some(IgnoreStep.Governed(governing, false, [], [ above ])) ->
governing
|> shouldEqual (fs.Path.Combine(root, "src", IgnoreFile.IgnoreFileName))

above.Path |> shouldEqual (fs.Path.Combine(root, IgnoreFile.IgnoreFileName))
// What it would have made of the file, asked of it exactly as the governing file is asked.
above.WouldIgnore |> shouldEqual true

above.Matches
|> List.map (fun (m: IgnoreMatch) -> m.LineNumber, m.Pattern)
|> shouldEqual [ (1, "*.g.fs") ]
| other -> failwith $"Expected one shadowed ignore file, got %A{other}"

[<Test>]
let ``the ordinary layout of one ignore file has nothing above it to report`` () =
let fs: IFileSystem = MockFileSystem()
let root: string = mockRoot fs
let file: string = fs.Path.Combine(root, "src", "A.fs")
write fs file "let a = 1\n"
fs.File.WriteAllText(fs.Path.Combine(root, IgnoreFile.IgnoreFileName), "*.fsx\n")

match (doctorResolving fs (InputPath.File file) |> completed).Ignore with
| Some(IgnoreStep.Governed(_, false, [], shadowed)) -> shadowed |> shouldEqual []
| other -> failwith $"Expected nothing above the governing ignore file, got %A{other}"

// ---- formatting, and what Fantomas makes of what it produced ----

[<Test>]
Expand Down
54 changes: 54 additions & 0 deletions src/Fantomas.Tests/IgnoreFileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,57 @@ let ``a pattern the ignore library will not compile fails the whole ignore file`
match loaded with
| Ok _ -> failwith "Expected the ignore file to fail to load."
| Error error -> error.Message |> shouldContainText "Unterminated"

/// An ignore file at each of the given folders, relative to a mock root, and the one that governs
/// the given file.
let private chainOf (folders: string list) (relativePath: string) : IFileSystem * IgnoreFile * string =
let fs: IFileSystem = MockFileSystem()
let root: string = fs.Path.GetTempPath() |> fs.Path.GetPathRoot

let under (relative: string) : string =
fs.Path.Combine(root, relative.Replace("/", string<char> fs.Path.DirectorySeparatorChar))

let file: string = under relativePath
makeFileHierarchy fs [ file ]

for folder in folders do
let ignoreFilePath: string =
fs.Path.Combine(under folder, IgnoreFile.IgnoreFileName)

makeFileHierarchy fs [ ignoreFilePath ]
fs.File.WriteAllText(ignoreFilePath, folder)

match IgnoreFile.find fs (IgnoreFile.loadIgnoreList fs) file with
| None -> failwith $"No ignore file was found above %s{file}."
| Some ignoreFile -> fs, ignoreFile, file

[<Test>]
let ``findAbove returns nothing when the governing ignore file is the only one`` () =
let fs, ignoreFile, _ = chainOf [ "" ] "src/A.fs"

IgnoreFile.findAbove fs (IgnoreFile.loadIgnoreList fs) ignoreFile
|> List.map (fun (found: IgnoreFile) -> found.Location.FullName)
|> shouldEqual []

[<Test>]
let ``findAbove returns every ignore file above the governing one, nearest first`` () =
// The layout the command line could not see before 8.0 and the daemon always could. None of
// these applies; `doctor` is the only thing that asks, and it asks so that a pattern somebody
// wrote at the root and cannot find the effect of can be pointed at.
let fs, ignoreFile, _ = chainOf [ ""; "src"; "src/deep" ] "src/deep/A.fs"

let above: string list =
IgnoreFile.findAbove fs (IgnoreFile.loadIgnoreList fs) ignoreFile
|> List.map (fun (found: IgnoreFile) -> found.Location.Directory.Name)

// The governing file is the one in `src/deep`, so it is not among these.
above |> shouldEqual [ "src"; fs.Path.GetPathRoot(fs.Path.GetTempPath()) ]

[<Test>]
let ``findAbove does not return the ignore file it was given`` () =
let fs, ignoreFile, _ = chainOf [ ""; "src" ] "src/A.fs"

IgnoreFile.findAbove fs (IgnoreFile.loadIgnoreList fs) ignoreFile
|> List.map (fun (found: IgnoreFile) -> found.Location.FullName)
|> List.contains ignoreFile.Location.FullName
|> shouldEqual false
Loading