Skip to content

Commit

Permalink
Fix absolute path handling for config validation in windows (#7704)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 19, 2023
1 parent 019b797 commit d78db48
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .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
5 changes: 2 additions & 3 deletions packages/astro/src/core/config/config.ts
Expand Up @@ -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';
Expand All @@ -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')) {
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 18 additions & 8 deletions packages/astro/src/core/config/schema.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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);
}

0 comments on commit d78db48

Please sign in to comment.