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

tsc complains require() file path outside of rootDir only when await import() is used #38611

Closed
ychi opened this issue May 16, 2020 · 3 comments · Fixed by #39617
Closed

tsc complains require() file path outside of rootDir only when await import() is used #38611

ychi opened this issue May 16, 2020 · 3 comments · Fixed by #39617
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@ychi
Copy link

ychi commented May 16, 2020

TypeScript Version: 4.0.0-dev.20200516

Search Terms: commonjs, esmodule, rootDir

Code
Consider this file:

onst {version: VERSION} = require('../package.json');
console.log(VERSION);

async function foo() {
    let obj = {};
    console.log(obj);
}

export async function bar() {
    console.log('foo');
    await foo();
}

And this file:

const {version: VERSION} = require('../package.json');
console.log(VERSION);

async function foo() {
    let obj = await import('./anotherFile');
    console.log(obj);
}

export async function bar() {
    console.log('foo');
    await foo();
}

Expected behavior:
Running tsc to compile them, they should both succeed or both fail?
Actual behavior:
The first file can be compiled smoothly, while the second was complained about error TS6059 and error TS6307.
Playground Link: https://glitch.com/~aromatic-bittersweet-biplane

Related Issues:

@cspotcode
Copy link

cspotcode commented May 17, 2020

I am seeing this bug triggered by a slightly different reason: adding an import() type annotation.

I have created a minimal reproduction here: https://github.com/TypeStrong/ts-node-repros/tree/ts-38611
It executed here: https://github.com/TypeStrong/ts-node-repros/runs/682702614

declare const require: any;

// Bug occurs when an import() is in the file
const a: typeof import('./other') = null as any

function foo() {
    const a = require('../outside-of-rootdir/foo'); // Semantic error: File ... is not under 'rootDir' ...
    const {other}: {other: string} = require('./other'); // No error here, even though is semantically invalid, because `other` is of type `number`.
    
    // It appears as though the resolver is trying to locate the targets of require() calls, but the typechecker does not care.
}

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jun 8, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0 milestone Jun 8, 2020
@sheetalkamat
Copy link
Member

@ychi your repro link seems to be not working, Can you please share the repro again. Thanks

@sheetalkamat sheetalkamat added Needs More Info The issue still hasn't been fully clarified and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 15, 2020
@sheetalkamat
Copy link
Member

Actually @cspotcode repro seems to be working.. Thanks.

@sheetalkamat sheetalkamat added Needs Investigation This issue needs a team member to investigate its status. Bug A bug in TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels Jul 15, 2020
@sheetalkamat sheetalkamat added Fix Available A PR has been opened for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 15, 2020
sheetalkamat added a commit that referenced this issue Jul 15, 2020
…dynamic import (#39617)

* Existing tests showing require in ts file is not used for module resolution

* Do not resolve require calls in typescript files even if it contains dynamic import
Fixes #38611
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
4 participants