Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add--allowArbitraryExtensions, a flag for allowing arbitrary extensions on import paths#51435

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

weswigham
Copy link
Member

And, correspondingly, the.d.ext.ts files needed to describe the shape of whatever it is your importing.

Implements#50133

Option name, as always, open to 🚴🏚️.

mizdra reacted with hooray emojitonivj5 reacted with eyes emoji
@weswighamweswigham added the Breaking ChangeWould introduce errors in existing code labelNov 8, 2022
@typescript-bottypescript-bot added Author: Team For Uncommitted BugPR for untriaged, rejected, closed or missing bug labelsNov 8, 2022
@Thundercraft5
Copy link

Thundercraft5 commentedNov 8, 2022
edited
Loading

A name suggestion: How about--resolveArbitraryExtensions or--allowArbitraryFileExtensions?

Copy link
Member

@jakebaileyjakebailey left a comment

Choose a reason for hiding this comment

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

This all seems good and straightforward to me; mostly test changes and the actual changes are really nice and minimal.

My only question is about performance; we're doing quick checks to avoid extra FS calls, right?

@@ -9775,7 +9776,7 @@ namespace IncrementalParser {

/** @internal */
export function isDeclarationFileName(fileName: string): boolean {
return fileExtensionIsOneOf(fileName, supportedDeclarationExtensions);
return fileExtensionIsOneOf(fileName, supportedDeclarationExtensions) || (fileExtensionIs(fileName, Extension.Ts) && stringContains(fileName, ".d."));
Copy link
Member

Choose a reason for hiding this comment

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

I guess we're going so far as to supportfoo.d.tar.gz.ts? i.e. multiple dots?

Copy link
MemberAuthor

@weswighamweswighamNov 8, 2022
edited
Loading

Choose a reason for hiding this comment

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

Right now we interpret such a name as a declaration file, but will never match it, since we only consider everything after the last dot for the extracted extension. Somewhat on the fence about it right now tbh - I dunno if we need to handle compound extensions or not. Maybe I should adjust the lookup logic so it does?

Copy link
Member

Choose a reason for hiding this comment

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

I honestly have no clue. It seems like someone's going to try to do it... but maybe not?

@weswigham
Copy link
MemberAuthor

Yeah, we only do the new fs check when the extension in the import isn't a TS or JS one.

@andrewbranch
Copy link
Member

I’ve only just glanced at the implementation, but re: naming—in#51171 I introduceallowImportingTsExtensions, and I think I’ve managed to convince myself that its functionality really shouldn’t be combined with this flag you’ve added, because

  • Importing.ts extensions requiresnoEmit oremitDeclarationOnly, whereas this flag does not.
  • If you specifically indicate that you want to import.ts extensions, we can use that knowledge in path completions and auto-imports.

To that end, I would like to brainstorm flag names that don’t seem to imply.ts. I think “arbitrary” is a step in the right direction; maybe “unrecognized”? I haven’t thought of anything I really like yet, just wanted to throw that out and get thoughts.

@andrewbranch
Copy link
Member

Also: don’t you need corresponding changes in moduleSpecifiers.ts and path completions? E.g., what happens in declaration emit when you have:

// foo.d.html.tsexportdeclareclassCustomHtmlRepresentationThing{}exportdeclareconstsomeHtml:CustomHtmlRepresentationThing;// index.tsimport{someHtml}from"./foo.html";exportfunctiongetHtml(){returnsomeHtml;}

@weswigham
Copy link
MemberAuthor

@andrewbranch Done - I've also simplified things in the module name resolverquite a bit, in part thanks to your refactorings. As an added bonus,.ts file imports nowresolve during module resolution, but we still issue retain an error on the import in the checker, for our usual "ts files won't resolve at runtime" reasons. Should makeallowImportingTsExtensions pretty easy to implement~

@@ -444,6 +450,8 @@ FsWatches::
{}

FsWatchesRecursive::
/user/username/projects/myproject/lib1:
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

Just as an FYI, all these extra watchers are here because these tests have an import that looks like"./tools/tools.interface" which now looks for a./tools/tools.d.interface.ts (for the potential literaltools.interface file) before adding a.js or.ts extension onto the import to find what it currently finds. The additional failed lookup location triggers adding a directory watcher, since now a new file may appear which changes the resolution result~

Copy link
Member

Choose a reason for hiding this comment

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

can you instead change the test to import "./tools/toolsInterface" since the extension part is irrelevant for these tests?


import z from "./z.d.ts";
>z :any
>z :number
Copy link
MemberAuthor

Choose a reason for hiding this comment

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

It is probably worth noting that while these types refering to a ts source file in the import do now resolve, the error on the import is still present - it's just added in the checker even on successful resolution now.

@andrewbranch
Copy link
Member

As an added bonus, .ts file imports now resolve during module resolution, but we still issue retain an error on the import in the checker, for our usual "ts files won't resolve at runtime" reasons. Should make allowImportingTsExtensions pretty easy to implement~

I’ve already done this too 😅 let the merge conflicts continue!

weswigham reacted with heart emoji

@@ -4753,6 +4753,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
}

if (moduleNotFoundError) {
if (tryGetExtensionFromPath(sourceFile.fileName) === tryGetExtensionFromPath(moduleReference)) {
Copy link
Member

Choose a reason for hiding this comment

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

Doesn’t this falsely trigger onimport {} from "./foo.ts" when the source file isfoo.ts.ts? In my implementation, I couldn’t find a better way than to add a property ontoResolvedModule that indicated whether the.ts extension was actually used to match a.ts file extension.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

The error says that the "import path can't end in.ts" not that it can'tresolve to TS, so I'd argue it's not a false positive. Honestly, a"./foo.ts" that resolves to"./foo.ts.js" is expempted from the error right now, but I'm questioning more ifthat's OK - blanket saying "relative import specifiers can't end in .ts unless you pass some flag" just seems easier to understand, and covers weird cjs corners like this, if overzealously.

@@ -444,6 +450,8 @@ FsWatches::
{}

FsWatchesRecursive::
/user/username/projects/myproject/lib1:
Copy link
Member

Choose a reason for hiding this comment

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

can you instead change the test to import "./tools/toolsInterface" since the extension part is irrelevant for these tests?

@@ -43,6 +43,9 @@
"File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
"Matched 'exports' condition 'types'.",
"Using 'exports' subpath './yep' with target './types/foo.d.ts'.",
"File name '/node_modules/exports-and-types-versions/types/foo.d.ts' has a '.d.ts' extension - stripping it.",
"File '/node_modules/exports-and-types-versions/types/foo.ts' does not exist.",
Copy link
Member

Choose a reason for hiding this comment

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

These checks seem non intuitive. When types or exports entry already has "d.ts" extension and we are saying we prefer ".ts" over ".d.ts" even though field said to use ".d.ts" .. Just more configuration error prone i think.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

This is generally true all the time -.ts files that correspond to a.d.ts file are higher priority than the.d.ts file, even if your path explicitly uses the.d.ts extension. This is basically to mirror our wildcard include behavior which likewise prefers the.ts file to the.d.ts file.

@weswighamweswighamforce-pushed thearbitrary-declaration-file-extensions branch from17e1a6b tob1fe76fCompareJanuary 4, 2023 19:11
@weswigham
Copy link
MemberAuthor

@andrewbranch conflicts resolved, there's a minor change in thebundler resolution mode baseline; namely changing module resolution to consistently prefer loading the associated.ts source file, even when the import explicitly says.d.ts. There's also some weirdness with.tsx loading that we really haven't resolved (historically a.js file of the same name would get loaded at a higher priority than the.jsx file, even if you imported the.jsx directly -bunder mode kinda changed that with it's exact match branch, and I've formally encoded the change in behavior for tsx/ts while removing the bundler mode special case in module loading), though maybe it's not terribly important to resolve, since having aa.ts anda.tsx side-by-side is, as far as we're concerned, not really supported anyway.

@weswighamweswigham changed the titleAdd--allowNonJsExtensions, a flag for allowing arbitrary extensions on import pathsAdd--allowArbitraryExtensions, a flag for allowing arbitrary extensions on import pathsJan 9, 2023
Copy link
Member

@jakebaileyjakebailey left a comment
edited
Loading

Choose a reason for hiding this comment

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

Not an expert but the code seems fine to me; I think the new name is fine as well. (I'm guessing Andrew will look click a button too but here's my click.)

};
const lib1ToolsInterface: File = {
path: `/user/username/projects/myproject/lib1/tools/tools.interface.ts`,
path: `/user/username/projects/myproject/lib1/tools/toolsinterface.ts`,
Copy link
Member

Choose a reason for hiding this comment

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

Are filenames like this problematic now?

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

No, they just add a (higher priority) lookup location which has knock-on effects regarding file watchers in the baseline, so to avoid baseline noise Sheetal asked I simply change the filename.

andrewbranch reacted with thumbs up emoji
Copy link
Member

@andrewbranchandrewbranch left a comment

Choose a reason for hiding this comment

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

Looks good oncemoduleResolutionSupportsResolvingTsExtensions is deleted.

@weswigham
Copy link
MemberAuthor

@andrewbranch done~

{
name: "allowArbitraryExtensions",
type: "boolean",
affectsModuleResolution: true,
Copy link
Member

Choose a reason for hiding this comment

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

I was gathering module-resolution-affecting compiler options for docs, and noticed this. AFAICT it doesn’t affect module resolution, just whether an error is issued. This should probably be swapped foraffectsSemanticDiagnostics.

Copy link
Member

Choose a reason for hiding this comment

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

Is this something that should be in 5.0 beta? I just got to this comment in my email and noticed a PR wasn't sent for this (but I don't know how these two settings matter myself)

@weswigham
Copy link
MemberAuthor

weswigham commentedJan 26, 2023 via email

It just means we're over-invalidating incremental builds a little bit,since we're tossing resolutions that we could probably reuse if the optionvalue is changed. Honestly not too high impact, so we don't need to squeezeit into the beta if it'll delay things, but we can probably ship a fix inthe rc no problem, since a fix is just changing the field name here toanother one.
On Thu, Jan 26, 2023, 11:40 AM Jake Bailey ***@***.***> wrote: ***@***.**** commented on this pull request. ------------------------------ In src/compiler/commandLineParser.ts <#51435 (comment)> : > @@ -1206,6 +1206,14 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [ description: Diagnostics.Enable_importing_json_files, defaultValueDescription: false, }, + { + name: "allowArbitraryExtensions", + type: "boolean", + affectsModuleResolution: true, Is this something that should be in 5.0 beta? I just got to this comment in my email and noticed a PR wasn't sent for this (but I don't know how these two settings matter myself) — Reply to this email directly, view it on GitHub <#51435 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAWMAMTG6YIP7AQ4B6R4WSTWULHMLANCNFSM6AAAAAARZXVPPM> . You are receiving this because you modified the open/close state.Message ID: ***@***.***>

@jakebailey
Copy link
Member

I sent#52437 for it just to have it off of my mind 😅

@sheetalkamat
Copy link
Member

Apart from incremental build, it also means that source files are not reused in LS ifaffectsModuleResolution is set to true. And that has perf impact so something good to have i guess.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@andrewbranchandrewbranchandrewbranch approved these changes

@jakebaileyjakebaileyjakebailey approved these changes

@sheetalkamatsheetalkamatAwaiting requested review from sheetalkamat

Assignees

@weswighamweswigham

Labels
Author: TeamBreaking ChangeWould introduce errors in existing codeFor Uncommitted BugPR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@weswigham@Thundercraft5@andrewbranch@jakebailey@sheetalkamat@typescript-bot

[8]ページ先頭

©2009-2025 Movatter.jp