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

No fallback to index.js when importing a directory using an alias #15858

Closed
7 tasks done
charlesmass2 opened this issue Feb 9, 2024 · 2 comments · Fixed by #15983
Closed
7 tasks done

No fallback to index.js when importing a directory using an alias #15858

charlesmass2 opened this issue Feb 9, 2024 · 2 comments · Fixed by #15983
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release

Comments

@charlesmass2
Copy link

charlesmass2 commented Feb 9, 2024

Describe the bug

Importing a directory using import won't fallback to the index.js file anymore.
Get the following error:
image

I created a very simple repo for an easy reproduction.

I noticed that the issue started at version 5.1.0, more specifically with version 5.1.0-beta.4.

Reproduction

https://github.com/charlesmass2/vite-index-fallback-fail

Steps to reproduce

Run npm install followed by npm run dev

System Info

System:
    OS: Linux 6.5 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i7-13700H
    Memory: 7.28 GB / 31.05 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.3.1 - /usr/bin/node
    npm: 9.6.7 - /usr/bin/npm
  Browsers:
    Chrome: 121.0.6167.139
  npmPackages:
    @vitejs/plugin-vue: ^5.0.3 => 5.0.3 
    vite: 5.1.0 => 5.1.0

This is also reproduced in Alpine docker container.

Used Package Manager

npm

Logs

No response

Validations

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Feb 10, 2024

It looks like newly introduced server.fs.cachedChecks (#15279) have some inconsistency when handling absolute path with trailing slash. Thanks for the reproduction btw, I updated it to compare several cases:

https://stackblitz.com/edit/github-d5e7nn?file=src%2Fmain.js

// fails
import * as lib1 from '@/utils/'; // @ alias is rewritten to absolute path
console.log({ lib1 });

// others all work
import * as lib2 from '@/utils';
console.log({ lib2 });

import * as lib3 from '@/utils/index.js';
console.log({ lib3 });

 // for relative path, `path.resolve` (which removes trailing slash)  happens before reaching fs caching, so this doesn't manifest the issue
import * as lib4 from './utils/';
console.log({ lib4 });

import * as lib5 from './utils';
console.log({ lib5 });

import * as lib6 from './utils///././//'; // still normalized to the same "utils"
console.log({ lib6 });

Quick workaround would be to disable server.fs.cachedChecks by:

export default defineConfig({
  server: {
    fs: {
      cachedChecks: false,
    },
  },
});

I think non-cached version (e.g plain NodeJS's fs.statSync) normalizes path automatically (e.g. relative path, . and .., trailing slash removal etc... probably equivalent to path.resolve?), but cached version is missing trailing slash removal.

@hi-ogawa hi-ogawa added p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release and removed pending triage labels Feb 10, 2024
@charlesmass2
Copy link
Author

I can confirm that disabling cachedChecks works. Good workaround in my case, until this minor issue is resolved.
Thanks for the quick reply !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants