Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/vite/src/node/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,41 @@ describe('loadConfigFromFile', () => {
})
})

describe('root resolution', () => {
const tmpBase = path.join(os.tmpdir(), 'vite-root-test')

afterEach(() => {
if (fs.existsSync(tmpBase)) {
fs.rmSync(tmpBase, { recursive: true, force: true })
}
})

test('resolves a symlinked root to its real path', async () => {
const realDir = path.join(tmpBase, 'realpath', 'real')
const linkDir = path.join(tmpBase, 'realpath', 'link')
fs.mkdirSync(realDir, { recursive: true })
fs.symlinkSync(realDir, linkDir, 'junction')

const config = await resolveConfig({ root: linkDir }, 'serve')

expect(config.root).toBe(normalizePath(fs.realpathSync.native(realDir)))
})

test('keeps a symlinked root when resolve.preserveSymlinks is true', async () => {
const realDir = path.join(tmpBase, 'preserve-symlinks', 'real')
const linkDir = path.join(tmpBase, 'preserve-symlinks', 'link')
fs.mkdirSync(realDir, { recursive: true })
fs.symlinkSync(realDir, linkDir, 'junction')

const config = await resolveConfig(
{ root: linkDir, resolve: { preserveSymlinks: true } },
'serve',
)

expect(config.root).toBe(normalizePath(linkDir))
})
})

describe('resolveServerOptions', () => {
const warnFn = vi.fn()
const logger = { warn: warnFn } as unknown as Logger
Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1600,9 +1600,11 @@ export async function resolveConfig(
let nonNormalizedResolvedRoot = config.root
? path.resolve(config.root)
: process.cwd()
try {
nonNormalizedResolvedRoot = safeRealpathSync(nonNormalizedResolvedRoot)
} catch {}
if (!config.resolve?.preserveSymlinks) {
try {
nonNormalizedResolvedRoot = safeRealpathSync(nonNormalizedResolvedRoot)
} catch {}
}
const resolvedRoot = normalizePath(nonNormalizedResolvedRoot)

checkBadCharactersInPath(
Expand Down
Loading