Alea 3.0.3 C# sample program crashing with reference to F# System.TypeInitializationException - aleagpu

I started a new VS2017 c# console app project and installed Alea and Alea.Fody from package manager. Running a piece of sample code from the website gave me the following exception.
System.TypeInitializationException occurred
HResult=0x80131534
Message=The type initializer for 'Alea.GlobalImplicitMemoryTracker' threw an exception.
Source=Alea
StackTrace:
at Alea.GlobalImplicitMemoryTracker.GetInManagedFlag()
at gputest.Program.DelegateWithClosureGpu()
at gputest.Program.Main(String[] args) in c:\Users\myuser\documents\visual studio 2017\Projects\gputest\gputest\Program.cs:line 14
Inner Exception 1:
TypeInitializationException: The type initializer for 'A.cf5aded17df9f7cc4c132234dda010fa7' threw an exception.
Inner Exception 2:
FileNotFoundException: Could not load file or assembly 'FSharp.Core, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Here is the entire program.
using Alea;
using Alea.Parallel;
using System.Linq;
namespace gputest
{
class Program
{
private const int Length = 1000000;
static void Main(string[] args)
{
DelegateWithClosureGpu();
}
[GpuManaged]
public static void DelegateWithClosureGpu()
{
var arg1 = Enumerable.Range(0, Length).ToArray();
var arg2 = Enumerable.Range(0, Length).ToArray();
var result = new int[Length];
Gpu.Default.For(0, result.Length, i => result[i] = arg1[i] + arg2[i]);
var expected = arg1.Zip(arg2, (x, y) => x + y);
//Assert.That(result, Is.EqualTo(expected));
}
}
}

I had to install F# a little differently for VS2017 by modifying the .NET desktop development options to install F# language support but after that it did work. It is not intuitive that using Alea for C# requires F# support. That should be listed in the install guide.

You need to install F# runtime. In VS2015, select your vs installation and choose modify, add F# feature.

I had a similar error in VS2017.3 with F# already installed, starting from a clean project, using Alea GPU 3.0.3 from NuGet. It turns out that the package contains all the DLLs referenced, including the FSharp.Core.dll (check the .../packages/ sub-directory of your C# project). I copied all the DLLs over to the directory containing the executable (e.g., .../bin/Debug), unchecked the property of the C# program for "prefer 32-bit", and rebuilt. My program then ran fine. Note, I think the Alea GPU .targets file might need to be corrected to copy required files to the appropriate directory.

Related

Error while running MSBuild.SonarQube.Runner against C# project

I’m trying to use MSBuild SonarQube scanner for my C# project. RootFolder has a .sln file, and each project (.csproj) is created in a separate folder inside the root folder. I've the project property file in RootFolder.
SonarQube server (LTS version 6.7.5) is up fine.
My Current env details:
Visual Studio 2010
.Net Framework 4.5.2
MSBuild version 4
sonarScanner for MSBuild 4.3.1.1372
Sonar Csharp Plugin version : 7.3.2.6129
When I’m trying to run below ‘begin’ command from project root directory, I see this error:
M:\myProjectCsharp>MSBuild.SonarQube.Runner.exe begin /k:”myProjectCsharp”/n: “myProjectCsharp”/v:”1.0”
Unhandled Exception: System.MissingMethodException: Method not found: '!!0[] System.Array.Empty()'.
at SonarScanner.MSBuild.Program.Execute(String[] args, Boolean showDeprecatedWarning)
at MSBuild.SonarQube.Runner.Program.Main(String[] args)
I guess it has something to do with compatibility of .Net Framework or MSBuild version ?
Also, what's the role of visual studio here, I already got.sln, .csproject and their corresponding .cs files in respective project directory under RootFolder 'myProjectCsharp'
(I don’t see any error if I simply run the sonar-scanner-msbuild-4.3.1.1372-net46\sonar-scanner-3.2.0.1227\bin\sonar-scanner but this doesn’t seems to update anything on dashboard (Just show 0 Bugs,Vulnerabilities & code smells) )
Any clue please ?
Thanks.

FsUnit not defined

I'm learning F# recently. Today I wrote some code and tried to add unit tests.
I planed to create two projects, one is for code and the other is for test.
So I created a new solution called DataLab with a project called Source in Visual Studio 2015 Community. Then I added another project called Test.
The structure is like below:
Solution 'DataLab'
Source
Queue.fs
Test
QueueTest.fs
Queue.fs is:
namespace DataLab
type Queue<'a> = Queue of 'a list * 'a list
module Queue =
let init<'a> : Queue<'a> = Queue ([], [])
QueueTest.fs is:
namespace DataLab.Test
module QueueTest =
open DataLab
open FsUnit
open NUnit.Framework
[<Test>]
let ``test Init`` () =
let expected : Queue<int> = Queue ([], [])
Queue.init<int> |> should equal expected
Then VS warned me about the open statements in QueueTest.fs. One of the three was:
The namespace or module 'FsUnit' is not defined
So I added reference to Source project for Test project. I also installed FsUnit via NuGet for Test project.
I didn't install NUnit because it's a dependency of FsUnit.
And I didn't install FsUnit for Source project because Source does not need testing libraries.
I wanted to make it similar to <scope>test</scope> thing for JUnit dependency in maven.
I built the whole solution successfully.
When I run my test in 'Test Explorer' (I also installed the 'NUnit3 Test Adapter' extension), I got error below:
System.MissingMethodException : Method not found: 'DataLab.Queue`1<!0> DataLab.Queue`1.NewQueue(Microsoft.FSharp.Collections.FSharpList`1<!0>, Microsoft.FSharp.Collections.FSharpList`1<!0>)'.
After some random try, I got the test executed successfully by installing FsUnit to Source project too.
I have a theory about this problem.
I installed FsUnit 2.0.0 which has a dependency of FSharp.Core (>= 3.1.2.5). So FSharp.Core 3.1.2.5 has also been installed to my Test project. And this polluted my Test.fsproj file.
I have below lines in Test.fsproj:
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFSharpCoreVersion>4.4.0.0</TargetFSharpCoreVersion>
<ItemGroup>
<Reference Include="FSharp.Core">
<HintPath>..\packages\FSharp.Core.3.1.2.5\lib\net40\FSharp.Core.dll</HintPath>
<Private>True</Private>
</Reference>
Meanwhile the Source project still using VS 2015 built-in F#, whose version is 4.0+ . So the two versions of F# conflict.
But I don't know how to prove it or how to fix it.

VS2010 web performance test/Load test stopped working after installing VS2013

My web performance/load test has been working fine in VS2010 until yesterday when I installed VS2013. I didn't upgrade my project/solution file. I am still trying to run my tests within VS2010.
When I am trying to run webtest locally, I got the following error:
Request failed: Exception occurred: Object reference not set to an instance of an object.
at Microsoft.VisualStudio.TestTools.WebStress.WebTestCaseVariation.ProcessTransactionStarting(String transactionName, Boolean isForIncludedWebTest, Int32 declarativeWebTestItemId, Guid sourceWebTestId)
at Microsoft.VisualStudio.TestTools.WebTesting.WebTest.BeginTransaction(String transactionName, Boolean isForIncludedWebTest, Int32 declarativeWebTestItemId, Guid sourceWebTestId)
at Microsoft.VisualStudio.TestTools.WebTesting.WebTest.<GetEnumerator>d__0.MoveNext()
at Microsoft.VisualStudio.TestTools.WebTesting.DeclarativeWebTest.<EnumerateRequests>d__0.MoveNext()
at Microsoft.VisualStudio.TestTools.WebStress.WebTestCaseVariation.ExecuteNextTopLevelRequest()
I can run the test without an issue remotely though(my test controller is vs2010)
I can also run load test remotely, but got the following error thus not able to see the test results:
Exception LoadTestException 1 Results collection stopped due to the following error: Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.LoadTest, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
my projects are still referencing version 10.0.0.0 dlls,auto redirected by Visual Studio to the ones under vs2013 installation folder though, not sure why it's trying to load version 12.0.0.0
Much appreciate for any help.

