Bug 48709 - CSharpCodeProvider fails with CS7027 when AssemblyKeyFileAttribute is in the source
Summary: CSharpCodeProvider fails with CS7027 when AssemblyKeyFileAttribute is in the ...
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2016-12-02 14:11 UTC by Robert van der Boon
Modified: 2016-12-12 15:00 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

CSharpCodeProviderWithSigning.cs (2.82 KB, text/plain)
2016-12-02 14:11 UTC, Robert van der Boon
CSharpCodeProviderWithSigning.cs (2.58 KB, text/plain)
2016-12-05 09:40 UTC, Robert van der Boon

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Robert van der Boon 2016-12-02 14:11:07 UTC
Created attachment 18754 [details]

If I'm using CSharpCodeProvider.CompileAssemblyFromSource(CompilerParameters,string[]) it fails with error CS7027 when the source contains:
[assembly: System.Reflection.AssemblyKeyFileAttribute("path/to/key.snk")]

If I add the same "path/to/key.snk" as the "keyfile" parameter in the CSharpCodeProvider constructor it does work correctly. (It doesn't matter whether path/to/key.snk is relative or absolute)

The failure is a recent regression, it does correctly work with .Net framework and with Mono (and before).

See attached a reproduction case.

create a snk file: sn -k key.snk
compile the test: mcs -t:exe CSharpCodeProviderWithSigning.cs
- using the ctor keyfile: mono --debug CSharpCodeProviderWithSigning.exe key.snk y
   output: Success!
- using the attribute: mono --debug CSharpCodeProviderWithSigning.exe key.snk
Error Compiling:
[assembly: System.Reflection.AssemblyKeyFileAttribute(@"key.snk")]
namespace T {
   public class Test {
      public int I = 0;
:0:CS7027:Error signing output with public key from file 'key.snk' -- mscoree.dll
Comment 1 Robert van der Boon 2016-12-02 14:12:59 UTC
output of mono --version:
Mono JIT compiler version 4.9.0 (master/e3e699f Fri Dec  2 09:49:05 CET 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen (concurrent by default)

OS: RedHat 6.6, x86_64
Comment 2 Robert van der Boon 2016-12-05 09:32:56 UTC
The sample program was wrong, and thus the statement in the original description is false:
> If I add the same "path/to/key.snk" as the "keyfile" parameter
> in the CSharpCodeProvider constructor it does work correctly.
Specifying the key in CSharpCodeProvider in the constructor doesn't cause the key to be signed (in mono nor .net framework).

If I specify the key in the CompilerOptions though (compilerParameters.CompilerOptions = "/keyfile:" + snkFile) it does sign on windows, but fails with the exact same error on mono head/1e890d84.

I'll upload an updated sample.
Comment 3 Robert van der Boon 2016-12-05 09:40:31 UTC
Created attachment 18770 [details]
Comment 4 Marek Safar 2016-12-12 15:00:16 UTC
Fixed in master