Skip to content

Commit fa36003

Browse files
authored
Merge pull request dotnet-script#236 from SlowLogicBoy/master
Fix for dotnet-script#235
2 parents 54a883b + 15cb326 commit fa36003

File tree

2 files changed

+67
-21
lines changed

2 files changed

+67
-21
lines changed

src/Dotnet.Script.Core/ScriptCompiler.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public virtual ScriptOptions CreateScriptOptions(ScriptContext context, IList<Ru
8080
.WithMetadataResolver(new NuGetMetadataReferenceResolver(ScriptMetadataResolver.Default.WithBaseDirectory(context.WorkingDirectory)))
8181
.WithEmitDebugInformation(true)
8282
.WithFileEncoding(context.Code.Encoding);
83-
83+
8484
if (!string.IsNullOrWhiteSpace(context.FilePath))
8585
{
8686
opts = opts.WithFilePath(context.FilePath);
8787
}
88-
88+
8989
return opts;
9090
}
9191

@@ -110,7 +110,7 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
110110
.ToDictionary(f => f.Name, f => f.ResolvedRuntimeAssembly, StringComparer.OrdinalIgnoreCase);
111111

112112
foreach (var runtimeAssembly in dependencyMap.Values)
113-
{
113+
{
114114
Logger.Verbose("Adding reference to a runtime dependency => " + runtimeAssembly);
115115
opts = opts.AddReferences(MetadataReference.CreateFromFile(runtimeAssembly.Path));
116116
}
@@ -126,15 +126,15 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
126126
foreach (var inheritedAssemblyName in inheritedAssemblyNames)
127127
{
128128
// Always prefer the resolved runtime dependency rather than the inherited assembly.
129-
if(!dependencyMap.ContainsKey(inheritedAssemblyName.Name))
129+
if(!dependencyMap.ContainsKey(inheritedAssemblyName.Name))
130130
{
131131
Logger.Verbose($"Adding reference to an inherited dependency => {inheritedAssemblyName.FullName}");
132132
var assembly = Assembly.Load(inheritedAssemblyName);
133-
opts = opts.AddReferences(assembly);
133+
opts = opts.AddReferences(assembly);
134134
}
135135
}
136136

137-
AppDomain.CurrentDomain.AssemblyResolve +=
137+
AppDomain.CurrentDomain.AssemblyResolve +=
138138
(sender, args) => MapUnresolvedAssemblyToRuntimeLibrary(dependencyMap, args);
139139

140140
// when processing raw code, make sure we inject new lines after preprocessor directives
@@ -156,7 +156,7 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
156156

157157
if (context.OptimizationLevel == OptimizationLevel.Release)
158158
{
159-
Logger.Verbose("Configuration/Optimization mode: Release");
159+
Logger.Verbose("Configuration/Optimization mode: Release");
160160
SetReleaseOptimizationLevel(script.GetCompilation());
161161
}
162162
else
@@ -176,22 +176,29 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
176176
}
177177

178178
private static void SetReleaseOptimizationLevel(Compilation compilation)
179-
{
179+
{
180180
var compilationOptionsField = typeof(CSharpCompilation).GetTypeInfo().GetDeclaredField("_options");
181181
var compilationOptions = (CSharpCompilationOptions)compilationOptionsField.GetValue(compilation);
182182
compilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Release);
183-
compilationOptionsField.SetValue(compilation, compilationOptions);
183+
compilationOptionsField.SetValue(compilation, compilationOptions);
184184
}
185-
185+
186186
private Assembly MapUnresolvedAssemblyToRuntimeLibrary(IDictionary<string, RuntimeAssembly> dependencyMap, ResolveEventArgs args)
187187
{
188188
var assemblyName = new AssemblyName(args.Name);
189189
if (dependencyMap.TryGetValue(assemblyName.Name, out var runtimeAssembly))
190190
{
191191
if (runtimeAssembly.Name.Version > assemblyName.Version)
192192
{
193-
Logger.Log($"Redirecting {assemblyName} to {runtimeAssembly.Name}");
194-
return Assembly.LoadFile(runtimeAssembly.Path);
193+
var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies()
194+
.FirstOrDefault(a => a.GetName().Name == assemblyName.Name);
195+
if(loadedAssembly != null)
196+
{
197+
Logger.Log($"Redirecting {assemblyName} to already loaded {loadedAssembly.GetName().Name}");
198+
return loadedAssembly;
199+
}
200+
Logger.Log($"Redirecting {assemblyName} to {runtimeAssembly.Name}");
201+
return Assembly.LoadFrom(runtimeAssembly.Path);
195202
}
196203
}
197204

src/Dotnet.Script.Tests/ScriptExecutionTests.cs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ScriptExecutionTests
1212
{
1313
[Fact]
1414
public void ShouldExecuteHelloWorld()
15-
{
15+
{
1616
var result = ExecuteInProcess(Path.Combine("HelloWorld", "HelloWorld.csx"));
1717
//Assert.Contains("Hello World", result.output);
1818
}
@@ -39,9 +39,9 @@ public void ShouldHandlePackageWithNativeLibraries()
3939
{
4040
var result = Execute(Path.Combine("NativeLibrary", "NativeLibrary.csx"));
4141
Assert.Contains("Connection successful", result.output);
42-
}
42+
}
4343
}
44-
44+
4545
[Fact]
4646
public static void ShouldReturnExitCodeOnenWhenScriptFails()
4747
{
@@ -80,7 +80,7 @@ public static void ShouldHandleIssue166()
8080
{
8181
var result = Execute(Path.Combine("Issue166", "Issue166.csx"));
8282
Assert.Contains("Connection successful", result.output);
83-
}
83+
}
8484
}
8585