Merging Visual Studio Code Coverage fails with ImageNotFoundException

I'm trying to export visual studio code coverage files (data.coverage) into xml as described in this blog post from the code analysis team. I've moved the code example in that post into a custom MSBuild task. My custom task references the Microsoft.VisualStudio.Coverage.Analysis.dll located in the PrivateAssemblies folder of Visual Studio.
Right off the bat, trying to load the code coverage file throws an code analysis typed exception ImageNotFoundException, stating that the "Image file fully-qualified-file-path-to-dll could not be found."
// the following line throws an exception
CoverageInfo current =
CoverageInfo.CreateFromFile( "c:\path\testresults\x\y\z\data.coverage");
The path is fully qualified and the DLL it refers to does exist. My testsettings has this file listed as the assembly to instrument and the "Instrument in place" checkbox is set. I can view code coverage within Visual Studio, so I know coverage is working.
I'm running my MSBuild script from the Visual Studio command line. It looks like this:
<Project ToolsVersion="4.0" DefaultTargets="Default;"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="CustomTasks.MergeCoverageTask"
AssemblyFile="CustomTasks.dll"
/>
<Target Name="Default">
<ItemGroup>
<CoverageFiles Include="**\data.coverage" />
</ItemGroup>
<MergeCoverageTask
CoverageFiles="#(CoverageFiles)"
OutputFile="output.xml"
/>
</Target>
</Project>
Can anyone suggest what I need to do to get this working correctly?
5 hours later and this is a tumbleweed. I found some additional detail here, which helped get me further down the path.
In order for this to work, you need to include a few additional files alongside the custom task and supply folder locations for the pdb's and instrumented dll's.
Regarding additional files, you need the following:
The custom build task must reference Microsoft.VisualStudio.Coverage.Analysis.dll
Your bin folder must contain the following additional files:
Microsoft.VisualStudio.Coverage.Symbols.dll
dbghelp.dll
(If you don't have visual studio installed, you must perform regsvr32.exe on msdia100.dll)
Regarding paths to assemblies and symbols, the CreateFromFile method takes a collection of folders to search. What seems really strange is that error complains about not being able to locate missing instrumented assemblies, and it specifies the full path..
Image file c:\project\output\Assembly.dll could not be found.
...but if you specify that path, it doesn't work.
CoverageInfo current =
CoverageInfo.CreateFromFile( "c:\project\testresults\x\In\data.coverage",
new string[] { "c:\project\output" },
new string[] { "c:\project\output" });
However, changing the path to be the folder of the TestResults output works fine:
CoverageInfo current =
CoverageInfo.CreateFromFile( "c:\project\testresults\x\In\data.coverage",
new string[] { "c:\project\testresults\x\Out" },
new string[] { "c:\project\testresults\x\Out" });
I question whether "instrument in place" really means in that folder, or instrument and copy to the MS Test run folder.
Well dear SO folk, if you're reading this, you get a cookie.

run a custom msbuild target from VisualStudio

Suppose I add a custom target to a csproj file. Is there a way to run that target from visual studio? I don't want it make it a prebuild or postbuild step, I just want to be able to run this target (and its dependencies) from visual studio.
There is a simple way (though not all that satisfying) using a custom external tool.
Assuming your project file has the following modification:
<Target Name="CalledFromIde">
<Error Text="Called from the IDE!" />
</Target>
Go to Tools | External Tools and add one like this:
Title: Called from IDE
Command: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe
Arguments: $(ProjectDir)$(ProjectFileName) /t:CalledFromIde
Initial directory: $(ProjectDir)
Use Output window: checked
Running this produces output as:
Build FAILED.
"F:\Code\CsProject\CsProject.csproj" (CalledFromIde target) (1) ->
(CalledFromIde target) ->
F:\Code\CsProject\CsProject.csproj(57,5): error : Called from the IDE!
What you are doing is calling out to MSBuild as an external tool and having it run the target directly. You have to supply the full path to MSBuild because the IDE doesn't maintain the same properties that the build environment it creates has available.
You can hook this up to a shortcut by figuring out which command # it is in the set Tools.ExternalCommand#.
If you're looking for a solution with more sophistication, it is a bit more involved. Here it is in a nutshell (for VS2010):
1) Create a VS Addin (File | New | Project | Other Project Types | Extensibility | Visual Studio Add-in). I'm not sure if you have to have the VS SDK installed to get this, it is available in the extension manager.
Select the following options in the wizard:
- Microsoft Visual Studio 2010
- Yes, create a 'Tools' menu item
- Load when the Application starts
- My Add-in will never put up modal UI, and can be used with command line builds.
2) Add references to Microsoft.Build and Microsoft.Build.Framework
3) Find the implementation of Exec in the Connect.cs file
4) Replace it with this code:
public void Exec(
string commandName,
vsCommandExecOption executeOption,
ref object varIn,
ref object varOut,
ref bool handled)
{
handled = false;
if (executeOption != vsCommandExecOption.vsCommandExecOptionDoDefault)
return;
if (commandName != "BuildAddin.Connect.BuildAddin")
return;
var doc = _applicationObject.ActiveDocument;
var projectItem = doc.ProjectItem;
var project = projectItem.ContainingProject;
var evalProject =
Microsoft.Build.Evaluation.ProjectCollection
.GlobalProjectCollection.LoadProject(project.FullName);
var execProject = evalProject.CreateProjectInstance();
bool success = execProject.Build("CalledFromIde", null);
var window = _applicationObject.Windows.Item(Constants.vsWindowKindOutput);
var output = (OutputWindow)window.Object;
OutputWindowPane pane = output.OutputWindowPanes.Add("BuildAddin");
pane.OutputString(success ? "built /t:CalledFromIde" : "build failed");
handled = true;
return;
}
5) A better custom target while debugging, since the previous one errors:
<Target Name="CalledFromIde">
<WriteLinesToFile File="CalledFromIde.txt" Lines="Called from the IDE!" />
</Target>
6) The code above has no error checking for brevity, you'll want to be much cleaner since it will be running in the IDE. The addin will place a menu item on your Tools menu. As written above, it simply looks for the project containing the currently active editor document, which would need some better plumbing for whatever you are cooking up.
This technique gets the build engine instance from within the IDE and has it execute a build on a separate instance of the project.
If you are running the build inside of Visual Studio there will be a build variable of VisualStudioDir during the build.
To execute only is a VS build session do this:
<Target Name="Test" BeforeTargets="Build" Condition="'$(VisualStudioDir)' != ''>
</Target>
To execute only in a build outside of VS do this:
<Target Name="Test" BeforeTargets="Build" Condition="'$(VisualStudioDir)' == ''>
</Target>
You will need to include your custom targets file in one of two ways.
Set the environment variable CustomBeforeMicrosoftCommonTargets
Edit you project file to include your custom targets file by adding an import
<Imports Project="CustomBuildTasks.Targets"><Imports/>
You don't have to code with the Exec, although that is one way to do it. The easier way is to do the following:
Change the DefaultTargets="Build" attribute to a custom Target you create, say "All" like so:
DefaultTargets="All"
Then in your custom "All" target, you can use the DependsOnTargets attribute, like the following:
"<Target Name="All" DependsOnTargets="ZipOutputFiles;Build">
</Target>"
This will then build, and out put zip files in your custom "All" target.
Some time ago I had the same problem and decided to write VS add-in.
Try it:
https://github.com/Serg046/MsBuildTaskExplorer
https://marketplace.visualstudio.com/items?itemName=saaseev.MsBuildTaskExplorer

Resources