Skip to content

Conversation

@naitokosuke
Copy link

@naitokosuke naitokosuke commented Jan 17, 2026

Summary

Thank you for maintaining this amazing project! 🙏

This PR fixes an issue where getCollectionByFilePath incorrectly matches files from the content directory to collections with non-root prefixes (e.g., /docs).

Problem

When using multiple collections with different cwd values:

// content.config.ts
const content = defineCollection({
  type: "page",
  source: "**/*.{md,yml,json}",  // content directory
})

const docs = defineCollection({
  type: "page",
  source: {
    include: "**/*.md",
    prefix: "/docs",
    cwd: "docs",  // docs directory
  },
})

Selecting a file like content/dev-env-2026/index.md in Studio throws:

Error: Collection not found for fsPath: dev-env-2026/index.md

Root Cause

  1. getCollectionByFilePath sorts collections by prefix length (longer first)
  2. For dev-env-2026/index.md, the docs collection is checked first
  3. minimatch('dev-env-2026/index.md', '**/*.md') returns true
  4. The file is incorrectly assigned to the docs collection
  5. generateIdFromFsPath creates an invalid ID
  6. Database query fails

Why source.cwd doesn't work

I initially tried filtering by source.cwd, but discovered that @nuxt/content's defineLocalSource always sets cwd: "" at the end:

const resolvedSource = {
  // ...
  ...source,
  cwd: ""  // Always empty!
}

Solution

Filter sources by source.prefix instead:

const isRootSource = !source.prefix || source.prefix === '/'
return isRootSource && include && !exclude

This ensures that files from the content directory (passed as relative paths) only match collections with root prefix (/).

Testing

I couldn't find documentation on how to run the Studio editor locally for testing. So I verified this fix by:

  1. Building the module with dist/ included
  2. Pushing the built dist/ to my fork branch
  3. Installing from GitHub in my project: "nuxt-studio": "github:naitokosuke/nuxt-studio#fix/get-collection-by-file-path"
  4. Deploying to production and testing /_studio directly
  5. Confirmed that selecting content/dev-env-2026/index.md no longer throws errors

If there's a recommended way to test Studio locally, I'd appreciate guidance for future contributions!

Related

Fixes #235

source.cwd is always empty after @nuxt/content resolves it.
Use source.prefix to filter non-root sources (e.g., /docs).

Fixes nuxt-content#235

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@vercel
Copy link
Contributor

vercel bot commented Jan 17, 2026

@naitokosuke is attempting to deploy a commit to the Nuxt Team on Vercel.

A member of the Team first needs to authorize it.

@naitokosuke naitokosuke marked this pull request as ready for review January 17, 2026 14:57
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 19, 2026

npm i https://pkg.pr.new/nuxt-content/nuxt-studio@286

commit: 5a75f1a

@vercel
Copy link
Contributor

vercel bot commented Jan 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
content-studio Ready Ready Preview, Comment Jan 19, 2026 9:34am
nuxt.studio Ready Ready Preview Jan 19, 2026 9:34am

@larbish
Copy link
Contributor

larbish commented Jan 19, 2026

Hey @naitokosuke, thanks for your feedback and detailed issue and PR. I confirm the issue you're facing since we do not handle cwd in Studio. We might and we'll do but it needs a massive refactor I don't want to go in now.

Your fix did fix your case but it does not make sense in all cases. The prefix is used by Nuxt Content to generate the path (route URL) of the file. Some users might want to define it also for the content collection and thus your use case would be wrong.

That being said, the cwd makes sense for files located outside the content directory. Is there any reason you don't want to put your docs folder inside it?

By doing so you could just update your collections definition to have:

// content.config.ts
const content = defineCollection({
  type: "page",
  source: "**/*.{md,yml,json}",  // content directory
  exclude: ["docs/**"]
})

const docs = defineCollection({
  type: "page",
  source: {
    include: "docs/**/*.md",
    prefix: "/docs"
  },
})

And this might work as expected.

Tell me if it's not the case.

@larbish larbish closed this Jan 19, 2026
@naitokosuke
Copy link
Author

@larbish

Thank you for the detailed explanation — I realize now that I had misunderstood the intent around prefix and cwd.

Your clarification makes a lot of sense, especially regarding how prefix is fundamentally about route generation rather than filesystem context. I’ll try the approach you suggested by placing the docs folder inside the content directory and adjusting the collections accordingly.

Thank you as well for taking the time to explain this so clearly, and for confirming the behavior on the Studio side. Congratulations on the Studio v1 release — it’s a great milestone. I’m really looking forward to seeing how it continues to evolve.

Thanks again for everything, and keep up the great work.

naitokosuke added a commit to naitokosuke/blog that referenced this pull request Jan 19, 2026
Reverts from fork branch to official ^1.1.0 release.
The fork was used for testing PR nuxt-content/nuxt-studio#286,
which will not be merged.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
naitokosuke added a commit to naitokosuke/blog that referenced this pull request Jan 19, 2026
Nuxt Studio does not support `cwd` option for collections.
Moving docs inside content directory allows using include/exclude
patterns instead.

Refs: nuxt-content/nuxt-studio#286

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
naitokosuke added a commit to naitokosuke/blog that referenced this pull request Jan 19, 2026
* fix: revert nuxt-studio to official package

Reverts from fork branch to official ^1.1.0 release.
The fork was used for testing PR nuxt-content/nuxt-studio#286,
which will not be merged.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* refactor: move docs/ into content/docs/

Nuxt Studio does not support `cwd` option for collections.
Moving docs inside content directory allows using include/exclude
patterns instead.

Refs: nuxt-content/nuxt-studio#286

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: update content.config.ts for Nuxt Studio compatibility

- Add exclude pattern for docs in content collection
- Update docs collection to use include pattern instead of cwd
- Remove unsupported cwd option

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore: update pnpm-lock.yaml

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Editor now showing files while having a collection with prefix: "/something"

2 participants