From 8ca12a6b53b9f259b42cbea997df03e180cfb9a6 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 15 Nov 2021 17:38:16 -0800 Subject: [PATCH 1/2] Fix broken module resolution after edits in nodenext --- src/harness/fourslashImpl.ts | 3 ++- src/services/services.ts | 4 +++- .../fourslash/server/impliedNodeFormat.ts | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/server/impliedNodeFormat.ts diff --git a/src/harness/fourslashImpl.ts b/src/harness/fourslashImpl.ts index 0e8b1825d9e58..a086eff4fc25e 100644 --- a/src/harness/fourslashImpl.ts +++ b/src/harness/fourslashImpl.ts @@ -637,7 +637,8 @@ namespace FourSlash { ts.forEachKey(this.inputFiles, fileName => { if (!ts.isAnySupportedFileExtension(fileName) || Harness.getConfigNameFromFileName(fileName) - || !ts.getAllowJSCompilerOption(this.getProgram().getCompilerOptions()) && !ts.resolutionExtensionIsTSOrJson(ts.extensionFromPath(fileName)) + // Can't get a Program in Server tests + || this.testType !== FourSlashTestType.Server && !ts.getAllowJSCompilerOption(this.getProgram().getCompilerOptions()) && !ts.resolutionExtensionIsTSOrJson(ts.extensionFromPath(fileName)) || ts.getBaseFileName(fileName) === "package.json") return; const errors = this.getDiagnostics(fileName).filter(e => e.category !== ts.DiagnosticCategory.Suggestion); if (errors.length) { diff --git a/src/services/services.ts b/src/services/services.ts index c9c54c0234ec6..77517bb7debb8 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1124,7 +1124,9 @@ namespace ts { } // Otherwise, just create a new source file. - return createLanguageServiceSourceFile(sourceFile.fileName, scriptSnapshot, sourceFile.languageVersion, version, /*setNodeParents*/ true, sourceFile.scriptKind); + const newSourceFile = createLanguageServiceSourceFile(sourceFile.fileName, scriptSnapshot, sourceFile.languageVersion, version, /*setNodeParents*/ true, sourceFile.scriptKind); + newSourceFile.impliedNodeFormat = sourceFile.impliedNodeFormat; + return newSourceFile; } const NoopCancellationToken: CancellationToken = { diff --git a/tests/cases/fourslash/server/impliedNodeFormat.ts b/tests/cases/fourslash/server/impliedNodeFormat.ts new file mode 100644 index 0000000000000..d7faa92d3b8e5 --- /dev/null +++ b/tests/cases/fourslash/server/impliedNodeFormat.ts @@ -0,0 +1,20 @@ +/// + +// @Filename: /tsconfig.json +//// { "compilerOptions": { "module": "nodenext" } } + +// @Filename: /package.json +//// { "name": "foo", "type": "module", "exports": { ".": "./main.js" } } + +// @Filename: /main.ts +//// export {}; + +// @Filename: /index.ts +//// import {} from "foo"; + +goTo.file("/index.ts"); +verify.noErrors(); + +edit.paste(`\n"${"a".repeat(256)}";`); + +verify.noErrors(); From 151497fba87be7e7ca7a2221909efc9cd5a3d71c Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 15 Nov 2021 17:53:39 -0800 Subject: [PATCH 2/2] Move field copying to a better place I guess --- src/compiler/program.ts | 1 + src/services/services.ts | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index c29876ce619f5..0176f2acb7f40 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1577,6 +1577,7 @@ namespace ts { newSourceFile.originalFileName = oldSourceFile.originalFileName; newSourceFile.resolvedPath = oldSourceFile.resolvedPath; newSourceFile.fileName = oldSourceFile.fileName; + newSourceFile.impliedNodeFormat = oldSourceFile.impliedNodeFormat; const packageName = oldProgram.sourceFileToPackageName.get(oldSourceFile.path); if (packageName !== undefined) { diff --git a/src/services/services.ts b/src/services/services.ts index 77517bb7debb8..c9c54c0234ec6 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1124,9 +1124,7 @@ namespace ts { } // Otherwise, just create a new source file. - const newSourceFile = createLanguageServiceSourceFile(sourceFile.fileName, scriptSnapshot, sourceFile.languageVersion, version, /*setNodeParents*/ true, sourceFile.scriptKind); - newSourceFile.impliedNodeFormat = sourceFile.impliedNodeFormat; - return newSourceFile; + return createLanguageServiceSourceFile(sourceFile.fileName, scriptSnapshot, sourceFile.languageVersion, version, /*setNodeParents*/ true, sourceFile.scriptKind); } const NoopCancellationToken: CancellationToken = {