TIL: The order of <Import> elements in C# projects is important.

Patrick Dinh
2 min readOct 1, 2018

Occasionally after install a new nuget package I see an <Import> element is added to my C# project file. They are for sharing common tasks between projects. For example, installing System.Data.SQLite.Core adds an <import> element pointing to System.Data.SQLite.Core.targets. The targets file describes a task to copy native SQLite assemblies to the output folder when the project is built. Without them, System.Data.SQLite.Core won’t function properly.

My knowledge about <Import> elements stopped there until a recent encounter. I tried to get OctoPack to create a nuget package for my project which should include GDAL native assemblies. However, despite my efforts, OctoPack kept complaining that it couldn’t find GDAL native assemblies.

Unhappy msbuild

Later I found an article on Microsoft docs. It says that the import elements are executed in the order that they are saved in C# project files.

In my case, I installed GDAL after OctoPack, therefore, GDAL’s <import> was placed after OctoPack’s <import>. As a results, GDAL assemblies were copied to the output directory AFTER OctoPack ran. That explains why OctoPack couldn’t find them.

Import elements in C# project are in the wrong order

The solution for all my pain was simple. I just needed to swap their positions and the tasks are run in the right order again. TADA, happy msbuild.

--

--