接獲報案,專案修改無法使用TFS Build Service編譯,錯誤訊息如下:
D:\Works\1\BLAH\src\Web\Scripts\Foo.ts (112): Build: 'let' declarations are only available when targeting ECMAScript 6 and higher.
訊息指向程式使用TypeScript新支援的let語法,只能編譯成ES6,但專案設定輸出ES5。但專案在本機Visual Studio + TypeScript 1.8編譯正常,TFS Build Service使用MSBuild編譯才出錯。
優先懷疑問題出在TFS Build Service主機的TypeScript未更新,依據官方文件,TypeScript 1.8起才支援在ES3及ES5時使用let及const,確認為版本過舊引起:
let and const support
ES6 let and const declarations are now supported when targeting ES3 and ES5.
檢查\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\TypeScript\TypeScript.Tasks.dll為v12.18.50.50301,已是最新版。(註:我們的TFS Build Service使用VS2013 MSBuild,故為v12;若使用VS2015,則為v14)
回頭檢查MSBuild編輯失敗Log,找到線索:
2>PreComputeCompileTypeScript:
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.4\tsc.exe --sourcemap --target ES5 --noEmitOnError COMPUTE_PATHS_ONLY "D:\Works\1\BLAH\src\Web\Scripts\Foo.ts" "D:\Works\1\BLAH\src\Web\Scripts\Bar.ts" "D:\Works\1\…
以此研判,MSBuild仍使用1.4版TypeScript Compiler編譯程式,導致無法在ES5使用let。檢查csproj檔案,發現問題所在:
<ProjectToolsVersion="4.0"DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportProject="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
\TypeScript\Microsoft.TypeScript.Default.props"
Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
\TypeScript\Microsoft.TypeScript.Default.props')"/>
<ImportProject="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<PropertyGroup>
<ConfigurationCondition=" '$(Configuration)' == '' ">Debug</Configuration>
<PlatformCondition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<!-- 省略 -->
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<TargetFrameworkProfile/>
<TypeScriptToolsVersion>1.4</TypeScriptToolsVersion>
<MvcProjectUpgradeChecked>true</MvcProjectUpgradeChecked>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>4.0</OldToolsVersion>
</PropertyGroup>
其中<TypeScriptToolsVersion>1.4</TypeScriptToolsVersion>不知何故還停在1.4未改成1.8,但顯然在Visual Studio編譯時會忽略此設定,仍以1.8編譯。
手動修改csproj將版號改為1.8,問題排除,收隊。
學到三件事:
- MSBuild的TypeScript Task設定放在 \Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\TypeScript\
- TypeScript Compiler(tsc.exe)則在\Program Files (x86)\Microsoft SDKs\TypeScript\1.x
- MSBuild時會依csproj裡的<TypeScriptToolsVersion>決定tsc.exe版本,與Visual Studio編譯使用版本不一定相同