Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`visual studio 2017 add as link content files (aspnetcore)

Since VS2017 returned to csproj, I am again able to add files "as link"... NOT!

When I link a file I also want it to be copied to the solution folder (not output directory, just solution), for example js files, webpack config files etc, I have them shared and "linked". But in order for these to be really "seen" by visual studio, I have to copy them to the solutions. I do this in a BeforeTargets="Build" event in the csProj file. That is how I did it before the project.json change (during the project.json period I just didnt do it at all).

But... what i find now it that when I re-open the solution, it says "linked file duplicated" and does not load the file. This is correct, since the file was copied during build. This USED TO WORK because previous web application csproj was declaratively listing all the filed in the project. Now it looks the directory and just add whatever's in there. Any idea how to make this work again?

Sorry if it is not clear I can develop further but S.O wants me to keep questions short

thanks!

UPDATE: I've reinstalled VS 2017. Reproduced the case. Same error. I am now going to give you guys more detail: 1. I added a linked file and csproj did this:

  <ItemGroup>
    <None Include="..\..\..\..\Proyects\SomeProject\src\core.module.ts" Link="app\core\core.module.ts" />
  </ItemGroup>
  1. It shows in solution explorer:

enter image description here

  1. I modify csproj to physically copy it to target folder. This resource is NOT a cs file but IT IS compiled. I need vs to be able to physically have it:

    < Target Name="CopyLinkedContentFiles" BeforeTargets="Build"> < Copy SourceFiles="%(Content.Identity)" DestinationFiles="%(Content.Link)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Condition="'%(Content.Link)' != ''" /> < /Target>

  2. Reload solution: "duplicated linked item".

A few more remarks: a. This happens on css files b. With ts files... IT DOES NOT EVEN COPY THEM. Just ignores the copy completelly

like image 833
user5328504 Avatar asked Feb 05 '23 21:02

user5328504


1 Answers

Ok I pulled it off, but it is not nice.

First of all, lets understand what the problem was. 1. Add CONTENT (js, ts, css, images) files "as link" 2. Files need to be physically there. Why? In a classic web, you need those resources in the output directory, cause they're going to be needed only when running the app. Since mines is a webapi + angular app, I need to actually work a lot with these files and also they have to be available to my IDE physically. First of all, webpack needs to compile them, I have less and sass files that are also compiled. I also do chrome debugging... files need to be there... so you make a msbuild task to move files from their source location to your destination. Files must be copied again (if chenged) from their source. This is a standard practice, not a hack of myself 3. On reload, visual studio sees the links and tryes to (virtually) add them to project structure .Now here's the problem... files are already there, cause you forced them to be there in point 2.

The reason why this works in 2015 is because csproj in 2015 does not inspect the folder structure to list its content. Files should be declarated in csproj in order to be in the project structure. Now 2017 uses a mix of csproj and the old "website" project, which would list the directory contents. Since vs 2017 "sees" the files, then it gets confussed when trying to add them...

So my work around was to change my csproj in this way:

  1. Exclude the linked files <-- this is what changed from vs2015
  2. Add the linked filed
  3. Copy the linked files

so it looks like this: REMOVE

 <ItemGroup>
    <None Remove="app\core\somefile.ts" />
  </ItemGroup>

ADD.

  <ItemGroup>
    <None Include="..\..\..\..\Projects\Core\somefile.ts">
      <Link>app\core\somefile.ts</Link>
    </None>

COPY ON BUILD

  <Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
    <Copy SourceFiles="%(None.Identity)" DestinationFiles="%(None.Link)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" ContinueOnError="true" Condition="'%(None.Link)' != ''" />
  </Target>

I would also like to mention that I improved it a bit to be recursive so I dont end up with all references in there, so my point 2 actually looks like this:

<None Include="..\..\..\..\Projects\Core\**\*.*">
  <Link>app\Core\%(RecursiveDir)%(FileName)%(Extension)</Link>
</None>

After many days of fighting with this I must say I am not 100% happy with the journey of csproj 2015 to xproj 2017 to csproj 2017. I think it was done with a good harth but they screwed a LOT of programmers with the way it was handled. I still love it, but it made me (and many others) suffer a lot and waste many many days in simmilar matters. This issue I faced was just one among many in a sea of documentation that goes obsolete, project name changes, and such.

like image 93
user5328504 Avatar answered Mar 08 '23 15:03

user5328504