microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.28k stars 12.52k forks source link

TypeError: Cannot read property 'path' of undefined #26460

Closed akosyakov closed 6 years ago

akosyakov commented 6 years ago

I am trying to migrate Theia monorepo to project references: https://github.com/theia-ide/theia/pull/2563/files

But it fails during the compilation:

gitpod /workspace/theia $ yarn compile -v
yarn run v1.9.4
$ tsc -b -v
message TS6355: Projects in this build:
    * dev-packages/application-package/tsconfig.json
    * dev-packages/application-manager/tsconfig.json
    * dev-packages/cli/tsconfig.json
    * packages/core/tsconfig.json
    * packages/bunyan/tsconfig.json
    * packages/output/tsconfig.json
    * packages/process/tsconfig.json
    * packages/filesystem/tsconfig.json
    * packages/variable-resolver/tsconfig.json
    * packages/workspace/tsconfig.json
    * packages/languages/tsconfig.json
    * packages/editor/tsconfig.json
    * packages/navigator/tsconfig.json
    * packages/markers/tsconfig.json
    * packages/outline-view/tsconfig.json
    * packages/monaco/tsconfig.json
    * packages/callhierarchy/tsconfig.json
    * packages/userstorage/tsconfig.json
    * packages/preferences/tsconfig.json
    * packages/cpp/tsconfig.json
    * packages/editorconfig/tsconfig.json
    * packages/extension-manager/tsconfig.json
    * packages/file-search/tsconfig.json
    * packages/git/tsconfig.json
    * packages/java/tsconfig.json
    * packages/keymaps/tsconfig.json
    * packages/merge-conflicts/tsconfig.json
    * packages/messages/tsconfig.json
    * packages/metrics/tsconfig.json
    * packages/mini-browser/tsconfig.json
    * packages/plugin/tsconfig.json
    * packages/terminal/tsconfig.json
    * packages/plugin-ext/tsconfig.json
    * packages/plugin-ext-vscode/tsconfig.json
    * packages/preview/tsconfig.json
    * packages/python/tsconfig.json
    * packages/search-in-workspace/tsconfig.json
    * packages/task/tsconfig.json
    * packages/textmate-grammars/tsconfig.json
    * packages/typescript/tsconfig.json
    * examples/browser/tsconfig.json
    * tsconfig.json

message TS6351: Project 'dev-packages/application-package/tsconfig.json' is up to date because newest input 'dev-packages/application-package/src/application-package.ts' is o
lder than oldest output 'dev-packages/application-package/lib/json-file.js'

message TS6351: Project 'dev-packages/application-manager/tsconfig.json' is up to date because newest input 'dev-packages/application-manager/src/application-package-manager.ts' is older than oldest output 'dev-packages/application-manager/lib/generator/abstract-generator.js'

message TS6351: Project 'dev-packages/cli/tsconfig.json' is up to date because newest input 'dev-packages/cli/src/theia.ts' is older than oldest output 'dev-packages/cli/lib/theia.js'

message TS6351: Project 'packages/core/tsconfig.json' is up to date because newest input 'packages/core/src/browser/about-dialog.ts' is older than oldest output 'packages/core/lib/common/types.js'

message TS6351: Project 'packages/bunyan/tsconfig.json' is up to date because newest input 'packages/bunyan/src/package.spec.ts' is older than oldest output 'packages/bunyan/lib/package.spec.js'

message TS6351: Project 'packages/output/tsconfig.json' is up to date because newest input 'packages/output/src/package.spec.ts' is older than oldest output 'packages/output/lib/package.spec.js'

message TS6351: Project 'packages/process/tsconfig.json' is up to date because newest input 'packages/process/src/node/index.ts' is older than oldest output 'packages/process/lib/node/process.js'

message TS6352: Project 'packages/filesystem/tsconfig.json' is out of date because output file 'packages/filesystem/lib/browser/file-dialog-service.js' does not exist

message TS6358: Building project '/workspace/theia/packages/filesystem/tsconfig.json'...

/workspace/theia/node_modules/typescript/lib/tsc.js:67622
                throw e;
                ^

TypeError: Cannot read property 'path' of undefined
    at /workspace/theia/node_modules/typescript/lib/tsc.js:45483:46
    at Map.forEach (<anonymous>)
    at createResolver (/workspace/theia/node_modules/typescript/lib/tsc.js:45478:49)
    at Object.createTypeChecker (/workspace/theia/node_modules/typescript/lib/tsc.js:23812:28)
    at getDiagnosticsProducingTypeChecker (/workspace/theia/node_modules/typescript/lib/tsc.js:67529:93)
    at /workspace/theia/node_modules/typescript/lib/tsc.js:67844:32
    at runWithCancellationToken (/workspace/theia/node_modules/typescript/lib/tsc.js:67615:24)
    at getDeclarationDiagnosticsForFileNoCache (/workspace/theia/node_modules/typescript/lib/tsc.js:67843:20)
    at getAndCacheDiagnostics (/workspace/theia/node_modules/typescript/lib/tsc.js:67855:26)
    at getDeclarationDiagnosticsWorker (/workspace/theia/node_modules/typescript/lib/tsc.js:67840:20)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
akosyakov commented 6 years ago

@weswigham let me know if I can do something to help track down the issue, would be nice to have it in 3.1 🙏

raymondfeng commented 6 years ago

I ran into the same issue too after adding project references.

raymondfeng commented 6 years ago

Here is screenshot from my debugging session:

image

Please note the red line is offending. I also see some strange file names such as dist8-* instead of dist8/, which is my outDir.

garthk commented 6 years ago

Which version of TypeScript was that, @akosyakov? I'm getting something similar in a different line with 3.0.3 while trying to use references:

TypeError: Cannot read property 'path' of undefined
    at node_modules/typescript/lib/tsc.js:45496:46
    at Map.forEach (<anonymous>)
    at createResolver (node_modules/typescript/lib/tsc.js:45491:49)
    at Object.createTypeChecker (node_modules/typescript/lib/tsc.js:23813:28)
    at getDiagnosticsProducingTypeChecker (node_modules/typescript/lib/tsc.js:67561:93)
    at Object.getGlobalDiagnostics (node_modules/typescript/lib/tsc.js:67906:53)
    at Object.emitFilesAndReportErrors (node_modules/typescript/lib/tsc.js:70217:46)
    at performCompilation (node_modules/typescript/lib/tsc.js:71962:29)
    at Object.executeCommandLine (node_modules/typescript/lib/tsc.js:71800:17)
    at Object.<anonymous> (node_modules/typescript/lib/tsc.js:72087:4)

It's failing in the fileToDirective.set(file.path, key) line below:

        function createResolver() {
            var resolvedTypeReferenceDirectives = host.getResolvedTypeReferenceDirectives();
            var fileToDirective;
            if (resolvedTypeReferenceDirectives) {
                fileToDirective = ts.createMap();
                resolvedTypeReferenceDirectives.forEach(function (resolvedDirective, key) {
                    if (!resolvedDirective || !resolvedDirective.resolvedFileName) {
                        return;
                    }
                    var file = host.getSourceFile(resolvedDirective.resolvedFileName);
                    fileToDirective.set(file.path, key);
                });
            }
            return {
…
                }
            };

In my case, host.getSourceFile returned undefined for node_modules/@types/code/index.d.ts, as shipped in @types/code 4.0.4.

If I return if !file, I get

error TS6305: Output file '/Users/me/src/repo/dist/node_modules/@types/code/index.d.ts' has not been built from source file '/Users/me/src/repo/pkg/packagename/node_modules/@types/code/index.d.ts'.

I'm not sure why it's expecting an output file in ../../../dist relative to the directory I'm running tsc in, /Users/me/src/repo/pkg/packagename/test.

akosyakov commented 6 years ago

I've tried with rc 3.1. It's still there.

sheetalkamat commented 6 years ago

This seems to be fixed with typescript@next and errors are reported. without any crash

akosyakov commented 6 years ago

Thank you! Just verified it does not fail anymore.