【问题标题】:Gulp task failed with error: MSBuild failed with code 1! in Visual Studio 2019Gulp 任务失败并出现错误:MSBuild 失败,代码为 1!在 Visual Studio 2019 中
【发布时间】:2021-12-18 10:29:32
【问题描述】:

我知道关于 SO 的问题很少,但要么没有答案,要么解决方案对我没有帮助。

此 gulp 任务在具有 Visual Studio 2017 的其他机器上成功执行,但在 Visual Studio 2019 上却没有。

当我运行“构建解决方案”任务时,它失败并出现以下错误

[11:33:58] 开始“构建解决方案”...

MSBUILD:错误 MSB1001:

未知开关。开关:/restore 对于开关语法,键入“MSBuild

/help" [11:33:59] MSBuild 失败,代码为 1![11:33:59]

“构建解决方案”在 1.04 秒后出错 [11:33:59] 错误:MSBuild 失败 使用代码 1!

at ChildProcess.<anonymous> (C:\Projects\MyProj\node_modules\gulp-msbuild\lib\msbuild-runner.js:66:25)

at ChildProcess.emit (events.js:314:20)

at ChildProcess.EventEmitter.emit (domain.js:506:15)

at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)

at Process.callbackTrampoline (internal/async_hooks.js:126:14) Process terminated with code 1.

【问题讨论】:

  • 看来Unknown switch. Switch: /restore是关键信息,问题应该与/restore有关。我没有看到完整的命令/任务,但是这个 Switch 是否正确编写并匹配格式/规则?也许/restore 应该替换为-restore/-r? (MSBuild CLI)
  • @Tianyu 这适用于 VS2017,所以我相信它是正确编写的。和VS2019有关系吗?
  • 也许VS 2019有一些调整,包括一些新的严格规则。对 Gulp 任务不是很熟悉,但 MSBuild 文档确实显示使用 --restore 语法。

标签: visual-studio msbuild gulp visual-studio-2019 sitecore


【解决方案1】:

所以这取决于您使用的构建脚本。 Sitecore Habitat 使用的默认 gulp 脚本正在使用这个 gulp 模块: https://www.npmjs.com/package/gulp-msbuild

而且,你正在使用这个模块来构建这样的任务

gulp.task("default", function() {
return gulp.src("./project.sln")
    .pipe(msbuild({
        targets: ['Clean', 'Build'],
        toolsVersion: 3.5
        })
    );

});

如果您正在使用这个,我猜您正在尝试在您的任务中使用 msbuild 开关,例如:

gulp.task("default", function() {
return gulp.src("./project.sln")
    .pipe(msbuild({
        ...,
        restore:true
        ...
        })
    );

});

因为是的,Msbuild 提供了这样的开关,你可以找到here,但如果我没记错的话,gulp-msbuild 插件默认尝试使用 vs2017。 从msbuild-finder.js可以看到它正在vs2017目录中寻找msbuild工具:

  function detectMsBuild15Dir(pathRoot) {

  const vs2017Path = path.join(pathRoot, "Microsoft Visual Studio", "2017");
  const possibleFolders = [ "BuildTools", "Enterprise", "Professional", "Community" ];

  for (let index = 0; index < possibleFolders.length; index++) {
    try {
      const folderPath = path.join(vs2017Path, possibleFolders[index]);
      fs.statSync(folderPath);
      return folderPath;
    } catch (e) {
    }
  }
}

但是,如果你指定版本路径,你应该是好的(来自msbuild-command-builder.js):

  if (!options.msbuildPath) {
    const msbuildFinder = require("./msbuild-finder");
    options.msbuildPath = msbuildFinder.find(options);
  }

我相信,安装 Visual Studio 2019 后,您应该使用 buildToolsVersion 15.0。见this thread

另一方面,您可能想改用msbuild plugin。我也有an article 涵盖从 gulp3.9 到 gulp4 的切换,其中包含一些 msbuild 示例。您可能会发现它相关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2022-01-11
    相关资源
    最近更新 更多