使用gulp-typescript在VS2015中编译TypeScript时无法显示错误信息

7

这是之前工作正常的,所以我不确定自那时起发生了什么变化(除了更新到 ASP.NET Core RC2 并安装一些针对 VS2015 的扩展,如我所记得的)。

问题在于,当从 VS2015 运行 Gulp 任务来编译我的 TypeScript 文件时,如果出现错误,则会显示例如:

[10:02:54] Compiling typescript into javascript
[10:02:56] TypeScript: 1 semantic error
[10:02:56] TypeScript: emit succeeded (with errors)
[10:02:56] Finished 'compile' after 2.47 s
Process terminated with code 0.

没有任何错误描述。

在CMD中:

$ tsc -v
Version 1.8.10

在VS2015套件管理器控制台中:
PM> tsc -v
Version 1.8.10

我认为VS2015至少使用了路径中相同的TypeScript编译器,这不应该是一个问题。另外,这是最新版本,但我也尝试过1.7版本,结果仍然一样。

我的gulp任务:

gulp.task('compile', function () {
    log('Compiling typescript into javascript');
    return gulp
            .src(config.allts)
            .pipe($.sourcemaps.init())
            .pipe($.typescript({
                noImplicitAny: true,
                target: 'ES5'
            }))
            .pipe($.sourcemaps.write('.'))
            .pipe(gulp.dest(config.compileFolder));
});

我正在使用:

"gulp-typescript": "2.10.0"

虽然我已经尝试使用最新的版本:
"gulp-typescript": "2.13.4"

没有成功。

据我所知,由于我使用了gulp-typescript并且已经在gulp任务中传递了编译器选项,因此我不需要在项目根目录下创建tsconfig.json文件。因此,我已删除了之前的tsconfig.json文件,因为似乎没有用到。

如果我从gulp任务中删除所有的编译器选项:

gulp.task('compile', function () {
    log('Compiling typescript into javascript');
    return gulp
            .src(config.allts)
            .pipe($.sourcemaps.init())
            .pipe($.typescript({
                //removed
            }))
            .pipe($.sourcemaps.write('.'))
            .pipe(gulp.dest(config.compileFolder));
});

我即使没有描述也会遇到更多的语义错误。

[10:12:57] Compiling typescript into javascript
[10:13:00] TypeScript: 184 semantic errors
[10:13:00] TypeScript: emit succeeded (with errors)
[10:13:01] Finished 'compile' after 3.83 s
Process terminated with code 0.

所以这些选项确实被使用。

如果我在CMD中进入我有TypeScript的文件夹,尝试用以下命令编译它:

C:/>Sample/app> tsc mytestfile.ts

我可以正确查看所有typescript编译错误。

您认为我的VS2015或gulp-typescript有什么问题吗?

更新:我已经尝试使用gulp-tsc而不是gulp-typescript,它运行良好。所以问题必须出在gulp-typescript上。

gulp.task('compile', function () {
    log('Compiling typescript into javascript');
    return gulp
            .src(config.allts)
            .pipe($.sourcemaps.init())
            .pipe($.tsc({
                noImplicitAny: true,
                target: 'ES5'
            }))
            .pipe($.sourcemaps.write('.'))
            .pipe(gulp.dest(config.compileFolder));
});

我在我的队友的机器上使用VS2015 + gulp-typecript时遇到了相同的问题,他的终端显示出了神秘的“N个语义错误”。项目配置是相同的,但在我的终端上,我可以看到确切的错误描述。 - Cubius
我的当前解决方法是切换到VS“错误列表”窗口中的“构建+智能感知”,并使用那里的信息来修复错误。 - user764754
2个回答

4

我至少找到了部分答案。如果你从nodejs命令提示符中运行gulp-typescript,它会显示错误。但是,gulp-typescript打印错误消息的方式在Visual Studio任务运行器中没有显示。

如果我更改用于typescript的报告者,则可以正常显示错误(将此函数添加到您的gulpfile.js中)。

function visualStudioReporter() {
    return {
        error: function (error) {
            //This works
            gutil.log("Typescript: error", error.message);
            //This isn't shown
            console.error(error.message);
        },
        finish: ts.reporter.defaultReporter().finish
    };
}

现在您可以像这样使用报告器。
var ts = require("gulp-typescript")

gulp.task("compile", function() {
    gulp.src("./**/*.ts")
       .pipe(ts(tsProject, undefined, visualStudioReporter()))
});

可能很明显,但是 ICYMI var gutil = require('gulp-util'); 是指 https://www.npmjs.com/package/gulp-util。非常棒的解决方案,谢谢! - Lee Richardson

3
如果您已经安装了Microsoft .Net Core 1.0.0 RC2 Tooling Preview 1,那么似乎存在一个问题:安装工具预览版1后,TypeScript错误没有显示#526 在发布 .Net Core 1 / Tooling Preview 2版本之后更新 更新/安装 .Net Core 1.0的发布版本,该版本将工具更新为Preview 2,可解决此问题。

enter image description here

在此之前,卸载工具预览1并重新安装vs 2015的Web开发工具将解决未显示错误详细信息的问题。 我也遇到了同样的问题。由于我没有使用 .Net Core 1RC2预览版的功能,因此我能够通过GitHub上报告中提到的解决方法来解决不显示typescript错误的问题:
  1. 卸载'Microsoft .Net Core 1.0.0 RC2 - VS 2015 Tooling Preview 1'
  2. enter image description here
  3. 在添加/删除Visual Studio中重新安装Visual Studio 2015的Web开发工具,修改。 enter image description here
这样做之后,我可以再次在任务运行器资源管理器中看到typescript错误消息。 enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接