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

[Bug] GitVersion.MSBuild dotnet pack fails under Linux #2591

Closed
NameOfTheDragon opened this issue Feb 9, 2021 · 15 comments · Fixed by #2604
Closed

[Bug] GitVersion.MSBuild dotnet pack fails under Linux #2591

NameOfTheDragon opened this issue Feb 9, 2021 · 15 comments · Fixed by #2604
Labels
Milestone

Comments

@NameOfTheDragon
Copy link

NameOfTheDragon commented Feb 9, 2021

Describe the bug
My solution builds correctly and sets the TeamCity build number with the correct SemVer. It runs and passes all the unit tests.
Then it fails when running dotnet pack with the following build log output:

13:53:16]	     1>/root/.nuget/packages/gitversion.msbuild/5.6.6/tools/GitVersion.MsBuild.targets(110,9): error : ArgumentException: Unknown language detected: 'en_US:en'
[13:53:16]	/root/.nuget/packages/gitversion.msbuild/5.6.6/tools/GitVersion.MsBuild.targets(110,9): error :    at GitVersion.MsBuild.FileHelper.GetFileExtension(String language) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\Helpers\FileHelper.cs:line 56
[13:53:16]	/root/.nuget/packages/gitversion.msbuild/5.6.6/tools/GitVersion.MsBuild.targets(110,9): error :    at GitVersion.MsBuild.FileHelper.GetFileWriteInfo(String intermediateOutputPath, String language, String projectFile, String outputFileName) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\Helpers\FileHelper.cs:line 143
[13:53:16]	/root/.nuget/packages/gitversion.msbuild/5.6.6/tools/GitVersion.MsBuild.targets(110,9): error :    at GitVersion.MsBuild.GitVersionTaskExecutor.GenerateGitVersionInformation(GenerateGitVersionInformation task) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\GitVersionTaskExecutor.cs:line 57
[13:53:16]	/root/.nuget/packages/gitversion.msbuild/5.6.6/tools/GitVersion.MsBuild.targets(110,9): error :    at GitVersion.MsBuild.GitVersionTasks.<>c__DisplayClass2_0.<GenerateGitVersionInformation>b__0(IGitVersionTaskExecutor executor) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\GitVersionTasks.cs:line 17
[13:53:16]	/root/.nuget/packages/gitversion.msbuild/5.6.6/tools/GitVersion.MsBuild.targets(110,9): error :    at GitVersion.MsBuild.GitVersionTasks.ExecuteGitVersionTask[T](T task, Action`1 action) in D:\a\GitVersion\GitVersion\src\GitVersion.MsBuild\GitVersionTasks.cs:line 30
[13:53:16]	/root/.nuget/packages/gitversion.msbuild/5.6.6/tools/GitVersion.MsBuild.targets(110,9): error :
[13:53:16]	     1>Done Building Project "/opt/buildagent/work/8f12cde64aeec7e3/TA.Ascom.ReactiveCommunications/TA.Ascom.ReactiveCommunications.csproj" (pack target(s)) -- FAILED.
[13:53:16]

(full build log at http://teamcity.tigra-astronomy.com:8111/viewLog.html?buildId=9842&buildTypeId=TigraOss_TaReactiveCommunications&tab=buildLog&branch_TigraOss=hotfix%2F1.3.2)

Expected Behavior

I would expect this step to have succeeded since the build step worked successfully and produced the correct SemVer.
I do not understand how I get different results for dotnet build and dotnet pack.

Actual Behavior

Some crazy error about an "unknown language". Why does it even require a language?

Steps to Reproduce

Unfortunately I'm not able to provide a test solution as I don't really understand the failure mechanism.

Context

It's a showstopper, I'm unable to complete my build. I don't believe there's anything wrong with my solution or build configuration. I'm not aware that GitVersion provides any way for me to pass in a Language parameter so this appears to be purely an internal error.

Your Environment

TeamCity build server
Linux (Ubuntu 20.04) build agent (in a docker container) with GitVersion installed on the agent image.
The solution being built is https://github.com/Tigra-Astronomy/TA.ReactiveCommunications/tree/hotfix/1.3.2
The TeamCity build is http://teamcity.tigra-astronomy.com:8111/viewType.html?buildTypeId=TigraOss_TaReactiveCommunications&branch_TigraOss=hotfix%2F1.3.2&tab=buildTypeStatusDiv

@NameOfTheDragon NameOfTheDragon changed the title [Bug] dotnet pack fails under Linux [Bug] GitVersion.MSBuild dotnet pack fails under Linux Feb 9, 2021
@arturcic
Copy link
Member

arturcic commented Feb 9, 2021

There is a property that sets the Language to C#,

<Language Condition=" '$(Language)' == '' ">C#</Language>
, but I assume you have in your project this property set to "en_US:en" that's why it fails.

@NameOfTheDragon
Copy link
Author

There is a property that sets the Language to C#,

<Language Condition=" '$(Language)' == '' ">C#</Language>

, but I assume you have in your project this property set to "en_US:en" that's why it fails.

No, I'm not setting it at all. I'm using the vanilla 'pack' target. What it looks like to me is that the en-US:en value is somehow being picked up from an environment variable when run under Linux. If I open my build agent container with an interactive shell, and look at the environment variables, there's your language value right there:
image

@arturcic
Copy link
Member

arturcic commented Feb 9, 2021

You have 2 options, either remove/update that environment variable (but that might break something), or you can set the Language as a msbuild property (either in the csproj directly or via commandline)

@NameOfTheDragon
Copy link
Author

You have 2 options, either remove/update that environment variable (but that might break something), or you can set the Language as a msbuild property (either in the csproj directly or via commandline)

Those are good suggestions and I'll certainly try them as a workaround and report back. However, please note:

  1. This is the jetbrains/teamcity-agent image with GitVersion installed on it, so this might be a wider issue than just me. That environment variable seems to be pretty standard under Linux. Perhaps a better name could be used in GitVersion? Something like ProgrammingLanguage or something?
  2. Why isn't the dotnet build command affected? What's different with dotnet pack?

@arturcic
Copy link
Member

arturcic commented Feb 9, 2021

  1. Something like ProgrammingLanguage or something

Actually that is not what GitVersion sets. That's the msbuild Property that msbuild sets

@NameOfTheDragon
Copy link
Author

You have 2 options, either remove/update that environment variable (but that might break something), or you can set the Language as a msbuild property (either in the csproj directly or via commandline)

Those are good suggestions and I'll certainly try them as a workaround and report back. However, please note:

  1. This is the jetbrains/teamcity-agent image with GitVersion installed on it, so this might be a wider issue than just me. That environment variable seems to be pretty standard under Linux. Perhaps a better name could be used in GitVersion? Something like CompilerLanguage or something?
  2. Why isn't the dotnet build command affected? What's different with dotnet pack?

@NameOfTheDragon
Copy link
Author

Ok so we have a workaround (and the problem is confirmed). In my TeamCity build step, I added:

image

...and the result...

image

@NameOfTheDragon
Copy link
Author

  1. Something like ProgrammingLanguage or something

Actually that is not what GitVersion sets. That's the msbuild Property that msbuild sets

I believe that MSBuild promotes environment variables to build properties. So this LANGUAGE environment variable (which doesn't usually exist under Windows but does under Linux) will become a build property under Linux. I would imagine that the normal semantics are then applied, i.e. default values are set only if the property doesn't already have a value. So the en_US:en value, I think, is getting carried through into the build of the pack target. So you have a name collision under Linux between an environment variable and a build property.

I don't understand why the pack target is affected differently than the build target. Perhaps it's because during a compile operation, the LANGUAGE is set explicitly and not during pack. The innards of MSBuild and dotnet.exe are a bit beyond what I can afford to delve into, unfortunately. But I think you have here a 'smoking gun' that points to a cross-platform issue.

@NameOfTheDragon
Copy link
Author

In issue #2595 I have made some suggestions for handling the Language setting differently. The current approach is clearly flawed and in my humble opinion something needs to change. In my opinion, things should work for any valid MSBuild project on any platform/OS and not just for C#, F# and VB projects under Windows.

@ermshiperete
Copy link
Contributor

This is a very annoying issue. The LANGUAGE environment variables is used by GNU gettext, so it will be set on most Linux systems.

@ermshiperete
Copy link
Contributor

MSBuild's LANGUAGE gets set in /usr/share/dotnet/sdk/5.0.103/Microsoft.CSharp.CurrentVersion.targets (and Microsoft.VisualBasic.CurrentVersion.targets). When running the pack target, these *.targets files don't get imported.

When looking at those files, I notice that in addition to Language the property DefaultLanguageSourceExtension gets set. So a possible fix would be to include that property in the condition:

<Language Condition=" '$(Language)' == '' Or '$(DefaultLanguageSourceExtension)' == ''">C#</Language>

@arturcic
Copy link
Member

@ermshiperete thanks for taking the time to dig in. Can you try locally on your machine this potential fix? If it works can you fill in a PR?

@ermshiperete
Copy link
Contributor

Can you try locally on your machine this potential fix? If it works can you fill in a PR?

Yes, I tried it locally and it works.

@arturcic
Copy link
Member

Great, then, could you please send us a PR with the fix?

@asbjornu asbjornu added this to the 5.6.7 milestone Feb 24, 2021
arturcic pushed a commit that referenced this issue Feb 24, 2021
During regular builds MSBuild initializes `Language` to the
language of the currently building project, e.g. C#. However, when
running some targets like `pack` this doesn't happen. Therefore
GitVersion has to add a default value for `Language`. Unfortunately
`Language` is also an environment variable that GNU gettext uses,
so the `pack` target will fail on most Linux systems where that
environment variable is set.

This change implements a fix by additionally checking
`DefaultLanguageSourceExtension` which gets set by MSBuild at the
same time as `Language`. If that isn't set we can be pretty sure
that `Language` didn't get set automatically by MSBuild and can set
a default value.

This will cause existing builds to break IF they rely on being able
to explicitly set `Language` to a value different from C#. However,
I don't think this is a scenario that happens very often, and it can
be easily worked around by setting the `DefaultLanguageSourceExtension`
property to a non-empty value.

This fixes #2591.
@arturcic
Copy link
Member

🎉 This issue has been resolved in version 5.6.7 🎉
The release is available on:

Your GitReleaseManager bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants