Skip to content

Commit

Permalink
Merge pull request dotnet#6237 from tmat/HideHostAssembly
Browse files Browse the repository at this point in the history
Hide namespaces and types of the host assembly
  • Loading branch information
tmat committed Oct 22, 2015
2 parents 6b1cc03 + d2bfd11 commit 438b888
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 42 deletions.
11 changes: 10 additions & 1 deletion src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,16 @@ internal NamedTypeSymbol GetTopLevelTypeByMetadataName(
"Never include references for a non-source assembly, because they don't know about aliases.");

var assemblies = ArrayBuilder<AssemblySymbol>.GetInstance();
DeclaringCompilation.GetUnaliasedReferencedAssemblies(assemblies);

// ignore reference aliases if searching for a type from a specific assembly:
if (assemblyOpt != null)
{
assemblies.AddRange(DeclaringCompilation.GetBoundReferenceManager().ReferencedAssemblies);
}
else
{
DeclaringCompilation.GetUnaliasedReferencedAssemblies(assemblies);
}

// Lookup in references
foreach (var assembly in assemblies)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ public class ReferenceManagerTests : CSharpTestBase
{
private static readonly CSharpCompilationOptions s_signedDll =
TestOptions.ReleaseDll.WithCryptoPublicKey(TestResources.TestKeys.PublicKey_ce65828c82a341f2);

private static IEnumerable<string> GetAssemblyAliases(Compilation compilation)
{
return compilation.GetBoundReferenceManager().GetReferencedAssemblyAliases().
Select(t => $"{t.Item1.Identity.Name}{(t.Item2.IsEmpty ? "" : ": " + string.Join(",", t.Item2))}");
}


[Fact]
public void WinRtCompilationReferences()
{
Expand Down Expand Up @@ -2202,12 +2196,11 @@ public void ReferenceDirective_RecursiveReferenceWithNoAliases()

c.VerifyDiagnostics();

AssertEx.Equal(new[]
{
c.VerifyAssemblyAliases(
"mscorlib",
"B: X,global",
"A"
}, GetAssemblyAliases(c));
);
}

[Fact]
Expand All @@ -2234,12 +2227,10 @@ public void ReferenceDirective_NonRecursiveReferenceWithNoAliases()
// new B()
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "B").WithArguments("B"));

AssertEx.Equal(new[]
{
c.VerifyAssemblyAliases(
"mscorlib",
"B: X",
"A"
}, GetAssemblyAliases(c));
"A");
}

[Fact]
Expand Down Expand Up @@ -2272,12 +2263,10 @@ public class P

c.VerifyDiagnostics();

AssertEx.Equal(new[]
{
c.VerifyAssemblyAliases(
"B: X,Y",
"A: global,Y",
"mscorlib: global,Y"
}, GetAssemblyAliases(c));
"mscorlib: global,Y");
}

[Fact]
Expand Down Expand Up @@ -2310,12 +2299,10 @@ public class P

c.VerifyDiagnostics();

AssertEx.Equal(new[]
{
c.VerifyAssemblyAliases(
"B: X,Y",
"A: global,Y",
"mscorlib: global,Y"
}, GetAssemblyAliases(c));
"mscorlib: global,Y");
}

[Fact]
Expand Down Expand Up @@ -2350,12 +2337,10 @@ public class P

c.VerifyDiagnostics();

AssertEx.Equal(new[]
{
c.VerifyAssemblyAliases(
"B: X,Y",
"A: global,Y",
"mscorlib: global,Y"
}, GetAssemblyAliases(c));
"mscorlib: global,Y");
}

[Fact]
Expand Down Expand Up @@ -2391,13 +2376,11 @@ public class P

c.VerifyDiagnostics();

AssertEx.Equal(new[]
{
c.VerifyAssemblyAliases(
"B: X,Y,Y,Z",
"A: Y,Y,Z",
"D: Z",
"mscorlib: global,Y,Y,Z"
}, GetAssemblyAliases(c));
"mscorlib: global,Y,Y,Z");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ private static PortableExecutableReference ResolveReferenceDirective(string refe
// checked earlier:
Debug.Assert(compilation.Options.MetadataReferenceResolver != null);

var references = compilation.Options.MetadataReferenceResolver.ResolveReference(reference, basePath, MetadataReferenceProperties.Assembly);
var references = compilation.Options.MetadataReferenceResolver.ResolveReference(reference, basePath, MetadataReferenceProperties.Assembly.WithRecursiveAliases(true));
if (references.IsDefaultOrEmpty)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private void ProcessStarting(bool initialize)

var metadataService = _workspace.CurrentSolution.Services.MetadataService;
var mscorlibRef = metadataService.GetReference(typeof(object).Assembly.Location, MetadataReferenceProperties.Assembly);
var interactiveHostObjectRef = metadataService.GetReference(typeof(InteractiveScriptGlobals).Assembly.Location, MetadataReferenceProperties.Assembly);
var interactiveHostObjectRef = metadataService.GetReference(typeof(InteractiveScriptGlobals).Assembly.Location, Script.HostAssemblyReferenceProperties);

_references = ImmutableHashSet.Create<MetadataReference>(mscorlibRef, interactiveHostObjectRef);
_rspImports = ImmutableArray<string>.Empty;
Expand Down
12 changes: 12 additions & 0 deletions src/Interactive/HostTest/InteractiveHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,18 @@ public void ReferenceDirectives()
Assert.Equal("1\r\n2\r\n", output);
}

[Fact]
public void Script_NoHostNamespaces()
{
Execute("nameof(Microsoft.CodeAnalysis)");

AssertEx.AssertEqualToleratingWhitespaceDifferences(@"
(1,8): error CS0234: The type or namespace name 'CodeAnalysis' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)",
ReadErrorOutputToEnd());

Assert.Equal("", ReadOutputToEnd());
}

[Fact]
public void ExecutesOnStaThread()
{
Expand Down
19 changes: 19 additions & 0 deletions src/Scripting/CSharpTest/CommandLineRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,25 @@ public void Script_BadUsings()
", runner.Console.Out.ToString());
}

[Fact]
public void Script_NoHostNamespaces()
{
var runner = CreateRunner(input: "nameof(Microsoft.CodeAnalysis)");

runner.RunInteractive();

AssertEx.AssertEqualToleratingWhitespaceDifferences(
$@"Microsoft (R) Visual C# Interactive Compiler version {CompilerVersion}
Copyright (C) Microsoft Corporation. All rights reserved.
Type ""#help"" for more information.
> nameof(Microsoft.CodeAnalysis)
«Red»
(1,8): error CS0234: The type or namespace name 'CodeAnalysis' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
«Gray»
> ", runner.Console.Out.ToString());
}

[Fact]
public void SourceSearchPaths1()
{
Expand Down
185 changes: 185 additions & 0 deletions src/Scripting/CSharpTest/InteractiveSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using Microsoft.CodeAnalysis.Scripting.Test;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -1442,6 +1444,189 @@ public void HostObjectInRootNamespace()
Assert.Equal(1, r1.Result);
}

[Fact]
public void HostObjectAssemblyReference1()
{
var scriptCompilation = CSharpScript.Create(
"nameof(Microsoft.CodeAnalysis.Scripting)",
globalsType: typeof(CommandLineScriptGlobals)).GetCompilation();

scriptCompilation.VerifyDiagnostics(
// (1,8): error CS0234: The type or namespace name 'CodeAnalysis' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
Diagnostic(ErrorCode.ERR_DottedTypeNameNotFoundInNS, "Microsoft.CodeAnalysis").WithArguments("CodeAnalysis", "Microsoft"));

scriptCompilation.VerifyAssemblyAliases(
"mscorlib: global,<host>",
"Microsoft.CodeAnalysis.Scripting: <host>",
"System.Collections.Immutable: <implicit>,<host>",
"Microsoft.CodeAnalysis: <implicit>,<host>",
"System.Diagnostics.Tools: <implicit>,<host>",
"System.Resources.ResourceManager: <implicit>,<host>",
"System.Console: <implicit>,<host>",
"System.Diagnostics.StackTrace: <implicit>,<host>",
"System.IO.FileSystem: <implicit>,<host>",
"System.Linq: <implicit>,<host>",
"System.Text.Encoding: <implicit>,<host>",
"System.IO.FileSystem.Primitives: <implicit>,<host>",
"System.Reflection.Extensions: <implicit>,<host>",
"System.Core: <implicit>,<host>",
"System: <implicit>,<host>",
"System.Xml: <implicit>,<host>",
"System.Numerics: <implicit>,<host>",
"System.Security: <implicit>,<host>",
"System.Data.SqlXml: <implicit>,<host>",
"System.Configuration: <implicit>,<host>",
"System.Runtime: <implicit>,<host>",
"System.Diagnostics.Debug: <implicit>,<host>",
"System.Runtime.InteropServices: <implicit>,<host>",
"System.Reflection.Metadata: <implicit>,<host>",
"System.IO: <implicit>,<host>",
"System.Collections: <implicit>,<host>",
"System.Threading.Tasks: <implicit>,<host>",
"System.Reflection.Primitives: <implicit>,<host>",
"System.Reflection: <implicit>,<host>",
"System.Globalization: <implicit>,<host>",
"System.Runtime.Extensions: <implicit>,<host>",
"System.Runtime.Numerics: <implicit>,<host>",
"System.Runtime.Serialization.Json: <implicit>,<host>",
"System.Collections.Concurrent: <implicit>,<host>",
"System.Xml.ReaderWriter: <implicit>,<host>",
"System.Xml.XDocument: <implicit>,<host>",
"System.Dynamic.Runtime: <implicit>,<host>",
"System.Threading: <implicit>,<host>",
"System.Text.Encoding.Extensions: <implicit>,<host>",
"System.Xml.Linq: <implicit>,<host>",
"System.Runtime.Serialization: <implicit>,<host>",
"System.ServiceModel.Internals: <implicit>,<host>",
"SMDiagnostics: <implicit>,<host>",
"System.ComponentModel.Composition: <implicit>,<host>");
}

[Fact]
public void HostObjectAssemblyReference2()
{
var scriptCompilation = CSharpScript.Create(
"typeof(Microsoft.CodeAnalysis.Scripting.Script)",
options: ScriptOptions.Default.WithReferences(typeof(CSharpScript).GetTypeInfo().Assembly),
globalsType: typeof(CommandLineScriptGlobals)).GetCompilation();

scriptCompilation.VerifyDiagnostics();

scriptCompilation.VerifyAssemblyAliases(
"mscorlib: global,<host>",
"Microsoft.CodeAnalysis.Scripting: <host>,global",
"Microsoft.CodeAnalysis.Scripting.CSharp",
"Microsoft.CodeAnalysis.CSharp: <implicit>,global",
"Microsoft.CodeAnalysis: <implicit>,<host>,global",
"System.Collections.Immutable: <implicit>,<host>,global",
"System.Diagnostics.Tools: <implicit>,<host>,global",
"System.Resources.ResourceManager: <implicit>,<host>,global",
"System.Text.Encoding: <implicit>,<host>,global",
"System.AppContext: <implicit>,global",
"System.Reflection.Extensions: <implicit>,<host>,global",
"System: <implicit>,<host>,global",
"System.Configuration: <implicit>,<host>,global",
"System.Xml: <implicit>,<host>,global",
"System.Data.SqlXml: <implicit>,<host>,global",
"System.Security: <implicit>,<host>,global",
"System.Core: <implicit>,<host>,global",
"System.Numerics: <implicit>,<host>,global",
"System.Runtime: <implicit>,<host>,global",
"System.Diagnostics.Debug: <implicit>,<host>,global",
"System.Collections: <implicit>,<host>,global",
"System.Linq: <implicit>,<host>,global",
"System.Runtime.Extensions: <implicit>,<host>,global",
"System.Globalization: <implicit>,<host>,global",
"System.Threading: <implicit>,<host>,global",
"System.ComponentModel.Composition: <implicit>,<host>,global",
"System.Runtime.InteropServices: <implicit>,<host>,global",
"System.Reflection.Metadata: <implicit>,<host>,global",
"System.IO: <implicit>,<host>,global",
"System.Threading.Tasks: <implicit>,<host>,global",
"System.Reflection.Primitives: <implicit>,<host>,global",
"System.Reflection: <implicit>,<host>,global",
"System.Runtime.Numerics: <implicit>,<host>,global",
"System.Runtime.Serialization.Json: <implicit>,<host>,global",
"System.Collections.Concurrent: <implicit>,<host>,global",
"System.Xml.ReaderWriter: <implicit>,<host>,global",
"System.Xml.XDocument: <implicit>,<host>,global",
"System.Dynamic.Runtime: <implicit>,<host>,global",
"System.Text.Encoding.Extensions: <implicit>,<host>,global",
"System.Xml.Linq: <implicit>,<host>,global",
"System.Runtime.Serialization: <implicit>,<host>,global",
"System.ServiceModel.Internals: <implicit>,<host>,global",
"SMDiagnostics: <implicit>,<host>,global",
"System.Linq.Expressions: <implicit>,global",
"System.Threading.Tasks.Parallel: <implicit>,global",
"System.Console: <implicit>,<host>,global",
"System.Diagnostics.StackTrace: <implicit>,<host>,global",
"System.IO.FileSystem: <implicit>,<host>,global",
"System.IO.FileSystem.Primitives: <implicit>,<host>,global");
}

[Fact]
public void HostObjectAssemblyReference3()
{
string source = $@"
#r ""{typeof(CSharpScript).GetTypeInfo().Assembly.ManifestModule.FullyQualifiedName}""
typeof(Microsoft.CodeAnalysis.Scripting.Script)
";
var scriptCompilation = CSharpScript.Create(source, globalsType: typeof(CommandLineScriptGlobals)).GetCompilation();

scriptCompilation.VerifyDiagnostics();

scriptCompilation.VerifyAssemblyAliases(
"Microsoft.CodeAnalysis.Scripting.CSharp",
"mscorlib: global,<host>",
"Microsoft.CodeAnalysis.Scripting: global,<host>",
"System.Collections.Immutable: <implicit>,global,<host>",
"Microsoft.CodeAnalysis: <implicit>,global,<host>",
"System.Diagnostics.Tools: <implicit>,global,<host>",
"System.Resources.ResourceManager: <implicit>,global,<host>",
"System.Console: <implicit>,global,<host>",
"System.Diagnostics.StackTrace: <implicit>,global,<host>",
"System.IO.FileSystem: <implicit>,global,<host>",
"System.Linq: <implicit>,global,<host>",
"System.Text.Encoding: <implicit>,global,<host>",
"System.IO.FileSystem.Primitives: <implicit>,global,<host>",
"System.Reflection.Extensions: <implicit>,global,<host>",
"System.Core: <implicit>,global,<host>",
"System: <implicit>,global,<host>",
"System.Xml: <implicit>,global,<host>",
"System.Numerics: <implicit>,global,<host>",
"System.Security: <implicit>,global,<host>",
"System.Data.SqlXml: <implicit>,global,<host>",
"System.Configuration: <implicit>,global,<host>",
"System.Runtime: <implicit>,global,<host>",
"System.Diagnostics.Debug: <implicit>,global,<host>",
"System.Runtime.InteropServices: <implicit>,global,<host>",
"System.Reflection.Metadata: <implicit>,global,<host>",
"System.IO: <implicit>,global,<host>",
"System.Collections: <implicit>,global,<host>",
"System.Threading.Tasks: <implicit>,global,<host>",
"System.Reflection.Primitives: <implicit>,global,<host>",
"System.Reflection: <implicit>,global,<host>",
"System.Globalization: <implicit>,global,<host>",
"System.Runtime.Extensions: <implicit>,global,<host>",
"System.Runtime.Numerics: <implicit>,global,<host>",
"System.Runtime.Serialization.Json: <implicit>,global,<host>",
"System.Collections.Concurrent: <implicit>,global,<host>",
"System.Xml.ReaderWriter: <implicit>,global,<host>",
"System.Xml.XDocument: <implicit>,global,<host>",
"System.Dynamic.Runtime: <implicit>,global,<host>",
"System.Threading: <implicit>,global,<host>",
"System.Text.Encoding.Extensions: <implicit>,global,<host>",
"System.Xml.Linq: <implicit>,global,<host>",
"System.Runtime.Serialization: <implicit>,global,<host>",
"System.ServiceModel.Internals: <implicit>,global,<host>",
"SMDiagnostics: <implicit>,global,<host>",
"System.ComponentModel.Composition: <implicit>,global,<host>",
"Microsoft.CodeAnalysis.CSharp: <implicit>,global",
"System.AppContext: <implicit>,global",
"System.Linq.Expressions: <implicit>,global",
"System.Threading.Tasks.Parallel: <implicit>,global");
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ internal static MetadataReferenceResolver GetMetadataReferenceResolver(CommandLi
(path, properties) =>
{
loggerOpt?.AddRead(path);
return MetadataReference.CreateFromFile(path);
return MetadataReference.CreateFromFile(path, properties);
});
}

Expand Down
Loading

0 comments on commit 438b888

Please sign in to comment.