From fc09a21a6acd33e717edf2807b24e98996150bca Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 28 Jun 2023 15:04:10 -0700 Subject: [PATCH 1/4] Fix self-name input linking for --allowJs --declaration projects --- src/compiler/moduleNameResolver.ts | 12 +++++- .../reference/nodeAllowJsPackageSelfName2.js | 33 ++++++++++++++++ .../nodeAllowJsPackageSelfName2.symbols | 10 +++++ .../nodeAllowJsPackageSelfName2.types | 11 ++++++ .../allowJs/nodeAllowJsPackageSelfName2.ts | 38 +++++++++++++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/nodeAllowJsPackageSelfName2.js create mode 100644 tests/baselines/reference/nodeAllowJsPackageSelfName2.symbols create mode 100644 tests/baselines/reference/nodeAllowJsPackageSelfName2.types create mode 100644 tests/cases/conformance/node/allowJs/nodeAllowJsPackageSelfName2.ts diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index fbe1332b939b8..8e41872fa3feb 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -35,6 +35,7 @@ import { forEach, forEachAncestorDirectory, formatMessage, + getAllowJSCompilerOption, getBaseFileName, GetCanonicalFileName, getCommonSourceDirectory, @@ -2420,7 +2421,16 @@ function loadModuleFromSelfNameReference(extensions: Extensions, moduleName: str // in order to be consistent with (non-self) library-name loading in // `loadModuleFromNearestNodeModulesDirectoryWorker`, which uses two passes in order // to prioritize `@types` packages higher up the directory tree over untyped - // implementation packages. + // implementation packages. See the selfNameModuleAugmentation.ts test for why this + // matters. + // + // However, there's an exception. If the user has `allowJs` and `declaration, we need + // to ensure that self-name imports of their own package can resolve back to their + // input JS files via `tryLoadInputFileForPath` at a higher priority than their output + // declaration files, so we need to do a single pass with all extensions for that case. + if (getAllowJSCompilerOption(state.compilerOptions) && !pathContainsNodeModules(directory)) { + return loadModuleFromExports(scope, extensions, subpath, state, cache, redirectedReference); + } const priorityExtensions = extensions & (Extensions.TypeScript | Extensions.Declaration); const secondaryExtensions = extensions & ~(Extensions.TypeScript | Extensions.Declaration); return loadModuleFromExports(scope, priorityExtensions, subpath, state, cache, redirectedReference) diff --git a/tests/baselines/reference/nodeAllowJsPackageSelfName2.js b/tests/baselines/reference/nodeAllowJsPackageSelfName2.js new file mode 100644 index 0000000000000..8d0b9db1924f9 --- /dev/null +++ b/tests/baselines/reference/nodeAllowJsPackageSelfName2.js @@ -0,0 +1,33 @@ +//// [tests/cases/conformance/node/allowJs/nodeAllowJsPackageSelfName2.ts] //// + +//// [package.json] +{ + "name": "js-self-name-import", + "type": "module", + "exports": { + "./*": { + "types": "./types/src/*", + "default": "./src/*" + } + } +} + +//// [foo.d.ts] +export const foo: 1; + +//// [foo.d.ts] +export {}; + +//// [foo.js] +export const foo = 1; + +//// [foo.js] +import { foo } from "js-self-name-import/foo.js"; + + + + +//// [foo.d.ts] +export const foo: 1; +//// [foo.d.ts] +export {}; diff --git a/tests/baselines/reference/nodeAllowJsPackageSelfName2.symbols b/tests/baselines/reference/nodeAllowJsPackageSelfName2.symbols new file mode 100644 index 0000000000000..cf553ecca4d11 --- /dev/null +++ b/tests/baselines/reference/nodeAllowJsPackageSelfName2.symbols @@ -0,0 +1,10 @@ +//// [tests/cases/conformance/node/allowJs/nodeAllowJsPackageSelfName2.ts] //// + +=== /src/foo.js === +export const foo = 1; +>foo : Symbol(foo, Decl(foo.js, 0, 12)) + +=== /test/foo.js === +import { foo } from "js-self-name-import/foo.js"; +>foo : Symbol(foo, Decl(foo.js, 0, 8)) + diff --git a/tests/baselines/reference/nodeAllowJsPackageSelfName2.types b/tests/baselines/reference/nodeAllowJsPackageSelfName2.types new file mode 100644 index 0000000000000..b5d4bfc7cf225 --- /dev/null +++ b/tests/baselines/reference/nodeAllowJsPackageSelfName2.types @@ -0,0 +1,11 @@ +//// [tests/cases/conformance/node/allowJs/nodeAllowJsPackageSelfName2.ts] //// + +=== /src/foo.js === +export const foo = 1; +>foo : 1 +>1 : 1 + +=== /test/foo.js === +import { foo } from "js-self-name-import/foo.js"; +>foo : 1 + diff --git a/tests/cases/conformance/node/allowJs/nodeAllowJsPackageSelfName2.ts b/tests/cases/conformance/node/allowJs/nodeAllowJsPackageSelfName2.ts new file mode 100644 index 0000000000000..5aa706a15a082 --- /dev/null +++ b/tests/cases/conformance/node/allowJs/nodeAllowJsPackageSelfName2.ts @@ -0,0 +1,38 @@ +// @Filename: /tsconfig.json +{ + "compilerOptions": { + "module": "nodenext", + "target": "esnext", + "emitDeclarationOnly": true, + "declaration": true, + "declarationDir": "./types", + "checkJs": true, + "rootDir": ".", + "strict": true, + }, + "include": ["src", "test"] +} + +// @Filename: /package.json +{ + "name": "js-self-name-import", + "type": "module", + "exports": { + "./*": { + "types": "./types/src/*", + "default": "./src/*" + } + } +} + +// @Filename: /types/src/foo.d.ts +export const foo: 1; + +// @Filename: /types/test/foo.d.ts +export {}; + +// @Filename: /src/foo.js +export const foo = 1; + +// @Filename: /test/foo.js +import { foo } from "js-self-name-import/foo.js"; From 712dfe15e5040040ba81f734d97b4842962d2c09 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 28 Jun 2023 15:33:59 -0700 Subject: [PATCH 2/4] Update src/compiler/moduleNameResolver.ts Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com> --- src/compiler/moduleNameResolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 8e41872fa3feb..7028493771345 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -2424,7 +2424,7 @@ function loadModuleFromSelfNameReference(extensions: Extensions, moduleName: str // implementation packages. See the selfNameModuleAugmentation.ts test for why this // matters. // - // However, there's an exception. If the user has `allowJs` and `declaration, we need + // However, there's an exception. If the user has `allowJs` and `declaration`, we need // to ensure that self-name imports of their own package can resolve back to their // input JS files via `tryLoadInputFileForPath` at a higher priority than their output // declaration files, so we need to do a single pass with all extensions for that case. From 657551d05f3aadd73c514d586c3b4009ecc5b537 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 28 Jun 2023 15:41:26 -0700 Subject: [PATCH 3/4] Add affectsModuleResolution to checkJs --- src/compiler/commandLineParser.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 3a90ec43fed03..1443d98edb3b2 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -657,6 +657,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [ { name: "checkJs", type: "boolean", + affectsModuleResolution: true, showInSimplifiedHelpView: true, category: Diagnostics.JavaScript_Support, description: Diagnostics.Enable_error_reporting_in_type_checked_JavaScript_files, From 22ded91c91db980e90f04ea9b87d214edd8da5bf Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 28 Jun 2023 16:09:21 -0700 Subject: [PATCH 4/4] Update baselines --- ...script-info-is-orphan,-and-orphan-script-info-changes.js | 6 +++--- .../Caches-the-source-file-if-script-info-is-orphan.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan,-and-orphan-script-info-changes.js b/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan,-and-orphan-script-info-changes.js index fb5aff08f7ab6..0ac82cd1f3984 100644 --- a/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan,-and-orphan-script-info-changes.js +++ b/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan,-and-orphan-script-info-changes.js @@ -69,7 +69,7 @@ Info seq [hh:mm:ss:mss] Open files: Info seq [hh:mm:ss:mss] FileName: /user/username/projects/myproject/index.ts ProjectRootPath: undefined Info seq [hh:mm:ss:mss] Projects: /user/username/projects/myproject/tsconfig.json DocumentRegistry:: - Key:: undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined + Key:: undefined|undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined /user/username/projects/myproject/index.ts: TS 1 /user/username/projects/myproject/module1.d.ts: TS 1 /a/lib/lib.d.ts: TS 1 @@ -88,7 +88,7 @@ Info seq [hh:mm:ss:mss] Files (2) Info seq [hh:mm:ss:mss] ----------------------------------------------- DocumentRegistry:: - Key:: undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined + Key:: undefined|undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined /user/username/projects/myproject/index.ts: TS 1 /a/lib/lib.d.ts: TS 1 Info seq [hh:mm:ss:mss] FileWatcher:: Triggered with /user/username/projects/myproject/module1.d.ts 1:: WatchInfo: /user/username/projects/myproject/module1.d.ts 500 undefined WatchType: Closed Script info @@ -111,7 +111,7 @@ Info seq [hh:mm:ss:mss] Files (3) Info seq [hh:mm:ss:mss] ----------------------------------------------- DocumentRegistry:: - Key:: undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined + Key:: undefined|undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined /user/username/projects/myproject/index.ts: TS 1 /a/lib/lib.d.ts: TS 1 /user/username/projects/myproject/module1.d.ts: TS 1 \ No newline at end of file diff --git a/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan.js b/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan.js index 4c987008954c7..ce09ea9b1b5d1 100644 --- a/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan.js +++ b/tests/baselines/reference/tsserver/documentRegistry/Caches-the-source-file-if-script-info-is-orphan.js @@ -69,7 +69,7 @@ Info seq [hh:mm:ss:mss] Open files: Info seq [hh:mm:ss:mss] FileName: /user/username/projects/myproject/index.ts ProjectRootPath: undefined Info seq [hh:mm:ss:mss] Projects: /user/username/projects/myproject/tsconfig.json DocumentRegistry:: - Key:: undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined + Key:: undefined|undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined /user/username/projects/myproject/index.ts: TS 1 /user/username/projects/myproject/module1.d.ts: TS 1 /a/lib/lib.d.ts: TS 1 @@ -88,7 +88,7 @@ Info seq [hh:mm:ss:mss] Files (2) Info seq [hh:mm:ss:mss] ----------------------------------------------- DocumentRegistry:: - Key:: undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined + Key:: undefined|undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined /user/username/projects/myproject/index.ts: TS 1 /a/lib/lib.d.ts: TS 1 Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /user/username/projects/myproject/tsconfig.json @@ -109,7 +109,7 @@ Info seq [hh:mm:ss:mss] Files (3) Info seq [hh:mm:ss:mss] ----------------------------------------------- DocumentRegistry:: - Key:: undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined + Key:: undefined|undefined|undefined|undefined|undefined|false|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined|undefined /user/username/projects/myproject/index.ts: TS 1 /a/lib/lib.d.ts: TS 1 /user/username/projects/myproject/module1.d.ts: TS 1 \ No newline at end of file