ivogabe / gulp-typescript

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

tsconfig's rootDirs overridden by synthesized rootDir in compiler.ts #679

Open joeyparrish opened 1 year ago

joeyparrish commented 1 year ago

Expected behavior: If I use rootDirs (plural) and not rootDir (singular) in tsconfig.json, only rootDirs should be passed to the compiler.

Actual behavior: compiler.ts synthesizes a value for rootDir, which is then passed to the compiler. This overrides rootDirs and causes my project to fail with gulp-typescript, even though it builds with tsc.

Your gulpfile:

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

import gulp from 'gulp';
import ts from 'gulp-typescript';

const mainProject = ts.createProject('tsconfig.json');

function buildTypeScriptProject(project) {
  const projectSources = project.src();
  const projectBuilder = project();
  const outDir = project.options.outDir;
  const buildResults = projectSources.pipe(projectBuilder);
  return buildResults.js.pipe(gulp.dest(outDir));
}

// Omitting the rather complicated generate-sources task...

gulp.task('build', gulp.series('generate-sources', () => {
  return buildTypeScriptProject(mainProject);
}));

tsconfig.json

Include your tsconfig, if related to this issue.

{
  // Use these TypeScript settings as defaults:
  "extends": "./tsconfig-base.json",

  "compilerOptions": {
    // output JS to dist/
    "outDir": "dist",

    // Some generated modules should be found in generated/ as well.
    // Treat src/ and generated/ as equivalent during module resolution.
    "rootDirs": ["src/", "generated/"],
  },
  "include": [
    "src/**/*.ts",
  ],
}

Code

Include your TypeScript code, if necessary. Not strictly necessary, given:

I have a simple fix in mind that works for me locally. In compiler.ts, rootDir is created on this.project.options, when it should instead be created on this. It should fall back through options.rootDir, options.rootDirs[0], and then finally the computed version that exists today. With this change made locally, everything works correctly. Either rootDir or rootDirs[0] can be used to determine the locations of things, but the config from tsconfig.json won't be modified inappropriately before being passed to the compiler.

I will send a PR shortly.

joeyparrish commented 1 year ago

This fails with TypeScript 4.9.4, but doesn't fail with the 3.6 or 3.7 versions that you test with in gulp-typescript.

joeyparrish commented 1 year ago

It looks like you haven't updated typescript/dev in 3 years, but a regression test for this will not be effective without testing against TypeScript v4.

joeyparrish commented 1 year ago

The minimum version I can find that fails in this case is v4.3.x.