-
Notifications
You must be signed in to change notification settings - Fork 332
feat(cli): allow docs.yml redirects to reference a YAML file #17311
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
Open
devin-ai-integration
wants to merge
4
commits into
main
Choose a base branch
from
devin/1785509387-redirects-filepath
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2dc7240
feat(cli): allow docs.yml redirects to reference a YAML file
willkendall01 bf96a5b
fix(cli): accept absolute redirects filepath
willkendall01 10b07fa
chore(internal): regenerate fern-yml json schema
willkendall01 1cbd50f
feat(cli): require redirects key at root of redirects file
willkendall01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 11 additions & 4 deletions
15
packages/cli/cli-v2/src/docs/adapter/LegacyDocsWorkspaceAdapter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,32 @@ | ||
| import type { docsYml } from "@fern-api/configuration-loader"; | ||
| import { type docsYml, resolveRedirects } from "@fern-api/configuration-loader"; | ||
| import { type AbsoluteFilePath, dirname } from "@fern-api/fs-utils"; | ||
| import type { DocsWorkspace } from "@fern-api/workspace-loader"; | ||
| import type { DocsConfig } from "../config/DocsConfig.js"; | ||
|
|
||
| export class LegacyDocsWorkspaceAdapter { | ||
| public adapt({ | ||
| public async adapt({ | ||
| docsConfig, | ||
| absoluteFilePath | ||
| }: { | ||
| docsConfig: DocsConfig; | ||
| absoluteFilePath: AbsoluteFilePath; | ||
| }): DocsWorkspace { | ||
| }): Promise<DocsWorkspace> { | ||
| // absoluteFilePath is the path to the docs.yml file. | ||
| // DocsWorkspace.absoluteFilePath must be the containing directory (the "fern folder"). | ||
| const docsFilePath = docsConfig.absoluteFilePath ?? absoluteFilePath; | ||
| const raw = docsConfig.raw as docsYml.RawSchemas.DocsConfiguration; | ||
| return { | ||
| type: "docs", | ||
| workspaceName: undefined, | ||
| absoluteFilePath: dirname(docsFilePath), | ||
| absoluteFilepathToDocsConfig: docsFilePath, | ||
| config: docsConfig.raw as docsYml.RawSchemas.DocsConfiguration | ||
| config: { | ||
| ...raw, | ||
| redirects: await resolveRedirects({ | ||
| redirects: raw.redirects, | ||
| absoluteFilepathToDocsConfig: docsFilePath | ||
| }) | ||
| } | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| - summary: | | ||
| `redirects` in `docs.yml` now accepts a relative filepath to a YAML file containing only the | ||
| list of redirects, in addition to an inline list. The file's contents are validated the same | ||
| way as inline redirects. | ||
| type: feat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/cli/configuration-loader/src/docs-yml/__test__/resolveRedirects.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils"; | ||
|
|
||
| import { mkdtemp, writeFile } from "fs/promises"; | ||
| import { tmpdir } from "os"; | ||
| import path from "path"; | ||
| import { beforeAll, describe, expect, it } from "vitest"; | ||
|
|
||
| import { resolveRedirects } from "../resolveRedirects.js"; | ||
|
|
||
| describe("resolveRedirects", () => { | ||
| let absoluteFilepathToDocsConfig: AbsoluteFilePath; | ||
|
|
||
| beforeAll(async () => { | ||
| const dir = AbsoluteFilePath.of(await mkdtemp(path.join(tmpdir(), "resolve-redirects-"))); | ||
| absoluteFilepathToDocsConfig = join(dir, RelativeFilePath.of("docs.yml")); | ||
| await writeFile( | ||
| join(dir, RelativeFilePath.of("redirects.yml")), | ||
| [ | ||
| "- source: /old-plants", | ||
| " destination: /plants", | ||
| "- source: /plants/:plantId/legacy", | ||
| " destination: /plants/:plantId", | ||
| " permanent: false" | ||
| ].join("\n") | ||
| ); | ||
| await writeFile(join(dir, RelativeFilePath.of("invalid.yml")), "- source: /old-plants\n target: /plants"); | ||
| await writeFile(join(dir, RelativeFilePath.of("not-a-list.yml")), "redirects:\n - source: /old-plants"); | ||
| }); | ||
|
|
||
| it("passes through an inline list", async () => { | ||
| const redirects = [{ source: "/old-plants", destination: "/plants" }]; | ||
| expect(await resolveRedirects({ redirects, absoluteFilepathToDocsConfig })).toEqual(redirects); | ||
| }); | ||
|
|
||
| it("passes through undefined", async () => { | ||
| expect(await resolveRedirects({ redirects: undefined, absoluteFilepathToDocsConfig })).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("loads redirects from a filepath", async () => { | ||
| expect(await resolveRedirects({ redirects: "./redirects.yml", absoluteFilepathToDocsConfig })).toEqual([ | ||
| { source: "/old-plants", destination: "/plants" }, | ||
| { source: "/plants/:plantId/legacy", destination: "/plants/:plantId", permanent: false } | ||
| ]); | ||
| }); | ||
|
|
||
| it("fails when the file does not exist", async () => { | ||
| await expect( | ||
| resolveRedirects({ redirects: "./missing.yml", absoluteFilepathToDocsConfig }) | ||
| ).rejects.toThrowError(/does not exist/); | ||
| }); | ||
|
|
||
| it("fails when a redirect is invalid", async () => { | ||
| await expect( | ||
| resolveRedirects({ redirects: "./invalid.yml", absoluteFilepathToDocsConfig }) | ||
| ).rejects.toThrowError(/Failed to parse/); | ||
| }); | ||
|
|
||
| it("fails when the file holds anything other than a list of redirects", async () => { | ||
| await expect( | ||
| resolveRedirects({ redirects: "./not-a-list.yml", absoluteFilepathToDocsConfig }) | ||
| ).rejects.toThrowError(/Failed to parse/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/cli/configuration-loader/src/docs-yml/resolveRedirects.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { docsYml } from "@fern-api/configuration"; | ||
| import { AbsoluteFilePath, dirname, doesPathExist, RelativeFilePath, resolve } from "@fern-api/fs-utils"; | ||
| import { CliError } from "@fern-api/task-context"; | ||
|
|
||
| import { readFile } from "fs/promises"; | ||
| import yaml from "js-yaml"; | ||
|
|
||
| const RedirectsFile = docsYml.DocsYmlSchemas.RedirectConfig.strict().array(); | ||
|
|
||
| /** | ||
| * A docs.yml configuration whose `redirects` filepath (if any) has already been read off disk. | ||
| */ | ||
| export type DocsConfigurationWithResolvedRedirects = Omit<docsYml.RawSchemas.DocsConfiguration, "redirects"> & { | ||
| redirects?: docsYml.RawSchemas.RedirectConfig[]; | ||
| }; | ||
|
|
||
| /** | ||
| * `redirects` accepts either an inline list or a relative filepath to a YAML file containing only | ||
| * that list. Resolving the file here lets every downstream consumer work with a plain list. | ||
| */ | ||
| export async function resolveRedirects({ | ||
| redirects, | ||
| absoluteFilepathToDocsConfig | ||
| }: { | ||
| redirects: docsYml.RawSchemas.RedirectsConfiguration | undefined; | ||
| absoluteFilepathToDocsConfig: AbsoluteFilePath; | ||
| }): Promise<docsYml.RawSchemas.RedirectConfig[] | undefined> { | ||
| if (redirects == null || typeof redirects !== "string") { | ||
| return redirects; | ||
| } | ||
|
|
||
| const absoluteFilepathToRedirects = resolve(dirname(absoluteFilepathToDocsConfig), RelativeFilePath.of(redirects)); | ||
| if (!(await doesPathExist(absoluteFilepathToRedirects))) { | ||
| throw new CliError({ | ||
| message: `Failed to load redirects: ${absoluteFilepathToRedirects} does not exist`, | ||
| code: CliError.Code.ParseError | ||
| }); | ||
| } | ||
|
|
||
| let contents: unknown; | ||
| try { | ||
| contents = yaml.load((await readFile(absoluteFilepathToRedirects)).toString()); | ||
| } catch (error) { | ||
| if (!(error instanceof yaml.YAMLException)) { | ||
| throw error; | ||
| } | ||
| throw new CliError({ | ||
| message: `Failed to parse ${absoluteFilepathToRedirects}: ${error.message}`, | ||
| code: CliError.Code.ParseError | ||
| }); | ||
| } | ||
|
|
||
| const parsed = RedirectsFile.safeParse(contents ?? []); | ||
| if (!parsed.success) { | ||
| throw new CliError({ | ||
| message: `Failed to parse ${absoluteFilepathToRedirects}. The file must contain only a list of redirects:\n${parsed.error.issues | ||
| .map((issue) => ` - ${issue.path.join(".")}: ${issue.message}`) | ||
| .join("\n")}`, | ||
| code: CliError.Code.ParseError | ||
| }); | ||
| } | ||
|
|
||
| return parsed.data; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...configuration/src/docs-yml/schemas/sdk/api/resources/docs/types/RedirectsConfiguration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // This file was auto-generated by Fern from our API Definition. | ||
|
|
||
| import type * as FernDocsConfig from "../../../index.js"; | ||
|
|
||
| /** | ||
| * Either an inline list of redirects, or a relative filepath to a YAML file containing | ||
| * only the list of redirects. | ||
| * | ||
| * ```yaml | ||
| * redirects: ./redirects.yml | ||
| * ``` | ||
| */ | ||
| export type RedirectsConfiguration = FernDocsConfig.RedirectConfig[] | string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...ion/src/docs-yml/schemas/sdk/serialization/resources/docs/types/RedirectsConfiguration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // This file was auto-generated by Fern from our API Definition. | ||
|
|
||
| import type * as FernDocsConfig from "../../../../api/index.js"; | ||
| import * as core from "../../../../core/index.js"; | ||
| import type * as serializers from "../../../index.js"; | ||
| import { RedirectConfig } from "./RedirectConfig.js"; | ||
|
|
||
| export const RedirectsConfiguration: core.serialization.Schema< | ||
| serializers.RedirectsConfiguration.Raw, | ||
| FernDocsConfig.RedirectsConfiguration | ||
| > = core.serialization.undiscriminatedUnion([core.serialization.list(RedirectConfig), core.serialization.string()]); | ||
|
|
||
| export declare namespace RedirectsConfiguration { | ||
| export type Raw = RedirectConfig.Raw[] | string; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Pointing redirects at an absolute file path crashes the CLI with an internal error
The redirects path from
docs.ymlis required to be relative (RelativeFilePath.of(redirects)atpackages/cli/configuration-loader/src/docs-yml/resolveRedirects.ts:32) before any friendly error handling runs, so users who give a full path see an internal crash instead of a clear message.Impact: The command aborts with a raw internal error and no guidance about what is wrong in the configuration file.
Mechanism: RelativeFilePath.of throws a plain Error for absolute inputs, bypassing CliError handling
RelativeFilePath.ofthrowsnew Error("Filepath is not relative: " + value)when given an absolute path (packages/commons/fs-utils/src/RelativeFilePath.ts:11-13).resolveRedirectsis invoked during workspace load (packages/cli/workspace/loader/src/loadDocsWorkspace.ts:124-127) and inparseDocsConfiguration, neither of which converts that generic error into aCliError, so every command that loads docs (check,generate,docs dev) fails with an unexpected-error stack rather than the intendedFailed to load redirects: ...message.The rest of the docs config resolves user-supplied paths with
resolve(dirname(absoluteFilepathToDocsConfig), unresolvedFilepath)(packages/cli/configuration-loader/src/docs-yml/parseDocsConfiguration.ts:1694-1702), which tolerates absolute inputs;resolveRedirectsdeviates from that convention.A related edge case:
redirects: ""resolves to the containing directory,doesPathExistreturns true, andreadFilethen throws an uncaughtEISDIR.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in bf96a5b.
resolveRedirectsnow usesresolve(dirname(docsConfig), redirects)directly (matchingresolveFilepath), so absolute paths work instead of throwing a rawFilepath is not relativeerror. Also guarded theredirects: ""case and switched todoesPathExist(..., "file")so a directory reportsis not a filerather than blowing up withEISDIR. Tests added for both.