Fix absolute path handling for config validation in windows (#7704)

This commit is contained in:
Bjorn Lu 2023-07-19 15:53:24 +08:00 committed by GitHub
parent 019b797bf8
commit d78db48ac4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix absolute path handling when passing `root`, `srcDir`, `publicDir`, `outDir`, `cacheDir`, `build.client`, and `build.server` configs in Windows

View file

@ -4,7 +4,7 @@ import type { AstroConfig, AstroUserConfig, CLIFlags } from '../../@types/astro'
import * as colors from 'kleur/colors';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { fileURLToPath } from 'node:url';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { mergeConfig } from './merge.js';
import { createRelativeSchema } from './schema.js';
@ -28,7 +28,6 @@ export async function validateConfig(
root: string,
cmd: string
): Promise<AstroConfig> {
const fileProtocolRoot = pathToFileURL(root + path.sep);
// Manual deprecation checks
/* eslint-disable no-console */
if (userConfig.hasOwnProperty('renderers')) {
@ -78,7 +77,7 @@ export async function validateConfig(
}
/* eslint-enable no-console */
const AstroConfigRelativeSchema = createRelativeSchema(cmd, fileProtocolRoot);
const AstroConfigRelativeSchema = createRelativeSchema(cmd, root);
// First-Pass Validation
const result = await AstroConfigRelativeSchema.parseAsync(userConfig);

View file

@ -4,6 +4,8 @@ import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
import type { AstroUserConfig, ViteUserConfig } from '../../@types/astro';
import type { OutgoingHttpHeaders } from 'node:http';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { BUNDLED_THEMES } from 'shiki';
import { z } from 'zod';
import { appendForwardSlash, prependForwardSlash, trimSlashes } from '../path.js';
@ -256,31 +258,31 @@ export const AstroConfigSchema = z.object({
legacy: z.object({}).optional().default({}),
});
export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
// We need to extend the global schema to add transforms that are relative to root.
// This is type checked against the global schema to make sure we still match.
const AstroConfigRelativeSchema = AstroConfigSchema.extend({
root: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.root)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
srcDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.srcDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
compressHTML: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.compressHTML),
publicDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.publicDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
outDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.outDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
cacheDir: z
.string()
.default(ASTRO_CONFIG_DEFAULTS.cacheDir)
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
build: z
.object({
format: z
@ -291,12 +293,12 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.client)
.transform((val) => new URL(val, fileProtocolRoot)),
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
server: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.server)
.transform((val) => new URL(val, fileProtocolRoot)),
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
assetsPrefix: z.string().optional(),
serverEntry: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.serverEntry),
@ -376,3 +378,11 @@ A future version of Astro will stop using the site pathname when producing <link
return AstroConfigRelativeSchema;
}
function resolveDirAsUrl(dir: string, root: string) {
let resolvedDir = path.resolve(root, dir);
if (!resolvedDir.endsWith(path.sep)) {
resolvedDir += path.sep;
}
return pathToFileURL(resolvedDir);
}