Skip to content

auto import always uses relative (to the current file) module paths in presense of baseUri #21860

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

Open
zpdDG4gta8XKpMCd opened this issue Feb 10, 2018 · 12 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Feb 10, 2018

back in a day it used to be different

  • at first it was always an absolute path (that is without "./" or "../" in it) relative to the baseUri
  • then starting at some point it began to always show a popup with 2 options: absolute or relavive
  • now (since like a week ago or so) it just always uses a relative path with "./" and "../" to the current file no matter what, without asking, which is very frustrating and annoying

how can i get the auto import to use absolute paths like it was back in the day?

@ghost
Copy link

ghost commented Feb 12, 2018

There are two different auto import features:
a) import on completion: This is when you type an identifier for the first time and it is automatically imported. VSCode only wants to get one code action here because they don't want to display a dialog on completions.
2) Import fix: This is when there's an existing cannot-find-name error and we provide a codefix (light bulb) for it. This should be giving you both baseUrl and local path options.

Also, when choosing the action to use for completions (or display first for a code fix), we will prefer the shortest path. So we will only use a relative path if we see that it is simpler then coming down from the baseUrl.

@zpdDG4gta8XKpMCd
Copy link
Author

i am talking about import on completion, because i cannot opt out from completion itself and import just comes with it

there are a few reasons i don't want ./ and ../ in my module paths:

  • i compile the same code for AMD and CommonJs, and absolute paths are a common ground
  • relative paths are a pain to maintain (move files around)

so how can i get the import on completion to use absolute paths (relative to baseUri) please?

@ghost
Copy link

ghost commented Feb 12, 2018

It's possible that we could add an option in #20619 to always use baseUrl paths.
You could also set "typescript.autoImportSuggestions.enabled": false, in your vscode settings to opt out of the import completions feature entirely.

@alextreppass
Copy link

alextreppass commented May 15, 2018

Hi @Andy-MS - did #20619 allow for imports to use baseUrl paths? I see it's marked as closed via #22236, but I'm not sure whether that added the proposed option

Thanks

@mhegazy
Copy link
Contributor

mhegazy commented May 15, 2018

This should be exposed in VSCode (insiders as of now) under "typescript.preferences.importModuleSpecifier": "non-relative"

@jabsatz
Copy link

jabsatz commented Sep 20, 2018

This keeps happening even with that option enabled, I read in another issue that the completion uses whichever path is shortest as a string and that you were looking into making that into an option. Any updates on that?

@ghost
Copy link

ghost commented Sep 20, 2018

@jabsatz Could you provide a concrete reproducible example? We should always use a non-relative path if possible given that option.

@jabsatz
Copy link

jabsatz commented Sep 20, 2018

jsconfig.json:

{
	"compilerOptions": {
		"target": "esnext",
		"allowSyntheticDefaultImports": true,
		"baseUrl": "./src",
		"paths": {
			"*": ["*"]
		},
	},
	"exclude": ["node_modules", "build"]
}

settings:

{
	"typescript.preferences.importModuleSpecifier": "non-relative",
}

If I'm correct, this should resolve packages non-relatively, and it does, but only sometimes. Say for example my folder structure is somewhat like this:

src/
├── components/
│   ├── common/
│   │   └── Dialog.jsx
│   └── profile/
│       └── Profile.jsx
└── utils/
    └── calc.js

If I write Dialog in Profile.jsx, intellisense will suggest auto-import from "../common/Dialog", however, if I try to write calc, instead of suggesting "../../utils/calc" it just suggests "utils/calc" (as I expect).

@ghost
Copy link

ghost commented Sep 20, 2018

@jabsatz That's working for me (adds import { Dialog } from "components/common/Dialog";) when I tried it. Are you set up to use typescript@next?

@jabsatz
Copy link

jabsatz commented Sep 20, 2018

No, should I?

@ghost
Copy link

ghost commented Sep 20, 2018

Before that, I just noticed you're using .jsx files -- you might need to set "javascript.preferences.importModuleSpecifier" instead.

@jabsatz
Copy link

jabsatz commented Sep 21, 2018

That did the trick! Thank you!

@weswigham weswigham added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants