8000 Assembly isolation is opt-in at dotnet-script level. · dotnet-script/dotnet-script@104b62f · GitHub
[go: up one dir, main page]

Skip to content

Commit 104b62f

Browse files
committed
Assembly isolation is opt-in at dotnet-script level.
1 parent 353609e commit 104b62f

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/Dotnet.Script.Tests/ScriptExecutionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public void ShouldIgnoreGlobalJsonInScriptFolder()
475475
[Fact]
476476
public void ShouldIsolateScriptAssemblies()
477477
{
478-
var result = ScriptTe 8000 stRunner.Default.ExecuteFixture("Isolation");
478+
var result = ScriptTestRunner.Default.ExecuteFixture("Isolation", "--isolated-load-context");
479479
Assert.Contains("10.0.0.0", result.output);
480480
}
481481

src/Dotnet.Script/Program.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ private static int Wain(string[] args)
6767
var debugMode = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
6868
var verbosity = app.Option("--verbosity", " Set the verbosity level of the command. Allowed values are t[trace], d[ebug], i[nfo], w[arning], e[rror], and c[ritical].", CommandOptionType.SingleValue);
6969
var nocache = app.Option("--no-cache", "Disable caching (Restore and Dll cache)", CommandOptionType.NoValue);
70+
var isolatedLoadContext = app.Option("--isolated-load-context", "Use isolated assembly load context", CommandOptionType.NoValue);
7071
var infoOption = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);
7172

7273
var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
73-
var argsAfterDoubleHyphen = args.SkipWhile(a => a != "--").Skip(1).ToArray();
74+
var argsAfterDoubleHyphen = args.SkipWhile(a => a != "--").Skip(1).ToArray();
7475

7576
const string helpOptionTemplate = "-? | -h | --help";
7677
app.HelpOption(helpOptionTemplate);
@@ -99,7 +100,7 @@ private static int Wain(string[] args)
99100
}
100101

101102
var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
102-
var options = new ExecuteCodeCommandOptions(source, cwd.Value(), app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(),configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug, nocache.HasValue(),packageSources.Values?.ToArray());
103+
var options = new ExecuteCodeCommandOptions(source, cwd.Value(), app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(), configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug, nocache.HasValue(), packageSources.Values?.ToArray());
103104
return await new ExecuteCodeCommand(ScriptConsole.Default, logFactory).Execute<int>(options);
104105
});
105106
});
@@ -231,11 +232,15 @@ private static int Wain(string[] args)
231232
return 0;
232233
}
233234

235+
AssemblyLoadContext assemblyLoadContext = null;
236+
if (isolatedLoadContext.HasValue())
237+
assemblyLoadContext = new ScriptAssemblyLoadContext();
238+
234239
if (scriptFile.HasValue)
235240
{
236241
if (interactive.HasValue())
237242
{
238-
return await RunInteractiveWithSeed(file.Value, logFactory, scriptArguments, packageSources.Values?.ToArray());
243+
return await RunInteractiveWithSeed(file.Value, logFactory, scriptArguments, packageSources.Values?.ToArray(), assemblyLoadContext);
239244
}
240245

241246
var fileCommandOptions = new ExecuteScriptCommandOptions
@@ -248,42 +253,40 @@ private static int Wain(string[] args)
248253
nocache.HasValue()
249254
)
250255
{
251-
AssemblyLoadContext = CreateAssemblyLoadContext()
256+
AssemblyLoadContext = assemblyLoadContext
252257
};
253258

254259
var fileCommand = new ExecuteScriptCommand(ScriptConsole.Default, logFactory);
255260
return await fileCommand.Run<int, CommandLineScriptGlobals>(fileCommandOptions);
256-
}
261+
}
257262
else
258263
{
259-
await RunInteractive(!nocache.HasValue(), logFactory, packageSources.Values?.ToArray());
264+
await RunInteractive(!nocache.HasValue(), logFactory, packageSources.Values?.ToArray(), assemblyLoadContext);
260265
}
261266
return exitCode;
262267
});
263268

264269
return app.Execute(argsBeforeDoubleHyphen);
265270
}
266271

267-
private static async Task<int> RunInteractive(bool useRestoreCache, LogFactory logFactory, string[] packageSources)
272+
private static async Task<int> RunInteractive(bool useRestoreCache, LogFactory logFactory, string[] packageSources, AssemblyLoadContext assemblyLoadContext)
268273
{
269274
var options = new ExecuteInteractiveCommandOptions(null, Array.Empty<string>(), packageSources)
270275
{
271-
AssemblyLoadContext = CreateAssemblyLoadContext()
276+
AssemblyLoadContext = assemblyLoadContext
272277
};
273278
await new ExecuteInteractiveCommand(ScriptConsole.Default, logFactory).Execute(options);
274279
return 0;
275280
}
276281

277-
private async static Task<int> RunInteractiveWithSeed(string file, LogFactory logFactory, string[] arguments, string[] packageSources)
282+
private async static Task<int> RunInteractiveWithSeed(string file, LogFactory logFactory, string[] arguments, string[] packageSources, AssemblyLoadContext assemblyLoadContext)
278283
{
279284
var options = new ExecuteInteractiveCommandOptions(new ScriptFile(file), arguments, packageSources)
280285
{
281-
AssemblyLoadContext = CreateAssemblyLoadContext()
286+
AssemblyLoadContext = assemblyLoadContext
282287
};
283288
await new ExecuteInteractiveCommand(ScriptConsole.Default, logFactory).Execute(options);
284289
return 0;
285290
}
286-
287-
static AssemblyLoadContext CreateAssemblyLoadContext() => new ScriptAssemblyLoadContext();
288291
}
289292
}

0 commit comments

Comments
 (0)
0