Skip to content

[Bug] Build fails when srcDir is set to a directory outside project root #4612

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
4 tasks done
kawamataryo opened this issue Mar 9, 2025 · 2 comments
Open
4 tasks done
Labels

Comments

@kawamataryo
Copy link

kawamataryo commented Mar 9, 2025

First of all, thank you for creating Vitepress - it's an amazing tool. I really appreciate all the hard work that goes into maintaining it.

Description

When configuring Vitepress with a srcDir that points to a directory outside the project root, the build process fails with a "Rollup failed to resolve import 'vue'" error.

$ vitepress build

  vitepress v1.6.3

x Build failed in 12ms
✖ building client + server bundles...
build error:
[vite]: Rollup failed to resolve import "vue" from "/xxxxx/vitepress-src-dir-bug/api-examples.md".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
[vite]: Rollup failed to resolve import "vue" from "/xxxxx/vitepress-src-dir-bug/api-examples.md".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`

Reproduction

I've created a reproduction repository: vitepress-src-dir-bug

Reproduce step

$ git clone  https://github.com/kawamataryo/vitepress-src-dir-bug
$ cd vitepress-example
$ yarn
$ yarn docs:dev // It work
$ yarn docs:build // It don't work

Expected behavior

Vitepress should successfully build the documentation regardless of the srcDir location.

System Info

System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M2
    Memory: 449.28 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.asdf/installs/nodejs/20.17.0/bin/node
    Yarn: 1.22.22 - ~/.asdf/installs/nodejs/20.17.0/bin/yarn
    npm: 10.8.2 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 10.3.0 - ~/.asdf/installs/nodejs/20.17.0/bin/pnpm
    bun: 1.0.29 - /opt/homebrew/bin/bun
    Watchman: 2024.12.02.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 133.0.6943.143
    Edge: 134.0.3124.51
    Safari: 17.5

Additional context

No response

Validations

@kawamataryo kawamataryo added the bug: pending triage Maybe a bug, waiting for confirmation label Mar 9, 2025
@brc-dd
Copy link
Member

brc-dd commented Mar 9, 2025

Hmm, the issue is that it needs vue to be resolvable from that markdown file. So, srcDir technically works, but you need to install vitepress on the outer package.json. This structure will work:

Image


Alternatively,

// vitepress-example/.vitepress/config.mts

import { createRequire } from 'node:module'
import { defineConfig } from 'vitepress'

const require = createRequire(import.meta.url)

export default defineConfig({
  // ...
  vite: {
    resolve: {
      alias: {
        'vue/server-renderer': require.resolve('vue/server-renderer'),
        vue: require.resolve('vue')
      }
    }
  }
})

But you might need doing this for all deps.

PS: Most likely also related to #3035. It probably should fail dev too.

Might have some edge cases, but seems to work:

  vite: {
    plugins: [
      {
        name: 'node-resolve-from-different-root',
        resolveId(id) {
          try {
            const resolve = require.resolve(id)
            if (resolve) return { id: resolve }
          } catch (e) {}
        }
      }
    ]
  }

@brc-dd brc-dd added has-workaround Has workaround, low priority needs more discussion and removed bug: pending triage Maybe a bug, waiting for confirmation labels Mar 9, 2025
@kawamataryo
Copy link
Author

kawamataryo commented Mar 9, 2025

@brc-dd
Thank you for the prompt response! I will attempt to implement the workaround.

@github-actions github-actions bot added the stale label May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants