Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gulp-typescript doesn't resolve @types in outter node_modules #563

Closed
FunkMonkey opened this issue Feb 17, 2018 · 12 comments · Fixed by #610
Closed

gulp-typescript doesn't resolve @types in outter node_modules #563

FunkMonkey opened this issue Feb 17, 2018 · 12 comments · Fixed by #610

Comments

@FunkMonkey
Copy link

Expected behavior:
With a project structure like this, which uses Yarn workspaces

- workspace
  - node_modules
    - @types
      - node
  - packages
    - package-to-compile
      - tsconfig.json

there should not be compiler errors like error TS2304: Cannot find name 'require'. or error TS2688: Cannot find type definition file for 'node'. (after adding node to types in tsconfig.json.

Actual behavior:

Getting error TS2304: Cannot find name 'require'.

Your gulpfile:

const gulp = require( "gulp" );
const typescript = require( "gulp-typescript" );

const SRC_GLOB =  "./src/**/*.ts";

var tsProject = typescript.createProject( 'tsconfig.json' );

gulp.task( "build:scripts", function() {
  return gulp.src( SRC_GLOB )
    .pipe( tsProject() )
    .pipe( gulp.dest( "build" ) );
});

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "target": "es2015"
    },
}

Code

import { Observable } from 'rxjs';
import { IPluginLoadInfo } from './iplugininfo';

export default {
  require( { id }: IPluginLoadInfo ) {
    const mod = require( id );
    return Observable.of( { id, factory: mod.default || mod } );
  }
};

Just using tsc works flawlessly, so it must be something inside gulp-typescript!

@FunkMonkey
Copy link
Author

Ups. Just saw #515 and the solution was just to do gulp.src( SRC_GLOB, { cwd: 'src' } ).

Thanks anyway!

@FunkMonkey
Copy link
Author

Nope, didn't work after all (just didn't do anything in the end because the SRC_GLOB was wrong then of course). Could it be related to #422 ?

@FunkMonkey FunkMonkey reopened this Feb 17, 2018
@ivogabe
Copy link
Owner

ivogabe commented Feb 26, 2018

I haven't used yarn workspaces myself, so I'm not familiar with those. Could you create a basic example that illustrates the problem, which I can clone and install to see the bug?

@ivogabe
Copy link
Owner

ivogabe commented Sep 11, 2018

Is this still an issue?

@FunkMonkey
Copy link
Author

FunkMonkey commented Sep 12, 2018

Sorry, I was quite busy. I'll test it once I have a little time at the end of september and will create that basic example for you!

Thanks for your patience :)

@iby
Copy link

iby commented Sep 22, 2018

@ivogabe it is. Trying to migrate an old project and can't get my head around this. Works if add symbolic link for node_modules inside the build folder.

@FunkMonkey
Copy link
Author

FunkMonkey commented Oct 16, 2018

@ivogabe Alright, I finally managed to create a test case:
https://github.com/FunkMonkey/gulp-typescript-bug

just do

yarn install
npm run build-gulp
npm run build-tsc

I tested it on windows, not sure if the run commands work from the workspace on a different OS. If not, just go to packages/typescript-test and do npm run build-gulp or npm run build-tsc there.

As you see npm run build-tsc works, while npm run build-gulp doesnt...

Thanks a lot!

@FunkMonkey
Copy link
Author

@ivogabe any update on this? Thanks!

@FunkMonkey
Copy link
Author

@ivogabe

OK, I digged a lot and found the problem. For whatever reason typescript does not traverse the parent node_modules folders when the configFilePath in the parsed compiler options is not an absolute path!!

The problem lies in main.ts#222. Just remove path.basename there and maybe replace it with path.resolve( process.cwd(), tsConfigFileName ) or anything else that makes sense. I just tried it and it works.

Thank you very much!

@pixnblox
Copy link

I just had the same problem... took me quite a long (and frustrating) time to test options and search for solutions, but I eventually found this issue. Thanks @FunkMonkey for logging it!

@norech
Copy link

norech commented Feb 11, 2019

Another solution is to use yarn nohoist feature with "**/@types/**" pattern, on the root project.
You will need to install typings dependencies inside of every subprojects, but at least it works without forking gulp-typescript or using tricky gulp files.

It would be great if this issue was fixed. It could avoid to a lot of people to spend a lot of hours to find why theirs projects are not building, and how to fix it.

lddubeau added a commit to lddubeau/gulp-typescript that referenced this issue Feb 14, 2019
lddubeau added a commit to lddubeau/gulp-typescript that referenced this issue Feb 14, 2019
lddubeau added a commit to lddubeau/gulp-typescript that referenced this issue Feb 14, 2019
Fixes ivogabe#563

The fix is actually the change in main.ts. The same change was applied to
project.ts though I've not found a way to create a test case that generates
there. (It may not be currently possible to generate such a case.) It just seems
that the two calls to parseJsonConfigFileContent should pass the whole path to
the config.
@ivogabe
Copy link
Owner

ivogabe commented Feb 14, 2019

Sorry for the late reply. Thanks @FunkMonkey for the debugging! @lddubeau has opened a PR, I'll review that tomorrow. So we'll have this issue fixed soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants