Relative imports in files inside folders resolve from src/ instead of the importing file

Open
#407 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
frontend

Research direction

Start in src/output/moduleCompiler.ts, in processModule, where local specifiers are rewritten with source.replace(/^./+/, 'src/') and only the './' prefix is checked for ImportDeclaration, ExportNamedDeclaration and dynamic import(). Resolve './' and '../' against the directory of the importing filename and normalize '.' and '..' segments. Done when the components/c.js and '../d.js' cases in the issue compile to the correct module keys, which you can check by reproducing them in the playground.

Written by the indexing model from the issue text.

Description

When a file is placed in a subfolder (for example components/c.js, which FileSelector, setFiles, and Monaco/Volar all accept), relative imports in that file are resolved from src/ instead of from the importing file's directory, and ../ specifiers are not resolved as local modules.

Reproduction
  1. Sibling ./d.js in components/c.js:

App.vue

<script setup>
import { y } from './components/c.js'
</script>

<template>
  <h1>{{ y }}</h1>
</template>

components/c.js

import { z } from './d.js'
export const y = z

components/d.js

export const z = 'from components/d.js'
  1. Same setup when d.js (export const z = 'from src/d.js') also exists at root:

  2. Parent ../d.js in components/c.js importing root d.js:

Actual
  1. Preview fails with File "./d.js" does not exist..
  2. When src/d.js also exists, components/c.js compiles to const __import_1__ = __modules__["src/d.js"] and silently renders from src/d.js instead of from components/d.js.
  3. With import { z } from '../d.js', the import statement is left untouched in src/components/c.js, src/d.js is not compiled, and the preview stays blank (export { z as y } from '../d.js' emits __export__(__module__, "y", () => z), and export * from '../d.js' throws File "../d.js" does not exist.).
Expected

Relative specifiers (./ and ../, including re-exports and dynamic import()) resolve against the directory of the importing filename and normalize . and .., so ./d.js in src/components/c.js resolves to src/components/d.js and ../d.js resolves to src/d.js.

Cause

In src/output/moduleCompiler.ts, processModule rewrites local specifiers with source.replace(/^\.\/+/, 'src/') without using filename's directory, and only checks source.startsWith('./') for ImportDeclaration, ExportNamedDeclaration, and dynamic import().

Versions

@vue/[email protected] and main (59212b9), [email protected] (verified on play.vuejs.org, repl-vuejs.vercel.app in Chromium 156, and Node v26.13.0).

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
1.1k
Forks
222
Avg merge
2h 10m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vuejs/repl

All issues in vuejs/repl

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.