Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix self-name input linking for --allowJs --declaration projects #54819

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
forEach,
forEachAncestorDirectory,
formatMessage,
getAllowJSCompilerOption,
getBaseFileName,
GetCanonicalFileName,
getCommonSourceDirectory,
Expand Down Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with this we need to add affectsModuleResolution to "checkJs" in commandline options declaration.

Copy link
Member Author

@andrewbranch andrewbranch Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, yikes. I can do that for now, but maybe it would be better to update getCompilerOptionValue (called by optionsHaveChanges functions) to use all the computed compiler option getter functions sometime in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkJs doesn’t even have affectsSemanticDiagnostics. Does it need it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does. #53403 never made it in i guess.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll leave it off of this one and then we should revisit #53403

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)
Expand Down
33 changes: 33 additions & 0 deletions tests/baselines/reference/nodeAllowJsPackageSelfName2.js
Original file line number Diff line number Diff line change
@@ -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 {};
10 changes: 10 additions & 0 deletions tests/baselines/reference/nodeAllowJsPackageSelfName2.symbols
Original file line number Diff line number Diff line change
@@ -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))

11 changes: 11 additions & 0 deletions tests/baselines/reference/nodeAllowJsPackageSelfName2.types
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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";