Skip to content
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

Support VS Mac 17.x #131

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Roslyn compiler was not using the best supported C# lang version
The CSharpParseOptions was not using the best language version. The
CSharpParseOptions was created but no language version was used from
the RuntimeInfo nor the language version specified by the Roslyn code
compiler.
  • Loading branch information
mrward committed Jun 6, 2022
commit d84065e3081af2bf587b3855532d13ea034a89ee
18 changes: 12 additions & 6 deletions Mono.TextTemplating.Roslyn/RoslynCodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ CodeCompilerResult CompileFileInternal (
var parseOptions = args?.ParseOptions ?? new CSharpParseOptions();

if (arguments.LangVersion != null) {
if (LanguageVersionFacts.TryParse(arguments.LangVersion, out var langVersion)) {
parseOptions = parseOptions.WithLanguageVersion (langVersion);
} else {
throw new System.Exception($"Unknown value '{arguments.LangVersion}' for langversion");
}
parseOptions = WithLanguageVersion (parseOptions, arguments.LangVersion);
} else {
// need to update this when updating referenced roslyn binaries
CSharpLangVersionHelper.GetBestSupportedLangVersion (runtime, CSharpLangVersion.v9_0);
var langVersion = CSharpLangVersionHelper.GetBestSupportedLangVersion (runtime, CSharpLangVersion.v9_0);
parseOptions = WithLanguageVersion (parseOptions, CSharpLangVersionHelper.ToString (langVersion));
}

var syntaxTrees = new List<SyntaxTree> ();
Expand Down Expand Up @@ -120,5 +117,14 @@ CodeCompilerResult CompileFileInternal (
Errors = errors
};
}

static CSharpParseOptions WithLanguageVersion (CSharpParseOptions parseOptions, string langVersionText)
{
if (LanguageVersionFacts.TryParse (langVersionText, out var langVersion)) {
return parseOptions.WithLanguageVersion (langVersion);
} else {
throw new System.Exception ($"Unknown value '{langVersionText}' for langversion");
}
}
}
}
1 change: 0 additions & 1 deletion Mono.TextTemplating.Roslyn/TemplatingEngineExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static void UseInProcessCompiler (this TemplatingEngine engine)
engine.SetCompilerFunc ((RuntimeInfo r) => new RoslynCodeCompiler (r));

RuntimeInfo.ThrowOnMissingDotNetCoreSdkDirectory = false;
RuntimeInfo.DefaultMaxSupportedLangVersion = CSharpLangVersion.v9_0;
}

public static void UseInProcessCompiler (this TemplateGenerator generator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static bool HasLangVersionArg (string args) =>
&& ProcessArgumentBuilder.TryParse (args, out var parsedArgs)
&& parsedArgs.Any (a => a.IndexOf("langversion") == 1);

static string ToString (CSharpLangVersion v) => v switch {
internal static string ToString (CSharpLangVersion v) => v switch {
CSharpLangVersion.v5_0 => "5",
CSharpLangVersion.v6_0 => "6",
CSharpLangVersion.v7_0 => "7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,7 @@ static RuntimeInfo GetDotNetCoreSdk ()
return FromError (RuntimeKind.NetCore, "Could not find csc.dll in any .NET Core SDK");
}
string cscPath = sdkDir == null ? null : MakeCscPath (sdkDir);
CSharpLangVersion maxCSharpVersion;
if (sdkVersion.Equals(SemVersion.Zero) && DefaultMaxSupportedLangVersion.HasValue) {
maxCSharpVersion = DefaultMaxSupportedLangVersion.Value;
} else {
maxCSharpVersion = CSharpLangVersionHelper.FromNetCoreSdkVersion (sdkVersion);
}
CSharpLangVersion maxCSharpVersion = CSharpLangVersionHelper.FromNetCoreSdkVersion (sdkVersion);

// it's ok if this is null, we may be running on an older SDK that didn't support packs
//in which case we fall back to resolving from the runtime dir
Expand Down