Handle more places where package direcroy is converted to canonical f… · WebReflection/TypeScript@6b890f9 · GitHub
Skip to content

Commit 6b890f9

Browse files
authored
Handle more places where package direcroy is converted to canonical file path (microsoft#50740)
* Add test for node16 resolution with package json lookup making casing incorrect * Handle more places where package direcroy is converted to canonical file path
1 parent f5f2923 commit 6b890f9

13 files changed

Lines changed: 219 additions & 68 deletions

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion

src/compiler/moduleNameResolver.ts

Lines changed: 43 additions & 39 deletions
Large diffs are not rendered by default.

src/compiler/moduleSpecifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ namespace ts.moduleSpecifiers {
807807
let maybeBlockedByTypesVersions = false;
808808
const cachedPackageJson = host.getPackageJsonInfoCache?.()?.getPackageJsonInfo(packageJsonPath);
809809
if (typeof cachedPackageJson === "object" || cachedPackageJson === undefined && host.fileExists(packageJsonPath)) {
810-
const packageJsonContent = cachedPackageJson?.packageJsonContent || JSON.parse(host.readFile!(packageJsonPath)!);
810+
const packageJsonContent = cachedPackageJson?.contents.packageJsonContent || JSON.parse(host.readFile!(packageJsonPath)!);
811811
const importMode = overrideMode || importingSourceFile.impliedNodeFormat;
812812
if (getEmitModuleResolutionKind(options) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(options) === ModuleResolutionKind.NodeNext) {
813813
const conditions = ["node", importMode === ModuleKind.ESNext ? "import" : "require", "types"];

src/compiler/program.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ namespace ts {
849849

850850
/*@internal*/
851851
export function getImpliedNodeFormatForFileWorker(
852-
fileName: Path,
852+
fileName: string,
853853
packageJsonInfoCache: PackageJsonInfoCache | undefined,
854854
host: ModuleResolutionHost,
855855
options: CompilerOptions,
@@ -870,7 +870,7 @@ namespace ts {
870870
state.failedLookupLocations = packageJsonLocations;
871871
state.affectingLocations = packageJsonLocations;
872872
const packageJsonScope = getPackageScopeForPath(fileName, state);
873-
const impliedNodeFormat = packageJsonScope?.packageJsonContent.type === "module" ? ModuleKind.ESNext : ModuleKind.CommonJS;
873+
const impliedNodeFormat = packageJsonScope?.contents.packageJsonContent.type === "module" ? ModuleKind.ESNext : ModuleKind.CommonJS;
874874
return { impliedNodeFormat, packageJsonLocations, packageJsonScope };
875875
}
876876
}
@@ -2834,7 +2834,7 @@ namespace ts {
28342834
// It's a _little odd_ that we can't set `impliedNodeFormat` until the program step - but it's the first and only time we have a resolution cache
28352835
// and a freshly made source file node on hand at the same time, and we need both to set the field. Persisting the resolution cache all the way
28362836
// to the check and emit steps would be bad - so we much prefer detecting and storing the format information on the source file node upfront.
2837-
const result = getImpliedNodeFormatForFileWorker(toPath(fileName), moduleResolutionCache?.getPackageJsonInfoCache(), host, options);
2837+
const result = getImpliedNodeFormatForFileWorker(getNormalizedAbsolutePath(fileName, currentDirectory), moduleResolutionCache?.getPackageJsonInfoCache(), host, options);
28382838
const languageVersion = getEmitScriptTarget(options);
28392839
const setExternalModuleIndicator = getSetExternalModuleIndicator(options);
28402840
return typeof result === "object" ?

src/compiler/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ namespace ts {
262262
if (file.packageJsonScope) {
263263
(result ??= []).push(chainDiagnosticMessages(
264264
/*details*/ undefined,
265-
file.packageJsonScope.packageJsonContent.type ?
265+
file.packageJsonScope.contents.packageJsonContent.type ?
266266
Diagnostics.File_is_CommonJS_module_because_0_has_field_type_whose_value_is_not_module :
267267
Diagnostics.File_is_CommonJS_module_because_0_does_not_have_field_type,
268268
toFileName(last(file.packageJsonLocations!), fileNameConvertor)

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ namespace ts.server {
13781378
const packageDirectory = fileName.substring(0, nodeModulesPathParts.packageRootIndex);
13791379
const packageJsonCache = project.getModuleResolutionCache()?.getPackageJsonInfoCache();
13801380
const compilerOptions = project.getCompilationSettings();
1381-
const packageJson = getPackageScopeForPath(project.toPath(packageDirectory + "/package.json"), getTemporaryModuleResolutionState(packageJsonCache, project, compilerOptions));
1381+
const packageJson = getPackageScopeForPath(getNormalizedAbsolutePath(packageDirectory + "/package.json", project.getCurrentDirectory()), getTemporaryModuleResolutionState(packageJsonCache, project, compilerOptions));
13821382
if (!packageJson) return undefined;
13831383
// Use fake options instead of actual compiler options to avoid following export map if the project uses node16 or nodenext -
13841384
// Mapping from an export map entry across packages is out of scope for now. Returned entrypoints will only be what can be

src/testRunner/unittests/tscWatch/forceConsistentCasingInFileNames.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,36 @@ a;b;
326326
}, { currentDirectory: "/Users/name/projects/web" }),
327327
changes: emptyArray,
328328
});
329+
330+
331+
verifyTscWatch({
332+
scenario: "forceConsistentCasingInFileNames",
333+
subScenario: "package json is looked up for file",
334+
commandLineArgs: ["-w", "--explainFiles"],
335+
sys: () => createWatchedSystem({
336+
"/Users/name/projects/lib-boilerplate/package.json": JSON.stringify({
337+
name: "lib-boilerplate",
338+
version: "0.0.2",
339+
type: "module",
340+
exports: "./src/index.ts",
341+
}),
342+
"/Users/name/projects/lib-boilerplate/src/index.ts": Utils.dedent`
343+
export function thing(): void {}
344+
`,
345+
"/Users/name/projects/lib-boilerplate/test/basic.spec.ts": Utils.dedent`
346+
import { thing } from 'lib-boilerplate'
347+
`,
348+
"/Users/name/projects/lib-boilerplate/tsconfig.json": JSON.stringify({
349+
compilerOptions: {
350+
module: "node16",
351+
target: "es2021",
352+
forceConsistentCasingInFileNames: true,
353+
traceResolution: true,
354+
}
355+
}),
356+
"/a/lib/lib.es2021.full.d.ts": libFile.content,
357+
}, { currentDirectory: "/Users/name/projects/lib-boilerplate" }),
358+
changes: emptyArray,
359+
});
329360
});
330361
}

tests/baselines/reference/reactJsxReactResolvedNodeNext.trace.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
"'package.json' does not have a 'typings' field.",
2626
"'package.json' has 'types' field 'index.d.ts' that references 'tests/cases/compiler/node_modules/@types/react/index.d.ts'.",
2727
"File 'tests/cases/compiler/node_modules/@types/react/index.d.ts' exist - use it as a name resolution result.",
28-
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1'. ========",
28+
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
2929
"File 'tests/cases/compiler/node_modules/@types/react/package.json' exists according to earlier cached lookups.",
3030
"======== Resolving module './' from 'tests/cases/compiler/node_modules/@types/react/jsx-runtime.d.ts'. ========",
3131
"Resolution for module './' was found in cache from location 'tests/cases/compiler/node_modules/@types/react'.",
32-
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1'. ========",
32+
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
3333
"File 'tests/cases/compiler/node_modules/@types/react/package.json' exists according to earlier cached lookups.",
3434
"======== Resolving module './' from 'tests/cases/compiler/node_modules/@types/react/jsx-dev-runtime.d.ts'. ========",
3535
"Resolution for module './' was found in cache from location 'tests/cases/compiler/node_modules/@types/react'.",
36-
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1'. ========",
36+
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
3737
"File 'package.json' does not exist.",
3838
"File '/package.json' does not exist according to earlier cached lookups.",
3939
"File 'package.json' does not exist according to earlier cached lookups.",

tests/baselines/reference/reactJsxReactResolvedNodeNextEsm.trace.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
"'package.json' does not have a 'typings' field.",
2020
"'package.json' has 'types' field 'index.d.ts' that references 'tests/cases/compiler/node_modules/@types/react/index.d.ts'.",
2121
"File 'tests/cases/compiler/node_modules/@types/react/index.d.ts' exist - use it as a name resolution result.",
22-
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1'. ========",
22+
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
2323
"File 'tests/cases/compiler/node_modules/@types/react/package.json' exists according to earlier cached lookups.",
2424
"======== Resolving module './' from 'tests/cases/compiler/node_modules/@types/react/jsx-runtime.d.ts'. ========",
2525
"Resolution for module './' was found in cache from location 'tests/cases/compiler/node_modules/@types/react'.",
26-
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1'. ========",
26+
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
2727
"File 'tests/cases/compiler/node_modules/@types/react/package.json' exists according to earlier cached lookups.",
2828
"======== Resolving module './' from 'tests/cases/compiler/node_modules/@types/react/jsx-dev-runtime.d.ts'. ========",
2929
"Resolution for module './' was found in cache from location 'tests/cases/compiler/node_modules/@types/react'.",
30-
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/index.d.ts@0.0.1'. ========",
30+
"======== Module name './' was successfully resolved to 'tests/cases/compiler/node_modules/@types/react/index.d.ts' with Package ID '@types/react/ndex.d.ts@0.0.1'. ========",
3131
"File 'package.json' does not exist.",
3232
"File '/package.json' does not exist.",
3333
"File 'package.json' does not exist according to earlier cached lookups.",
Lines changed: 116 additions & 0 deletions

0 commit comments

Comments
 (0)