-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Cannot read property 'length' of undefined in v4 version #41717
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
Comments
Pinning |
I've had similar issues with 4.1.2, I've had switch to 4.0.5 (I was trying to upgrade from 3.9.7) I have |
I've had same issues with 4.1.2 |
any updates? |
I've had same issues with 4.1.2 too |
in my case, after some Investigation, i found the new sourcefile with undefined ... ...
function transformDeclarations(context) {
... ...
return transformRoot;
... ...
function transformRoot(node) {
... ...
else {
var statements = ts.visitNodes(node.statements, visitDeclarationStatements);
combinedStatements = ts.setTextRange(factory.createNodeArray(transformAndReplaceLatePaintedStatements(statements)), node.statements);
refs.forEach(referenceVisitor);
emittedImports = ts.filter(combinedStatements, ts.isAnyImportSyntax);
if (ts.isExternalModule(node) && (!resultHasExternalModuleIndicator || (needsScopeFixMarker && !resultHasScopeMarker))) {
combinedStatements = ts.setTextRange(factory.createNodeArray(__spreadArrays(combinedStatements, [ts.createEmptyExports(factory)])), combinedStatements);
}
}
/// <<<<< here
var updated = factory.updateSourceFile(node, combinedStatements, /*isDeclarationFile*/ true, references, getFileReferencesForUsedTypeReferences(), node.hasNoDefaultLib, getLibReferences());
/// <<<<< here
updated.exportedModulesFromDeclarationEmit = exportedModulesFromDeclarationEmit;
return updated;
... ...
}
} and if add copy text property in the function cloneSourceFileWithChanges(source, statements, isDeclarationFile, referencedFiles, typeReferences, hasNoDefaultLib, libReferences) {
var node = baseFactory.createBaseSourceFileNode(297 /* SourceFile */);
for (var p in source) {
if (p === "emitNode" || ts.hasProperty(node, p) || !ts.hasProperty(source, p)) {
debug && console.log('skip property', p);
continue;
}
node[p] = source[p];
}
node.flags |= source.flags;
// add this line
node.text = source.text;
node.statements = createNodeArray(statements);
node.endOfFileToken = source.endOfFileToken;
node.isDeclarationFile = isDeclarationFile;
node.referencedFiles = referencedFiles;
node.typeReferenceDirectives = typeReferences;
node.hasNoDefaultLib = hasNoDefaultLib;
node.libReferenceDirectives = libReferences;
node.transformFlags =
propagateChildrenFlags(node.statements) |
propagateChildFlags(node.endOfFileToken);
return node;
} im not sure if this is the proper fix. |
when i'm trying write a workaround for this problem, i found a strange thing function patchTransformDeclarations() {
const oldTransformDeclarations = (ts as any).transformDeclarations;
const oldCreateSourceFile = (ts as any).createSourceFile;
(ts as any).transformDeclarations = function (context: ts.TransformationContext) {
const transformRoot = oldTransformDeclarations(context);
function wrappedTransformRoot(node: ts.SourceFile) {
const oldText = node.text;
const updated = transformRoot(node) as ts.SourceFile;
if (node.fileName !== (node as any).originSourceFile) {
console.log(`node.fileName == node.originSourceFile false ${updated.text == undefined}`);
} else if (updated.text == undefined){
console.log(`!!!!!!!!!!!!
node.fileName == node.originSourceFile true updated.text == undefined true
!!!!!!!!!!!!`);
}
if (updated.text == undefined) {
// 这种文件的ast似乎不对,根本就是另外一个文件的内容
// 也许我应该再这里重新transform一下?
updated.text = fs.readFileSync(updated.fileName, 'utf8');
const originText = fs.readFileSync((node as any).originSourceFile, 'utf8');
console.log(
`node.fileName ${node.fileName}
updated.fileName ${updated.fileName}
node.fileName == updated.fileName ${node.fileName == updated.fileName}
(node as any).originSourceFile ${(node as any).originSourceFile}
oldText == updated.text ${oldText == updated.text}
oldText == originText ${oldText == originText}
`,
!(oldText == updated.text) && (`oldText:
${oldText.slice(0, 100)}
------------------------------
updated.text:
${updated.text.slice(0, 100)}
`));
}
return updated;
}
return wrappedTransformRoot
};
(ts as any).createSourceFile = (...args: any[]) => {
const ret = oldCreateSourceFile(...args);
ret.originSourceFile = args[0];
return ret;
};
return {
depatch() {
(ts as any).transformDeclarations = oldTransformDeclarations;
(ts as any).createSourceFile = oldCreateSourceFile;
}
}
} and got this log:
this seems like those SourceFiles with undefined text are in fact with wrong source before tranform to declare file... |
these sourcefile with modified fileName is from createRedirectSourceFile, which seems work incorrect, it redirect declartion file but redirect other things...
log
|
i found the redirect condition is check if has same package id, and the incorrect redirect file is with a package.json at same folder, which have the same package name....
maybe the redirct action should only happened in |
Thanks man :D |
This is a rollback pending a release containing a fix for microsoft/TypeScript#41717 We have also encountered another likely unrelated TypeError ('has' property) in emitter.ts that should be re-checked following a release containing microsoft/TypeScript#42676
@MohamedLamineAllal are you still experiencing the issue? If so, could you share a working repro of the problem. I have tried with the gist and tsconfig that you sent, but I don't seem to reproduce the problem with any of the versions you mentioned, including the nightly as well. Here's the repo I created for testing if you want to to take a look: https://github.com/armanio123/gh-ts-41717 |
It's not the smallest repro you could possibly get but cloning yarnpkg/berry@5dc54a4 and just running TypeScript is in a zip file so you can run |
In case this helps anyone - I could avoid this error for me like so: I have a yarn workspace setup:
and the client was not emitting. I received the length of undefined error in Comparing the tsconfigs, I saw this and changed my {
"include": [
"src",
- "../../node_modules/<common>"
+ "../../node_modules/<common>/@types"
]
} Somehow including the entire common package was causing this error. Luckily I didn't need to include it in the first place. |
In case it helps anyone, the error in our TypeScript codebase was caused by a one-line diff that we added: Two things worked for us (pick one):
We picked the latter. |
I experienced this issue just now. Ended up being a missing dependency resulting in an additional virtual package for a local package in my monorepo (using Yarn Berry's PnP). Adding the missing dependency and thus reducing the module instances from 2 -> 1 solved this. Seems like an odd error though. Using Yarn Berry (3.0.0-rc.5), Jest 27, ts-node (10.0.0), node (14.17.0), typescript (4.2.4) Reproduced this issue in the commit before https://github.com/tophat/monodeploy/commit/b2d532d6f3b1e0686d2857d1195409149591b4eb. Fixed with #44554. |
I also hit this with Yarn PnP (Yarn v2.4.1, TS v4.2.4). In my case it was an incorrect peer dependency, rather than a missing dependency, but otherwise it was the same as @noahnu described (additional virtual package, multiple instances of the module, etc). I resolved it by fixing the peer dependency, which to be fair Yarn had flagged up during I tried patching in the fix from #44554 and can confirm that would have solved the problem too. |
Closed by #48862 |
The issue happen with v4 version (tested on v4.0.3, v4.0.5 and the latest v4.1.2)
With the same config and code base it compile with no problem in v3.9
Expected behavior:
No such error! No failing at internal level! (Plus it works in v3.9 and not v4)
text should not come as undefined! Or should be handled without problem!
Actual behavior:
In v3.9 the code compile with the same config with no problem! In v4 i get Cannot read property 'length' of undefined error! And internal failing!
Through the error stack! i get where the error happened and it was at the function computeLineStarts!
text is coming undefined! i did some console logging! And i got what file was been treated! And what text node were printing! The file is all normal and correct! A comment at the end was the last thing which after it the code fail!
Then i added another instruction after it! (A variable assignment
const someVar = '';
) and the error went up! The last treated nodes before failing came more up! Here bellow the file at which my console.logging stopped at!File on which the resolution failed at:
https://gist.github.com/MohamedLamineAllal/472cf2043a100cb03244de5fb1138034
When i added
const someVar = "";
at the end! the last treated node was}
fromHow i debugged!?
in tsc.js
And at
First Compilation execution (original file)
Without adding the last
const someVar = '';
https://gist.github.com/MohamedLamineAllal/653fd4f597aa821f416a33e86d4514eb
Second compilation (after adding the last line)
https://gist.github.com/MohamedLamineAllal/022f6f285313aec30b82bb72238bcaeb
Always it fails at computeLineStarts() method! And because text is undefined.
Error stack
For the shared links i used typescript Version 4.1.2! Otherwise i tested with v4.0.3 and v4.0.5
tsconfig.json
Related Issues:
The text was updated successfully, but these errors were encountered: