【问题标题】:TypeScript not building with Azure Hosted AgentTypeScript 未使用 Azure 托管代理构建
【发布时间】:2021-03-15 08:50:31
【问题描述】:

我有一个用 .Net5 编写的 ASP.NET MVC 应用程序。

这使用打字稿文件,我有 NuGet 包对以下内容的引用:

<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.0.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />

在我的项目中,我有:

  • 名为 node-modules 的文件夹(未推送到 Azure Devops)
  • bundleconfig.json
  • gulpfile.js
  • libman.json
  • package.json (+ package-lock.json)

package.json 是:

{
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "devDependencies": {
    "gulp": "4.0.2",
    "del": "5.1.0",
    "@types/bootstrap": "4.5.0",
    "@types/google.visualization": "0.0.53",
    "@types/jquery": "3.5.1",
    "@types/jqueryui": "1.12.13",
    "@types/jquery.datatables": "1.10.36",
    "@types/jquery.ui.datetimepicker": "0.3.29"
  }
}

当我在 Visual Studio 2019(最新)中编译时,它完美编译,一切正常。

但我确实进入了构建输出(信息):

项目打开时的包还原已禁用。更改 npm 包 项目属性中的管理设置以启用项目恢复 打开。

我找到了“项目保存时恢复”设置并将其更改为true。这将以下内容添加到我的 csproj 文件中:

<ProjectExtensions><VisualStudio><UserProperties appsettings_1qaf_1json__JsonSchema="https://gitpod.io/schemas/gitpod-schema.json" NpmRestoreOnProjectOpen="True" /></VisualStudio></ProjectExtensions>

我将它推送到 Azure DevOps,但是当代理构建它时(为了清楚起见,删除了一些步骤):

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:

- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true

我收到以下错误:

##[错误]MyApp\Scripts\MyTypeScript.ts(13,9):错误 TS2581:构建:找不到名称“$”。是否需要安装类型 jQuery 的定义?试试npm i @types/jquery

因此,托管代理显然没有检索类型。我猜我需要调整我的 YAML 文件,但不确定如何...

【问题讨论】:

  • 嗨@DrGriff。这张票有什么更新吗?如果答案能给你一些帮助,请随时告诉我。只是提醒this

标签: typescript npm azure-devops typescript-typings azure-pipelines-build-task


【解决方案1】:

根据错误信息和 Yaml 定义,您似乎缺少 NPM Install 安装所需 npm 包的步骤。

VSBuild任务本身只能恢复nuget包,不能安装npm包,所以需要额外添加任务来安装npm包。

这是我的示例:您可以尝试在 VSBuild 任务之前添加 Npm Install task

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:

- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'

- task: Npm@1
  displayName: 'npm install'
  inputs:
    workingDir: '$(build.sourcesdirectory)'
    verbose: false


- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true

结果:

它可以正常工作。

【讨论】:

  • 为我解决了。仅供参考,package.json 文件与我的 *.csproj 文件位于同一路径中,因此我只需将上面的答案修改为 workingDir: 'MyApplicationName'
猜你喜欢
  • 2020-01-01
  • 2020-06-08
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 2020-03-27
相关资源
最近更新 更多