From d2bfd11ddada278f3343d23746b9c3c2f18e3312 Mon Sep 17 00:00:00 2001
From: Tomas Matousek
Date: Thu, 22 Oct 2015 09:19:12 -0700
Subject: [PATCH] Hide namespaces and types of the host assembly
---
.../CSharp/Portable/Symbols/AssemblySymbol.cs | 11 +-
.../Compilation/ReferenceManagerTests.cs | 43 ++--
.../CommonReferenceManager.Resolution.cs | 2 +-
.../Interactive/InteractiveEvaluator.cs | 2 +-
.../HostTest/InteractiveHostTests.cs | 12 ++
.../CSharpTest/CommandLineRunnerTests.cs | 19 ++
.../CSharpTest/InteractiveSessionTests.cs | 185 ++++++++++++++++++
.../Hosting/CommandLine/CommandLineRunner.cs | 2 +-
.../RuntimeMetadataReferenceResolver.cs | 12 +-
src/Scripting/Core/Script.cs | 6 +-
src/Scripting/Core/ScriptOptions.cs | 15 +-
.../Compilation/CompilationExtensions.cs | 8 +
12 files changed, 275 insertions(+), 42 deletions(-)
diff --git a/src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs b/src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs
index b8d32d13938e6..5e99a4d7e175c 100644
--- a/src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs
+++ b/src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs
@@ -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.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)
diff --git a/src/Compilers/CSharp/Test/Symbol/Compilation/ReferenceManagerTests.cs b/src/Compilers/CSharp/Test/Symbol/Compilation/ReferenceManagerTests.cs
index 303701c226cd9..67b67c278713d 100644
--- a/src/Compilers/CSharp/Test/Symbol/Compilation/ReferenceManagerTests.cs
+++ b/src/Compilers/CSharp/Test/Symbol/Compilation/ReferenceManagerTests.cs
@@ -19,13 +19,7 @@ public class ReferenceManagerTests : CSharpTestBase
{
private static readonly CSharpCompilationOptions s_signedDll =
TestOptions.ReleaseDll.WithCryptoPublicKey(TestResources.TestKeys.PublicKey_ce65828c82a341f2);
-
- private static IEnumerable GetAssemblyAliases(Compilation compilation)
- {
- return compilation.GetBoundReferenceManager().GetReferencedAssemblyAliases().
- Select(t => $"{t.Item1.Identity.Name}{(t.Item2.IsEmpty ? "" : ": " + string.Join(",", t.Item2))}");
- }
-
+
[Fact]
public void WinRtCompilationReferences()
{
@@ -2202,12 +2196,11 @@ public void ReferenceDirective_RecursiveReferenceWithNoAliases()
c.VerifyDiagnostics();
- AssertEx.Equal(new[]
- {
+ c.VerifyAssemblyAliases(
"mscorlib",
"B: X,global",
"A"
- }, GetAssemblyAliases(c));
+ );
}
[Fact]
@@ -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]
@@ -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]
@@ -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]
@@ -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]
@@ -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]
diff --git a/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs b/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs
index add05da2d8bde..7b16a18844fcc 100644
--- a/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs
+++ b/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs
@@ -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;
diff --git a/src/Interactive/EditorFeatures/Core/Extensibility/Interactive/InteractiveEvaluator.cs b/src/Interactive/EditorFeatures/Core/Extensibility/Interactive/InteractiveEvaluator.cs
index 0c8cfa2ef48f5..724bd79f6daab 100644
--- a/src/Interactive/EditorFeatures/Core/Extensibility/Interactive/InteractiveEvaluator.cs
+++ b/src/Interactive/EditorFeatures/Core/Extensibility/Interactive/InteractiveEvaluator.cs
@@ -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(mscorlibRef, interactiveHostObjectRef);
_rspImports = ImmutableArray.Empty;
diff --git a/src/Interactive/HostTest/InteractiveHostTests.cs b/src/Interactive/HostTest/InteractiveHostTests.cs
index 28bb5ca211d21..656e6fde9175f 100644
--- a/src/Interactive/HostTest/InteractiveHostTests.cs
+++ b/src/Interactive/HostTest/InteractiveHostTests.cs
@@ -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()
{
diff --git a/src/Scripting/CSharpTest/CommandLineRunnerTests.cs b/src/Scripting/CSharpTest/CommandLineRunnerTests.cs
index b955ce77f3515..285ff4e76f17c 100644
--- a/src/Scripting/CSharpTest/CommandLineRunnerTests.cs
+++ b/src/Scripting/CSharpTest/CommandLineRunnerTests.cs
@@ -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()
{
diff --git a/src/Scripting/CSharpTest/InteractiveSessionTests.cs b/src/Scripting/CSharpTest/InteractiveSessionTests.cs
index fbd898d3f86c8..27aba2af31b77 100644
--- a/src/Scripting/CSharpTest/InteractiveSessionTests.cs
+++ b/src/Scripting/CSharpTest/InteractiveSessionTests.cs
@@ -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;
@@ -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,",
+ "Microsoft.CodeAnalysis.Scripting: ",
+ "System.Collections.Immutable: ,",
+ "Microsoft.CodeAnalysis: ,",
+ "System.Diagnostics.Tools: ,",
+ "System.Resources.ResourceManager: ,",
+ "System.Console: ,",
+ "System.Diagnostics.StackTrace: ,",
+ "System.IO.FileSystem: ,",
+ "System.Linq: ,",
+ "System.Text.Encoding: ,",
+ "System.IO.FileSystem.Primitives: ,",
+ "System.Reflection.Extensions: ,",
+ "System.Core: ,",
+ "System: ,",
+ "System.Xml: ,",
+ "System.Numerics: ,",
+ "System.Security: ,",
+ "System.Data.SqlXml: ,",
+ "System.Configuration: ,",
+ "System.Runtime: ,",
+ "System.Diagnostics.Debug: ,",
+ "System.Runtime.InteropServices: ,",
+ "System.Reflection.Metadata: ,",
+ "System.IO: ,",
+ "System.Collections: ,",
+ "System.Threading.Tasks: ,",
+ "System.Reflection.Primitives: ,",
+ "System.Reflection: ,",
+ "System.Globalization: ,",
+ "System.Runtime.Extensions: ,",
+ "System.Runtime.Numerics: ,",
+ "System.Runtime.Serialization.Json: ,",
+ "System.Collections.Concurrent: ,",
+ "System.Xml.ReaderWriter: ,",
+ "System.Xml.XDocument: ,",
+ "System.Dynamic.Runtime: ,",
+ "System.Threading: ,",
+ "System.Text.Encoding.Extensions: ,",
+ "System.Xml.Linq: ,",
+ "System.Runtime.Serialization: ,",
+ "System.ServiceModel.Internals: ,",
+ "SMDiagnostics: ,",
+ "System.ComponentModel.Composition: ,");
+ }
+
+ [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,",
+ "Microsoft.CodeAnalysis.Scripting: ,global",
+ "Microsoft.CodeAnalysis.Scripting.CSharp",
+ "Microsoft.CodeAnalysis.CSharp: ,global",
+ "Microsoft.CodeAnalysis: ,,global",
+ "System.Collections.Immutable: ,,global",
+ "System.Diagnostics.Tools: ,,global",
+ "System.Resources.ResourceManager: ,,global",
+ "System.Text.Encoding: ,,global",
+ "System.AppContext: ,global",
+ "System.Reflection.Extensions: ,,global",
+ "System: ,,global",
+ "System.Configuration: ,,global",
+ "System.Xml: ,,global",
+ "System.Data.SqlXml: ,,global",
+ "System.Security: ,,global",
+ "System.Core: ,,global",
+ "System.Numerics: ,,global",
+ "System.Runtime: ,,global",
+ "System.Diagnostics.Debug: ,,global",
+ "System.Collections: ,,global",
+ "System.Linq: ,,global",
+ "System.Runtime.Extensions: ,,global",
+ "System.Globalization: ,,global",
+ "System.Threading: ,,global",
+ "System.ComponentModel.Composition: ,,global",
+ "System.Runtime.InteropServices: ,,global",
+ "System.Reflection.Metadata: ,,global",
+ "System.IO: ,,global",
+ "System.Threading.Tasks: ,,global",
+ "System.Reflection.Primitives: ,,global",
+ "System.Reflection: ,,global",
+ "System.Runtime.Numerics: ,,global",
+ "System.Runtime.Serialization.Json: ,,global",
+ "System.Collections.Concurrent: ,,global",
+ "System.Xml.ReaderWriter: ,,global",
+ "System.Xml.XDocument: ,,global",
+ "System.Dynamic.Runtime: ,,global",
+ "System.Text.Encoding.Extensions: ,,global",
+ "System.Xml.Linq: ,,global",
+ "System.Runtime.Serialization: ,,global",
+ "System.ServiceModel.Internals: ,,global",
+ "SMDiagnostics: ,,global",
+ "System.Linq.Expressions: ,global",
+ "System.Threading.Tasks.Parallel: ,global",
+ "System.Console: ,,global",
+ "System.Diagnostics.StackTrace: ,,global",
+ "System.IO.FileSystem: ,,global",
+ "System.IO.FileSystem.Primitives: ,,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,",
+ "Microsoft.CodeAnalysis.Scripting: global,",
+ "System.Collections.Immutable: ,global,",
+ "Microsoft.CodeAnalysis: ,global,",
+ "System.Diagnostics.Tools: ,global,",
+ "System.Resources.ResourceManager: ,global,",
+ "System.Console: ,global,",
+ "System.Diagnostics.StackTrace: ,global,",
+ "System.IO.FileSystem: ,global,",
+ "System.Linq: ,global,",
+ "System.Text.Encoding: ,global,",
+ "System.IO.FileSystem.Primitives: ,global,",
+ "System.Reflection.Extensions: ,global,",
+ "System.Core: ,global,",
+ "System: ,global,",
+ "System.Xml: ,global,",
+ "System.Numerics: ,global,",
+ "System.Security: ,global,",
+ "System.Data.SqlXml: ,global,",
+ "System.Configuration: ,global,",
+ "System.Runtime: ,global,",
+ "System.Diagnostics.Debug: ,global,",
+ "System.Runtime.InteropServices: ,global,",
+ "System.Reflection.Metadata: ,global,",
+ "System.IO: ,global,",
+ "System.Collections: ,global,",
+ "System.Threading.Tasks: ,global,",
+ "System.Reflection.Primitives: ,global,",
+ "System.Reflection: ,global,",
+ "System.Globalization: ,global,",
+ "System.Runtime.Extensions: ,global,",
+ "System.Runtime.Numerics: ,global,",
+ "System.Runtime.Serialization.Json: ,global,",
+ "System.Collections.Concurrent: ,global,",
+ "System.Xml.ReaderWriter: ,global,",
+ "System.Xml.XDocument: ,global,",
+ "System.Dynamic.Runtime: ,global,",
+ "System.Threading: ,global,",
+ "System.Text.Encoding.Extensions: