diff options
author | 2023-07-19 15:53:24 +0800 | |
---|---|---|
committer | 2023-07-19 15:53:24 +0800 | |
commit | d78db48ac48bec6bd550b937a896cbcc747625f1 (patch) | |
tree | a457453b44959926898d19dcc320763bf623d917 | |
parent | 019b797bf83201d2d4834cc9e0dde30f6a48daa2 (diff) | |
download | astro-d78db48ac48bec6bd550b937a896cbcc747625f1.tar.gz astro-d78db48ac48bec6bd550b937a896cbcc747625f1.tar.zst astro-d78db48ac48bec6bd550b937a896cbcc747625f1.zip |
Fix absolute path handling for config validation in windows (#7704)
-rw-r--r-- | .changeset/loud-hairs-tell.md | 5 | ||||
-rw-r--r-- | packages/astro/src/core/config/config.ts | 5 | ||||
-rw-r--r-- | packages/astro/src/core/config/schema.ts | 26 |
3 files changed, 25 insertions, 11 deletions
diff --git a/.changeset/loud-hairs-tell.md b/.changeset/loud-hairs-tell.md new file mode 100644 index 000000000..f2b7ca694 --- /dev/null +++ b/.changeset/loud-hairs-tell.md @@ -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 diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index e5fa8d5b0..a3bec4f8c 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -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); diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 70c9e03a7..9463469d1 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -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); +}
\ No newline at end of file |