-
Notifications
You must be signed in to change notification settings - Fork 21
fix(yeoman-ui): sub-generator on wrong yeoman-environment error #577
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,11 @@ export type EnvGen = { | |
| gen: any; | ||
| }; | ||
|
|
||
| export type PrepareEnvGen = ( | ||
| env: Environment, | ||
| gen: any | ||
| ) => void | Promise<void>; | ||
|
|
||
| export type GeneratorData = { | ||
| generatorMeta: LookupGeneratorMeta; | ||
| generatorPackageJson: any; | ||
|
|
@@ -57,12 +62,20 @@ class EnvUtil { | |
|
|
||
| public isEnvIncompatibilityError(error: unknown): boolean { | ||
| return ( | ||
| (error as Error)?.message?.startsWith( | ||
| (error as Error)?.message?.includes( | ||
| Constants.ENV_INCOMPATIBILITY_MESSAGE_PREFIX | ||
| ) ?? false | ||
| ); | ||
| } | ||
|
|
||
| private isV3RuntimeIncompatibilityError(error: unknown): boolean { | ||
| const message = (error as Error)?.message ?? ""; | ||
| return ( | ||
| message.includes("requires yeoman-environment") || | ||
| message.includes("object is not extensible") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "object is not extensible" is not a yeoman specific error. In what scenario do we get this error? |
||
| ); | ||
| } | ||
|
|
||
| public loadNpmPath(_force = false) { | ||
| return this; | ||
| } | ||
|
|
@@ -180,19 +193,7 @@ class EnvUtil { | |
| ); | ||
|
|
||
| try { | ||
| const v6Env: Environment = this.createEnvInstance( | ||
| { sharedOptions: { forwardErrorToEnvironment: true } as any }, | ||
| adapter | ||
| ); | ||
|
|
||
| v6Env.register(meta.resolved!, { | ||
| namespace: genNamespace, | ||
| packagePath: meta.packagePath, | ||
| }); | ||
|
|
||
| const gen: any = await v6Env.create(genNamespace, { options } as any); | ||
|
|
||
| return { env: v6Env, gen }; | ||
| return await this.createV6EnvAndGen(genNamespace, meta, options, adapter); | ||
| } catch (v6Error) { | ||
| const shouldFallbackToV3 = this.isEnvIncompatibilityError(v6Error); | ||
| if (!shouldFallbackToV3) { | ||
|
|
@@ -229,6 +230,91 @@ class EnvUtil { | |
| } | ||
| } | ||
|
|
||
| public async createRunGen( | ||
| genNamespace: string, | ||
| options: any, | ||
| adapter: any, | ||
| prepare: PrepareEnvGen | ||
| ): Promise<void> { | ||
| const meta: LookupGeneratorMeta = await this.getGenMetadata(genNamespace); | ||
|
|
||
| this.unloadGeneratorModules(genNamespace); | ||
| let v3EnvGen: EnvGen | undefined; | ||
| try { | ||
| v3EnvGen = this.createLegacyV3EnvAndGen( | ||
| genNamespace, | ||
| meta, | ||
| options, | ||
| adapter | ||
| ); | ||
| } catch (v3CreateError) { | ||
| if (this.isV3RuntimeIncompatibilityError(v3CreateError)) { | ||
| this.logger?.info( | ||
| `generator ${genNamespace} needs yeoman-environment v6; instantiation on v3 was rejected`, | ||
| { error: (v3CreateError as Error)?.message } | ||
| ); | ||
| } else { | ||
| this.logger?.debug( | ||
| `generator ${genNamespace} failed to instantiate on yeoman-environment v3; surfacing the error (not a v6-runtime signal)`, | ||
| { error: (v3CreateError as Error)?.message } | ||
| ); | ||
| throw v3CreateError; | ||
| } | ||
| } | ||
|
|
||
| if (v3EnvGen) { | ||
| this.logger?.debug( | ||
| `routing generator ${genNamespace} to yeoman-environment v3` | ||
| ); | ||
| await this.prepareAndRun(v3EnvGen.env, v3EnvGen.gen, adapter, prepare); | ||
| return; | ||
| } | ||
|
|
||
| this.logger?.debug( | ||
| `routing generator ${genNamespace} to yeoman-environment v6` | ||
| ); | ||
| this.unloadGeneratorModules(genNamespace); | ||
| const { env, gen } = await this.createV6EnvAndGen( | ||
| genNamespace, | ||
| meta, | ||
| options, | ||
| adapter | ||
| ); | ||
| await this.prepareAndRun(env, gen, adapter, prepare); | ||
| } | ||
|
|
||
| private async prepareAndRun( | ||
| env: Environment, | ||
| gen: any, | ||
| adapter: any, | ||
| prepare: PrepareEnvGen | ||
| ): Promise<void> { | ||
| adapter?.resetSignal?.(); | ||
| await prepare(env, gen); | ||
| await Promise.resolve(env.runGenerator(gen)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think This:
|
||
| } | ||
|
|
||
| private async createV6EnvAndGen( | ||
| genNamespace: string, | ||
| meta: LookupGeneratorMeta, | ||
| options: any, | ||
| adapter: any | ||
| ): Promise<EnvGen> { | ||
| const v6Env: Environment = this.createEnvInstance( | ||
| { sharedOptions: { forwardErrorToEnvironment: true } as any }, | ||
| adapter | ||
| ); | ||
|
|
||
| v6Env.register(meta.resolved!, { | ||
| namespace: genNamespace, | ||
| packagePath: meta.packagePath, | ||
| }); | ||
|
|
||
| const gen: any = await v6Env.create(genNamespace, { options } as any); | ||
|
|
||
| return { env: v6Env, gen }; | ||
| } | ||
|
|
||
| private createLegacyV3EnvAndGen( | ||
| genNamespace: string, | ||
| meta: LookupGeneratorMeta, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| "use strict"; | ||
|
|
||
| const Generator = require("yeoman-generator-v5"); | ||
| const Base = Generator.default || Generator; | ||
|
|
||
| module.exports = class ComposeSubGenerator extends Base { | ||
| writing() { | ||
| // Signal on the shared env options so the test can observe the sub ran | ||
| if (this.options && this.options.composeMarker) { | ||
| this.options.composeMarker.subRan = true; | ||
| } | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "generator-compose-sub", | ||
| "type": "commonjs" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| "use strict"; | ||
|
|
||
| const Generator = require("yeoman-generator-v5"); | ||
| const Base = Generator.default || Generator; | ||
|
|
||
| module.exports = class ComposeTopGenerator extends Base { | ||
| writing() { | ||
| this.composeWith( | ||
| { | ||
| Generator: require("../../../generator-compose-sub/generators/app/index.js"), | ||
| path: require.resolve( | ||
| "../../../generator-compose-sub/generators/app/index.js" | ||
| ), | ||
| }, | ||
| this.options | ||
| ); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "generator-compose-top", | ||
| "type": "commonjs" | ||
| } |
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.
does the error
requires yeoman-environmentinclude more details?as in the version required?