✅ [SOLVED] MSBuild Task says file does not exist

I've made a custom MSBuild task to zip up the newly created .dll along with relevant files for release whenever the project is built. For some reason though, it says the newly created .dll does not exist... When the log literally shows its existence in the exact location like two line up:
CopyFilesToOutputDirectory:
Copying file from "/home/newmaz/Documents/HFF_ObjectGrabber/build/obj/ObjectGrabber/Debug/Debug/ObjectGrabber.dll" to "/home/newmaz/Documents/HFF_ObjectGrabber/build/bin/output/ObjectGrabber.dll".
ObjectGrabber -> /home/newmaz/Documents/HFF_ObjectGrabber/build/bin/output/ObjectGrabber.dll
ZipOutput:
Zipping build
File copied: /home/newmaz/Documents/HFF_ObjectGrabber/src/../README.md
File copied: /home/newmaz/Documents/HFF_ObjectGrabber/src/../icon.png
File copied: /home/newmaz/Documents/HFF_ObjectGrabber/src/../manifest.json
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error : File does not exist: /home/newmaz/Documents/HFF_ObjectGrabber/src/../build/bin/output/ObjectGrabber.dll
CopyFilesToOutputDirectory:
Copying file from "/home/newmaz/Documents/HFF_ObjectGrabber/build/obj/ObjectGrabber/Debug/Debug/ObjectGrabber.dll" to "/home/newmaz/Documents/HFF_ObjectGrabber/build/bin/output/ObjectGrabber.dll".
ObjectGrabber -> /home/newmaz/Documents/HFF_ObjectGrabber/build/bin/output/ObjectGrabber.dll
ZipOutput:
Zipping build
File copied: /home/newmaz/Documents/HFF_ObjectGrabber/src/../README.md
File copied: /home/newmaz/Documents/HFF_ObjectGrabber/src/../icon.png
File copied: /home/newmaz/Documents/HFF_ObjectGrabber/src/../manifest.json
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error : File does not exist: /home/newmaz/Documents/HFF_ObjectGrabber/src/../build/bin/output/ObjectGrabber.dll
Like, it just copied the file into the exact location it's complaining about Here's the relevant snippet of the ZipTask:
foreach (ITaskItem input_file in Files)
{
string file_string = input_file.ItemSpec;
if (File.Exists(file_string))
{
string file_to_zip = Path.Combine(directory_to_zip, Path.GetFileName(file_string));
File.Copy(file_string, file_to_zip, true);
Log.LogMessage("File copied: " + file_string);
}
else
{
Log.LogError("File does not exist: " + file_string);
return false;
}
}
foreach (ITaskItem input_file in Files)
{
string file_string = input_file.ItemSpec;
if (File.Exists(file_string))
{
string file_to_zip = Path.Combine(directory_to_zip, Path.GetFileName(file_string));
File.Copy(file_string, file_to_zip, true);
Log.LogMessage("File copied: " + file_string);
}
else
{
Log.LogError("File does not exist: " + file_string);
return false;
}
}
And here's the part of my project's .csproj that calls it:
<UsingTask TaskName="ObjectGrabberBuildTasks.ZipTask"
AssemblyFile="$(RootDir)\tasks\build\bin\tasks\ZipTask.dll"/>

<Target Name="ZipOutput"
AfterTargets="CopyFilesToOutputDirectory">

<ItemGroup>
<ZipFiles Include="$(RootDir)\README.md" />
<ZipFiles Include="$(RootDir)\icon.png" />
<ZipFiles Include="$(RootDir)\manifest.json" />
<ZipFiles Include="$(OutputPath)$(AssemblyName).dll" />
</ItemGroup>

<Message Importance="high" Text="Zipping build" />

<ZipTask Files="@(ZipFiles)"
OutputName="GrabCountTracker"
OutputLocation="$(SharedBuildDir)"
/>
</Target>
<UsingTask TaskName="ObjectGrabberBuildTasks.ZipTask"
AssemblyFile="$(RootDir)\tasks\build\bin\tasks\ZipTask.dll"/>

<Target Name="ZipOutput"
AfterTargets="CopyFilesToOutputDirectory">

<ItemGroup>
<ZipFiles Include="$(RootDir)\README.md" />
<ZipFiles Include="$(RootDir)\icon.png" />
<ZipFiles Include="$(RootDir)\manifest.json" />
<ZipFiles Include="$(OutputPath)$(AssemblyName).dll" />
</ItemGroup>

<Message Importance="high" Text="Zipping build" />

<ZipTask Files="@(ZipFiles)"
OutputName="GrabCountTracker"
OutputLocation="$(SharedBuildDir)"
/>
</Target>
Yes, I'm aware that there's no \ between the $(OutputPath) and $(AssemblyName), the $(OutputPath) property has the \ included, and you can tell in the error message that it's successfully adding a / between the directory and file name. Any thoughts?
18 Replies
ero
ero2d ago
is there a particular reason you created a custom task for this?
乙ᗰᗩᑎ350᙭
as opposed to just calling exec on a zip command, I'm assuming?
ero
ero2d ago
instead of using the already existing ZipDirectory
乙ᗰᗩᑎ350᙭
I was not aware that that task existed. I suppose I could achieve this by combining that task with the Copy and Delete tasks, although I do feel my approach is cleaner ¯\_(ツ)_/¯ Would that fix the issue?
ero
ero2d ago
as clean as having to maintain another library can be ;)
乙ᗰᗩᑎ350᙭
and/or do you know why File.Exists() fails in this case? fair enough lol
ero
ero2d ago
i would assume your target runs before the actual build so there simply is no built dll yet
乙ᗰᗩᑎ350᙭
Look at the initial error message, it runs the task after the copy step putting AfterTargets="Build" also fails and if that is the case, using the ZipDirectory task wouldn't solve anything
sibber
sibber2d ago
btw $fileexists
ero
ero2d ago
yeah msbuild does this quite differently https://github.com/dotnet/msbuild/blob/main/src/Tasks/ZipDirectory.cs they always fall back to a try catch
乙ᗰᗩᑎ350᙭
apparently will throw it in the task rq I take back that "apparently"
ZipOutput:
Zipping build
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: The "ZipTask" task failed unexpectedly.
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: System.NotImplementedException: The method or operation is not implemented.
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at System.Diagnostics.Debugger.Launch () [0x00000] in <d636f104d58046fd9b195699bcb1a744>:0
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at ObjectGrabberBuildTasks.ZipTask.Execute () [0x00001] in <b1013ce523be46928187f3751e5ec03c>:0
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00029] in <3fa5db086dc941c1a57d0f5b812004f0>:0
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask (Microsoft.Build.BackEnd.ITaskExecutionHost taskExecutionHost, Microsoft.Build.BackEnd.Logging.TaskLoggingContext taskLoggingContext, Microsoft.Build.BackEnd.TaskHost taskHost, Microsoft.Build.BackEnd.ItemBucket bucket, Microsoft.Build.BackEnd.TaskExecutionMode howToExecuteTask) [0x002b9] in <3fa5db086dc941c1a57d0f5b812004f0>:0
Done Building Project "/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj" (default targets) -- FAILED.
Done Building Project "/home/newmaz/Documents/HFF_ObjectGrabber/ObjectGrabber.sln" (default targets) -- FAILED.
ZipOutput:
Zipping build
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: The "ZipTask" task failed unexpectedly.
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: System.NotImplementedException: The method or operation is not implemented.
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at System.Diagnostics.Debugger.Launch () [0x00000] in <d636f104d58046fd9b195699bcb1a744>:0
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at ObjectGrabberBuildTasks.ZipTask.Execute () [0x00001] in <b1013ce523be46928187f3751e5ec03c>:0
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00029] in <3fa5db086dc941c1a57d0f5b812004f0>:0
/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj(34,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask (Microsoft.Build.BackEnd.ITaskExecutionHost taskExecutionHost, Microsoft.Build.BackEnd.Logging.TaskLoggingContext taskLoggingContext, Microsoft.Build.BackEnd.TaskHost taskHost, Microsoft.Build.BackEnd.ItemBucket bucket, Microsoft.Build.BackEnd.TaskExecutionMode howToExecuteTask) [0x002b9] in <3fa5db086dc941c1a57d0f5b812004f0>:0
Done Building Project "/home/newmaz/Documents/HFF_ObjectGrabber/src/ObjectGrabber.csproj" (default targets) -- FAILED.
Done Building Project "/home/newmaz/Documents/HFF_ObjectGrabber/ObjectGrabber.sln" (default targets) -- FAILED.
actually, that may be because I'm not running this in VS regaredless, that sucks I can always try passing in a hard-coded absolute path
<ZipFiles Include="/home/newmaz/Documents/HFF_ObjectGrabber/build/bin/output/ObjectGrabber.dll" />
<ZipFiles Include="/home/newmaz/Documents/HFF_ObjectGrabber/build/bin/output/ObjectGrabber.dll" />
nope, same error will do a try/catch
ero
ero2d ago
won't really change anything i'd think what even is that output dir lol
乙ᗰᗩᑎ350᙭
?
ero
ero2d ago
well it's just that it's really unusual and there's not ever really a reason to mess with the output dir
乙ᗰᗩᑎ350᙭
ah my other project has a lot of funky stuff going on w/ manipulating the build output (hence the changes), and I just copied the directory props/targets from it when I made this project OH I'M A FUCKING DUMBASS :blobfacepalm: So... fun fact It turns out that if you don't accidently delete the file in the task before you go to copy it... It will still exist 😐
ero
ero2d ago
$close
MODiX
MODiX2d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?