Skip to content

Commit 9a294eb

Browse files
authored
Merge pull request #225 from 1eyewonder/using-fake-docs
Update Getting Started Docs: Calling Analyzers w/ FAKE
2 parents ac7d475 + 6798d4d commit 9a294eb

File tree

12 files changed

+175
-63
lines changed

12 files changed

+175
-63
lines changed

docs/content/Dual Analyzer.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(**
22
---
33
category: end-users
4-
categoryindex: 1
5-
index: 3
4+
categoryindex: 2
5+
index: 2
66
---
77
88
# Writing an analyzer for both console and editor

docs/content/Getting Started Writing.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(**
22
---
33
category: end-users
4-
categoryindex: 1
5-
index: 2
4+
categoryindex: 2
5+
index: 1
66
---
77
88
# Getting started writing an analyzer

docs/content/Programmatic access.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(**
22
---
33
category: end-users
4-
categoryindex: 1
5-
index: 4
4+
categoryindex: 2
5+
index: 3
66
---
77
88
# Programmatically running an analyzer

docs/content/Running during CI.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
category: end-users
3-
categoryindex: 1
4-
index: 6
3+
categoryindex: 2
4+
index: 5
55
---
66

77
# Running analyzers during continuous integration

docs/content/Unit Testing.fsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(**
22
---
33
category: end-users
4-
categoryindex: 1
5-
index: 5
4+
categoryindex: 2
5+
index: 4
66
---
77
88
# Unit testing an analyzer
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
category: getting-started
3+
categoryindex: 1
4+
index: 3
5+
---
6+
7+
# Command Line Arguments
8+
9+
## Example Command
10+
11+
When running the CLI tool from the command line (and after installing analyzers), the minimum console arguments you need to provide is the path to the project file(s) you want to analyze.
12+
13+
```shell
14+
dotnet fsharp-analyzers --project ./YourProject.fsproj --analyzers-path ./path/to/analyzers/directory
15+
```
16+
17+
⚠️ If you don't provide the `--analyzers-path` argument, it will default to `packages/analyzers`. If you are using Paket with a group called `analyzers`, this default path should work for you.
18+
19+
## Viewing Additional Commands
20+
21+
You can view the full list of commands available by running:
22+
23+
```shell
24+
dotnet fsharp-analyzers --help
25+
```
26+
27+
[Next]({{fsdocs-next-page-link}})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
category: getting-started
3+
categoryindex: 1
4+
index: 2
5+
---
6+
7+
# Configuring for the IDE
8+
9+
## Visual Studio Code
10+
11+
In order to configure analyzers for VSCode, you will need to update your project's `.vscode/settings.json` file or your user settings. You should need the settings shown below.
12+
13+
```json
14+
{
15+
"FSharp.enableAnalyzers": true,
16+
"FSharp.analyzersPath": ["path/to/analyzers/directory"]
17+
}
18+
```
19+
20+
📓 Note: Issue created [here](https://github.com/ionide/FsAutoComplete/issues/1350) regarding analyzers & SDK mismatches in the logs
21+
22+
After saving your new settings, make sure to restart VSCode. Once VSCode restarts, you should be able to test and see if the analyzers are working by opening a F# file in your workspace and entering the following code
23+
24+
![Analyzers Inline Warning](../../images/analyzers-inline-warning.png)
25+
26+
[Next]({{fsdocs-next-page-link}})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
category: getting-started
3+
categoryindex: 1
4+
index: 1
5+
---
6+
# Installation
7+
8+
## Installing the Tool
9+
10+
A dotnet CLI tool, called [fsharp-analyzers](https://github.com/ionide/FSharp.Analyzers.SDK/), is used to run analyzers outside the context of an IDE. Add it to your tool-manifest with:
11+
12+
```shell
13+
dotnet tool install fsharp-analyzers --create-manifest-if-needed
14+
```
15+
16+
## Installing Analyzers
17+
18+
### Suggested Packages
19+
20+
1. [Ionide Analyzers](https://github.com/ionide/FSharp.Analyzers.SDK/)
21+
2. [G-Research Analyzers](https://github.com/G-Research/fsharp-analyzers/)
22+
23+
### Nuget
24+
25+
If you are using Nuget as your package manager, add the `PackageReference` pointing to your favorite analyzers to the `.fsproj` file of the project you want to analyze.
26+
27+
```xml
28+
<PackageReference Include="G-Research.FSharp.Analyzers" Version="0.12.1">
29+
<PrivateAssets>all</PrivateAssets>
30+
<IncludeAssets>analyzers</IncludeAssets>
31+
</PackageReference>
32+
<PackageReference Include="Ionide.Analyzers" Version="0.28.0">
33+
<PrivateAssets>all</PrivateAssets>
34+
<IncludeAssets>analyzers</IncludeAssets>
35+
</PackageReference>
36+
```
37+
38+
### Paket
39+
40+
If you are using Paket as your package manager, add the package to your `paket.dependencies` file. The example below uses a paket group, but it is not required.
41+
42+
```paket
43+
group analyzers
44+
source https://api.nuget.org/v3/index.json
45+
46+
nuget Ionide.Analyzers
47+
nuget G-Research.FSharp.Analyzers
48+
```
49+
50+
[Next]({{fsdocs-next-page-link}})
Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,45 @@
11
---
2-
category: end-users
2+
category: getting-started
33
categoryindex: 1
4-
index: 1
4+
index: 4
55
---
66

7-
# Getting started using analyzers
7+
# MSBuild
88

9-
## Premise
9+
## Using Analyzer Build Target in a Project
1010

11-
We assume the analyzers you want to use are distributed as a nuget package.
11+
The path to the analyzer DLL files could be tricky to get right across a wide range of setups. Luckily, we can use a MSBuild custom target to take care of the path construction. Add [FSharp.Analyzers.Build](https://www.nuget.org/packages/FSharp.Analyzers.Build) to your project. This imports a new target to your project file (`AnalyzeFSharpProject`) and will allow us to easily run the analyzers for our project.
1212

13-
## Using analyzers in a single project
13+
### Installing Target via Nuget
1414

15-
### Raw command line
16-
17-
A dotnet CLI tool, called [fsharp-analyzers](https://www.nuget.org/packages/fsharp-analyzers), is used to run analyzers outside the context of an IDE.
18-
Add it to your tool-manifest with:
19-
```shell
20-
dotnet tool install fsharp-analyzers
21-
```
22-
23-
Next, add the `PackageReference` pointing to your favorite analyzers to the `.fsproj` file of the project you want to analyze:
15+
If you are using Nuget, add it to your `.fsproj` file:
2416

2517
```xml
26-
<PackageReference Include="G-Research.FSharp.Analyzers" Version="0.4.0">
18+
<PackageReference Include="FSharp.Analyzers.Build" Version="0.2.0">
2719
<PrivateAssets>all</PrivateAssets>
28-
<IncludeAssets>analyzers</IncludeAssets>
20+
<IncludeAssets>build</IncludeAssets>
2921
</PackageReference>
3022
```
3123

32-
At the time of writing, the [G-Research analyzers](https://github.com/g-research/fsharp-analyzers) [package](https://www.nuget.org/packages/G-Research.FSharp.Analyzers) contains the only analyzers compatible with the latest CLI tool.
33-
With the package downloaded, we can run the CLI tool:
24+
### Installing Target via Paket
3425

35-
```shell
36-
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
37-
```
26+
If you are using Paket, add it to your `paket.dependencies`
3827

39-
### Using an MSBuild target
28+
```paket
29+
group analyzers
30+
source https://api.nuget.org/v3/index.json
4031
41-
As you can see, the path to the analyzer DLL files could be tricky to get right across a wide range of setups.
42-
Luckily, we can use an MSBuild custom target to take care of the path construction.
32+
nuget FSharp.Analyzers.Build
33+
```
4334

44-
Add [FSharp.Analyzers.Build](https://www.nuget.org/packages/FSharp.Analyzers.Build) to your `fsproj`:
35+
as well as the `paket.references` of your project:
4536

46-
```xml
47-
<PackageReference Include="FSharp.Analyzers.Build" Version="0.2.0">
48-
<PrivateAssets>all</PrivateAssets>
49-
<IncludeAssets>build</IncludeAssets>
50-
</PackageReference>
37+
```paket
38+
group analyzers
39+
FSharp.Analyzers.Build
5140
```
5241

53-
This imports a new target to your project file: `AnalyzeFSharpProject`.
54-
And will allow us to easily run the analyzers for our project.
42+
### Configuring the Build Target
5543

5644
Before we can run `dotnet msbuild /t:AnalyzeFSharpProject`, we need to specify our settings in a property called `FSharpAnalyzersOtherFlags`:
5745

@@ -62,21 +50,25 @@ Before we can run `dotnet msbuild /t:AnalyzeFSharpProject`, we need to specify o
6250
```
6351

6452
To locate the analyzer DLLs in the filesystem, we use the variable `$(PkgG-Research_FSharp_Analyzers)`. It's produced by NuGet and normalized to be usable by [MSBuild](https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#generatepathproperty).
65-
In general, a `Pkg` prefix is added and dots in the package ID are replaced by underscores. But make sure to look at the [nuget.g.props](https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#restore-outputs) file in the `obj` folder for the exact string.
53+
In general, a `Pkg` prefix is added and dots in the package ID are replaced by underscores. But make sure to look at the [nuget.g.props](https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#restore-outputs) file in the `obj` folder for the exact string.
54+
6655
The `/analyzers/dotnet/fs` subpath is a convention analyzer authors should follow when creating their packages.
6756

57+
### Running the Build Target
58+
6859
At last, you can run the analyzer from the project folder:
6960

7061
```shell
7162
dotnet msbuild /t:AnalyzeFSharpProject
7263
```
7364

74-
Note: if your project has multiple `TargetFrameworks` the tool will be invoked for each target framework.
65+
📓 Note: If your project has multiple `TargetFrameworks` the tool will be invoked for each target framework.
66+
67+
## Using Analyzer Build Target in a Solution
7568

76-
## Using analyzers in a solution
69+
Adding the custom target from above to all `.fsproj` files of a solution doesn't scale very well. We can use the MSBuild infrastructure to add the needed package reference and the MSBuild target to all projects in one go.
7770

78-
Adding the custom target from above to all `.fsproj` files of a solution doesn't scale very well.
79-
So we use the MSBuild infrastructure to add the needed package reference and the MSBuild target to all projects in one go.
71+
### Setting up Directory.Build.props
8072

8173
We start with adding the `PackageReference` pointing to your favorite analyzers to the [Directory.Build.props](https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory?view=vs-2022) file.
8274
This adds the package reference to all `.fsproj` files that are in a subfolder of the file location of `Directory.Build.props`:
@@ -94,8 +86,10 @@ This adds the package reference to all `.fsproj` files that are in a subfolder o
9486
</ItemGroup>
9587
```
9688

97-
Likewise we add the `FSharpAnalyzersOtherFlags` property to the [Directory.Build.targets](https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory?view=vs-2022) file.
98-
This is effectively the same as adding a property to each `*proj` file which exists in a subfolder.
89+
### Setting up Directory.Build.targets
90+
91+
Likewise we add the `FSharpAnalyzersOtherFlags` property to the [Directory.Build.targets](https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory?view=vs-2022) file. For first time MSBuild users, this is effectively the same as adding a property to each `*proj` file which exists in a subfolder.
92+
9993
```xml
10094
<Project>
10195
<PropertyGroup>
@@ -106,34 +100,49 @@ This is effectively the same as adding a property to each `*proj` file which exi
106100
</Project>
107101
```
108102

109-
⚠️ We are adding the `FSharpAnalyzersOtherFlags` property to our **Directory.Build.targets** and **not to** any **Directory.Build.props** file!
110-
MSBuild will first evaluate `Directory.Build.props` which has no access to the generated nuget.g.props. `$(PkgG-Research_FSharp_Analyzers)` won't be known at this point. `Directory.Build.targets` is evaluated after the project file and has access to `Pkg` generated properties.
103+
⚠️ We are adding the `FSharpAnalyzersOtherFlags` property to our **Directory.Build.targets** and **not to** any **Directory.Build.props** file! MSBuild will first evaluate `Directory.Build.props` which has no access to the generated nuget.g.props. `$(PkgG-Research_FSharp_Analyzers)` won't be known at this point. `Directory.Build.targets` is evaluated after the project file and has access to `Pkg` generated properties.
111104

112-
### All projects in the solution
105+
### Run Target for All Projects in the Solution
113106

114107
We can run the `AnalyzeFSharpProject` target against all projects in a solution
115108

116109
```shell
117110
dotnet msbuild YourSolution.sln /t:AnalyzeFSharpProject
118111
```
119112

120-
### Select specific projects
113+
### Configuring Specific Projects to Run
121114

122-
As we don't want to target all projects in the solution, we create a second custom MSBuild target that calls the project-specific target for all relevant projects.
115+
As we may not always want to target every project in a solution, we can create a second custom MSBuild target that calls the project-specific target for all relevant projects.
123116
Add the following custom target to the [Directory.Solution.targets](https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-solution-build?view=vs-2022) file to be able to invoke analysis from all selected projects in one simple command:
124117

125118
```xml
126119
<Project>
127120
<ItemGroup>
128121
<ProjectsToAnalyze Include="src/**/*.fsproj" />
129122
</ItemGroup>
123+
124+
<Target Name="AnalyzeSolution">
125+
<MSBuild Projects="@(ProjectsToAnalyze)" Targets="AnalyzeFSharpProject" />
126+
</Target>
127+
</Project>
128+
```
129+
130+
You can also exclude certain projects from the analysis if they fall within the same pattern
130131

132+
```xml
133+
<Project>
134+
<ItemGroup>
135+
<ProjectsToAnalyze Include="src/**/*.fsproj" Exclude="src/**/Special.fsproj" />
136+
</ItemGroup>
137+
131138
<Target Name="AnalyzeSolution">
132139
<MSBuild Projects="@(ProjectsToAnalyze)" Targets="AnalyzeFSharpProject" />
133140
</Target>
134141
</Project>
135142
```
136143

144+
### Running the Solution Target
145+
137146
At last, you can run the analyzer from the solution folder:
138147

139148
```shell
@@ -142,15 +151,15 @@ dotnet msbuild /t:AnalyzeSolution
142151

143152
Note: we passed the `--code-root` flag so that the `*.sarif` report files will report file paths relative to this root. This can be imported for certain editors to function properly.
144153

145-
## MSBuild tips and tricks
154+
## MSBuild Tips and Tricks
146155

147156
MSBuild can be overwhelming for the uninitiated. Here are some tricks we've seen in the wild:
148157

149-
### Use well-known properties
158+
### Use Well-Known Properties
150159

151160
Checkout the [MSBuild reserved and well-known properties](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-reserved-and-well-known-properties?view=vs-2022) to use existing variables like `$(MSBuildProjectFile)`.
152161

153-
### Wrap path arguments in quotes
162+
### Wrap Path Arguments in Quotes
154163

155164
As MSBuild is all XML, you can use `&quot;` to wrap evaluated values in quotes:
156165

@@ -160,7 +169,7 @@ As MSBuild is all XML, you can use `&quot;` to wrap evaluated values in quotes:
160169
</PropertyGroup>
161170
```
162171

163-
### Extend `<FSharpAnalyzersOtherFlags>` in multiple lines
172+
### Extend `<FSharpAnalyzersOtherFlags>` in Multiple Lines
164173

165174
You can extend the value of `$(FSharpAnalyzersOtherFlags)` by setting it again in multiple lines:
166175

@@ -173,7 +182,7 @@ You can extend the value of `$(FSharpAnalyzersOtherFlags)` by setting it again i
173182
</PropertyGroup>
174183
```
175184

176-
### Verify parameters are present
185+
### Verify Parameters are Present
177186

178187
It can be a bit confusing to find out if a variable contains the value you think it does.
179188
We often add a dummy target to a project to print out some values:
@@ -186,4 +195,4 @@ We often add a dummy target to a project to print out some values:
186195

187196
Run `dotnet msbuild YourProject.fsproj /t:Dump` and verify that `CodeRoot` has a value or not.
188197

189-
[Next]({{fsdocs-next-page-link}})
198+
[Next]({{fsdocs-next-page-link}})
18.9 KB
Loading

0 commit comments

Comments
 (0)