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

import from "." doesn't reference folder if folder and file have same name #45563

Closed
ghost opened this issue Aug 24, 2021 · 6 comments Β· Fixed by #47850
Closed

import from "." doesn't reference folder if folder and file have same name #45563

ghost opened this issue Aug 24, 2021 · 6 comments Β· Fixed by #47850
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@ghost
Copy link

ghost commented Aug 24, 2021

Bug Report

πŸ”Ž Search Terms

import folder file refactor

πŸ•— Version & Regression Information

4.4.0-insiders.20210805, next, and the 4.3.x branch

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ...?

⏯ Playground Link

Can't reproduce in playground as it has to do with multiple files and imports

πŸ’» Code

This has to do with file imports, so I made a minimal repro:

https://github.com/MarkKahn/ts-import-refactor-bug.git

πŸ™ Actual behavior

I was refactoring a file, module.ts into a directory, module. I created module/index.ts, and in module/submodule.ts I had written: import { otherSubModule } from '.'

TS threw an error saying Module '"."' has no exported member 'otherSubModule'.ts(2305)

After some banging my head on the wall I figured out that from '.' wasn't referencing ./index.ts, but instead was referencing ../module.ts

πŸ™‚ Expected behavior

import from '.' should reference the current folder, always. This is likely only a bug that would be triggered when refactoring code as I can't think of a use case for a file and folder to have the same name and be a valid structure.

@andrewbranch
Copy link
Member

Your minimal repro doesn’t seem to be a reproβ€”it doesn’t have any imports from "."

@andrewbranch andrewbranch added the Needs More Info The issue still hasn't been fully clarified label Aug 24, 2021
@ghost
Copy link
Author

ghost commented Aug 24, 2021

whoops, I hit undo one too many times before committing. Fixed, sorry. Issue is in a/c.ts

@andrewbranch
Copy link
Member

Thanks! Now if you don’t mind, can you restate your actual behavior and expected behavior in terms of the file names and identifiers used in that repro? I had a little trouble following your hypothetical example.

@ghost
Copy link
Author

ghost commented Aug 24, 2021

Sure.

c.ts

import { b } from '.';

I would expect '.' here to look at index.ts in the same folder. It doesn't, it references ../a.ts instead.

@ghost
Copy link
Author

ghost commented Aug 24, 2021

Also I just tested this in node using module.exports and require, and require('.') does reference the folder.

And I learned that es6 modules oddly don't officially support directory imports...

@andrewbranch andrewbranch added Bug A bug in TypeScript and removed Needs More Info The issue still hasn't been fully clarified labels Aug 25, 2021
@andrewbranch andrewbranch self-assigned this Aug 25, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.0 milestone Aug 25, 2021
@Jamesernator
Copy link

So in Node's official ESM support yes you can't import directories, you have to import the file you want directly or use a package and "exports".

Technically TypeScript doesn't support Node's ESM behaviour at all atm. Although the issue you're getting is a TypeScript specific issue as it only recognizes CommonJS style imports atm. So yeah import foo from '.'; should resolve the same as import foo = require('.'); in TypeScript's current resolution.

Proper ESM support with the same behaviour as Node's esm mode is expected in the next major version of TypeScript (but it could be delayed as its a big job). In this case it should be an error as per Node's official ESM resolution.

@andrewbranch andrewbranch added the Rescheduled This issue was previously scheduled to an earlier milestone label Dec 7, 2021
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants