Skip to content

Commit e1b1c47

Browse files
committed
Handle more places where package direcroy is converted to canonical file path
1 parent 8c62477 commit e1b1c47

12 files changed

+82
-87
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3629,7 +3629,7 @@ namespace ts {
36293629
if (ext === Extension.Ts || ext === Extension.Js || ext === Extension.Tsx || ext === Extension.Jsx) {
36303630
const scope = currentSourceFile.packageJsonScope;
36313631
const targetExt = ext === Extension.Ts ? Extension.Mts : ext === Extension.Js ? Extension.Mjs : undefined;
3632-
if (scope && !scope.packageJsonContent.type) {
3632+
if (scope && !scope.contents.packageJsonContent.type) {
36333633
if (targetExt) {
36343634
diagnosticDetails = chainDiagnosticMessages(
36353635
/*details*/ undefined,

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

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.",

tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/package-json-is-looked-up-for-file.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,21 @@ Output::
3232
>> Screen clear
3333
[12:00:29 AM] Starting compilation in watch mode...
3434

35-
File '/users/name/projects/lib-boilerplate/src/package.json' does not exist.
36-
Found 'package.json' at '/users/name/projects/lib-boilerplate/package.json'.
35+
File '/Users/name/projects/lib-boilerplate/src/package.json' does not exist.
36+
Found 'package.json' at '/Users/name/projects/lib-boilerplate/package.json'.
3737
'package.json' does not have a 'typesVersions' field.
38-
File '/users/name/projects/lib-boilerplate/test/package.json' does not exist.
39-
File '/users/name/projects/lib-boilerplate/package.json' exists according to earlier cached lookups.
38+
File '/Users/name/projects/lib-boilerplate/test/package.json' does not exist.
39+
File '/Users/name/projects/lib-boilerplate/package.json' exists according to earlier cached lookups.
4040
======== Resolving module 'lib-boilerplate' from '/Users/name/projects/lib-boilerplate/test/basic.spec.ts'. ========
4141
Module resolution kind is not specified, using 'Node16'.
42-
File '/users/name/projects/lib-boilerplate/test/package.json' does not exist according to earlier cached lookups.
43-
File '/users/name/projects/lib-boilerplate/package.json' exists according to earlier cached lookups.
44-
File '/users/name/projects/lib-boilerplate/src/index.ts' exist - use it as a name resolution result.
45-
Resolving real path for '/users/name/projects/lib-boilerplate/src/index.ts', result '/Users/name/projects/lib-boilerplate/src/index.ts'.
46-
======== Module name 'lib-boilerplate' was successfully resolved to '/users/name/projects/lib-boilerplate/src/index.ts' with Package ID 'lib-boilerplate/src/index.ts@0.0.2'. ========
42+
File '/Users/name/projects/lib-boilerplate/test/package.json' does not exist according to earlier cached lookups.
43+
File '/Users/name/projects/lib-boilerplate/package.json' exists according to earlier cached lookups.
44+
File '/Users/name/projects/lib-boilerplate/src/index.ts' exist - use it as a name resolution result.
45+
Resolving real path for '/Users/name/projects/lib-boilerplate/src/index.ts', result '/Users/name/projects/lib-boilerplate/src/index.ts'.
46+
======== Module name 'lib-boilerplate' was successfully resolved to '/Users/name/projects/lib-boilerplate/src/index.ts' with Package ID 'lib-boilerplate/src/index.ts@0.0.2'. ========
4747
File '/a/lib/package.json' does not exist.
4848
File '/a/package.json' does not exist.
4949
File '/package.json' does not exist.
50-
test/basic.spec.ts:1:23 - error TS1149: File name '/users/name/projects/lib-boilerplate/src/index.ts' differs from already included file name '/Users/name/projects/lib-boilerplate/src/index.ts' only in casing.
51-
The file is in the program because:
52-
Matched by default include pattern '**/*'
53-
Imported via 'lib-boilerplate' from file '/Users/name/projects/lib-boilerplate/test/basic.spec.ts' with packageId 'lib-boilerplate/src/index.ts@0.0.2'
54-
File is ECMAScript module because '/users/name/projects/lib-boilerplate/package.json' has field "type" with value "module"
55-
56-
1 import { thing } from 'lib-boilerplate'
57-
   ~~~~~~~~~~~~~~~~~
58-
5950
../../../../a/lib/lib.es2021.full.d.ts
6051
Default library for target 'es2021'
6152
src/index.ts
@@ -65,7 +56,7 @@ src/index.ts
6556
test/basic.spec.ts
6657
Matched by default include pattern '**/*'
6758
File is ECMAScript module because 'package.json' has field "type" with value "module"
68-
[[90m12:00:34 AM[0m] Found 1 error. Watching for file changes.
59+
[[90m12:00:34 AM[0m] Found 0 errors. Watching for file changes.
6960

7061

7162

tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/self-name-package-reference.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Output::
3030
>> Screen clear
3131
[12:00:23 AM] Starting compilation in watch mode...
3232

33-
Found 'package.json' at '/users/name/projects/web/package.json'.
33+
Found 'package.json' at '/Users/name/projects/web/package.json'.
3434
'package.json' does not have a 'typesVersions' field.
3535
======== Resolving module '@this/package' from '/Users/name/projects/web/index.ts'. ========
3636
Module resolution kind is not specified, using 'NodeNext'.
37-
File '/users/name/projects/web/package.json' exists according to earlier cached lookups.
37+
File '/Users/name/projects/web/package.json' exists according to earlier cached lookups.
3838
File '/Users/name/projects/web/index.ts' exist - use it as a name resolution result.
3939
Resolving real path for '/Users/name/projects/web/index.ts', result '/Users/name/projects/web/index.ts'.
4040
======== Module name '@this/package' was successfully resolved to '/Users/name/projects/web/index.ts'. ========

tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/with-nodeNext-resolution.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ Output::
3333
>> Screen clear
3434
[12:00:35 AM] Starting compilation in watch mode...
3535

36-
File '/users/name/projects/web/src/package.json' does not exist.
37-
File '/users/name/projects/web/package.json' does not exist.
38-
File '/users/name/projects/package.json' does not exist.
39-
File '/users/name/package.json' does not exist.
40-
File '/users/package.json' does not exist.
36+
File '/Users/name/projects/web/src/package.json' does not exist.
37+
File '/Users/name/projects/web/package.json' does not exist.
38+
File '/Users/name/projects/package.json' does not exist.
39+
File '/Users/name/package.json' does not exist.
40+
File '/Users/package.json' does not exist.
4141
File '/package.json' does not exist.
4242
======== Resolving module 'yargs' from '/Users/name/projects/web/src/bin.ts'. ========
4343
Explicitly specified module resolution kind: 'NodeNext'.
44-
File '/users/name/projects/web/src/package.json' does not exist according to earlier cached lookups.
45-
File '/users/name/projects/web/package.json' does not exist according to earlier cached lookups.
46-
File '/users/name/projects/package.json' does not exist according to earlier cached lookups.
47-
File '/users/name/package.json' does not exist according to earlier cached lookups.
48-
File '/users/package.json' does not exist according to earlier cached lookups.
44+
File '/Users/name/projects/web/src/package.json' does not exist according to earlier cached lookups.
45+
File '/Users/name/projects/web/package.json' does not exist according to earlier cached lookups.
46+
File '/Users/name/projects/package.json' does not exist according to earlier cached lookups.
47+
File '/Users/name/package.json' does not exist according to earlier cached lookups.
48+
File '/Users/package.json' does not exist according to earlier cached lookups.
4949
File '/package.json' does not exist according to earlier cached lookups.
5050
Loading module 'yargs' from 'node_modules' folder, target file type 'TypeScript'.
5151
Directory '/Users/name/projects/web/src/node_modules' does not exist, skipping all lookups in it.
@@ -57,7 +57,7 @@ Found 'package.json' at '/Users/name/projects/web/node_modules/@types/yargs/pack
5757
File '/Users/name/projects/web/node_modules/@types/yargs/index.d.ts' exist - use it as a name resolution result.
5858
Resolving real path for '/Users/name/projects/web/node_modules/@types/yargs/index.d.ts', result '/Users/name/projects/web/node_modules/@types/yargs/index.d.ts'.
5959
======== Module name 'yargs' was successfully resolved to '/Users/name/projects/web/node_modules/@types/yargs/index.d.ts' with Package ID 'yargs/index.d.ts@17.0.12'. ========
60-
File '/users/name/projects/web/node_modules/@types/yargs/package.json' exists according to earlier cached lookups.
60+
File '/Users/name/projects/web/node_modules/@types/yargs/package.json' exists according to earlier cached lookups.
6161
======== Resolving type reference directive 'yargs', containing file '/Users/name/projects/web/__inferred type names__.ts', root directory '/Users/name/projects/web/node_modules/@types'. ========
6262
Resolving with primary search path '/Users/name/projects/web/node_modules/@types'.
6363
File '/Users/name/projects/web/node_modules/@types/yargs/package.json' exists according to earlier cached lookups.

0 commit comments

Comments
 (0)