microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.71k stars 12.45k forks source link

Microsoft.TypeScript.MSBuild 5.1.x builds fail in Visual Studio 2019 due to always using VS's private node.exe v12 #54686

Closed daiplusplus closed 1 year ago

daiplusplus commented 1 year ago

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

Since updating from Microsoft.TypeScript.MSBuild version 4.5.3 to 5.1.3 I got stung by the SyntaxError: Unexpected token '?' error, which is mentioned in the announcement blog post.

The change was in this PR: https://github.com/microsoft/TypeScript/pull/53291

⏯ Playground Link

N/A

💻 Code

N/A

🙁 Actual behavior

  1. Install NodeJS 18+ systemwide (using the official installer).
    • When I open a new cmd prompt and run where node it gives me C:\Program Files\nodejs\node.exe which is v18.16.0 - this happens in both a "normal" cmd prompt as well as the "Developer Command Prompt for VS 2019" version.
  2. Create an ASP.NET Core project in VS2019 v16.11.26 (I'm targeting .NET Core 3.1)
  3. Add the Microsoft.TypeScript.MSBuild NuGet reference - I did it by adding <PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.1.3" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" /> directly to the <ItemGroup> in the ASP.NET Core project .csproj.
  4. Add a tsconfig.json file with "target": "es2022".
  5. Add a TypeScript .ts file with some code in it.
  6. Build the project from within VS (Menu > Build > Build Solution).
  7. This error will appear in the Error List:

    Severity    Code    Description Project Path    File    Line    Source
    Error   MSB6006 "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\NodeJs\node.exe" exited with code 1. MyProject   C:\Users\David\.nuget\packages\microsoft.typescript.msbuild\5.1.3\tools C:\Users\David\.nuget\packages\microsoft.typescript.msbuild\5.1.3\tools\Microsoft.TypeScript.targets    485 Build
    

🙂 Expected behavior

jakebailey commented 1 year ago

@joj

joj commented 1 year ago

This is a known issue, more details (and workaround) here: https://developercommunity.visualstudio.com/t/TypeScript-51-not-working-properly-on-V/10383037

HolgerJeromin commented 1 year ago

Hacking in the registry is the recommend way to support VS2019? This is no ancient version, but got an update 10 days ago.

\packages\Microsoft.TypeScript.MSBuild.5.1.3\tools\Microsoft.TypeScript.targets is from this repo, so IMO it should be possible here to support global installed node without replacing unmaintained node.exe in VS2019 (which would be a good thing neitherless).

daiplusplus commented 1 year ago

Just in case someone from the future needs this workaround after developercommunity.visualstudio.com disappears (remember connect.ms? I remember...), So I've reposted it here:

The issue is that Visual Studio 2019 ships with Node 12, and TypeScript 5.1 is using newer idioms that are supported in Node 14 and up. The workaround is to make Visual Studio use Node 16. To do that:

  1. Install Node 16 from nodejs.org. (We tested Node 16 and Node 14 and both work correctly with Visual Studio 2019) Modify the registry to point to the node you installed.
  2. Add a string value on HKCU\Software\Software\Microsoft\VisualStudio\16.0_xxx\TypeScriptLanguageService, named “CustomNodePath”. By default, the installer will install in c:\Pogram Files\nodejs\node.exe.
  3. After that, VS should be using node 16 and everything should work as before.

For the record, at the time of posting even Node 16 is out-of-date, with the current LTS version being 18.16.1 - and the latest being 20.3.1, so I'm not sure testing only up-to Node 16 was a good idea.

I won't be able to try this out for a while - I'll report back after testing the above reg change with Node 18, and if I encounter any issues I'll try Node 16.

cbries commented 1 year ago

A workaround for your projects can look like this tested with 5.02-beta in Vs19.

  <Target Name="ReTargetNodePath" BeforeTargets="CompileTypeScript">
    <PropertyGroup>
      <NodePath>C:\Program Files\nodejs\node.exe</NodePath>
    </PropertyGroup>
  </Target>

This will set the globally installed node.exe variant for your build.

HolgerJeromin commented 1 year ago

@cbries Or more generic (will cover more msbuild targets): (note that this code needs to be added to your Visual Studio Project file.


<Target Name="ReTargetNodePath" BeforeTargets="$(CompileDependsOn)">
    <PropertyGroup>
        <NodePath>C:\Program Files\nodejs\node.exe</NodePath>
    </PropertyGroup>
</Target>
ga5tan commented 1 year ago

@HolgerJeromin worked for me, except had to remove node.exe from the NodePath:

C:\Program Files\nodejs\