From 9076f4d57d0108adf80dd7ba3ff852488d921273 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 31 Aug 2022 12:52:50 -0700 Subject: [PATCH] Fix self reference package with casing --- src/compiler/moduleNameResolver.ts | 4 ++-- .../self-name-package-reference.js | 17 ++++------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 652ab99abdb..9e3b15f0958 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -2249,7 +2249,7 @@ namespace ts { if ((extensions === Extensions.TypeScript || extensions === Extensions.JavaScript || extensions === Extensions.Json) && (state.compilerOptions.declarationDir || state.compilerOptions.outDir) && finalPath.indexOf("/node_modules/") === -1 - && (state.compilerOptions.configFile ? startsWith(toAbsolutePath(state.compilerOptions.configFile.fileName), scope.packageDirectory) : true) + && (state.compilerOptions.configFile ? containsPath(scope.packageDirectory, toAbsolutePath(state.compilerOptions.configFile.fileName), !useCaseSensitiveFileNames()) : true) ) { // So that all means we'll only try these guesses for files outside `node_modules` in a directory where the `package.json` and `tsconfig.json` are siblings. // Even with all that, we still don't know if the root of the output file structure will be (relative to the package file) @@ -2310,7 +2310,7 @@ namespace ts { for (const commonSourceDirGuess of commonSourceDirGuesses) { const candidateDirectories = getOutputDirectoriesForBaseDirectory(commonSourceDirGuess); for (const candidateDir of candidateDirectories) { - if (startsWith(finalPath, candidateDir)) { + if (containsPath(candidateDir, finalPath, !useCaseSensitiveFileNames())) { // The matched export is looking up something in either the out declaration or js dir, now map the written path back into the source dir and source extension const pathFragment = finalPath.slice(candidateDir.length + 1); // +1 to also remove directory seperator const possibleInputBase = combinePaths(commonSourceDirGuess, pathFragment); diff --git a/tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/self-name-package-reference.js b/tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/self-name-package-reference.js index fd91a8e170d..02b1358e97a 100644 --- a/tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/self-name-package-reference.js +++ b/tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/self-name-package-reference.js @@ -35,28 +35,19 @@ Found 'package.json' at '/users/name/projects/web/package.json'. ======== Resolving module '@this/package' from '/Users/name/projects/web/index.ts'. ======== Module resolution kind is not specified, using 'NodeNext'. File '/users/name/projects/web/package.json' exists according to earlier cached lookups. -File '/users/name/projects/web/index.ts' exist - use it as a name resolution result. -Resolving real path for '/users/name/projects/web/index.ts', result '/Users/name/projects/web/index.ts'. -======== Module name '@this/package' was successfully resolved to '/users/name/projects/web/index.ts'. ======== +File '/Users/name/projects/web/index.ts' exist - use it as a name resolution result. +Resolving real path for '/Users/name/projects/web/index.ts', result '/Users/name/projects/web/index.ts'. +======== Module name '@this/package' was successfully resolved to '/Users/name/projects/web/index.ts'. ======== File '/a/lib/package.json' does not exist. File '/a/package.json' does not exist. File '/package.json' does not exist. -index.ts:1:21 - error TS1149: File name '/users/name/projects/web/index.ts' differs from already included file name '/Users/name/projects/web/index.ts' only in casing. - The file is in the program because: - Matched by default include pattern '**/*' - Imported via "@this/package" from file '/Users/name/projects/web/index.ts' - File is ECMAScript module because '/users/name/projects/web/package.json' has field "type" with value "module" - -1 import * as me from "@this/package"; -   ~~~~~~~~~~~~~~~ - ../../../../a/lib/lib.esnext.full.d.ts Default library for target 'esnext' index.ts Matched by default include pattern '**/*' Imported via "@this/package" from file 'index.ts' File is ECMAScript module because 'package.json' has field "type" with value "module" -[12:00:36 AM] Found 1 error. Watching for file changes. +[12:00:36 AM] Found 0 errors. Watching for file changes.