Skip to content

feat(schema-files-loader): Support subfolders - #24378

Merged
SevInf merged 2 commits into
mainfrom
feat/subfolders
Jun 3, 2024
Merged

SevInf merged 2 commits into
mainfrom
feat/subfolders

Conversation

@SevInf

@SevInf SevInf commented May 31, 2024

Copy link
Copy Markdown
Contributor

Logic is the following:

  • When asked to load files from folder, it is simple - we just
    recursively walk the tree and load all .prisma files.
  • When asked to find all files belonging to the same schema, we walk up
    the directory tree and stop and the last directory that had any
    .prisma files - this directory will be the root of the schema.

So:

first
├── a.prisma
└── b.prisma
second
├── a.prisma
└── b.prisma

first and second describe different schema.

root.prisma
first
├── a.prisma
└── b.prisma
second
├── a.prisma
└── b.prisma

all files belong to the same schema.

Logic is the following:
- When asked to load files from folder, it is simple - we just
  recursively walk the tree and load all `.prisma` files.
- When asked to find all files belonging to the same schema, we walk up
  the directory tree and stop and the last directory that had any
  `.prisma` files - this directory will be the root of the schema.

So:

```
first
├── a.prisma
└── b.prisma
second
├── a.prisma
└── b.prisma
```

`first` and `second` describe different schema.

```
root.prisma
first
├── a.prisma
└── b.prisma
second
├── a.prisma
└── b.prisma
```

all files belong to the same schema.
@SevInf SevInf added this to the 5.15.0 milestone May 31, 2024
@github-actions

github-actions Bot commented May 31, 2024 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
packages/client/runtime/library.js 179.82 KB (0%)
packages/client/runtime/library.d.ts 81 B (0%)
packages/client/runtime/binary.js 600.89 KB (0%)
packages/client/runtime/binary.d.ts 26 B (0%)
packages/client/runtime/edge.js 159.02 KB (0%)
packages/client/runtime/edge-esm.js 158.91 KB (0%)
packages/client/runtime/wasm.js 114.9 KB (0%)
packages/client/runtime/index-browser.js 33.77 KB (0%)
packages/client/runtime/index-browser.d.ts 89 B (0%)
packages/cli/build/index.js 2.1 MB (+0.02% 🔺)
packages/client/prisma-client-0.0.0.tgz 2.93 MB (0%)
packages/cli/prisma-0.0.0.tgz 3.73 MB (-0.01% 🔽)
packages/bundle-size/da-workers-libsql/output.tgz 858.74 KB (0%)
packages/bundle-size/da-workers-neon/output.tgz 934.97 KB (0%)
packages/bundle-size/da-workers-pg/output.tgz 953.54 KB (0%)
packages/bundle-size/da-workers-pg-worker/output.tgz 909.25 KB (0%)
packages/bundle-size/da-workers-planetscale/output.tgz 870.88 KB (0%)
packages/bundle-size/da-workers-d1/output.tgz 830.95 KB (0%)

@SevInf
SevInf marked this pull request as ready for review May 31, 2024 16:51
@SevInf
SevInf requested a review from a team as a code owner May 31, 2024 16:51
@SevInf
SevInf requested review from Jolg42 and removed request for a team May 31, 2024 16:51
@Jolg42

Jolg42 commented Jun 3, 2024

Copy link
Copy Markdown
Contributor

Note: Linting error can be ignore, I already fixed it on main

@SevInf

SevInf commented Jun 3, 2024

Copy link
Copy Markdown
Contributor Author

Ok, I am going to merge this now, but if we find anything wrong with this feature before release we should revert it rather than rush a fix - it's nothing that can't be postponed until next release.

@SevInf
SevInf merged commit dff0ae6 into main Jun 3, 2024
@SevInf
SevInf deleted the feat/subfolders branch June 3, 2024 09:09
aqrln pushed a commit that referenced this pull request Jul 20, 2026
### Problem

`loadSchemaFiles` follows symlinks that resolve to a directory, but it
never records which directories it already walked into. Two directories
that link to each other are enough to make it recurse forever:

```
schema/
  a.prisma
  nested/
    b.prisma
    link -> ..
```

Running `prisma validate --schema schema` on that layout never returns.
Because the recursion is async, there is no stack overflow and no error,
the command just sits there while memory keeps growing (136 MB to 303 MB
over 20 seconds in my case). Directory symlinks used to be skipped
entirely before #24378 reworked the traversal into a generic recursive
function, and the cycle was never accounted for after that change.

### Fix

The traversal now carries a set of directories it has already entered
and stops when it sees one again.

The set is keyed by the canonical path rather than the path the
directory was reached through, otherwise the same directory can be
entered twice under two different spellings. That is easy to hit on
macOS, where `/tmp` is a symlink to `/private/tmp`: pointing the schema
root at `/tmp/...` made the loader return `a.prisma` twice, once for
each spelling, and validation then failed with

```
The model "A" cannot be defined because a model with that name already exists.
```

To get the canonical path, `getEntryType` now reports `realPath` for
directories too. The field is optional, so resolvers that have no notion
of symlinks (`InMemoryFilesResolver`) are unaffected and fall back to
the entry path. The paths in the returned files are untouched, they are
still reported the way the user spelled the schema root.

The same change also removes duplicates for a directory that is
reachable twice without any cycle, which is a fix on its own: duplicated
files are rejected later by validation.

### Test

The new test drives `loadSchemaFiles` with a resolver that describes two
directories linking to each other, and reaches the root through a second
path so both parts of the fix are covered. The resolver gives up after a
fixed number of lookups, so a regression fails the test right away
instead of taking the worker down with it.

### Note

`ignores symlinks to directories` does not test what its name says. Its
fixture links to a directory that only contains `.gitkeep`, so the
loader does walk into it and simply finds no `.prisma` files. I left it
alone since it is unrelated to the cycle, but it is worth renaming at
some point.
lh0x00 pushed a commit to lh0x00/prisma that referenced this pull request Aug 9, 2026
…9740)

### Problem

`loadSchemaFiles` follows symlinks that resolve to a directory, but it
never records which directories it already walked into. Two directories
that link to each other are enough to make it recurse forever:

```
schema/
  a.prisma
  nested/
    b.prisma
    link -> ..
```

Running `prisma validate --schema schema` on that layout never returns.
Because the recursion is async, there is no stack overflow and no error,
the command just sits there while memory keeps growing (136 MB to 303 MB
over 20 seconds in my case). Directory symlinks used to be skipped
entirely before prisma#24378 reworked the traversal into a generic recursive
function, and the cycle was never accounted for after that change.

### Fix

The traversal now carries a set of directories it has already entered
and stops when it sees one again.

The set is keyed by the canonical path rather than the path the
directory was reached through, otherwise the same directory can be
entered twice under two different spellings. That is easy to hit on
macOS, where `/tmp` is a symlink to `/private/tmp`: pointing the schema
root at `/tmp/...` made the loader return `a.prisma` twice, once for
each spelling, and validation then failed with

```
The model "A" cannot be defined because a model with that name already exists.
```

To get the canonical path, `getEntryType` now reports `realPath` for
directories too. The field is optional, so resolvers that have no notion
of symlinks (`InMemoryFilesResolver`) are unaffected and fall back to
the entry path. The paths in the returned files are untouched, they are
still reported the way the user spelled the schema root.

The same change also removes duplicates for a directory that is
reachable twice without any cycle, which is a fix on its own: duplicated
files are rejected later by validation.

### Test

The new test drives `loadSchemaFiles` with a resolver that describes two
directories linking to each other, and reaches the root through a second
path so both parts of the fix are covered. The resolver gives up after a
fixed number of lookups, so a regression fails the test right away
instead of taking the worker down with it.

### Note

`ignores symlinks to directories` does not test what its name says. Its
fixture links to a directory that only contains `.gitkeep`, so the
loader does walk into it and simply finds no `.prisma` files. I left it
alone since it is unrelated to the cycle, but it is worth renaming at
some point.
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.

2 participants