Cherry-pick PR #50557 into release-4.8 (#50576)

Component commits:
0cb4514766 Add test to show how scope messes with casing

a3ea961838 Do not canonicalize the file names when getting absolute paths Fixes #50544

dfb1e401f0 Unnecessary exports

fc35e0a032 Add test for self referencing package

9076f4d57d Fix self reference package with casing

Co-authored-by: Sheetal Nandi <shkamat@microsoft.com>
This commit is contained in:
TypeScript Bot
2022-09-01 06:49:26 -07:00
committed by GitHub
parent 4b7a7b5ddf
commit 8b79b2ffb3
4 changed files with 356 additions and 3 deletions

View File

@@ -2227,7 +2227,7 @@ namespace ts {
function toAbsolutePath(path: string | undefined): string | undefined;
function toAbsolutePath(path: string | undefined): string | undefined {
if (path === undefined) return path;
return hostGetCanonicalFileName({ useCaseSensitiveFileNames })(getNormalizedAbsolutePath(path, state.host.getCurrentDirectory?.()));
return getNormalizedAbsolutePath(path, state.host.getCurrentDirectory?.());
}
function combineDirectoryPath(root: string, dir: string) {
@@ -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);

View File

@@ -262,5 +262,69 @@ a;b;
verifyDirSymlink("when import matches disk but directory symlink target does not", `${projectRoot}/XY`, `${projectRoot}/XY`, `./Xy`);
verifyDirSymlink("when import and directory symlink target agree but do not match disk", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./Xy`);
verifyDirSymlink("when import, directory symlink target, and disk are all different", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./yX`);
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario: "with nodeNext resolution",
commandLineArgs: ["--w", "--explainFiles"],
sys: () => createWatchedSystem({
"/Users/name/projects/web/src/bin.ts": `import { foo } from "yargs";`,
"/Users/name/projects/web/node_modules/@types/yargs/index.d.ts": "export function foo(): void;",
"/Users/name/projects/web/node_modules/@types/yargs/index.d.mts": "export function foo(): void;",
"/Users/name/projects/web/node_modules/@types/yargs/package.json": JSON.stringify({
name: "yargs",
version: "17.0.12",
exports: {
".": {
types: {
import: "./index.d.mts",
default: "./index.d.ts"
}
},
}
}),
"/Users/name/projects/web/tsconfig.json": JSON.stringify({
compilerOptions: {
moduleResolution: "nodenext",
forceConsistentCasingInFileNames: true,
traceResolution: true,
}
}),
[libFile.path]: libFile.content,
}, { currentDirectory: "/Users/name/projects/web" }),
changes: emptyArray,
});
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario: "self name package reference",
commandLineArgs: ["-w", "--explainFiles"],
sys: () => createWatchedSystem({
"/Users/name/projects/web/package.json": JSON.stringify({
name: "@this/package",
type: "module",
exports: {
".": "./dist/index.js"
}
}),
"/Users/name/projects/web/index.ts": Utils.dedent`
import * as me from "@this/package";
me.thing();
export function thing(): void {}
`,
"/Users/name/projects/web/tsconfig.json": JSON.stringify({
compilerOptions: {
module: "nodenext",
outDir: "./dist",
declarationDir: "./types",
composite: true,
forceConsistentCasingInFileNames: true,
traceResolution: true,
}
}),
"/a/lib/lib.esnext.full.d.ts": libFile.content,
}, { currentDirectory: "/Users/name/projects/web" }),
changes: emptyArray,
});
});
}