Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty content collection when dev is first run #12866

Closed
1 task
ericswpark opened this issue Dec 31, 2024 · 24 comments · Fixed by #12818 or #12938
Closed
1 task

Empty content collection when dev is first run #12866

ericswpark opened this issue Dec 31, 2024 · 24 comments · Fixed by #12818 or #12938
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: content layer Related to the Content Layer feature (scope)

Comments

@ericswpark
Copy link
Contributor

Astro Info

Astro                    v5.1.1
Node                     v23.3.0
System                   Windows (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/mdx
                         @astrojs/tailwind

If this issue only occurs in one browser, which browser is a problem?

N/A

Describe the Bug

On Windows, Astro reports an empty Content Layer collection when running the server with npm run dev. Using the built preview (npm run build; npm run preview) is not affected and shows all content within the Content Layer collection. I've tested the same site on Linux and it does not suffer from this issue.

What's the expected result?

Astro should show the proper content within the Content Layer collection, instead of showing nothing.

Link to Minimal Reproducible Example

N/A - platform-specific bug

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 31, 2024
@ericswpark
Copy link
Contributor Author

ericswpark commented Dec 31, 2024

This is what I get when running npm run dev and navigating to my blog collection at /blog/:

The collection "blog" does not exist or is empty. Ensure a collection directory with this name exists.
20:00:46 [200] /blog/ 19ms

Here is the definition, which is the same as what is shown on the Astro "upgrade to v5"guide:

import { defineCollection, z } from "astro:content";
import { glob } from "astro/loaders";

const blogCollection = defineCollection({
  loader: glob({ pattern: "**/[^_]*.{md,mdx}", base: "./src/content/blog" }),
  schema: z.object({
    title: z.string(),
    author: z.string().optional(),
    date: z.coerce.date(),
    tags: z.array(z.string()).optional(),
  }),
});

export const collections = {
  blog: blogCollection,
};

I initially thought it might be a path delimiter issue (/ vs \), but again, building and running a preview works fine, so Astro does detect my content on Windows -- just not on the dev server.

@ericswpark
Copy link
Contributor Author

Even though I get a RenderUndefinedEntryError upon trying to load one of the blog posts, this issue does not seem to be related to issue #12773. I still get the error with the experimental--dev-after-sync branch.

@florian-lefebvre florian-lefebvre added the needs repro Issue needs a reproduction label Jan 1, 2025
Copy link
Contributor

github-actions bot commented Jan 1, 2025

Hello @ericswpark. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Jan 1, 2025
@ericswpark
Copy link
Contributor Author

@florian-lefebvre as I mentioned in the OP a repro would be of no use – Astro works fine on Linux and subsequently most of the web-based platforms that presumably is Linux container-based. This appears to be a platform specific issue.

@Gazook89
Copy link

Gazook89 commented Jan 2, 2025

I actually have what I believe is the same problem, but I am on Mac. My experience with the issue is documented in the discord support channel, but in summary:

I cannot get a migration to v5 to work on a very simple blog because content collections are empty. I initiated a new Astro Blog template project on both IDX and Stackblitz (minimal example) and had no issue. So then I downloaded the zip file from Stackblitz to my machine, uncompressed it, did npm install + npm run dev and had the issue with the collections again.

So I don't believe it's any issue with not following instructions, since it seems to work from the SB and IDX templates when used in those environments but just doesn't work locally.

Finding this thread just now, I tried npm run build + npm run preview which succeeded with both the downloaded SB template and my own personal project.

Again, I am on the current MacOS version, and had issues on both Node 20 and 22, and all Astro dependencies were up to date as far as I could tell.

I couldn't find any other errors in browser or in VS Code.

@florian-lefebvre
Copy link
Member

@ericswpark I understand it's OS specific but a repro is still useful for us because we can't take the time to replicate manually each issue. It doesn't have to be a stackblitz, a repo with the minimal conditions to reproduce your bug on windows is absolutely fine! We'll check it out on windows too

@florian-lefebvre
Copy link
Member

@Gazook89 in the project downloaded locally, can you run astro info and paste the result here?

@ericswpark
Copy link
Contributor Author

@florian-lefebvre understood, I thought we could only submit web container based from the issue form. Will submit a repro repo in a second!

@ericswpark
Copy link
Contributor Author

ericswpark commented Jan 2, 2025

@florian-lefebvre this is very weird and annoying. It appears that there is some sort of race condition going on here.

I had my site repo cloned to repoA, with the broken collections error message (see comment #12866 (comment)). I created an empty Astro project, brought over the minimal amount of code to replicate the collection page, and it worked.

I then re-cloned my repository to a separate directory repoA.reclone, ran npm i and npm run dev. This one worked, so I tried to figure out what was different between repoA and repoA.reclone, but everything was identical, except for the generated .astro/ directory and the node_modules/ directory. I then removed .astro and node_modules from repo A, re-ran npm i and npm run dev, and now repoA is rendering correctly as well with no collection error.

Keep in mind that I did this several times in repo A -- deleting all generated files and dependencies and reinstalling and re-running the dev server -- and it still didn't work until today. I can try and remove the .astro/ folder and node_modules and try reinstalling over and over until I can hit the race condition again.

Here is a link to the minimal example repo: https://github.com/ericswpark/astro-issue-12866-repro/
Although, I will probably have to reinstall and rerun a bunch of times to hit the race condition again.

@ascorbic
Copy link
Contributor

ascorbic commented Jan 2, 2025

I'm pretty sure this is a dupe of #12773. I know you've said experimental--dev-after-sync doesn't fix it, but can you ensure that you're using that in the repro.

@florian-lefebvre florian-lefebvre added needs triage Issue needs to be triaged feat: content layer Related to the Content Layer feature (scope) and removed needs repro Issue needs a reproduction labels Jan 2, 2025
@ericswpark
Copy link
Contributor Author

@ascorbic I already tried changing to that branch with my repository and still encountered the same issue. I don't remember if I removed .astro/ and node_modules but I'm pretty sure I did do that as a troubleshooting step, just in case. As my repository is no longer in the bugged state I most likely won't be able to reproduce unless I can run into that race condition again.

Feel free to close the issue -- I will comment if I ever re-run into this problem with the changes from that branch merged.

@ericswpark
Copy link
Contributor Author

ericswpark commented Jan 2, 2025

@ascorbic I've managed to recreate the issue with the experimental branch, and believe I have proper repro instructions:

  1. Remove .astro/
  2. Install npm i with experimental branch set in package.json
  3. npm run build
  4. npm run dev

Step 3 appears to be important -- without step 3, npm run dev shows all content in the collection, and subsequent calls to npm run build will not trigger the bugged state.

I've tried this 4~5 times and have gotten it to reliably break on all of the attempts. Please let me know if you can reproduce this problem.

EDIT: this is reproducible on both my main website repo and the minimal reproduction repo. I've updated the dependency to point to the experimental branch -- the problem still occurs.

My guess is something in the build step syncs content collections in such a way that the dev server thinks it doesn't exist? This kind of explains why if I do step 4 before step 3 then subsequent dev server invocations will still show the content collection properly.

@ascorbic
Copy link
Contributor

ascorbic commented Jan 2, 2025

In your repro, despite the version in the package.json it's installing 5.1.1. Try doing npm i astro@experimental--dev-after-sync manually and see if it works for you. If that doesn't, can you share the logs from when you run astro dev

@ericswpark
Copy link
Contributor Author

ericswpark commented Jan 2, 2025

@ascorbic you're right, I missed that part. However, I finally figured out why the experimental branch broke for me as well. Try this:

  1. Manual install with npm i astro@experimental--dev-after-sync
  2. npm run astro sync
  3. npm run build
  4. npm run dev

Again, skipping either 2 or 3 before running 4 will result in not hitting this edge case.

Once you reproduce using those steps, your .astro/ folder should be missing the data-store.json file.

After running step 4, you should get a log output like this:

$ npm run dev

> [email protected] dev
> astro dev

22:24:18 [types] Generated 1ms
22:24:18 [WARN] [content] Content config not loaded
22:24:18 [vite] Re-optimizing dependencies because vite config has changed

 astro  v0.0.0-dev-after-sync-20241223135326 ready in 888 ms

┃ Local    http://localhost:4321/
┃ Network  use --host to expose

▶ This is a  dev-after-sync-20241223135326  prerelease build!
  Report issues here: https://astro.build/issues

22:24:18 watching for file changes...
The collection "blog" does not exist or is empty. Please check your content config file for errors.
22:25:44 [200] /blog/ 401ms
The collection "blog" does not exist or is empty. Please check your content config file for errors.
22:25:44 [200] HEAD /ko/blog/ 19ms
22:25:44 [200] /_image 43ms

@Gazook89
Copy link

Gazook89 commented Jan 2, 2025

I just woke up so haven't run through the whole thread here, but for @florian-lefebvre here is my astro info:

Astro                    v5.1.1
Node                     v22.12.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/mdx

@ascorbic
Copy link
Contributor

ascorbic commented Jan 2, 2025

@ericswpark "Content config not loaded" is the important bit there. It means Vite can't load the content config. Do you get that in the minimal repro?

@ascorbic
Copy link
Contributor

ascorbic commented Jan 2, 2025

@Gazook89 can you try with npm i astro@experimental--dev-after-sync

@Gazook89
Copy link

Gazook89 commented Jan 2, 2025

@ascorbic doing npm i astro@experimental--dev-after-sync didn't seem to work for me. It installed, and npm run dev after didn't help. I also tried doing the build and retrying dev to no effect.

Here is my terminal from when i tried this with a basic blog template with no further changes after running the init script:

terminal
User@user withastro-astro-bwq4pw7d % npm i astro@experimental--dev-after-sync
npm warn ERESOLVE overriding peer dependency
npm warn While resolving: @example/[email protected]
npm warn Found: [email protected]
npm warn node_modules/astro
npm warn   peer astro@"^5.0.0" from @astrojs/[email protected]
npm warn   node_modules/@astrojs/mdx
npm warn     @astrojs/mdx@"^4.0.3" from the root project
npm warn   1 more (the root project)
npm warn
npm warn Could not resolve dependency:
npm warn peer astro@"^5.0.0" from @astrojs/[email protected]
npm warn node_modules/@astrojs/mdx
npm warn   @astrojs/mdx@"^4.0.3" from the root project

changed 1 package, and audited 361 packages in 1s

178 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
User@user withastro-astro-bwq4pw7d % npm run dev                             

> @example/[email protected] dev
> astro dev

08:36:07 [types] Generated 0ms
08:36:07 [content] Syncing content
08:36:07 [content] Synced content
08:36:07 [vite] Re-optimizing dependencies because lockfile has changed

 astro  v0.0.0-dev-after-sync-20241223135326 ready in 435 ms

┃ Local    http://localhost:4321/
┃ Network  use --host to expose

▶ This is a  dev-after-sync-20241223135326  prerelease build!
  Report issues here: https://astro.build/issues

08:36:07 watching for file changes...
08:36:14 [200] / 13ms
The collection "blog" does not exist or is empty. Please check your content config file for errors.
08:36:16 [200] /blog 9ms

@ericswpark
Copy link
Contributor Author

@ascorbic yes, with the reproduction steps above I was able to reproduce it in the minimal repro repo as well.

@Gazook89
Copy link

Gazook89 commented Jan 2, 2025

@ericswpark

Once you reproduce using those steps, your .astro/ folder should be missing the data-store.json file.

Should i be expecting to see a data-store.json file in that folder if everything is working correctly? I have never seen it, so I'm just curious. It'd be easier to spot that than switching back to my browser tab to check if something has worked.

@ericswpark
Copy link
Contributor Author

@Gazook89 if you've never seen it, probably not. I just did a diff between when the collection rendered properly and when it wouldn't. I think the only thing you can count on is the warning message: The collection "<collection name>" does not exist or is empty. Please check your content config file for errors.

@Gazook89
Copy link

Gazook89 commented Jan 7, 2025

I don't know if issues can be reopened in this repo, or if I should create a new one, but the v5.1.3 update to which #12818 was attached to hasn't resolved this issue for me.

To be sure, I created a new repo, did npm create astro@latest, set it up as the Blog template, ran npm run dev and as soon as I went to the /blog route got the same error:

16:24:49 [types] Generated 0ms
16:24:49 [content] Syncing content
16:24:50 [content] Synced content
16:24:50 [vite] Re-optimizing dependencies because vite config has changed

 astro  v5.1.3 ready in 414 ms

┃ Local    http://localhost:4321/
┃ Network  use --host to expose

16:24:50 watching for file changes...
The collection "blog" does not exist or is empty. Please check your content config file for errors.
16:24:54 [200] /blog 14ms"

I changed nothing from the template. I reran npm i just to be sure. As before, build and preview works fine, but obviously is a pain during development (at least compared to a working dev server).

Using the v5.1.3 with the Blog template on Stackblitz works fine.

Astro                    v5.1.3
Node                     v22.12.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             @astrojs/mdx
                         @astrojs/sitemap

@ascorbic
Copy link
Contributor

ascorbic commented Jan 8, 2025

OK, this is interesting. This doesn't occur with pnpm - just npm. It also works as soon as you first save a file.

@ascorbic ascorbic added - P4: important Violate documented behavior or significantly impacts performance (priority) and removed needs triage Issue needs to be triaged labels Jan 8, 2025
@ascorbic ascorbic changed the title Empty Content Layer collection on Windows for dev server only Empty content collection when dev is first run Jan 8, 2025
@Gazook89
Copy link

Gazook89 commented Jan 8, 2025

It also works as soon as you first save a file.

This isn't my experience as of a few minutes ago, as of Astro 5.1.3. I just wiped my test blog (created with the Astro Blog Template) and installed a new one. On NPM, if I do npm run dev, and then modify a file and save, I'm still hitting the empty collection problem.

So I'm waiting for this new patch to come down the pipe to try that out, but just noting here that right now, saving a file doesn't fix the problem (for me).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: content layer Related to the Content Layer feature (scope)
Projects
None yet
4 participants