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

construct scripting workspace information directly from the ProjectInfo #1272

Merged
merged 7 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 1 addition & 4 deletions src/OmniSharp.Script/ScriptContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ namespace OmniSharp.Script
{
public class ScriptContext
{
public ScriptContext(ScriptProjectProvider scriptProjectProvider, HashSet<MetadataReference> metadataReferences, HashSet<string> assemblyReferences, CompilationDependency[] compilationDependencies, Type globalsType)
public ScriptContext(ScriptProjectProvider scriptProjectProvider, HashSet<MetadataReference> metadataReferences, CompilationDependency[] compilationDependencies, Type globalsType)
{
ScriptProjectProvider = scriptProjectProvider;
MetadataReferences = metadataReferences;
AssemblyReferences = assemblyReferences;
CompilationDependencies = compilationDependencies;
GlobalsType = globalsType;
}
Expand All @@ -20,8 +19,6 @@ public ScriptContext(ScriptProjectProvider scriptProjectProvider, HashSet<Metada

public HashSet<MetadataReference> MetadataReferences { get; }

public HashSet<string> AssemblyReferences { get; }

public CompilationDependency[] CompilationDependencies { get; }

public Type GlobalsType { get; }
Expand Down
18 changes: 14 additions & 4 deletions src/OmniSharp.Script/ScriptContextModel.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace OmniSharp.Script
{
public class ScriptContextModel
{
public ScriptContextModel(string csxPath, ProjectInfo project, HashSet<string> implicitAssemblyReferences)
public ScriptContextModel(string csxPath, ProjectInfo project)
{
Path = csxPath;
ImplicitAssemblyReferences = implicitAssemblyReferences;
CommonUsings = ScriptProjectProvider.DefaultNamespaces;
AssemblyReferences = project.MetadataReferences.Select(a =>
{
if (a is PortableExecutableReference portableExecutableReference)
{
return portableExecutableReference.FilePath ?? portableExecutableReference.Display;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ever null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not at the moment, but once we support plugins to meddle with this project system it might 😃

}

return a.Display;
});
CommonUsings = ((CSharpCompilationOptions)(project.CompilationOptions)).Usings;
GlobalsType = project.HostObjectType;
}

public string Path { get; }

public HashSet<string> ImplicitAssemblyReferences { get; }
public IEnumerable<string> AssemblyReferences { get; }

public Type GlobalsType { get; }

Expand Down
20 changes: 7 additions & 13 deletions src/OmniSharp.Script/ScriptContextProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public ScriptContext CreateScriptContext(ScriptOptions scriptOptions)
}

var metadataReferences = new HashSet<MetadataReference>(MetadataReferenceEqualityComparer.Instance);
var assemblyReferences = new HashSet<string>();

var isDesktopClr = true;
// if we have no compilation dependencies
Expand All @@ -86,7 +85,7 @@ public ScriptContext CreateScriptContext(ScriptOptions scriptOptions)
if (!compilationDependencies.Any())
{
_logger.LogInformation("Unable to find dependency context for CSX files. Will default to non-context usage (Desktop CLR scripts).");
AddDefaultClrMetadataReferences(metadataReferences, assemblyReferences);
AddDefaultClrMetadataReferences(metadataReferences);
}
else
{
Expand All @@ -98,7 +97,7 @@ public ScriptContext CreateScriptContext(ScriptOptions scriptOptions)
if (loadedFiles.Add(Path.GetFileName(compilationAssembly)))
{
_logger.LogDebug("Discovered script compilation assembly reference: " + compilationAssembly);
AddMetadataReference(metadataReferences, assemblyReferences, compilationAssembly);
AddMetadataReference(metadataReferences, compilationAssembly);
}
}
}
Expand All @@ -107,30 +106,26 @@ public ScriptContext CreateScriptContext(ScriptOptions scriptOptions)
foreach (var inheritedCompileLib in inheritedCompileLibraries)
{
_logger.LogDebug("Adding implicit reference: " + inheritedCompileLib);
AddMetadataReference(metadataReferences, assemblyReferences, inheritedCompileLib.Location);
AddMetadataReference(metadataReferences, inheritedCompileLib.Location);
}

var scriptProjectProvider = new ScriptProjectProvider(scriptOptions, _env, _loggerFactory, isDesktopClr);

return new ScriptContext(scriptProjectProvider, metadataReferences, assemblyReferences, compilationDependencies, _defaultGlobalsType);
return new ScriptContext(scriptProjectProvider, metadataReferences, compilationDependencies, _defaultGlobalsType);
}

private void AddDefaultClrMetadataReferences(HashSet<MetadataReference> commonReferences, HashSet<string> assemblyReferences)
private void AddDefaultClrMetadataReferences(HashSet<MetadataReference> commonReferences)
{
var references = DefaultMetadataReferenceHelper.GetDefaultMetadataReferenceLocations()
.Select(l =>
{
assemblyReferences.Add(l);
return _metadataFileReferenceCache.GetMetadataReference(l);
});
.Select(l => _metadataFileReferenceCache.GetMetadataReference(l));

foreach (var reference in references)
{
commonReferences.Add(reference);
}
}

private void AddMetadataReference(ISet<MetadataReference> referenceCollection, HashSet<string> assemblyReferences, string fileReference)
private void AddMetadataReference(ISet<MetadataReference> referenceCollection, string fileReference)
{
if (!File.Exists(fileReference))
{
Expand All @@ -146,7 +141,6 @@ private void AddMetadataReference(ISet<MetadataReference> referenceCollection, H
}

referenceCollection.Add(metadataReference);
assemblyReferences.Add(fileReference);
_logger.LogDebug($"Added reference to '{fileReference}'");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/OmniSharp.Script/ScriptProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ Task<object> IProjectSystem.GetProjectModelAsync(string filePath)
return Task.FromResult<object>(null);
}

return Task.FromResult<object>(new ScriptContextModel(filePath, projectInfo, _scriptContext.Value.AssemblyReferences));
return Task.FromResult<object>(new ScriptContextModel(filePath, projectInfo));
}

Task<object> IProjectSystem.GetWorkspaceModelAsync(WorkspaceInformationRequest request)
{
var scriptContextModels = new List<ScriptContextModel>();
foreach (var project in _projects)
{
scriptContextModels.Add(new ScriptContextModel(project.Key, project.Value, _scriptContext.Value.AssemblyReferences));
scriptContextModels.Add(new ScriptContextModel(project.Key, project.Value));
}
return Task.FromResult<object>(new ScriptContextModelCollection(scriptContextModels));
}
Expand Down
4 changes: 4 additions & 0 deletions test-assets/test-scripts/EmptyScript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything
*
# Except gitignore file
!.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#r "System.Net.Http"

using System.Net.Http;
var client = new HttpClient();
Console.WriteLine(client.GetAsync("https://google.com"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/u:System.Web
/r:System.Web
31 changes: 29 additions & 2 deletions tests/OmniSharp.Script.Tests/WorkspaceInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ public async Task SingleCsiScript()
}
}

[Fact]
public async Task SingleCsiScriptWithCustomRspNamespacesAndReferences()
{
using (var testProject = TestAssets.Instance.GetTestScript("SingleCsiScriptWithCustomRsp"))
using (var host = CreateOmniSharpHost(testProject.Directory, new Dictionary<string, string>
{
["script:rspFilePath"] = Path.Combine(testProject.Directory, "test.rsp")
}))
{
var workspaceInfo = await GetWorkspaceInfoAsync(host);
var project = Assert.Single(workspaceInfo.Projects);

Assert.Equal("main.csx", Path.GetFileName(project.Path));

// should have reference to mscorlib
VerifyCorLib(project);

// default globals object
Assert.Equal(typeof(CommandLineScriptGlobals), project.GlobalsType);

// should have RSP inherited settings
VerifyAssemblyReference(project, "system.web");
var commonUsingStatement = Assert.Single(project.CommonUsings);
Assert.Equal("System.Web", commonUsingStatement);
}
}

[Fact]
public async Task CsiScriptWithFileCreatedAfterStartingServer()
{
Expand Down Expand Up @@ -163,12 +190,12 @@ public async Task DotnetCoreScriptWithNuget()

private void VerifyCorLib(ScriptContextModel project, bool expected = true)
{
var corLibFound = project.ImplicitAssemblyReferences.Any(r => r == GetMsCorlibPath());
var corLibFound = project.AssemblyReferences.Any(r => r == GetMsCorlibPath());
Assert.True(corLibFound == expected, $"{(expected ? "Missing" : "Unnecessary")} reference to mscorlib");
}

private void VerifyAssemblyReference(ScriptContextModel project, string partialName) =>
Assert.True(project.ImplicitAssemblyReferences.Any(r => r.IndexOf(partialName, StringComparison.OrdinalIgnoreCase) > 0), $"Missing reference to {partialName}");
Assert.True(project.AssemblyReferences.Any(r => r.IndexOf(partialName, StringComparison.OrdinalIgnoreCase) > 0), $"Missing reference to {partialName}");

private static async Task<ScriptContextModelCollection> GetWorkspaceInfoAsync(OmniSharpTestHost host)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/TestUtility/TestAssets.TestProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public TestProject(string name, string baseDirectory, string directory, bool sha

public string AddDisposableFile(string fileName, string contents = null)
{
var filePath = Path.Combine(BaseDirectory, fileName);
var filePath = Path.Combine(Directory, fileName);
File.WriteAllText(filePath, contents ?? string.Empty);
_disposableFiles.Add(filePath);

Expand All @@ -59,7 +59,7 @@ public virtual void Dispose()
foreach (var filePath in _disposableFiles)
{
RunWithRetry(() => File.Delete(filePath));
if (System.IO.Directory.Exists(this.BaseDirectory))
if (System.IO.File.Exists(filePath))
{
throw new InvalidOperationException($"{nameof(ITestProject)} file still exists: '{filePath}'");
}
Expand Down