-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Allow emitting debugging symbols via high level scripting APIs #16489
Changes from 19 commits
7973ec1
d722d4d
b480b9c
c1ded03
12eca6f
e211836
74dfe7c
c38b68f
a8be429
d743344
64bed32
0e95e93
2710583
78d2ed4
f7d2e80
f47a9b2
739b805
5fbe48b
931fdf9
3160a3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Scripting; | ||
using Microsoft.CodeAnalysis.Scripting.Hosting; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.Scripting | ||
{ | ||
|
@@ -23,7 +26,23 @@ public static class CSharpScript | |
/// <typeparam name="T">The return type of the script</typeparam> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
throw if code == null here as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and add doc comment that this method throws ArgNullEx In reply to: 99941131 [](ancestors = 99941131) |
||
public static Script<T> Create<T>(string code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null) | ||
{ | ||
return Script.CreateInitialScript<T>(CSharpScriptCompiler.Instance, code, options, globalsType, assemblyLoader); | ||
return Script.CreateInitialScript<T>(CSharpScriptCompiler.Instance, SourceText.From(code, options?.FileEncoding), options, globalsType, assemblyLoader); | ||
} | ||
|
||
/// <summary> | ||
/// Create a new C# script. | ||
/// </summary> | ||
/// <param name="code">The <see cref="Stream"/> representing the source code of the script.</param> | ||
/// <param name="options">The script options.</param> | ||
/// <param name="globalsType">Type of global object.</param> | ||
/// <param name="assemblyLoader">Custom assembly loader.</param> | ||
/// <typeparam name="T">The return type of the script</typeparam> | ||
/// <exception cref="ArgumentNullException">Stream is null.</exception> | ||
/// <exception cref="ArgumentException">Stream is not readable or seekable.</exception> | ||
public static Script<T> Create<T>(Stream code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
check code == null and throw |
||
if (code == null) throw new ArgumentNullException(nameof(code)); | ||
return Script.CreateInitialScript<T>(CSharpScriptCompiler.Instance, SourceText.From(code, options?.FileEncoding), options, globalsType, assemblyLoader); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -38,6 +57,21 @@ public static Script<object> Create(string code, ScriptOptions options = null, T | |
return Create<object>(code, options, globalsType, assemblyLoader); | ||
} | ||
|
||
/// <summary> | ||
/// Create a new C# script. | ||
/// </summary> | ||
/// <param name="code">The <see cref="Stream"/> representing the source code of the script.</param> | ||
/// <param name="options">The script options.</param> | ||
/// <param name="globalsType">Type of global object.</param> | ||
/// <param name="assemblyLoader">Custom assembly loader.</param> | ||
/// <exception cref="ArgumentNullException">Stream is null.</exception> | ||
/// <exception cref="ArgumentException">Stream is not readable or seekable.</exception> | ||
public static Script<object> Create(Stream code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null) | ||
{ | ||
if (code == null) throw new ArgumentNullException(nameof(code)); | ||
return Create<object>(code, options, globalsType, assemblyLoader); | ||
} | ||
|
||
/// <summary> | ||
/// Run a C# script. | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
static Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.Create(System.IO.Stream code, Microsoft.CodeAnalysis.Scripting.ScriptOptions options = null, System.Type globalsType = null, Microsoft.CodeAnalysis.Scripting.Hosting.InteractiveAssemblyLoader assemblyLoader = null) -> Microsoft.CodeAnalysis.Scripting.Script<object> | ||
static Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.Create<T>(System.IO.Stream code, Microsoft.CodeAnalysis.Scripting.ScriptOptions options = null, System.Type globalsType = null, Microsoft.CodeAnalysis.Scripting.Hosting.InteractiveAssemblyLoader assemblyLoader = null) -> Microsoft.CodeAnalysis.Scripting.Script<T> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,9 @@ private static ScriptOptions GetScriptOptions(CommandLineArguments arguments, st | |
references: ImmutableArray.CreateRange(resolvedReferences), | ||
namespaces: CommandLineHelpers.GetImports(arguments), | ||
metadataResolver: metadataResolver, | ||
sourceResolver: sourceResolver); | ||
sourceResolver: sourceResolver, | ||
emitDebugInformation: false, | ||
fileEncoding: null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't need to be fixed now but shouldn't the command line runner (csi) actually use this feature, so that we have better exception stack traces? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes that's what I thought as well - let's add it in a separate PR |
||
} | ||
|
||
internal static MetadataReferenceResolver GetMetadataReferenceResolver(CommandLineArguments arguments, TouchedFileLogger loggerOpt) | ||
|
@@ -176,7 +178,7 @@ private int RunScript(ScriptOptions options, string code, ErrorLogger errorLogge | |
var globals = new CommandLineScriptGlobals(_console.Out, _objectFormatter); | ||
globals.Args.AddRange(_compiler.Arguments.ScriptArguments); | ||
|
||
var script = Script.CreateInitialScript<int>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null); | ||
var script = Script.CreateInitialScript<int>(_scriptCompiler, SourceText.From(code), options, globals.GetType(), assemblyLoaderOpt: null); | ||
try | ||
{ | ||
return script.RunAsync(globals, cancellationToken).Result.ReturnValue; | ||
|
@@ -197,7 +199,7 @@ private void RunInteractiveLoop(ScriptOptions options, string initialScriptCodeO | |
|
||
if (initialScriptCodeOpt != null) | ||
{ | ||
var script = Script.CreateInitialScript<object>(_scriptCompiler, initialScriptCodeOpt, options, globals.GetType(), assemblyLoaderOpt: null); | ||
var script = Script.CreateInitialScript<object>(_scriptCompiler, SourceText.From(initialScriptCodeOpt), options, globals.GetType(), assemblyLoaderOpt: null); | ||
BuildAndRun(script, globals, ref state, ref options, displayResult: false, cancellationToken: cancellationToken); | ||
} | ||
|
||
|
@@ -249,7 +251,7 @@ private void RunInteractiveLoop(ScriptOptions options, string initialScriptCodeO | |
Script<object> newScript; | ||
if (state == null) | ||
{ | ||
newScript = Script.CreateInitialScript<object>(_scriptCompiler, code, options, globals.GetType(), assemblyLoaderOpt: null); | ||
newScript = Script.CreateInitialScript<object>(_scriptCompiler, SourceText.From(code ?? string.Empty), options, globals.GetType(), assemblyLoaderOpt: null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
code can't be null here |
||
} | ||
else | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Microsoft.CodeAnalysis.Scripting.Script.ContinueWith(System.IO.Stream code, Microsoft.CodeAnalysis.Scripting.ScriptOptions options = null) -> Microsoft.CodeAnalysis.Scripting.Script<object> | ||
Microsoft.CodeAnalysis.Scripting.Script.ContinueWith<TResult>(System.IO.Stream code, Microsoft.CodeAnalysis.Scripting.ScriptOptions options = null) -> Microsoft.CodeAnalysis.Scripting.Script<TResult> | ||
Microsoft.CodeAnalysis.Scripting.ScriptOptions.EmitDebugInformation.get -> bool | ||
Microsoft.CodeAnalysis.Scripting.ScriptOptions.FileEncoding.get -> System.Text.Encoding | ||
Microsoft.CodeAnalysis.Scripting.ScriptOptions.WithEmitDebugInformation(bool emitDebugInformation) -> Microsoft.CodeAnalysis.Scripting.ScriptOptions | ||
Microsoft.CodeAnalysis.Scripting.ScriptOptions.WithFileEncoding(System.Text.Encoding encoding) -> Microsoft.CodeAnalysis.Scripting.ScriptOptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not be disabling this analyzer. Need to address the problem and settle on a single overload with optional parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we need to address the problem in the analyzer. I don't think there is anything wrong with the API.