7

I'm trying to build a Xamarin iOS app in Visual Studio App Center. The solution contains two projects. One is a Xamarin iOS project. The other is a bindings library project. The bindings library project is configured with the Allow 'unsafe' code option.

enter image description here

But the Xamarin.iOS build task in the App Center fails saying:

Unsafe code may only appear if compiling with /unsafe

enter image description here

I'm able to build the solution locally without any issues.

Anybody an idea?


After trying different things I switched from the .csproj file to the .sln file in the build configuration.

enter image description here

Can somebody verify this?

Luca
  • 111
  • 6
  • 1
    Can you check if the setting is still checked if you switch to the `Release` configuration? – Martin Ullrich Feb 03 '18 at 15:50
  • The setting is still checked when I switch to the `Release` config and it builds successfully on my machine but still fails in the App Center. :( – Luca Feb 03 '18 at 16:29
  • When I take a look at the `msbuild`call then the `/unsafe`switch is not passed. `[command]/Library/Frameworks/Mono.framework/Versions/5_4_1/bin/msbuild /Users/vsts/agent/2.127.0/work/1/s/ios-xamarin/App.csproj /p:Configuration=Release /p:Platform=iPhoneSimulator /t:Rebuild` Maybe the `/unsafe` switch is not supported? – Luca Feb 03 '18 at 16:37
  • Local use of `msbuild` via terminal succeeds. Even without the `/unsafe` switch. – Luca Feb 03 '18 at 16:43
  • 1
    the unsafe parameter would be passed to the compiler, not to msbuild. the msbuild logic only uses the `AllowUnsafeBlocks` property defined in the csproj file to pass it on to the compiler. you can check the csproj file if this property is defined under any element with a `Condition` attribute that looks like it could affect the build. – Martin Ullrich Feb 03 '18 at 17:13
  • when you say "local use", did you pass the same parameters? especially `/p:Configuration=Release` – Martin Ullrich Feb 03 '18 at 17:14
  • Thank you for making clear that `msbuild` is using the `.csproj` content to pass things to the `csc`. I was not aware of it. When I say local I mean building on my local machine. This local build succeeds from Visual Studio and using `msbuild` via terminal. Even when using the `/p:Configuration=Release` argument. – Luca Feb 03 '18 at 17:55
  • @Luca could you solve your issue in the end? Running into the same problem ... – Sebastian Greifeneder Apr 06 '18 at 12:50
  • Yes it works now. I think switching to the solution in the build configuration did the trick. Before I referenced the .csproj file in the build config. After referencing the .sln file the build was successful. – Luca Apr 07 '18 at 21:49
  • Finally, I was also able to fix this problem by switching to the SLN file, and creating a new configuration mapping to build only the according csproj. I spent some time on this together with AppCenter support, and the mentioned problem is a known issue there. https://docs.microsoft.com/en-us/appcenter/build/xamarin/ios/solution-configuration-mappings – Sebastian Greifeneder Apr 25 '18 at 08:24

2 Answers2

3

Probably you encounter the error like this when building in App Center:

error CS0227: Unsafe code may only appear if compiling with /unsafe

Please try either building from .sln so that your PCL configuration will be honored or add Release|iPhone (or the one you're trying to build) configuration with unsafe enabled to the PCL/.NET Standard project.

Reason: when building from .csproj file, msbuild expects to have the same configuration names in all referenced projects, cause there is no possibility to know how to map iOS project configuration Release|iPhone to configurations in PCL project which normally do not have *|iPhone part in it.

Documentation reference - https://docs.microsoft.com/en-us/appcenter/build/xamarin/ios/#312-building-from-the-project-file-csprojfsproj

nevalenny
  • 71
  • 6
0

I had a xamarin.ios project that built locally with the 'allow unsafe code' boxes checked, but it gave the same error when I tried to build on AppCenter.

To get it to build successfully I added the AllowUnsafeBlocks as an Environment Variable then set it to true.

I was trying to figure out where to add it as an msbuild command as this person suggested https://stackoverflow.com/a/50755461/7933361 and for me it worked as an environment variable. When I remove the environment variable the build fails build setup

Tim
  • 133
  • 8