GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support go-to-definition for imports of arbitrary files by andrewbranch · Pull Request #42539 · microsoft/TypeScript

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Jan 28, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Fixes #41861

The first commit adds support for jumping to arbitrary files from import/require module specifiers, whether those are scripts or non-source files like CSS (thanks Daniel Rosenwasser (@DanielRosenwasser) for the idea that this should work).

The second commit goes further by allowing us to return a successful response for files that don’t exist. I was curious what would happen, so I removed the host.fileExists check and made sure we don’t try to read a file’s text to convert { position: 0 } to { line: 0, column: 0 }. The result in VS Code was this:

File does not exist, do you want to create it?

Having the quick option to create the file seemed like a win to me; better than doing nothing. But I’m open to debate on whether we want that.

Also, if anyone knows how this will work in Visual Studio or has a quick way to test it, that would be appreciated!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

DanielRosenwasser, mjbvz, maltenuhn, wongmjane, danilofuchs, huzefakagdi, tolu, arthurdenner, chkpnt, and liuliangsir reacted with heart emoji
All reactions
  • ❤️ 10 reactions
Andrew Branch (andrewbranch) added 2 commits January 28, 2021 12:47
@andrewbranch
Support go-to-definition for imports of scripts and arbitrary files
5a039af
@andrewbranch
Support go-to-definition for non-existent files
8a9cc94
@andrewbranch
Andrew Branch (andrewbranch) requested review from Daniel Rosenwasser (DanielRosenwasser), Nathan Shively-Sanders (sandersn) and Sheetal Nandi (sheetalkamat) January 28, 2021 20:56
@typescript-bot TypeScript Bot (typescript-bot) assigned Andrew Branch (andrewbranch) Jan 28, 2021
@typescript-bot TypeScript Bot (typescript-bot) added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jan 28, 2021
sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Jan 28, 2021
View reviewed changes
Comment thread src/services/goToDefinition.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Add missing file property
5f256f1
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jan 28, 2021

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) pack this

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jan 28, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Heya Daniel Rosenwasser (@DanielRosenwasser), I've started to run the tarball bundle task on this PR at 5f256f1. You can monitor the build here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jan 28, 2021

Copy link
Copy Markdown
Member

Heads up Matt Bierner (@mjbvz) and Mine Starks (@minestarks).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jan 28, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

How does this work with partial semantic mode (including in the browser)?

What will happen if we try go-to-definition on thing or the module specifier in this example?

import { thing } from "./vandelay.js"/*1*/;

thing/*2*/();
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@typescript-bot

TypeScript Bot (typescript-bot) commented Jan 28, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Hey Daniel Rosenwasser (@DanielRosenwasser), I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/94419/artifacts?artifactName=tgz&fileId=5DF50A9CF42BC37629C1481AFE23876083889D0471511DD2357A7CB0CDC5F1A602&fileName=/typescript-4.2.0-insiders.20210128.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented Jan 28, 2021

Copy link
Copy Markdown
Member Author

What will happen if we try go-to-definition on thing

Whatever the current behavior for go-to-definition on the identifier reference, that will be unchanged.

or the module specifier

Don’t we already resolve one level of direct imports from your current source file? That should be unchanged as well. If we don’t, we would return a response saying the definition is at position 0 of ./thing.js, resolved relative to whatever we said the path of the current source file is. If we were to re-add the host.fileExists check and we haven’t already successfully resolved and loaded ./thing.js for normal module resolution reasons, we would not offer a definition there.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Jan 28, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I mostly ask because #39037 is a related issue I've been interested in unlocking as well, but I get why this doesn't automatically unlock that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sandersn

Nathan Shively-Sanders (sandersn) commented Jan 28, 2021

Copy link
Copy Markdown
Member

Answering a question that nobody has asked, I tested this with emacs. This gives a picture of how a simply written language plugin will behave and is usually similar to how VS works too.

Everything works fine. For the non-existent file, the editor raises an error inside tide-get-file-buffer and since the error's not handled, it's displayed to the user. That's a fine user experience, and I would be surprised if it's worse in other editors.

andrewbranch reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Jan 28, 2021
View reviewed changes
Comment thread src/services/goToDefinition.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Use isExternalModuleNameRelative instead of !pathIsBareSpecifier
248ed37
@andrewbranch

Andrew Branch (andrewbranch) commented Feb 1, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

we dont resolve imports currently in partial semantic mode, so adding a test that it works for that would be good too

Sheetal Nandi (@sheetalkamat) I’m looking at adding a case in partialSemanticServer.ts now (just pushed a new commit), and it seems imports are resolved... the source file’s resolvedModules contains correct resolutions.

If we hadn’t resolved them, this case would be a problem, because the module specifier is extensionless. Without doing at least some basic module resolution, we wouldn’t know that the target file has a .ts extension. I guess in that situation, we would either want to turn this feature off or potentially do real module resolution on-demand. But at the moment it seems that module resolution did already happen so it’s not a problem.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Add partial semantic test
f6d3b18
@sheetalkamat

Sheetal Nandi (sheetalkamat) commented Feb 1, 2021

Copy link
Copy Markdown
Member

Andrew Branch (@andrewbranch) interesting.. till you pointed out, I didn't realize we were doing module resolution as part of noResolve. PR #4368 added this. Not sure if this additional cost is really what we want with partial semantic server or not but that can be handled separately.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch

Andrew Branch (andrewbranch) commented Feb 1, 2021

Copy link
Copy Markdown
Member Author

In that case, this feature currently works exactly the same for partial semantic mode as full semantic mode, and the new test will ensure that we don’t break it if we change partial semantic mode later.

sheetalkamat reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Andrew Branch (andrewbranch) force-pushed the bug/41861 branch from 2c49160 to f6d3b18 Compare February 1, 2021 20:47
@andrewbranch
Andrew Branch (andrewbranch) requested a review from Sheetal Nandi (sheetalkamat) February 2, 2021 17:40
sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Feb 2, 2021
View reviewed changes
Comment thread src/services/goToDefinition.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Combine with symbol search for non-source-file file references
a5195f5
@andrewbranch

Andrew Branch (andrewbranch) commented Feb 16, 2021

Copy link
Copy Markdown
Member Author

New changes:

  1. Whenever the file reference lookup does not yield an actual source file (this could be because the file doesn’t exist, or because it’s a file type like CSS that the compiler doesn’t care about), a new unverified: true property is added to the definition in the response. VS Code could use this to show a less-error-y prompt to create a new file if it finds that the file doesn’t exist.

  2. Whenever (1) happens, instead of immediately returning that speculative file result, we continue other methods of definition lookup and combine the results. This specifically allows us to return two definitions for pattern ambient module matches: one for the actual file referenced, and one for the ambient module declaration. Screen cap:

    Go to definition on index.css returns a result for the CSS file and for a "*.css" pattern ambient module declaration

    Notes: I’m not sure why the second definition is expanded by default instead of the first. Possibly because the first has a zero-length definition span? Also, the underlining on only part of the path seems to be unrelated to TypeScript’s response (the span we send there is the whole module specifier, quotes included).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
Andrew Branch (andrewbranch) requested a review from Sheetal Nandi (sheetalkamat) February 16, 2021 22:39
Andrew Branch (andrewbranch) added 2 commits February 16, 2021 14:54
@andrewbranch
Fix and accept API baselines
54d8a72
@andrewbranch
Fix useless or
48e0d26
RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) approved these changes Feb 25, 2021
View reviewed changes
sheetalkamat
Sheetal Nandi (sheetalkamat) requested changes Feb 25, 2021
View reviewed changes
Comment thread src/services/goToDefinition.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@andrewbranch
A definition is unverified if the file path was a guess, even if a so…
c2aa82c
…urce file has that path
@andrewbranch
Andrew Branch (andrewbranch) requested a review from Sheetal Nandi (sheetalkamat) February 26, 2021 23:20
sheetalkamat
Sheetal Nandi (sheetalkamat) approved these changes Mar 1, 2021
View reviewed changes
@andrewbranch
Merge branch 'master' into bug/41861
280926f
@andrewbranch
Andrew Branch (andrewbranch) merged commit 4b67b4a into microsoft:master Mar 1, 2021
sheetalkamat
Sheetal Nandi (sheetalkamat) reviewed Apr 1, 2021
View reviewed changes
Comment thread src/services/goToDefinition.ts
end: node.getEnd(),
fileName: node.text
},
unverified: !!verifiedFileName,

@sheetalkamat Sheetal Nandi (sheetalkamat) Apr 1, 2021

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Andrew Branch (@andrewbranch) just realised that we are not passing this on in the through server even though we added this property to protocol.
tsserver37220.log

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sheetalkamat Sheetal Nandi (sheetalkamat) mentioned this pull request Apr 1, 2021
Make cross-file go-to-definition work in the syntax-only server #39037
Closed
@andrewbranch
Andrew Branch (andrewbranch) deleted the bug/41861 branch April 1, 2021 16:15
@andrewbranch Andrew Branch (andrewbranch) mentioned this pull request Apr 1, 2021
Go to definition: pass unverified through server #43483
Merged
@mariusGundersen Marius Gundersen (mariusGundersen) mentioned this pull request May 26, 2021
[Feature] Import non-js content as const string #42219
Open
5 tasks done
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Jun 8, 2021
Go-to-definition doesn't work on non-module packages #44497
Closed
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) mentioned this pull request Aug 3, 2021
TSServer responses requesting further files to be opened #45315
Open
@andrewbranch

Andrew Branch (andrewbranch) commented Aug 27, 2021

Copy link
Copy Markdown
Member Author

That's correct, it was shipped in 4.3

liuliangsir reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@sheetalkamat Sheetal Nandi (sheetalkamat) sheetalkamat approved these changes
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) RyanCavanaugh approved these changes
@sandersn Nathan Shively-Sanders (sandersn) Awaiting requested review from sandersn
@DanielRosenwasser Daniel Rosenwasser (DanielRosenwasser) Awaiting requested review from DanielRosenwasser

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@andrewbranch Andrew Branch (andrewbranch)

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

PR Backlog
Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Go-to-definition doesn't work when a file exists but is not a module

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@andrewbranch @DanielRosenwasser @typescript-bot @sandersn @sheetalkamat @RyanCavanaugh
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.