Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best effort run mismatched version analyzer #181

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: CI
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main

permissions:
contents: read
Expand All @@ -28,15 +28,15 @@ jobs:
run: dotnet fsi build.fsx

- name: Upload documentation
if: github.ref == 'refs/heads/master'
if: github.ref == 'refs/heads/main'
uses: actions/upload-pages-artifact@v1
with:
path: ./output

deploy:
runs-on: ubuntu-latest
needs: ci
if: github.ref == 'refs/heads/master'
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to GitHub Pages
id: deployment
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Changed
* [Add path to ASTCollecting](https://github.com/ionide/FSharp.Analyzers.SDK/pull/171) (thanks @nojaf!)
* [Use Microsoft.Extensions.Logging instead of printf based logging infrastructure](https://github.com/ionide/FSharp.Analyzers.SDK/pull/175) (thanks @dawedawe!)

## [0.21.0] - 2023-11-22

Expand Down
26 changes: 13 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Guidelines for bug reports:
reported.

2. **Check if the issue has been fixed** — try to reproduce it using the
`master` branch in the repository.
`main` branch in the repository.

3. **Isolate and report the problem** — ideally create a reduced test
case.
Expand Down Expand Up @@ -97,16 +97,16 @@ in order to craft an excellent pull request:
2. If you cloned a while ago, get the latest changes from upstream, and update your fork:

```bash
git checkout master
git pull upstream master
git checkout main
git pull upstream main
git push
```

3. Create a new topic branch (off of `master`) to contain your feature, change,
3. Create a new topic branch (off of `main`) to contain your feature, change,
or fix.

**IMPORTANT**: Making changes in `master` is discouraged. You should always
keep your local `master` in sync with upstream `master` and make your
**IMPORTANT**: Making changes in `main` is discouraged. You should always
keep your local `main` in sync with upstream `main` and make your
changes in topic branches.

```bash
Expand Down Expand Up @@ -135,17 +135,17 @@ in order to craft an excellent pull request:
with a clear title and description.

8. If you haven't updated your pull request for a while, you should consider
rebasing on master and resolving any conflicts.
rebasing on main and resolving any conflicts.

**IMPORTANT**: _Never ever_ merge upstream `master` into your branches. You
should always `git rebase` on `master` to bring your changes up to date when
**IMPORTANT**: _Never ever_ merge upstream `main` into your branches. You
should always `git rebase` on `main` to bring your changes up to date when
necessary.

```bash
git checkout master
git pull upstream master
git checkout main
git pull upstream main
git checkout <your-topic-branch>
git rebase master
git rebase main
```


Expand All @@ -159,4 +159,4 @@ in order to craft an excellent pull request:
3. Make a new version tag (for example, `v0.45.0`)
1. `git tag v0.45.0`
4. Push changes to the repo.
1. `git push --atomic [insert-remote-branch] master v0.45.0`
1. `git push --atomic [insert-remote-branch] main v0.45.0`
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</PropertyGroup>

<PropertyGroup>
<FsDocsLicenseLink>https://github.com/ionide/FSharp.Analyzers.SDK/blob/master/LICENSE.md</FsDocsLicenseLink>
<FsDocsReleaseNotesLink>https://github.com/ionide/FSharp.Analyzers.SDK/blob/master/CHANGELOG.md</FsDocsReleaseNotesLink>
<FsDocsLicenseLink>https://github.com/ionide/FSharp.Analyzers.SDK/blob/main/LICENSE.md</FsDocsLicenseLink>
<FsDocsReleaseNotesLink>https://github.com/ionide/FSharp.Analyzers.SDK/blob/main/CHANGELOG.md</FsDocsReleaseNotesLink>
<RepositoryUrl>https://github.com/ionide/FSharp.Analyzers.SDK</RepositoryUrl>
</PropertyGroup>

Expand Down
3 changes: 3 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<PackageVersion Include="Microsoft.Build.Locator" Version="1.4.1" />
<!-- Need to update Directory.Build.props DotNet.ReproducibleBuilds.Isolated version when updating this-->
<PackageVersion Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="All" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="MSBuild.StructuredLogger" Version="2.1.815" />
<PackageVersion Include="NUnit" Version="3.13.3" />
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ F# analyzers are live, real-time, project based plugins that enables to diagnose
2. Run the console application:

```shell
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbose
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbosity d
```

You can also set up a run configuration of FSharp.Analyzers.Cli in your favorite IDE using similar arguments. This also allows you to debug FSharp.Analyzers.Cli.
Expand All @@ -36,7 +36,7 @@ There might be a little voice inside that tells you you're not ready; that you n

I assure you, that's not the case.

This project has some clear Contribution Guidelines and expectations that you can [read here](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/master/CONTRIBUTING.md).
This project has some clear Contribution Guidelines and expectations that you can [read here](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/main/CONTRIBUTING.md).

The contribution guidelines outline the process that you'll need to follow to get a patch merged. By making expectations and process explicit, I hope it will make it easier for you to contribute.

Expand All @@ -50,4 +50,4 @@ Thank you for contributing!
The project is hosted on [GitHub](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK) where you can [report issues](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/issues), fork
the project and submit pull requests.

The library is available under [MIT license](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/master/LICENSE.md), which allows modification and redistribution for both commercial and non-commercial purposes.
The library is available under [MIT license](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/main/LICENSE.md), which allows modification and redistribution for both commercial and non-commercial purposes.
2 changes: 1 addition & 1 deletion build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pipeline "Build" {
}
stage "sample" {
run
"dotnet run --project src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbose"
"dotnet run --project src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbosity d"
}
stage "docs" { run "dotnet fsdocs build --properties Configuration=Release --eval --clean --strict" }
runIfOnlySpecified false
Expand Down
6 changes: 3 additions & 3 deletions docs/content/Getting Started Using.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ At the time of writing, the [G-Research analyzers](https://github.com/g-research
With the package downloaded, we can run the CLI tool:

```shell
dotnet fsharp-analyzers --project ./YourProject.fsproj --analyzers-path C:\Users\yourusername\.nuget\packages\g-research.fsharp.analyzers\0.4.0\analyzers\dotnet\fs\ --verbose
dotnet fsharp-analyzers --project ./YourProject.fsproj --analyzers-path C:\Users\yourusername\.nuget\packages\g-research.fsharp.analyzers\0.4.0\analyzers\dotnet\fs\ --verbosity d
```

### Using an MSBuild target
Expand All @@ -57,7 +57,7 @@ Before we can run `dotnet msbuild /t:AnalyzeFSharpProject`, we need to specify o

```xml
<PropertyGroup>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --treat-as-warning IONIDE-004 --verbose</FSharpAnalyzersOtherFlags>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --treat-as-warning IONIDE-004 --verbosity d</FSharpAnalyzersOtherFlags>
</PropertyGroup>
```

Expand Down Expand Up @@ -101,7 +101,7 @@ This is effectively the same as adding a property to each `*proj` file which exi
<PropertyGroup>
<SarifOutput Condition="$(SarifOutput) == ''">./</SarifOutput>
<CodeRoot Condition="$(CodeRoot) == ''">.</CodeRoot>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(SarifOutput)$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --code-root $(CodeRoot) --treat-as-warning IONIDE-004 --verbose</FSharpAnalyzersOtherFlags>
<FSharpAnalyzersOtherFlags>--analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)/analyzers/dotnet/fs&quot; --report &quot;$(SarifOutput)$(MSBuildProjectName)-$(TargetFramework).sarif&quot; --code-root $(CodeRoot) --treat-as-warning IONIDE-004 --verbosity d</FSharpAnalyzersOtherFlags>
</PropertyGroup>
</Project>
```
Expand Down
9 changes: 8 additions & 1 deletion docs/content/Getting Started Writing.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ dotnet tool install --global fsharp-analyzers
```

```shell
fsharp-analyzers --project YourProject.fsproj --analyzers-path ./OptionAnalyzer/bin/Release --verbose
fsharp-analyzers --project YourProject.fsproj --analyzers-path ./OptionAnalyzer/bin/Release --verbosity d
```

### Packaging and Distribution
Expand Down Expand Up @@ -190,6 +190,13 @@ Target.create
)

(**

### Known footguns to avoid

There's a footgun in the FCS-API that you can easily trigger when working on an analyzer:
Accessing the [FullName](https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-symbols-fsharpentity.html#FullName) property of the [FSharpEntity](https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-symbols-fsharpentity.html) type throws an exception if the entity doesn't have one.
Use the [TryGetFullName](https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-symbols-fsharpentity.html#TryGetFullName) function for safe access.

[Previous]({{fsdocs-previous-page-link}})
[Next]({{fsdocs-next-page-link}})

Expand Down
1 change: 1 addition & 0 deletions docs/content/Programmatic access.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The `Client` needs to know what type of analyzer you intend to load: *console* o
(*** hide ***)
#r "../../src/FSharp.Analyzers.Cli/bin/Release/net6.0/FSharp.Analyzers.SDK.dll"
#r "../../src/FSharp.Analyzers.Cli/bin/Release/net6.0/FSharp.Compiler.Service.dll"
#r "../../src/FSharp.Analyzers.Cli/bin/Release/net6.0/Microsoft.Extensions.Logging.Abstractions.dll"
(** *)

open FSharp.Analyzers.SDK
Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ F# analyzers are live, real-time, project based plugins that enables to diagnose
2. Run the console application:

```shell
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbose
dotnet run --project src\FSharp.Analyzers.Cli\FSharp.Analyzers.Cli.fsproj -- --project ./samples/OptionAnalyzer/OptionAnalyzer.fsproj --analyzers-path ./samples/OptionAnalyzer/bin/Release --verbosity d
```

You can also set up a run configuration of FSharp.Analyzers.Cli in your favorite IDE using similar arguments. This also allows you to debug FSharp.Analyzers.Cli.
Expand All @@ -36,7 +36,7 @@ There might be a little voice inside that tells you you're not ready; that you n

I assure you, that's not the case.

This project has some clear Contribution Guidelines and expectations that you can [read here](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/master/CONTRIBUTING.md).
This project has some clear Contribution Guidelines and expectations that you can [read here](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/main/CONTRIBUTING.md).

The contribution guidelines outline the process that you'll need to follow to get a patch merged. By making expectations and process explicit, I hope it will make it easier for you to contribute.

Expand All @@ -50,6 +50,6 @@ Thank you for contributing!
The project is hosted on [GitHub](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK) where you can [report issues](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/issues), fork
the project and submit pull requests.

The library is available under [MIT license](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/master/LICENSE.md), which allows modification and redistribution for both commercial and non-commercial purposes.
The library is available under [MIT license](https://github.com/Krzysztof-Cieslak/FSharp.Analyzers.SDK/blob/main/LICENSE.md), which allows modification and redistribution for both commercial and non-commercial purposes.

[Next]({{fsdocs-next-page-link}})
80 changes: 80 additions & 0 deletions src/FSharp.Analyzers.Cli/CustomLogging.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module FSharp.Analyzers.Cli.CustomLogging

open System
open System.IO
open System.Runtime.CompilerServices
open Microsoft.Extensions.Logging
open Microsoft.Extensions.Logging.Console
open Microsoft.Extensions.Logging.Abstractions
open Microsoft.Extensions.Options

type CustomOptions() =
inherit ConsoleFormatterOptions()

/// if true: no LogLevel as prefix, colored output according to LogLevel
/// if false: LogLevel as prefix, no colored output
member val UseAnalyzersMsgStyle = false with get, set

type CustomFormatter(options: IOptionsMonitor<CustomOptions>) as this =
inherit ConsoleFormatter("customName")

let mutable optionsReloadToken: IDisposable = null
let mutable formatterOptions = options.CurrentValue
let origColor = Console.ForegroundColor

do optionsReloadToken <- options.OnChange(fun x -> this.ReloadLoggerOptions(x))

member private _.ReloadLoggerOptions(opts: CustomOptions) = formatterOptions <- opts

override this.Write<'TState>
(
logEntry: inref<LogEntry<'TState>>,
_scopeProvider: IExternalScopeProvider,
textWriter: TextWriter
)
=
let message = logEntry.Formatter.Invoke(logEntry.State, logEntry.Exception)

if formatterOptions.UseAnalyzersMsgStyle then
this.SetColor(textWriter, logEntry.LogLevel)
textWriter.WriteLine(message)
this.ResetColor()
else
this.WritePrefix(textWriter, logEntry.LogLevel)
textWriter.WriteLine(message)

member private _.WritePrefix(textWriter: TextWriter, logLevel: LogLevel) =
match logLevel with
| LogLevel.Trace -> textWriter.Write("trace: ")
| LogLevel.Debug -> textWriter.Write("debug: ")
| LogLevel.Information -> textWriter.Write("info: ")
| LogLevel.Warning -> textWriter.Write("warn: ")
| LogLevel.Error -> textWriter.Write("error: ")
| LogLevel.Critical -> textWriter.Write("critical: ")
| _ -> ()

// see https://learn.microsoft.com/en-us/dotnet/core/extensions/console-log-formatter
member private _.SetColor(textWriter: TextWriter, logLevel: LogLevel) =
let color =
match logLevel with
| LogLevel.Error -> "\x1B[1m\x1B[31m" // ConsoleColor.Red
| LogLevel.Warning -> "\x1B[33m" // ConsoleColor.DarkYellow
| LogLevel.Information -> "\x1B[1m\x1B[34m" // ConsoleColor.Blue
| LogLevel.Trace -> "\x1B[1m\x1B[36m" // ConsoleColor.Cyan
| _ -> "\x1B[37m" // ConsoleColor.Gray

textWriter.Write(color)

member private _.ResetColor() = Console.ForegroundColor <- origColor

interface IDisposable with
member _.Dispose() = optionsReloadToken.Dispose()

[<Extension>]
type ConsoleLoggerExtensions =

[<Extension>]
static member AddCustomFormatter(builder: ILoggingBuilder, configure: Action<CustomOptions>) : ILoggingBuilder =
builder
.AddConsole(fun options -> options.FormatterName <- "customName")
.AddConsoleFormatter<CustomFormatter, CustomOptions>(configure)
2 changes: 2 additions & 0 deletions src/FSharp.Analyzers.Cli/FSharp.Analyzers.Cli.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="CustomLogging.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

Expand All @@ -26,6 +27,7 @@
<PackageReference Include="Microsoft.Build.Locator" />
<PackageReference Include="Microsoft.Build.Tasks.Core" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
<PackageReference Include="Sarif.Sdk" />
</ItemGroup>

Expand Down
Loading