Skip to content

Commit 9645216

Browse files
committed
Start using ProviderOptions; Allow opt out of magic ASP.Net command-line compiler options
1 parent 2153c2e commit 9645216

9 files changed

Lines changed: 61 additions & 41 deletions

File tree

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/CSharpCompiler.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,22 @@ protected override string FullPathsOption {
6969

7070
protected override void FixUpCompilerParameters(CompilerParameters options) {
7171
base.FixUpCompilerParameters(options);
72-
List<string> noWarnStrings = new List<string>(5);
73-
noWarnStrings.AddRange(new string[] { "1659", "1699", "1701" });
7472

75-
// disableObsoleteWarnings
76-
noWarnStrings.Add("612"); // [Obsolete] without message
77-
noWarnStrings.Add("618"); // [Obsolete("with message")]
73+
// We used to magically add some ASP.net-centric options here. For compatibilities sake
74+
// we will continue to do so in ASP.Net mode. If these are getting in the way for people
75+
// though, disable ASP.Net mode and they will go away. (Sort of. These are the defaults
76+
// in the XDT config transform, so they will already be here anyway for most folks.)
77+
if (_providerOptions.UseAspNetSettings)
78+
{
79+
List<string> noWarnStrings = new List<string>(5);
80+
noWarnStrings.AddRange(new string[] { "1659", "1699", "1701" });
7881

79-
CompilationUtil.PrependCompilerOption(options, "/nowarn:" + String.Join(";", noWarnStrings));
82+
// disableObsoleteWarnings
83+
noWarnStrings.Add("612"); // [Obsolete] without message
84+
noWarnStrings.Add("618"); // [Obsolete("with message")]
85+
86+
CompilationUtil.PrependCompilerOption(options, "/nowarn:" + String.Join(";", noWarnStrings));
87+
}
8088
}
8189

8290
protected override string CmdArgsFromParameters(CompilerParameters parameters) {

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/Compiler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Microsoft.CodeDom.Providers.DotNetCompilerPlatform {
1717
internal abstract class Compiler : ICodeCompiler {
1818
private readonly CodeDomProvider _codeDomProvider;
19-
private readonly IProviderOptions _providerOptions;
19+
protected readonly IProviderOptions _providerOptions;
2020
private string _compilerFullPath = null;
2121
private const string CLR_PROFILING_SETTING = "COR_ENABLE_PROFILING";
2222
private const string DISABLE_PROFILING = "0";
@@ -165,10 +165,14 @@ private string GetCompilationArgumentString(CompilerParameters options) {
165165
// CodeDom sets TreatWarningAsErrors to true whenever warningLevel is non-zero.
166166
// However, TreatWarningAsErrors should be false by default.
167167
// And users should be able to set the value by set the value of option "WarnAsError".
168-
// ASP.Net does fix this "WarnAsError" option, but only for old CodeDom providers (CSharp/VB).
169-
// So we need to do this correction here.
170-
private static void FixTreatWarningsAsErrors(CompilerParameters parameters) {
171-
parameters.TreatWarningsAsErrors = false;
168+
// ASP.Net does fix this "WarnAsError" option in a like named function, but only for old CodeDom providers (CSharp/VB).
169+
// The ASP.Net fix was to set TreatWarningAsErrors to false anytime '/warnaserror' was
170+
// detected in the compiler command line options, thus allowing the user-specified
171+
// option to prevail. In these CodeDom providers though, users have control through
172+
// the 'WarnAsError' provider option as well as manual control over the command
173+
// line args. So just go with the 'WarnAsError' provider option here.
174+
private void FixTreatWarningsAsErrors(CompilerParameters parameters) {
175+
parameters.TreatWarningsAsErrors = _providerOptions.WarnAsError;
172176
}
173177

174178
private CompilerResults FromSourceBatch(CompilerParameters options, string[] sources) {

src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/VBCompiler.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,38 @@ protected override void ProcessCompilerOutputLine(CompilerResults results, strin
5252

5353
protected override void FixUpCompilerParameters(CompilerParameters options) {
5454
base.FixUpCompilerParameters(options);
55-
// Hard code OptionInfer to true, which is the default value in the root web config.
56-
// TODO This code should be removed once CodeDom directly supports provider options such as WarnAsError, OptionInfer
57-
CompilationUtil.PrependCompilerOption(options, " /optionInfer+");
5855

59-
List<string> noWarnStrings = new List<string>(3);
56+
// We used to magically add some ASP.net-centric options here. For compatibilities sake
57+
// we will continue to do so in ASP.Net mode. If these are getting in the way for people
58+
// though, disable ASP.Net mode and they will go away. (Sort of. These mostly are the defaults
59+
// in the XDT config transform, so they will already be here anyway for most folks.)
60+
if (_providerOptions.UseAspNetSettings)
61+
{
62+
// Hard code OptionInfer to true, which is the default value in the root web config.
63+
// TODO This code should be removed once CodeDom directly supports provider options such as WarnAsError, OptionInfer
64+
CompilationUtil.PrependCompilerOption(options, " /optionInfer+");
6065

61-
// If VB, add all the imported namespaces on the command line (DevDiv 21499).
62-
// This is VB only because other languages don't support global command line
63-
// namespace imports.
64-
AddVBGlobalNamespaceImports(options);
66+
List<string> noWarnStrings = new List<string>(3);
6567

66-
// Add any command line flags needed to support the My.* feature
67-
AddVBMyFlags(options);
68+
// If VB, add all the imported namespaces on the command line (DevDiv 21499).
69+
// This is VB only because other languages don't support global command line
70+
// namespace imports.
71+
AddVBGlobalNamespaceImports(options);
6872

69-
// Ignore vb warning that complains about assemblyKeyName (Dev10 662544)
70-
// but only for target 3.5 and above (715329)
71-
noWarnStrings.Add("41008");
73+
// Add any command line flags needed to support the My.* feature
74+
AddVBMyFlags(options);
7275

73-
// disable ObsoleteWarnings
74-
noWarnStrings.Add("40000"); // [Obsolete("with message")]
75-
noWarnStrings.Add("40008"); // [Obsolete] without message
76+
// Ignore vb warning that complains about assemblyKeyName (Dev10 662544)
77+
// but only for target 3.5 and above (715329)
78+
noWarnStrings.Add("41008");
7679

77-
if (noWarnStrings.Count > 0) {
78-
CompilationUtil.PrependCompilerOption(options, "/nowarn:" + String.Join(",", noWarnStrings));
80+
// disable ObsoleteWarnings
81+
noWarnStrings.Add("40000"); // [Obsolete("with message")]
82+
noWarnStrings.Add("40008"); // [Obsolete] without message
83+
84+
if (noWarnStrings.Count > 0) {
85+
CompilationUtil.PrependCompilerOption(options, "/nowarn:" + String.Join(",", noWarnStrings));
86+
}
7987
}
8088
}
8189

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/net45/app.config.install.xdt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
extension=".cs"
3737
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
3838
warningLevel="4"
39-
compilerOptions="/langversion:6 /nowarn:1659;1699;1701"
39+
compilerOptions="/langversion:6 /nowarn:1659;1699;1701;612;618"
4040
xdt:Transform="Insert" />
4141
</compilers>
4242
</system.codedom>
@@ -59,7 +59,7 @@
5959
extension=".vb"
6060
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
6161
warningLevel="4"
62-
compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
62+
compilerOptions="/langversion:14 /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
6363
xdt:Transform="Insert" />
6464
</compilers>
6565
</system.codedom>

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/net45/web.config.install.xdt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
extension=".cs"
2929
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
3030
warningLevel="4"
31-
compilerOptions="/langversion:6 /nowarn:1659;1699;1701"
31+
compilerOptions="/langversion:6 /nowarn:1659;1699;1701;612;618"
3232
xdt:Transform="Insert" />
3333
</compilers>
3434
</system.codedom>
@@ -51,7 +51,7 @@
5151
extension=".vb"
5252
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
5353
warningLevel="4"
54-
compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
54+
compilerOptions="/langversion:14 /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
5555
xdt:Transform="Insert" />
5656
</compilers>
5757
</system.codedom>

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/net46/app.config.install.xdt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
extension=".cs"
3737
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
3838
warningLevel="4"
39-
compilerOptions="/langversion:default /nowarn:1659;1699;1701"
39+
compilerOptions="/langversion:default /nowarn:1659;1699;1701;612;618"
4040
xdt:Transform="Insert" />
4141
</compilers>
4242
</system.codedom>
@@ -59,7 +59,7 @@
5959
extension=".vb"
6060
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
6161
warningLevel="4"
62-
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
62+
compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
6363
xdt:Transform="Insert" />
6464
</compilers>
6565
</system.codedom>

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/net46/web.config.install.xdt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
extension=".cs"
2929
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
3030
warningLevel="4"
31-
compilerOptions="/langversion:default /nowarn:1659;1699;1701"
31+
compilerOptions="/langversion:default /nowarn:1659;1699;1701;612;618"
3232
xdt:Transform="Insert" />
3333
</compilers>
3434
</system.codedom>
@@ -51,7 +51,7 @@
5151
extension=".vb"
5252
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
5353
warningLevel="4"
54-
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
54+
compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
5555
xdt:Transform="Insert" />
5656
</compilers>
5757
</system.codedom>

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/net472/app.config.install.xdt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
extension=".cs"
3737
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
3838
warningLevel="4"
39-
compilerOptions="/langversion:default /nowarn:1659;1699;1701"
39+
compilerOptions="/langversion:default /nowarn:1659;1699;1701;612;618"
4040
xdt:Transform="Insert" />
4141
</compilers>
4242
</system.codedom>
@@ -59,7 +59,7 @@
5959
extension=".vb"
6060
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
6161
warningLevel="4"
62-
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
62+
compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
6363
xdt:Transform="Insert" />
6464
</compilers>
6565
</system.codedom>

src/Packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/content/net472/web.config.install.xdt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
extension=".cs"
2929
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
3030
warningLevel="4"
31-
compilerOptions="/langversion:default /nowarn:1659;1699;1701"
31+
compilerOptions="/langversion:default /nowarn:1659;1699;1701;612;618"
3232
xdt:Transform="Insert" />
3333
</compilers>
3434
</system.codedom>
@@ -51,7 +51,7 @@
5151
extension=".vb"
5252
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
5353
warningLevel="4"
54-
compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
54+
compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"
5555
xdt:Transform="Insert" />
5656
</compilers>
5757
</system.codedom>

0 commit comments

Comments
 (0)