microsoft / microsoft/TypeScript

TSC fails to emit required files when run below node_modules

オープン
#28,432 コメント 10 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

TypeScript Version: 3.2.0-dev.20181107

Search Terms: node_modules

Code

/tmp/node_modules/tsc-test/tsconfig.json

{
  "compilerOptions": {
    "outDir": "dist"
  },
  "files": [
    "src/index.ts"
  ]
}

/tmp/node_modules/tsc-test/src/index.ts

import { A } from './A';

export const main = () => {
  console.log('A=', A);
};

/tmp/node_modules/tsc-test/src/A.ts

export const A = 'A';

Expected behavior:

Output files:

  • /tmp/node_modules/tsc-test/dist/index.js
  • /tmp/node_modules/tsc-test/dist/A.js

Actual behavior:

Output files:

/tmp/node_modules/tsc-test/dist/index.js

"use strict";
exports.__esModule = true;
var A_1 = require("./A");
exports.main = function () {
    console.log('A=', A_1.A);
};

No A.js is output

Notes:

I wanted to build a typescript project as part of an npm postinstall script, to enable installation from npm or from git. In the case of installation from npmjs, dist/ will already exist. In the case of installation from git, dist/ will be missing and build will be run.

After setting this up, I noticed that only files listed in my tsconfig files list were being emitted when installing from git. I looked around for tsconfig or tsc options that may change this behavior, but realized it probably had to do with 'node_modules' being in the path.

I debugged the issue by modifying the built tsc.js. In my case, the problematic code is in nodeModuleNameResolverWorker/tryResolve, specifically here:

const { path: candidate, parts } = normalizePathAndParts(combinePaths(containingDirectory, moduleName));
const resolved = nodeLoadModuleByRelativeName(extensions, candidate, /*onlyRecordFailures*/ false, state, /*considerPackageJson*/ true);
// Treat explicit "node_modules" import as an external library import.
return resolved && toSearchResult({ resolved, isExternalLibraryImport: contains(parts, "node_modules") });

The call to contains(parts, "node_modules") should likely consider the project root directory, and ignore "node_modules" in the path parts above the root.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

src/compiler/moduleNameResolver.ts の nodeModuleNameResolverWorker/tryResolve から始め、特に issue で説明されている contains(parts, "node_modules") のロジックを確認します。提供された /tmp/node_modules/tsc-test プロジェクトを TypeScript 3.2.0-dev.20181107 で再現し、生成されたファイルを比較します。プロジェクトが node_modules 配下にある場合に、ビルドによって dist/index.js と dist/A.js の両方が生成されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。