8686
[Fact]
@@ -101,7 +101,7 @@ public static void ShouldPassKnownArgumentToScriptWhenEscapedByDoubleHyphen()
101101
public static void ShouldNotPassUnEscapedKnownArgumentToScript()
102102
{
103103
var result = Execute($"{Path.Combine("Arguments", "Arguments.csx")}", "-v");
104-
Assert.DoesNotContain("-v", result.output);
104+
Assert.DoesNotContain("-v", result.output);
105105
}
106106

107107
[Fact]
@@ -120,15 +120,15 @@ public static void ShouldHandleIssue181()
120120

121121
[Fact]
122122
public static void ShouldHandleIssue198()
123-
{
123+
{
124124
var result = Execute(Path.Combine("Issue198", "Issue198.csx"));
125125
Assert.Contains("NuGet.Client", result.output);
126126
}
127127

128128

129129
[Fact]
130130
public static void ShouldHandleIssue204()
131-
{
131+
{
132132
var result = Execute(Path.Combine("Issue204", "Issue204.csx"));
133133
Assert.Contains("System.Net.WebProxy", result.output);
134134
}
@@ -192,6 +192,45 @@ public void ShouldSupportInlineNugetReferencesWithTrailingSemicoloninEvaluatedCo
192192
Assert.Contains("AutoMapper.MapperConfiguration", result.output);
193193
}
194194

195+
[Fact]
196+
public static void ShouldHandleIssue235()
197+
{
198+
string code =
199+
@"using AgileObjects.AgileMapper;
200+
public class TestClass
201+
{
202+
public TestClass()
203+
{
204+
IMapper mapper = Mapper.CreateNew();
205+
}
206+
}";
207+
208+
string script =
209+
@"#! ""netcoreapp2.0""
210+
#r ""nuget: AgileObjects.AgileMapper, 0.23.1""
211+
#r ""TestLibrary.dll""
212+
213+
using AgileObjects.AgileMapper;
214+
215+
IMapper mapper = Mapper.CreateNew();
216+
var testClass = new TestClass();
217+
Console.WriteLine(""Hello World!"");";
218+
219+
220+
using (var disposableFolder = new DisposableFolder())
221+
{
222+
var projectFolder = Path.Combine(disposableFolder.Path, "TestLibrary");
223+
ProcessHelper.RunAndCaptureOutput("dotnet", new[] { "new classlib -n TestLibrary" }, disposableFolder.Path);
224+
ProcessHelper.RunAndCaptureOutput("dotnet", new[] { "add TestLibrary.csproj package AgileObjects.AgileMapper -v 0.23.0" }, projectFolder);
225+
File.WriteAllText(Path.Combine(projectFolder, "Class1.cs"), code);
226+
File.WriteAllText(Path.Combine(projectFolder, "script.csx"), script);
227+
ProcessHelper.RunAndCaptureOutput("dotnet", new[] { "build -c release -o ./" }, projectFolder);
228+
229+
var dotnetScriptArguments = GetDotnetScriptArguments(Path.Combine(projectFolder, "script.csx"));
230+
var result = ProcessHelper.RunAndCaptureOutput("dotnet", dotnetScriptArguments);
231+
Assert.Contains("Hello World!", result.output);
232+
}
233+
}
195234
private static (string output, int exitCode) Execute(string fixture, params string[] arguments)
196235
{
197236
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments(Path.Combine("..", "..", "..", "TestFixtures", fixture), arguments));
@@ -209,8 +248,8 @@ private static (string output, int exitCode) ExecuteCode(string code)
209248
/// </summary>
210249
private static int ExecuteInProcess(string fixture, params string[] arguments)
211250
{
212-
var pathToFixture = Path.Combine("..", "..", "..","TestFixtures", fixture);
213-
var allArguments = new List<string>(new[] {pathToFixture});
251+
var pathToFixture = Path.Combine("..", "..", "..", "TestFixtures", fixture);
252+
var allArguments = new List<string>(new[] { pathToFixture });
214253
if (arguments != null)
215254
{
216255
allArguments.AddRange(arguments);

0 commit comments

Comments
 (0)