1

Background:

We have several ASP.NET projects with common Core. Static from Core is copied to all other projects. We've added TypeScript to all our projects.

Here is how TypeScript build looks in csproj:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props" />

When TypeScript files are compiled, all references files are compiled too. Some TypeScript files references files from Core project. So, files from Core project are sometimes compiled many times (if several files from other project references them).

Simple example:

Core.csproj
  -> Common.ts
A.csproj
  -> ScriptA.ts
B.csproj
  -> ScriptB.ts

ScriptA.ts:

/// <reference path="../Core/Common.ts" />
...

ScriptB.ts:

/// <reference path="../Core/Common.ts" />
...

Build of A or B project causes Common.ts from Core to be built also.

Problem:

It's not a problem that some files are built several times. BUT - if we building projects in parallel (and this is default VS behaviour!) - sometimes build crashed with exception:

[VsTsc] VSTSC error TS5033: Build: Could not write file '...'

Reason is that two or more projects tries to build TypeScript files and tries to build referenced files from some common project. One project starts building ts file to js file and locks js file. Other project tries to lock the same file and crashes.

So, the question is - how to avoid such parallel builds/locks? Referenced project must be compiled already, so may be say TypeScript compiler not to build files from other projects somehow?

VorobeY1326
  • 782
  • 10
  • 25

2 Answers2

0

I handle this by shipping shared libraries as a NuGet package. Not only does this make your builds independent, it lets you control your dependencies (i.e. you can upgrade when you decide to, not just because someone edited a shared file).

Fenton
  • 241,084
  • 71
  • 387
  • 401
0

I suggest you treat project Common as a library, not as a collection of individual files.

When you refer to to Common using line /// <reference path="../Core/Common.ts" />, this pulls in Common.ts -- and possibly files it references -- into projects A and B, thus duplicating it, and making it compiled several times.

Instead, what you need is

/// <reference path="../Core/Common.d.ts" />

That is, you only use declarations from the project Common. The declarations are not built by default, you should check option "Generate declaration files" on TypeScript page of project configuration, or if you prefer manually adding line

<TypeScriptGeneratesDeclarations>True</TypeScriptGeneratesDeclarations>

in your .csproj file. A small downside is that you have to load more than one .js file into your page or node.js project. However it is small price to pay for ability to compose your code into modules. For example, one day you might want to load both A.js and B.js into the same page, and you would have had a mess, because copies of common.ts would clash and override each other. Referencing by type declarations solves this problem, along with your specific problem of build breaks.

seva titov
  • 11,720
  • 2
  • 35
  • 54