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

VS Code support #38

Closed
sk0va opened this issue Feb 8, 2017 · 37 comments
Closed

VS Code support #38

sk0va opened this issue Feb 8, 2017 · 37 comments
Assignees

Comments

@sk0va
Copy link

sk0va commented Feb 8, 2017

It would great to have Roslynator as Visual Studio Code extension.

@josefpihrt
Copy link
Collaborator

I would like to, but currently it is not possible.

See dotnet/vscode-csharp#43

@josefpihrt
Copy link
Collaborator

@josefpihrt josefpihrt removed their assignment Aug 5, 2017
@snielsson
Copy link

snielsson commented Aug 17, 2017

So is this still blocked? Or is it now possible to make a VSCode Roslynator Add-in?

@josefpihrt
Copy link
Collaborator

I don't think it is possible yet, you can check that the issue mentioned above is still open.

@josefpihrt
Copy link
Collaborator

@colin-young
Copy link

@josefpihrt I've tried following those instructions, but get:

System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default [...]

I fixed that by "unblocking" the assemblies in Windows (7). Now I'm getting:

Expected to use Roslynator.CSharp.CodeFixes, Version=1.6.30.0, Culture=neutral, PublicKeyToken=94dac60bcac1903d in host services but the assembly cannot be loaded due to an exception: Could not load file or assembly 'Roslynator.Workspaces, Version=1.6.30.0, Culture=neutral, PublicKeyToken=8ca34578834dd224' or one of its dependencies. The system cannot find the file specified.. System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Any suggestions?

@josefpihrt
Copy link
Collaborator

Copying Roslynator.Workspaces.dll to your folder should solve the issue.

I have recently move code dependent on Microsoft.CodeAnalysis.CSharp.Workspaces to new assembly Roslynator.Workspaces but I have not update documentation yet.

@colin-young
Copy link

Thanks! One other thing to note is that I needed to restart VS Code to get it to load the DLLs. Simply restarting the Omnisharp extension was not sufficient. Maybe that's only the case when there are problems reading assemblies, and it's not a general problem if you manage to get your configuration right on your first try.

  1. Extract DLLs
  2. Unblock DLLs in Windows
  3. Add/create settings in omnisharp.json
  4. Start (or restart) VS Code

@josefpihrt
Copy link
Collaborator

I'm glad it works!

I will update documentation accordingly.

@VahidN
Copy link

VahidN commented Feb 11, 2018

A PowerShell script to automate this process

Write-Host "Download, unzip and enable Roslynator for Visual Studio Code"

$name = "josefpihrt.Roslynator2017"
$url = "https://marketplace.visualstudio.com/items?itemName=$name"
$currentDir = $PSScriptRoot
$file = "$currentDir\Roslynator.zip"

$pattern = "<script class=`"vss-extension`" defer=`"defer`" type=`"application\/json`">(.*?)<\/script>"
$regex = [regex]"(?m)$pattern"
Write-Host "Grab the home page of the $name."
$dom = (New-Object Net.WebClient).DownloadString($url); 
if($dom -and $dom -match $pattern) 
{
    $matches = $regex.Match($dom)
    $jsonText = $matches[0].Groups[1]

    $json = ConvertFrom-Json $jsonText

    $version = $Json.versions[0].version # Parse the json in the page for the latest version number
    $parts = $name.Split(".")
    $publisher = $parts[0]
    $package = $parts[1]

    # Assemble the url for the vsix package	
    $packageUrl = "https://marketplace.visualstudio.com/_apis/public/gallery/publishers/$publisher/vsextensions/$package/$version/vspackage"	
    Write-Host "Download the vsix package: $packageUrl"
    (New-Object Net.WebClient).DownloadFile($packageUrl, $file)
	
    Write-Host "Using $currentDir as the current dir."
    Write-Host "Unzip $file."
    $shellApp = new-object -com shell.application 
    $zipFile = $shellApp.namespace($file) 
    $destination = $shellApp.namespace($currentDir) 
    $destination.Copyhere($zipFile.items(), 0x14)	# overwrite and be silent
	
    Write-Host "Delete VS specific files. Otherwise they will interfere with the MEF services inside OmniSharp."
    Remove-Item "$currentDir\Roslynator.VisualStudio.Core.dll","$currentDir\Roslynator.VisualStudio.dll", "$currentDir\Roslynator.VisualStudio.pkgdef"
	
    $omnisharpJsonFilePath = "$env:USERPROFILE\.omnisharp\omnisharp.json";	
    Write-Host "Create $omnisharpJsonFilePath file."
    $omnisharpJson = @" 
{{
  "RoslynExtensionsOptions": {{
    "LocationPaths": [
      "{0}"
    ]
  }}
}}
"@ -f $currentDir -Replace "\\","\\"
    $omnisharpJson | Out-File "$omnisharpJsonFilePath" -Confirm
	
    Write-Host "Done!"
}
else
{
    Write-Host "Failed to find the packageUrl!"
}

@domints
Copy link

domints commented Dec 17, 2018

Are these DLLs .NET Standard? I mean, would that work with VS Code on Mac?

@josefpihrt
Copy link
Collaborator

Roslynator targets .NET Standard 1.3

@domints
Copy link

domints commented Dec 17, 2018

I'll give it a try then, and report the results :)

@divinebovine
Copy link

divinebovine commented Apr 11, 2019

Roslyn analyzers and code fixes support was merged recently into omnisharp-roslyn so this should now be possible.

Until a new preference and an updated stable release of omnisharp-roslyn gets shipped, you'll need to use "omnisharp.path": "latest" in your settings.json and the following in your omnisharp.json:

{
    "RoslynExtensionsOptions": {
        "EnableAnalyzersSupport": true,
        "LocationPaths": [
            "C:\\lib\\roslynator"
        ]
    }
}

I've tested this recently and was able to get both analyzer warnings/errors as well as codefixes.

@divinebovine
Copy link

Looks like the new preference is available in the latest omnisharp. This will remove the need for the "EnableAnalyzersSupport" setting in your omnisharp.json.

image

@divinebovine
Copy link

Can we remove the blocked label at this point?

@josefpihrt
Copy link
Collaborator

Does anybody know how to pack roslyn libraries as VS Code extension?

@josefpihrt josefpihrt self-assigned this Aug 13, 2019
@josefpihrt josefpihrt pinned this issue Aug 13, 2019
@josefpihrt
Copy link
Collaborator

Roslynator for Visual Studio Code is here! https://marketplace.visualstudio.com/items?itemName=josefpihrt-vscode.roslynator

@sbmw
Copy link

sbmw commented Aug 16, 2019

This is fantastic news!

One thing. Currently user-level rules can be set by creating files at %LOCALAPPDATA%\JosefPihrt\Roslynator. This isn't possible for people running VS Code on non-Windows environments.

I think you could just update roslyn/common/roslynator.config within the extension to include a couple of extra imports like:

<Include Path="~/roslynator.config" />
...
<RuleSet Path="~/roslynator.ruleset" />

and users would be able to create a config and ruleset in their home directory?

@josefpihrt
Copy link
Collaborator

I should not be necessary to add more paths to roslynator config. It should be sufficient to replace %LOCALAPPDATA% with value of returned by Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).

I do not have OSX or Linux installed on my machine but I found this page that lists all values:

ENM: System.Environment.SpecialFolder.LocalApplicationData
WIN: C:\Users\foo\AppData\Local
LIN: /home/foo/.local/share
OSX: /Users/foo/.local/share

@josefpihrt
Copy link
Collaborator

Is anybody able to test that following code snippet will work on non-Windows environment?

using System;
using System.IO;
using System.Diagnostics;

string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

string path = @"%LOCALAPPDATA%\JosefPihrt\Roslynator\VisualStudioCode";

path = path.Replace("%LOCALAPPDATA%", localAppDataPath);

// normalize directory separator
path = Path.GetFullPath(path);

// LINUX
Debug.Assert(path == "/home/foo/.local/share/JosefPihrt/Roslynator/VisualStudioCode");

// OSX
Debug.Assert(path == "/Users/foo/.local/share/JosefPihrt/Roslynator/VisualStudioCode");

Thanks.

@domints
Copy link

domints commented Aug 20, 2019

When I replaced /home/foo/ with my current user directory on Linux Assert failed because of wrong slashes.
Otherwise path looks fine (I added Console.WriteLine) :)

My Linux is some kind of Debian (9 I think) and my dotnet version is 2.2.300

@sbmw
Copy link

sbmw commented Aug 20, 2019

@josefpihrt yes, your way is definitely better than adding more paths.

As domints says, the directory normalizing isn't working.

You could do something like

if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
    path = path.Replace(@"\", "/");
}

Or else I think just using forward slashes as directory separators will work across all platforms?

@divinebovine
Copy link

You could use Path.PathSeparator for platform aware separators

@domints
Copy link

domints commented Aug 20, 2019

@sbmw that's not the good way :) I believe System.IO.Path has field specifying the correct path delimiter. It's DirectorySeparatorChar. But as AltDirectorySeparatorChar specifies on Windows, Windows also understands / slash, so that might be a way to go.

I think GetFullPath didn't fix slashes on Linux, because neither DirectorySeparatorChar nor AltDirectorySeparatorChar specify \ as proper separator on Linux.

@divinebovine Path.PathSeparator is meant to separate multiple paths, not directories in single path. It resolves to ; on Windows and : on Linux.

@ADIX7
Copy link

ADIX7 commented Aug 20, 2019

There is Path.Combine to combine path parts.

@divinebovine
Copy link

divinebovine commented Aug 20, 2019

@domints you're right, my mistake

@domints
Copy link

domints commented Aug 20, 2019

@ADIX7

There is Path.Combine to combine path parts.

yet it doesn't fix anything :)

@ADIX7
Copy link

ADIX7 commented Aug 21, 2019

Realy, I haven't read back enough posts...
Joining to @domints: doesn't / work on windows? Or you can expect / and replace it with Path.DirectorySeparatorChar.

According to this / should work on windows too:
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.directoryseparatorchar?view=netframework-4.8

@josefpihrt
Copy link
Collaborator

I think this should work:

string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

string path = @"%LOCALAPPDATA%/JosefPihrt/Roslynator/VisualStudioCode";

path = path.Replace("%LOCALAPPDATA%", localAppDataPath);

// "/home/foo/.local/share/JosefPihrt/Roslynator/VisualStudioCode" on Linux with user "foo"
Console.WriteLine(path);

On Windows there will be mixed slashes but I have checked that it's fine.

@wocar
Copy link

wocar commented Sep 3, 2019

how can i get roslynator to work with macos

@domints
Copy link

domints commented Sep 5, 2019

how can i get roslynator to work with macos

Just install VS Code extension and modify one file according to readme on extension page

@wocar
Copy link

wocar commented Sep 5, 2019 via email

@domints
Copy link

domints commented Sep 5, 2019

image
It is there ;)

@wocar
Copy link

wocar commented Sep 6, 2019

Ok the assemblies seem to load but still doesnt work :(

Can someone else confirm that works for mac

@domints
Copy link

domints commented Sep 6, 2019

@wocar
obraz

@josefpihrt
Copy link
Collaborator

I'm going to close this issue. Please file a new issue for anything related to VS Code. Thanks.

@josefpihrt josefpihrt unpinned this issue Oct 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants