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

Ensure Angular docsMode flag has effect #20711

Merged
merged 1 commit into from
Jan 25, 2023

Conversation

alexandertrefz
Copy link
Contributor

@alexandertrefz alexandertrefz commented Jan 20, 2023

What I did

Fix what #20608 was setting out to do: Fixing docs generation for Angular.

#20608 fixed the issue that the angular builders accept a docsMode flag, but were internally attempting to read a flag called docs to pass that along to runInstance, thereby always reading undefined and passing it along.

#20608 fixed this successfully, but introduced a new bug: It now passes along the docsMode flag, reading the value correctly, but runInstance doesn't accept this flag, it looks for a flag called docs.
It accepts a type called StandaloneOptions, which is a combination of CLIOptions and some other types, CLIOptions has the optional property docs as can be verified here: https://github.com/storybookjs/storybook/blob/next/code/lib/types/src/modules/core-common.ts#L159

I now simply read the docsMode flag but pass this along as docs. This fixes the generation successfully.

@alexandertrefz
Copy link
Contributor Author

/cc @ndelangen

@JReinhold
Copy link
Contributor

Hmm it means that there is no Story Indexer that can handle *.stories.mdx files, even though they are in the glob. Maybe we removed the indexer to handle story definitions in MDX, and forgot to remove this? I'm unsure, and I don't know if it is related to this PR or not.

@tmeasday or @shilman would know more.

@alexandertrefz
Copy link
Contributor Author

This PR changes 2 lines, in passing along a single argument to reenable docs generation for Angular. It literally just renames a single key in an object (in 2 separate files) - I could not imagine it possibly impacting Story indexing in any way.

@sheriffMoose
Copy link
Contributor

Hey @valentinpalkovic, @JReinhold
currently the common preset indexer works with .js|.jsx|.ts|.tsx and the indexing for .mdx resides in the addon-docs

@tmeasday
Copy link
Member

Y'all have this backwards I suspect.

The CLI option is --docs. It should set the config option docs.docsMode = true.

I think what this is doing is setting docs = <value of cli flag>, so when the CLI flag is unset, it is setting docs = false | null | whatever, which has the effect of disabling docs. So now .stories.mdx files are not being indexed, as that indexer is added by the docs addon.

@ssams
Copy link
Contributor

ssams commented Jan 23, 2023

#20608 fixed this successfully, but introduced a new bug: It now passes along the docsMode flag, reading the value correctly, but runInstance doesn't accept this flag, it looks for a flag called docs. It accepts a type called StandaloneOptions, which is a combination of CLIOptions and some other types, CLIOptions has the optional property docs as can be verified here: https://github.com/storybookjs/storybook/blob/next/code/lib/types/src/modules/core-common.ts#L159

interesting, when I created the original PR it certainly worked with the latest v7/next at that time (the patch from the linked PR for sure works with current v6 releases, as I'm using that locally). Something must have changed internally since then?
Note that in addition to the CLIOption you've linked, which is called indeed just docs:

There's also another docsMode in the BuilderOptions type just a few lines later in the file:

I'd assume it worked so far as it directly set the latter one and it got forwarded / kept included in the config option until reaching the actual builder, could be that types have been improved or gotten stricter in the meantime and it's only now filtered out somewhere in the call chain.

Also at the end of the Angular builder buildStaticStandalone is called with the collected options. This function apparently accepts a combination of CLIOptions and BuilderOptions, so from the type alone both docs or docsMode would be valid to pass (but apparently they don't work equally in practice):

() => buildStaticStandalone(options)

export type BuildStaticStandaloneOptions = CLIOptions &
LoadOptions &
BuilderOptions & { outputDir: string };
export async function buildStaticStandalone(options: BuildStaticStandaloneOptions) {

@alexandertrefz
Copy link
Contributor Author

Y'all have this backwards I suspect.

The CLI option is --docs. It should set the config option docs.docsMode = true.

I think what this is doing is setting docs = <value of cli flag>, so when the CLI flag is unset, it is setting docs = false | null | whatever, which has the effect of disabling docs. So now .stories.mdx files are not being indexed, as that indexer is added by the docs addon.

This was a good hint!

I know for certain that we need to set the docs flag for the docsMode to kick in. But you were right that my change does indeed seem to turn off the addon-docs in its entirety when docsMode is not set. I was able to reproduce the behaviour locally.

I have updated the PR to only set docs when docsMode is truthy, which has remedied the situation, at least locally.

@alexandertrefz alexandertrefz force-pushed the fix-angular-docs-mode branch 6 times, most recently from 3feab68 to 13fb012 Compare January 23, 2023 09:21
@alexandertrefz
Copy link
Contributor Author

alexandertrefz commented Jan 23, 2023

Some fighting with the linter later, and it all seems to run through green. 🎉

I guess someone needs to approve the other 2 workflows?

@ndelangen ndelangen assigned tmeasday and unassigned ndelangen Jan 23, 2023
@tmeasday
Copy link
Member

Ok, my apologies there was more going on here than I realised.

So this is pretty confusing. There is:

Then there is:

(The above is angular, but there's also a core-server version of that)

Additionally, there is:

I guess my suspicion about the indexer was wrong then? It seems an awful coincidence. Or could it be that options.presets.apply('docs') looks at options.docs somehow?

@alexandertrefz
Copy link
Contributor Author

alexandertrefz commented Jan 23, 2023

Then there is:

Actually no! They don't do the same thing. docsMode has been set since the original PR from last week (#20608) but it does not turn on --docs properly. Only passing docs to StandaloneOptions does turn on --docs. However, passing a falsy value to docs explicitly creates the MDX issues we observed in the beginning of this PR (probably turning off addon-docs entirely). Leaving docs out of the options entirely if it is falsy seems to create the default behaviour for the non-angular CLI.

I am unsure if docsMode has any effect at this point, but im not familiar enough with the codebase to make a determination either way.

@alexandertrefz
Copy link
Contributor Author

@tmeasday So can this get merged, or do you want to investigate deeper?

@tmeasday
Copy link
Member

@alexandertrefz I think the reason for that (needing to set options.docs = true to turn on docs mode) is due to this line:

https://github.com/storybookjs/storybook/blob/2cceaadef83bcc109af4144030a5e79af1a9d48f/code/lib/core-server/src/presets/common-preset.ts#L207-L214

The second argument to docs() there (a preset property) is really the whole Options object that includes both .docs and .docsMode but we only read options.docs when setting the default 'docs.docsMode' preset property.

That preset property is then read by:

I can't see any other places that either options.docs or options.docsMode is read 🤷

@alexandertrefz
Copy link
Contributor Author

@tmeasday So what should be the course of action? Should we attempt to change the common-preset to look at both docs and docsMode then?

@tmeasday
Copy link
Member

tmeasday commented Jan 24, 2023

OK, I tracked it down. The problem is the essentials addon drops the docs addon if PresetOptions['docs'] is false here:

.filter((key) => (options as any)[key] !== false)

So what's happening is that when we set options.docs = false it triggers the above. Setting it true works fine (thus this PR's ...(docsMode ? { docs: docsMode } : {}) works).

This simulates what SB proper does where the CLI option is left unset if you don't pass --docs (so you don't end up with options.docs = false).

Setting options.docsMode does nothing I'm pretty sure (right now) and probably would just confuse things on this PR.

However, this is all a big mess. @shilman we should discuss this.

@tmeasday
Copy link
Member

tmeasday commented Jan 25, 2023

Ok, discussed with @shilman.

It's pretty weird that we create that mess of StandaloneOptions (which can include both docs and docsMode booleans) and then combine it with the preset's actual options (in this case the options of the essential addon, which has a another, different option called docs). However, it would be a breaking change to do anything about the latter, and we are kind of stuck on the former as the CLI flag is called --docs also and we don't want to change it.

So the TLDR is .. it's super confusing, but the main thing is we do not ever want to set StandaloneOptions['docs'] to false. We should probably drop the unused docsMode boolean because it is just muddying the waters (and doesn't do anything AFAICT), but it doesn't need to happen in this PR. You could drop it from the angular side if you wanted.

So we can merge this PR as is or drop the docsMode, line. Or if we want to "fix" things I suggest we should drop docsMode everywhere and just rely on the option called docs. (i.e. delete this line and this line)

Copy link
Member

@ndelangen ndelangen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexandertrefz great work, could you apply the changes @tmeasday is asking about? then it's good to merge!

@alexandertrefz
Copy link
Contributor Author

alexandertrefz commented Jan 25, 2023

@tmeasday should we then also rename the angular flag to align with the rest and name it docs? So here renaming docsMode to docs and the same here?

So that docsMode is entirely gone?

(The PR is open to edits from you guys, if you just want to change those few lines yourselves, BTW)

@alexandertrefz alexandertrefz force-pushed the fix-angular-docs-mode branch 2 times, most recently from afd093c to e5e7f5b Compare January 25, 2023 11:43
@alexandertrefz
Copy link
Contributor Author

I have added the requested changes and renamed the docsMode flag in the angular builders to docs - this should probably also be updated in the documentation for v7 Angular? I have updated a reference I found in the getting started guide, but I am unsure where the proper docs are located for this.

@alexandertrefz alexandertrefz force-pushed the fix-angular-docs-mode branch 3 times, most recently from 698417d to 4f3d12a Compare January 25, 2023 12:09
@alexandertrefz
Copy link
Contributor Author

@ndelangen Just needs approval for the final workflows now and then it can get merged!

@ndelangen
Copy link
Member

Invited you to the org @alexandertrefz, will merge as soon as CI is green. Awesome work, thank you for contributing!

@ndelangen ndelangen merged commit bdf56cd into storybookjs:next Jan 25, 2023
@alexandertrefz alexandertrefz deleted the fix-angular-docs-mode branch January 25, 2023 13:34
@tmeasday
Copy link
Member

Thank you for working through this with us!

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

Successfully merging this pull request may close these issues.

7 participants