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

PreBuildEvent Use Macros Parameter On Linux Is Not Working Properly #2397

Open
Petermarcu opened this issue Aug 4, 2017 · 4 comments
Open

PreBuildEvent Use Macros Parameter On Linux Is Not Working Properly #2397

Petermarcu opened this issue Aug 4, 2017 · 4 comments

Comments

@Petermarcu
Copy link
Member

@Petermarcu Petermarcu commented Aug 4, 2017

From @baiyunchen in dotnet/core#786

.NET Core version:2.0.0-preview2-006497
operating system:CentOS
When I use PreBuildEvent to execute some commands, the path in Macros does not seem to work properly in Linux.
My command is: dotnet $(SolutionDir)tools/copyfiles/CopyFiles.dll "$(SolutionDir)src/Infrastructures/DbScript" "*.config" "Configuration/Data"

It can work in Windows VS, but in Linux will be thrown

No executable found matching command "dotnet-tools/copyfiles/CopyFiles.dll"
/opt/dotnet/sdk/2.0.0-preview2-006497/Microsoft.Common.CurrentVersion.targets(1274,5): error MSB3073: The command "dotnet tools/copyfiles/CopyFiles.dll "src/Infrastructures/DbScript" "*.config" "Configuration/Data"" exited with code 1. [/usr/gitlab/builds/bee2a437/0/xxx/src/XXX.csproj]
I tried to manually find the root directory of the solution implementation

dotnet $(SolutionDir)tools/copyfiles/CopyFiles.dll "$(SolutionDir)src/Infrastructures/DbScript" "*.config" "Configuration/Data"
It looks $(SolutionDir) is not working properly.

@rainersigwald

This comment has been minimized.

Copy link
Contributor

@rainersigwald rainersigwald commented Aug 9, 2017

@baiyunchen I tried to reproduce this with a simple one-project solution, and I don't see the behavior you describe. $(SolutionDir) is expanded to the right value as shown in the error (I didn't create a CopyFiles.dll

Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  No executable found matching command "dotnet-/mnt/s/work/solutiondir/tools/copyfiles/CopyFiles.dll"
/mnt/s/work/solutiondir/proj/proj.csproj(8,5): error MSB3073: The command "dotnet /mnt/s/work/solutiondir/tools/copyfiles/CopyFiles.dll" exited with code 1.

Build FAILED.

/mnt/s/work/solutiondir/proj/proj.csproj(8,5): error MSB3073: The command "dotnet /mnt/s/work/solutiondir/tools/copyfiles/CopyFiles.dll" exited with code 1.
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:04.91

I created the PreBuildEvent in Visual studio, which resulted in this change in my project file:

diff --git a/proj/proj.csproj b/proj/proj.csproj
index 049d1c7..b6370f7 100644
--- a/proj/proj.csproj
+++ b/proj/proj.csproj
@@ -4,4 +4,8 @@
     <TargetFramework>netstandard1.4</TargetFramework>
   </PropertyGroup>
 
+  <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
+    <Exec Command="dotnet $(SolutionDir)tools/copyfiles/CopyFiles.dll" />
+  </Target>
+
 </Project>

Questions

  1. How did you add the prebuild event?
  2. How is the prebuild event represented in your project file?
  3. How are you invoking the build? $(SolutionDir) is only meaningful if the build started with the .sln file.
@baiyunchen

This comment has been minimized.

Copy link

@baiyunchen baiyunchen commented Aug 10, 2017

Thanks for your answer!
I think I'm having trouble here that " $(SolutionDir) is only meaningful if the build started with the .sln file."。
Because I used $(SolutionDir) when I use dotnet publish command publish a website project.
But I can not understand why I use visual studio in the windows can carrectly publish the website?Does it work with the dotnet publish command?

I Define the pre-build event in the build event tab in the project property.

Here are some of the code in my .csproj file:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <UserSecretsId>aspnet-Lianluo.ItemMaintain.BaseInfo.Api-B0CF1B8D-EFA9-474E-AC55-6B7D05906A53</UserSecretsId>
    <PostBuildEvent></PostBuildEvent>
    <RunPostBuildEvent>Always</RunPostBuildEvent>
    <AssetTargetFallback>%3bnet461%3bportable-net45+win8</AssetTargetFallback>
    <PreBuildEvent>dotnet $(SolutionDir)tools/copyfiles/CopyFiles.dll "$(SolutionDir)src/Infrastructures/Lianluo.ItemMaintain.DbScript" "*.config" "Configuration/Data"</PreBuildEvent>
  </PropertyGroup>

CopyFiles.dll is a simple .net core console application for copying files in linux and windows.It contains three parameters,the first parameter for the source directory,the second parameter for the target directory,the last parameter for the file search condition.

@rainersigwald

This comment has been minimized.

Copy link
Contributor

@rainersigwald rainersigwald commented Aug 10, 2017

But I can not understand why I use visual studio in the windows can carrectly publish the website?Does it work with the dotnet publish command?

Builds from within the Visual Studio UI and command-line builds that start with the solution file both provide information to individual projects about the solution, including $(SolutionDir). Individual project builds from the command line can't provide that information, because it's not possible to know if there's a solution that contains a particular project, and there might be more than one.

If you must build the individual project because you can't publish through the solution, you can redefine the event so it's not dependent on solution variables.

You could just include a relative path like ../../tools/copyfiles/CopyFiles.dll

Or you could create a Directory.Build.props file in a directory above your projects (probably next to your .sln that defines a property like

<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>

and change the reference to dotnet $(RepoRoot)tools/copyfiles/CopyFiles.dll.

@baiyunchen

This comment has been minimized.

Copy link

@baiyunchen baiyunchen commented Aug 10, 2017

@rainersigwald Looks like a great solution , thank you very much !

ignatandrei added a commit to ignatandrei/InfoValutar that referenced this issue Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.