From 46b6e14265f81ffbf1a7511909d5a9954160b504 Mon Sep 17 00:00:00 2001
From: Happydev <81974850+MoustaphaDev@users.noreply.github.com>
Date: Tue, 24 Jan 2023 21:00:17 +0100
Subject: [PATCH] Fix Content Collections not loading config file when there
 are spaces in the folder tree (#5962)

* fix

* add test

* use `fileURLToPath` instead

* chore: changeset

* remove useless config file

* revert back to using `decodeURIComponent`

* test: better test

* re-revert back to using `fileURLToPath`
---
 .changeset/silly-bees-impress.md              |  5 +++
 packages/astro/src/content/utils.ts           |  3 +-
 .../astro/test/content-collections.test.js    | 13 +++++++
 .../package.json                              |  9 +++++
 .../src/content/config.ts                     | 34 +++++++++++++++++++
 .../src/pages/index.astro                     | 24 +++++++++++++
 pnpm-lock.yaml                                |  8 +++++
 7 files changed, 95 insertions(+), 1 deletion(-)
 create mode 100644 .changeset/silly-bees-impress.md
 create mode 100644 packages/astro/test/fixtures/content with spaces in folder name/package.json
 create mode 100644 packages/astro/test/fixtures/content with spaces in folder name/src/content/config.ts
 create mode 100644 packages/astro/test/fixtures/content with spaces in folder name/src/pages/index.astro

diff --git a/.changeset/silly-bees-impress.md b/.changeset/silly-bees-impress.md
new file mode 100644
index 0000000000..a253175243
--- /dev/null
+++ b/.changeset/silly-bees-impress.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix Content Collections not loading config file when there are spaces in the folder tree
diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts
index 9623054a83..5a3279f93f 100644
--- a/packages/astro/src/content/utils.ts
+++ b/packages/astro/src/content/utils.ts
@@ -227,7 +227,8 @@ export async function loadContentConfig({
 		return undefined;
 	}
 	try {
-		unparsedConfig = await tempConfigServer.ssrLoadModule(contentPaths.config.pathname);
+		const configPathname = fileURLToPath(contentPaths.config);
+		unparsedConfig = await tempConfigServer.ssrLoadModule(configPathname);
 	} catch (e) {
 		throw e;
 	} finally {
diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js
index 2561bdbabc..c8105184e8 100644
--- a/packages/astro/test/content-collections.test.js
+++ b/packages/astro/test/content-collections.test.js
@@ -187,6 +187,19 @@ describe('Content Collections', () => {
 		});
 	});
 
+	describe('With spaces in path', () => {
+		it('Does not throw', async () => {
+			const fixture = await loadFixture({ root: './fixtures/content with spaces in folder name/' });
+			let error = null;
+			try {
+				await fixture.build();
+			} catch (e) {
+				error = e.message;
+			}
+			expect(error).to.be.null;
+		});
+	});
+
 	describe('SSR integration', () => {
 		let app;
 
diff --git a/packages/astro/test/fixtures/content with spaces in folder name/package.json b/packages/astro/test/fixtures/content with spaces in folder name/package.json
new file mode 100644
index 0000000000..eed4ebb90f
--- /dev/null
+++ b/packages/astro/test/fixtures/content with spaces in folder name/package.json	
@@ -0,0 +1,9 @@
+{
+  "name": "@test/content-with-spaces-in-folder-name",
+  "version": "0.0.0",
+  "private": true,
+  "dependencies": {
+    "astro": "workspace:*",
+    "@astrojs/mdx": "workspace:*"
+  }
+}
diff --git a/packages/astro/test/fixtures/content with spaces in folder name/src/content/config.ts b/packages/astro/test/fixtures/content with spaces in folder name/src/content/config.ts
new file mode 100644
index 0000000000..fbd4e381da
--- /dev/null
+++ b/packages/astro/test/fixtures/content with spaces in folder name/src/content/config.ts	
@@ -0,0 +1,34 @@
+import { z, defineCollection } from 'astro:content';
+
+const withCustomSlugs = defineCollection({
+	schema: z.object({}),
+});
+
+const withSchemaConfig = defineCollection({
+	schema: z.object({
+		title: z.string(),
+		isDraft: z.boolean().default(false),
+		lang: z.enum(['en', 'fr', 'es']).default('en'),
+		publishedAt: z.date().transform((val) => new Date(val)),
+	})
+});
+
+const withUnionSchema = defineCollection({
+	schema: z.discriminatedUnion('type', [
+		z.object({
+			type: z.literal('post'),
+			title: z.string(),
+			description: z.string(),
+		}),
+		z.object({
+			type: z.literal('newsletter'),
+			subject: z.string(),
+		}),
+	]),
+});
+
+export const collections = {
+	'with-custom-slugs': withCustomSlugs,
+	'with-schema-config': withSchemaConfig,
+	'with-union-schema': withUnionSchema,
+}
diff --git a/packages/astro/test/fixtures/content with spaces in folder name/src/pages/index.astro b/packages/astro/test/fixtures/content with spaces in folder name/src/pages/index.astro
new file mode 100644
index 0000000000..468b70e7bc
--- /dev/null
+++ b/packages/astro/test/fixtures/content with spaces in folder name/src/pages/index.astro	
@@ -0,0 +1,24 @@
+---
+
+---
+<html lang="en">
+  <head>
+    <meta charset="utf-8" />
+    <meta name="viewport" content="width=device-width" />
+    <title>It's content time!</title>
+    <style>
+      html,
+      body {
+        font-family: system-ui;
+        margin: 0;
+      }
+      body {
+        padding: 2rem;
+      }
+    </style>
+  </head>
+  <body>
+    <main>
+    </main>
+  </body>
+</html>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ffca8b90f1..7059f476c6 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1647,6 +1647,14 @@ importers:
       '@astrojs/mdx': link:../../../../integrations/mdx
       astro: link:../../..
 
+  packages/astro/test/fixtures/content with spaces in folder name:
+    specifiers:
+      '@astrojs/mdx': workspace:*
+      astro: workspace:*
+    dependencies:
+      '@astrojs/mdx': link:../../../../integrations/mdx
+      astro: link:../../..
+
   packages/astro/test/fixtures/content-collections:
     specifiers:
       '@astrojs/mdx': workspace:*