Skip to content

Fix live content collections when project location space directory#13982

Closed
sgalcheung wants to merge 7 commits intowithastro:mainfrom
sgalcheung:fix-live-content-loaders
Closed

Fix live content collections when project location space directory#13982
sgalcheung wants to merge 7 commits intowithastro:mainfrom
sgalcheung:fix-live-content-loaders

Conversation

@sgalcheung
Copy link

@changeset-bot
Copy link

changeset-bot bot commented Jun 20, 2025

⚠️ No Changeset found

Latest commit: d5a659a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jun 20, 2025
@sgalcheung
Copy link
Author

sgalcheung commented Jun 20, 2025

This case is because the Vite eval() lost the original live.config.ts path.

We use new Error().stack to fetch the live.config.ts path, it works well, but if the project is located in a complex path (containing spaces), for example:

 Error: 
    at getImporterFilename (/home/projects/vitest-dev-vitest-5vznfg3r/src/getImporterFilename.ts:215:19)
    at defineLiveCollection (/home/projects/vitest-dev-vitest-5vznfg3r/src/defineLiveCollection.ts:4:20)
    at eval (eval at __global_eval__ (https://vitestdevvitest5vznfg3r-zbdw.w-corp-staticblitz.com/blitz.10996a95.js:12:10), <anonymous>:5:62)
    at VitestExecutor.runModule (file:///home/projects/vitest-dev-vitest-5vznfg3r/node_modules/vite-node/dist/client.mjs:414:4)
    at VitestExecutor.directRequest (file:///home/projects/vitest-dev-vitest-5vznfg3r/node_modules/vite-node/dist/client.mjs:392:3)
    at VitestExecutor.cachedRequest (file:///home/projects/vitest-dev-vitest-5vznfg3r/node_modules/vite-node/dist/client.mjs:206:11)
    at VitestExecutor.dependencyRequest (file:///home/projects/vitest-dev-vitest-5vznfg3r/node_modules/vite-node/dist/client.mjs:256:10)
    at eval (/home/projects/vitest-dev-vitest-5vznfg3r/test/defineLiveCollection.test.ts:21:17)
    at eval (file:///home/projects/vitest-dev-vitest-5vznfg3r/node_modules/@vitest/runner/dist/chunk-hooks.js:805:20)

In this case, we will hardly fetch it, because it cannot be found in the stack information.

I still haven't found a better method to resolve it. Should we replace new Error().stack?

@ascorbic
Copy link
Contributor

@sgalcheung can you give an example of a path that causes it to fail?

@sgalcheung
Copy link
Author

sgalcheung commented Jun 20, 2025

Add live content loaders with a space fixture
https://github.com/sgalcheung/astro/blob/83ec3c9664bd653bf3400f1226b7b9d7c19de6b0/packages/astro/test/live-loaders(with%20space).test.js#L13

I added a test file: live-loaders(with space).test.js, you can run it. @ascorbic

node --test "./test/live-loaders(with space).test.js"

@ascorbic
Copy link
Contributor

Thanks for looking into this. I had a PR with a fix for a separate but related issue, and I've added a fix for this into that (basically we'll no longer throw this error when we can't find the filename)

@sgalcheung
Copy link
Author

You have a PR to resolve the project location with the space path?
This isn't a print error problem; the key point is that Vite's eval() missed the original file path when the project location is within the path.

@sgalcheung sgalcheung changed the title Fix live content collections with space directory Fix live content collections when project with space directory Jun 20, 2025
@sgalcheung sgalcheung changed the title Fix live content collections when project with space directory Fix live content collections when project location space directory Jun 20, 2025
@ascorbic
Copy link
Contributor

Yes, the fix is to be forgiving if it can't get the filename, and not throw an error. It might mean we have some false negatives, but it will give an overall better result. See #13988

@sgalcheung sgalcheung mentioned this pull request Jul 22, 2025
1 task
@florian-lefebvre
Copy link
Member

I ran the test locally and seems to work, so I'm going to close this PR. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LiveContentConfigError when starting

3 participants