Skip to content

auto-import favors import from target instead of source #38856

Closed
@chengB12

Description

@chengB12

TypeScript Version: 4.0.0-dev.20200529
Verify no issue with vs code version 1.45.1 + typescript vsrion: 3.8
Verify issue with vs code + typescript 3.9.3
still have issue with vs code + typescript 4.0.0-dev20200529

Search Terms:
import / auto import
Code
we have project configure as

|
|-- sharedlib
       | -- src
       | -- dist
|-- package1
       | -- src
       | -- dist
|-- package2
       | -- src
       | -- dist

where src is *.ts source code, dist is where compiled *.js files

sharedlib/tsconfig.json

{"compilerOptions": {
    "rootDir": "./",
    "baseUrl": ".",
    "outDir": "dist",
    "paths": {
      "*": [
        "types/*"
      ]
    },
    "composite": true
  },
  "include": [
    "src/**/*.ts",
  ]
}

package1/tsconfig.json

{"compilerOptions": {
    "baseUrl": ".",
    "outDir": "dist",
    "rootDir": "./",
    "paths": {
      "*": [
        "../sharedlib/types/*"
      ],
      "sharedlib": [
        "../sharedlib"
      ]
    },
  },
}

sharedlib/src/foo.ts

export const foo = 1

package1/src/bar.ts

console.log(foo)

Expected behavior:
When in visual studio code + typescript 3.8
auto import will prompt me to import as

import foo from 'sharedlib/src/foo'

console.log(foo)

Actual behavior:
On typescript 3.9.3+
first, before compile sharedlib, it can't recognize foo, thus no auto import
after compile, it auto import the js from

import foo from 'sharedlib/dist/src/foo'

console.log(foo)

the problem are

  1. now when I add new exportable in sharedlib, I have to compile the package first before I am write reference code from other packages
  2. Because code write before/after ts 3.9 are import from different path even for same module, javascript will treat they as different modules, thus breaks states/singleton, if I didn't manually remove /dist from import

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions