ivogabe / gulp-typescript

A TypeScript compiler for gulp with incremental compilation support.
MIT License
838 stars 130 forks source link

gulp-typescript fail at windows. #322

Closed gintsgints closed 8 years ago

gintsgints commented 8 years ago

Expected behavior: gulp task finishes.

Actual behavior: while compile I get message: angular2-starter\node_modules\gulp-typescript\release\main.js:71 throw new Error('gulp-typescript: A project cannot be used in two compilations at the same time. Create multiple projects with createProject instead.'); ^

Error: gulp-typescript: A project cannot be used in two compilations at the same time. Create multiple projects with createProject instead.

Your gulpfile:

Include your gulpfile, or only the related task (with ts.createProject).

var gulp = require('gulp');
var requireDir = require('require-dir');
var tasks = requireDir('./tasks');

var config = require('./gulp.config')();

/* Default task */
gulp.task('default', ['serve-dev']);

tsconfig.json

Include your tsconfig, if related to this issue.

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

Code

Include your TypeScript code, if necessary.

import {Component} from 'angular2/core';
import {RouterOutlet, RouteConfig, RouteDefinition} from 'angular2/router';
import {APP_ROUTES} from './app.routes';
import {NavbarComponent} from './navbar/navbar.component';
import {LoggerService} from './blocks/logger.service';

@Component({
    selector: 'main-app',
    templateUrl: 'app/app.html',
    directives: [RouterOutlet, NavbarComponent]
})
@RouteConfig(APP_ROUTES)
export class AppComponent {
    public appRoutes: RouteDefinition[];
    private logger: LoggerService;

    constructor(logger: LoggerService) {
        this.logger = logger;
        this.appRoutes = APP_ROUTES;
    }
}
ivogabe commented 8 years ago

As the error message says, you cannot use the same project in multiple compilations that run at the same time. You're using the same project (tsProject) in the tasks tsc, tsc-app, tsc-unit and tsc-e2e. The error is shown when two of these tasks are running at the same time. You should create multiple projects for these tasks. The reason is that a project handles incremental compilation, and incremental compilation requires data from the previous compilation and when the previous compilation is not finished that obviously doesn't work.

You could also compile all sources in a single compilation. In most cases, it isn't worth to split the project into several compilations as a single compilation would be faster.

ivogabe commented 8 years ago

Also, this isn't only happening on Windows, but also on other platforms.

gintsgints commented 8 years ago

Thank you for explanation. Sounds like problem with angular2-starter.

smac89 commented 7 years ago

This was not only happening on windows, also on Linux. I am on Ubuntu 16.04 and I was experiencing this due to my watch tasks that sometimes occur in quick succession. For example, if I modify and save two files at the same time, this triggers two calls to the watch function which starts to execute each rebuild at once. The result is that pipe line gets congested when it reaches the compilation stage and the first compilation throws the error.

The solution I settled on is a wrapper queue around the project. https://gist.github.com/smac89/49f3b076cd987e0875ba9bfb3fe81ef9

Vitaminaq commented 3 years ago

const ts = require('gulp-typescript'); const tsProject = () => ts.createProject('tsconfig.json')();

return gulp .src(['*/[!.d].ts'], { cwd: Paths.src }) .pipe( alias({ paths: { '@': path.resolve(__dirname, '../src'), }, }) ) .pipe(tsProject()) .js.pipe(gulp.dest(Paths.dest),);